From f3d8db491bf781b29c82e1aa86ef5272282f8d96 Mon Sep 17 00:00:00 2001 From: Yousef El-Dardiry Date: Tue, 15 Jun 2021 16:36:22 +0200 Subject: [PATCH] implement hasAttribute --- src/types/YXmlElement.js | 13 +++++++++++++ tests/y-xml.tests.js | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/types/YXmlElement.js b/src/types/YXmlElement.js index b3ce6ef9..a0369c92 100644 --- a/src/types/YXmlElement.js +++ b/src/types/YXmlElement.js @@ -3,6 +3,7 @@ import { YXmlFragment, transact, typeMapDelete, + typeMapHas, typeMapSet, typeMapGet, typeMapGetAll, @@ -160,6 +161,18 @@ export class YXmlElement extends YXmlFragment { return /** @type {any} */ (typeMapGet(this, attributeName)) } + /** + * Returns whether an attribute exists + * + * @param {String} attributeName The attribute name to check for existence. + * @return {boolean} whether the attribute exists. + * + * @public + */ + hasAttribute (attributeName) { + return /** @type {any} */ (typeMapHas(this, attributeName)) + } + /** * Returns all attribute name/value pairs in a JSON Object. * diff --git a/tests/y-xml.tests.js b/tests/y-xml.tests.js index 31513091..2a97801c 100644 --- a/tests/y-xml.tests.js +++ b/tests/y-xml.tests.js @@ -15,6 +15,23 @@ export const testSetProperty = tc => { compare(users) } +/** + * @param {t.TestCase} tc + */ + export const testHasProperty = tc => { + const { testConnector, users, xml0, xml1 } = init(tc, { users: 2 }) + xml0.setAttribute('height', '10') + t.assert(xml0.hasAttribute('height'), 'Simple set+has works') + testConnector.flushAllMessages() + t.assert(xml1.hasAttribute('height'), 'Simple set+has works (remote)') + + xml0.removeAttribute('height') + t.assert(!xml0.hasAttribute('height'), 'Simple set+remove+has works') + testConnector.flushAllMessages() + t.assert(!xml1.hasAttribute('height'), 'Simple set+remove+has works (remote)') + compare(users) +} + /** * @param {t.TestCase} tc */