From 5c9bf42139a532d6b5e1f7b8a3febdfb652a686f Mon Sep 17 00:00:00 2001 From: dsimpsonOMF <57110869+dsimpsonOMF@users.noreply.github.com> Date: Tue, 31 Aug 2021 23:08:01 -0500 Subject: [PATCH 1/3] fix: Prototype fields conflict with dom attributes As noted in issue #252 some attributes conflict with prototype fields (such as an attribute `constructor`) thus we must remove the `in` operator and replace with `Object.prototype.hasOwnProperty()`. --- lib/sax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sax.js b/lib/sax.js index a7fa74ba6..ccc50d9a6 100644 --- a/lib/sax.js +++ b/lib/sax.js @@ -230,7 +230,7 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error * @param {number} startIndex */ function addAttribute(qname, value, startIndex) { - if (qname in el.attributeNames) errorHandler.fatalError('Attribute ' + qname + ' redefined') + if (el.attributeNames.hasOwnProperty(qname)) errorHandler.fatalError('Attribute ' + qname + ' redefined') el.addValue(qname, value, startIndex) } var attrName; From db7dce64d08482bc968c0961b19c707cc831ad68 Mon Sep 17 00:00:00 2001 From: Christian Bewernitz Date: Wed, 1 Sep 2021 08:27:27 +0200 Subject: [PATCH 2/3] style: Prettify changed code --- lib/sax.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/sax.js b/lib/sax.js index ccc50d9a6..6795df61c 100644 --- a/lib/sax.js +++ b/lib/sax.js @@ -230,7 +230,9 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error * @param {number} startIndex */ function addAttribute(qname, value, startIndex) { - if (el.attributeNames.hasOwnProperty(qname)) errorHandler.fatalError('Attribute ' + qname + ' redefined') + if (el.attributeNames.hasOwnProperty(qname)) { + errorHandler.fatalError('Attribute ' + qname + ' redefined') + } el.addValue(qname, value, startIndex) } var attrName; From 6026d1195a52d7edf927bbfc2380654075a3ead8 Mon Sep 17 00:00:00 2001 From: Christian Bewernitz Date: Wed, 1 Sep 2021 08:28:14 +0200 Subject: [PATCH 3/3] test: Add test for fix --- .../__snapshots__/parse-element.test.js.snap | 12 +++++++++++ test/parse/parse-element.test.js | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/test/parse/__snapshots__/parse-element.test.js.snap b/test/parse/__snapshots__/parse-element.test.js.snap index b4f7dcba1..50e2ddfd4 100644 --- a/test/parse/__snapshots__/parse-element.test.js.snap +++ b/test/parse/__snapshots__/parse-element.test.js.snap @@ -10,6 +10,18 @@ Object { } `; +exports[`XML Node Parse simple attributes should be able to have \`__prototype__\` attribute 1`] = ` +Object { + "actual": "", +} +`; + +exports[`XML Node Parse simple attributes should be able to have \`constructor\` attribute 1`] = ` +Object { + "actual": "", +} +`; + exports[`XML Node Parse simple attributes unclosed root tag will be closed 1`] = ` Object { "actual": "", diff --git a/test/parse/parse-element.test.js b/test/parse/parse-element.test.js index 404b9cb7e..3f550b996 100644 --- a/test/parse/parse-element.test.js +++ b/test/parse/parse-element.test.js @@ -81,6 +81,26 @@ describe('XML Node Parse', () => { expect({ actual, ...errors }).toMatchSnapshot() }) + + it('should be able to have `constructor` attribute', () => { + const { errors, parser } = getTestParser() + + const actual = parser + .parseFromString('', 'text/xml') + .toString() + + expect({ actual, ...errors }).toMatchSnapshot() + }) + + it('should be able to have `__prototype__` attribute', () => { + const { errors, parser } = getTestParser() + + const actual = parser + .parseFromString('', 'text/xml') + .toString() + + expect({ actual, ...errors }).toMatchSnapshot() + }) }) describe('namespaced attributes', () => {