Fix YXmlElement.clone and YXmlFragment.clone

This commit is contained in:
Sam Duvall 2021-06-08 21:30:08 -04:00
parent 097b9e8208
commit 5ddb7eefed
3 changed files with 20 additions and 2 deletions

View File

@ -81,7 +81,7 @@ export class YXmlElement extends YXmlFragment {
el.setAttribute(key, attrs[key])
}
// @ts-ignore
el.insert(0, el.toArray().map(item => item instanceof AbstractType ? item.clone() : item))
el.insert(0, this.toArray().map(item => item instanceof AbstractType ? item.clone() : item))
return el
}

View File

@ -167,7 +167,7 @@ export class YXmlFragment extends AbstractType {
clone () {
const el = new YXmlFragment()
// @ts-ignore
el.insert(0, el.toArray().map(item => item instanceof AbstractType ? item.clone() : item))
el.insert(0, this.toArray().map(item => item instanceof AbstractType ? item.clone() : item))
return el
}

View File

@ -133,3 +133,21 @@ export const testInsertafter = tc => {
el.insertAfter(deepsecond1, [new Y.XmlText()])
})
}
/**
* @param {t.TestCase} tc
*/
export const testClone = tc => {
const ydoc = new Y.Doc()
const yxml = ydoc.getXmlFragment()
const first = new Y.XmlText('text')
const second = new Y.XmlElement('p')
const third = new Y.XmlElement('p')
yxml.push([first, second, third])
t.compareArrays(yxml.toArray(), [first, second, third])
const cloneYxml = yxml.clone()
t.assert(cloneYxml.length === 3)
t.assert(cloneYxml.toJSON() === yxml.toJSON())
}