Skip to content

Commit

Permalink
Fix issue #267: escape double quotes in XML attributes' toString
Browse files Browse the repository at this point in the history
  • Loading branch information
Omri Luzon committed May 12, 2024
1 parent 992b74f commit bfe2274
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cjs/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Attr extends Node {
if (emptyAttributes.has(name) && !value) {
return ignoreCase(this) ? name : `${name}=""`;
}
const escapedValue = ignoreCase(this) ? value.replace(QUOTE, '"') : escape(value);
const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"');
return `${name}="${escapedValue}"`;
}

Expand Down
2 changes: 1 addition & 1 deletion esm/interface/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Attr extends Node {
if (emptyAttributes.has(name) && !value) {
return ignoreCase(this) ? name : `${name}=""`;
}
const escapedValue = ignoreCase(this) ? value.replace(QUOTE, '"') : escape(value);
const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"');
return `${name}="${escapedValue}"`;
}

Expand Down
10 changes: 9 additions & 1 deletion test/interface/attr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require('../assert.js').for('Attr');

const {parseHTML} = global[Symbol.for('linkedom')];
const {parseHTML, DOMParser} = global[Symbol.for('linkedom')];

const {document} = parseHTML('<html test />');

Expand All @@ -20,3 +20,11 @@ assert(attributes.removeNamedItem('test'), void 0, 'removeNamedItem');
assert(attributes.item(0), null, 'attributes.item()');
assert(attributes.setNamedItem(attr), void 0, 'setNamedItem');

/** @type {(str: string) => Document} */
const parseXML = xmlStr => new DOMParser().parseFromString(xmlStr, 'text/xml');
const xmlDoc = parseXML('<element attr="a&quot;b&quot;c"></element>');

assert(xmlDoc.toString(), '<?xml version="1.0" encoding="utf-8"?><element attr="a&quot;b&quot;c" />');
assert(xmlDoc.firstElementChild.toString(), '<element attr="a&quot;b&quot;c" />');
assert(xmlDoc.firstElementChild.outerHTML, '<element attr="a&quot;b&quot;c" />');
assert(xmlDoc.firstElementChild.attributes.attr.value, 'a"b"c');

0 comments on commit bfe2274

Please sign in to comment.