Skip to content

Commit

Permalink
[FIX] compiler: fix xmlns attribute not being set correctly in firefox
Browse files Browse the repository at this point in the history
Firefox will not serialize the xmlns attributes of node inside
XMLDocuments, see https://bugzilla.mozilla.org/show_bug.cgi?id=175946

To work around this issue, we replace the xmlns attribute (which is
present but won't be serialized) with a different attribute before
serializing, and then replace that attribute back with xmlns after
serialization. While this is not perfect, the risk of collision with
user content inside a template is minimal.
  • Loading branch information
sdegueldre committed Aug 4, 2023
1 parent 105ec7c commit 028cabf
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/compiler/code_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export interface CodeGenOptions extends Config {
// of HTML (as we will parse it as xml later)
const xmlDoc = document.implementation.createDocument(null, null, null);

// The following is a workaround for firefox not serializing the xmlns attribute
// https://bugzilla.mozilla.org/show_bug.cgi?id=175946
const nameSpaceEl = xmlDoc.createElement("t");
nameSpaceEl.setAttribute("xmlns", "http://www.w3.org/2000/svg");
const canSerializeXmlns = nameSpaceEl.outerHTML.includes("xmlns")

const MODS = new Set(["stop", "capture", "prevent", "self", "synthetic"]);

let nextDataIds: { [key: string]: number } = {};
Expand Down Expand Up @@ -148,7 +154,18 @@ class BlockDescription {
// append dom to any element and use innerHTML instead
const t = xmlDoc.createElement("t");
t.appendChild(this.dom!);
return t.innerHTML;
let xmlString = t.innerHTML;
if (canSerializeXmlns || !t.querySelector("[xmlns]")) {
return xmlString;
}
// The following is a workaround for firefox not serializing the xmlns attribute
// https://bugzilla.mozilla.org/show_bug.cgi?id=175946
t.querySelectorAll("[xmlns]").forEach((node) => {
const nameSpace = node.getAttribute("xmlns");
node.removeAttribute("xmlns");
node.setAttribute("__firefox_bug_175946_xmlns", nameSpace!);
});
return t.innerHTML.replace(/__firefox_bug_175946_xmlns/g, "xmlns");
}
}

Expand Down

0 comments on commit 028cabf

Please sign in to comment.