" converted to " on xml
Thank you for good project! (2)
import { DOMParser } from "linkedom";
console.log(new DOMParser().parseFromString(`<html id="main-window" data-l10n-args="{"content-title":"CONTENTTITLE"}"></html>`, "text/xml").toString());
console.log(new DOMParser().parseFromString(`<html id="main-window" data-l10n-args="{"content-title":"CONTENTTITLE"}"></html>`, "text/html").toString());
❯ node test.js
<?xml version="1.0" encoding="utf-8"?><html id="main-window" data-
l10n-args="{"content-title":"CONTENTTITLE"}" />
<html id="main-window" data-l10n-args="{"content-title":
"CONTENTTITLE"}"></html>
the " changed to ", and it occurs error in xml.
node v20.11.1 linkedom v0.16.10
@WebReflection If I understand this correctly, the escape function in toString doesn't escape quotes, so the replace should be called for XML documents as well.
So this: https://github.com/WebReflection/linkedom/blob/85d9f92a09bb2ecf8e9be1bd468d30257dbba21b/cjs/interface/attr.js#L44-L51
Should change to:
toString() {
const {name, [VALUE]: value} = this;
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}"`;
}
I don't know ... that change is recent and it was to avoid escaping other chars such as & but not single quote or others ... the double quote escape is actually due common double quotes used around attributes but I feel like there's no right answer here ... or better, I would like to read an exhaustive example and and a test with expectations and never change this again, thanks!
it is double quote issue.
Can close