Skip to content

Commit

Permalink
Add ydoc.getXmlElement
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Feb 5, 2024
1 parent 415a645 commit 16d9638
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -739,6 +739,8 @@ type. Doesn't log types that have not been defined (using
<dd>Define a shared Y.Map type. Is equivalent to <code>y.get(string, Y.Map)</code>.</dd>
<b><code>getText(string):Y.Text</code></b>
<dd>Define a shared Y.Text type. Is equivalent to <code>y.get(string, Y.Text)</code>.</dd>
<b><code>getXmlElement(string, string):Y.XmlElement</code></b>
<dd>Define a shared Y.XmlElement type. Is equivalent to <code>y.get(string, Y.XmlElement)</code>.</dd>
<b><code>getXmlFragment(string):Y.XmlFragment</code></b>
<dd>Define a shared Y.XmlFragment type. Is equivalent to <code>y.get(string, Y.XmlFragment)</code>.</dd>
<b><code>on(string, function)</code></b>
Expand Down
12 changes: 12 additions & 0 deletions src/utils/Doc.js
Expand Up @@ -8,6 +8,7 @@ import {
YArray,
YText,
YMap,
YXmlElement,
YXmlFragment,
transact,
ContentDoc, Item, Transaction, YEvent // eslint-disable-line
Expand Down Expand Up @@ -262,6 +263,17 @@ export class Doc extends Observable {
return this.get(name, YMap)
}

/**
* @param {string} [name]
* @return {YXmlElement}
*
* @public
*/
getXmlElement (name = '') {
// @ts-ignore
return this.get(name, YXmlElement)
}

/**
* @param {string} [name]
* @return {YXmlFragment}
Expand Down
13 changes: 13 additions & 0 deletions tests/y-xml.tests.js
Expand Up @@ -210,3 +210,16 @@ export const testFormattingBug = _tc => {
yxml.applyDelta(delta)
t.compare(yxml.toDelta(), delta)
}

/**
* @param {t.TestCase} _tc
*/
export const testElement = _tc => {
const ydoc = new Y.Doc()
const yxmlel = ydoc.getXmlElement()

const text1 = new Y.XmlText('text1')
const text2 = new Y.XmlText('text2')
yxmlel.insert(0, [text1, text2])
t.compareArrays(yxmlel.toArray(), [text1, text2])
}

0 comments on commit 16d9638

Please sign in to comment.