Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): keep non-breaking spaces in attribute values

Elian Doran882c6ff7868d
diff --git a/packages/trilium-core/src/services/export/format_html.spec.ts b/packages/trilium-core/src/services/export/format_html.spec.ts
index f22f9abfb8..86ac2e3ddd 100644
--- a/packages/trilium-core/src/services/export/format_html.spec.ts
+++ b/packages/trilium-core/src/services/export/format_html.spec.ts
@@ -76,6 +76,8 @@ describe("formatHtml", () => {
.toBe([ `<div id="d">`, ` <p>See <a class="reference-link" href='a b' data-x=1>x</a>.</p>`, "</div>" ].join("\n"));
expect(formatHtml(`<pre class="x"><code class="y"> <b >keep</b></code></pre>`))
.toBe(`<pre class="x"><code class="y"> <b >keep</b></code></pre>`);
+ expect(formatHtml(`<p><a href=chapter one.html data-x =1>x</a></p>`))
+ .toBe(`<p><a href=chapter one.html data-x =1>x</a></p>`);
});
it("formats a container with more blocks than a call takes arguments", () => {
diff --git a/packages/trilium-core/src/services/export/format_html.ts b/packages/trilium-core/src/services/export/format_html.ts
index f1a18d1e98..d688df3827 100644
--- a/packages/trilium-core/src/services/export/format_html.ts
+++ b/packages/trilium-core/src/services/export/format_html.ts
@@ -97,11 +97,18 @@ function normalizeAllAttributes(root: HTMLElement) {
}
}
-/** An attribute name, optionally followed by `=` and a quoted or unquoted value. */
-const ATTRIBUTE = /[^\s"'<>/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'<>=`]+))?/g;
+/**
+ * An attribute name, optionally followed by `=` and a quoted or unquoted value. HTML separates
+ * attributes only at ASCII whitespace, so `[\t\n\f\r ]` stands in for `\s`, which also matches
+ * U+00A0 and the other Unicode spaces.
+ */
+const ATTRIBUTE = /[^\t\n\f\r "'<>/=]+(?:[\t\n\f\r ]*=[\t\n\f\r ]*(?:"[^"]*"|'[^']*'|[^\t\n\f\r "'<>=`]+))?/g;
+
+/** The name of an attribute and its `=`, with the whitespace around the `=`. */
+const NAME_AND_EQUALS = /^([^\t\n\f\r =]+)[\t\n\f\r ]*=[\t\n\f\r ]*/;
/** Joins the attributes of `rawAttrs` with one space, without touching the text of their values. */
function normalizeAttributes(rawAttrs: string) {
- return Array.from(rawAttrs.matchAll(ATTRIBUTE), ([ attribute ]) => attribute.replace(/^([^=\s]+)\s*=\s*/, "$1="))
+ return Array.from(rawAttrs.matchAll(ATTRIBUTE), ([ attribute ]) => attribute.replace(NAME_AND_EQUALS, "$1="))
.join(" ");
}