fix: allow numbers in attribute values

This commit is contained in:
Tomi Blinnikka 2024-12-16 15:10:27 -06:00
parent d4d4ae5f53
commit 38bd3a35c5
2 changed files with 23 additions and 4 deletions

View File

@ -25,7 +25,7 @@ import {
* * An YXmlElement has attributes (key value pairs)
* * An YXmlElement has childElements that must inherit from YXmlElement
*
* @template {{ [key: string]: ValueTypes }} [KV={ [key: string]: string }]
* @template {{ [key: string]: ValueTypes }} [KV={ [key: string]: string | number }]
*/
export class YXmlElement extends YXmlFragment {
constructor (nodeName = 'UNDEFINED') {
@ -94,7 +94,7 @@ export class YXmlElement extends YXmlFragment {
const el = new YXmlElement(this.nodeName)
const attrs = this.getAttributes()
object.forEach(attrs, (value, key) => {
if (typeof value === 'string') {
if (typeof value === 'string' || typeof value === 'number') {
el.setAttribute(key, value)
}
})
@ -226,8 +226,8 @@ export class YXmlElement extends YXmlFragment {
const attrs = this.getAttributes()
for (const key in attrs) {
const value = attrs[key]
if (typeof value === 'string') {
dom.setAttribute(key, value)
if (typeof value === 'string' || typeof value === 'number') {
dom.setAttribute(key, String(value))
}
}
typeListForEach(this, yxml => {

View File

@ -195,6 +195,25 @@ export const testClone = _tc => {
t.compare(cloneYxml.toJSON(), yxml.toJSON())
}
/**
* @param {t.TestCase} _tc
*/
export const testCloneElementAttributes = _tc => {
const ydoc = new Y.Doc()
const yxml = ydoc.getXmlFragment()
const first = new Y.XmlText('text')
const second = new Y.XmlElement('p')
second.setAttribute('answer', 42)
const third = new Y.XmlElement('p')
third.setAttribute('question', 'unknown')
yxml.push([first, second, third])
t.compareArrays(yxml.toArray(), [first, second, third])
const cloneYxml = yxml.clone()
ydoc.getArray('copyarr').insert(0, [cloneYxml])
t.assert(cloneYxml.length === 3)
t.compare(cloneYxml.toJSON(), yxml.toJSON())
}
/**
* @param {t.TestCase} _tc
*/