Skip to content

Build your personal knowledge base with Trilium Notes

fix(attributes): escape commas in a definition's alias (closes #6853)

matt wilkie52730883c7cd
diff --git a/packages/commons/src/lib/promoted_attribute_definition_parser.spec.ts b/packages/commons/src/lib/promoted_attribute_definition_parser.spec.ts
index 00ae57e857..d3189d5a0d 100644
--- a/packages/commons/src/lib/promoted_attribute_definition_parser.spec.ts
+++ b/packages/commons/src/lib/promoted_attribute_definition_parser.spec.ts
@@ -62,8 +62,10 @@ describe("promoted_attribute_definition_parser.parse", () => {
expect(parse("options")).toEqual({ selectOptions: [] });
});
- it("parses the promoted alias verbatim, including spaces", () => {
+ it("parses the promoted alias, keeping spaces and decoding the escapes", () => {
expect(parse("alias=My Label")).toEqual({ promotedAlias: "My Label" });
+ // `%2C`/`%25` carry the characters the format claims for itself.
+ expect(parse("alias=My%2C Label 100%25")).toEqual({ promotedAlias: "My, Label 100%" });
expect(parse("alias=")).toEqual({ promotedAlias: "" });
// A missing value yields undefined rather than throwing.
expect(parse("alias")).toEqual({ promotedAlias: undefined });
@@ -127,6 +129,13 @@ describe("promoted_attribute_definition_parser.serialize", () => {
expect(serialize({ isPromoted: true }, "label")).toBe("promoted,single,text");
});
+ it("escapes the token separator and the escape character in the alias", () => {
+ expect(serialize({ promotedAlias: "myLabel, very pretty" }, "label"))
+ .toBe("alias=myLabel%2C very pretty,single,text");
+ // `;` separates nothing at this level, so it is written verbatim.
+ expect(serialize({ promotedAlias: "50%; a" }, "relation")).toBe("alias=50%25; a,single");
+ });
+
it("falls back to the multiplicity and label type the consumers assume", () => {
expect(serialize({}, "label")).toBe("single,text");
expect(serialize({}, "relation")).toBe("single");
@@ -192,6 +201,22 @@ describe("promoted_attribute_definition_parser round-trip", () => {
}
});
+ it("round-trips an alias through the escaping, edge cases included", () => {
+ // A fragment after a comma can spell a live token, and must not be read as one.
+ for (const promotedAlias of [
+ "myLabel, very pretty", "Size, number", "Foo, promoted", "a, multi, options=x",
+ "100%", "50%25", "%2C", "a=b", "a;b"
+ ]) {
+ expect(parse(serialize({ promotedAlias, labelType: "text" }, "label"))).toEqual({
+ promotedAlias, multiplicity: "single", labelType: "text"
+ });
+ }
+
+ expect(parse(serialize({ promotedAlias: "Parent, inverse=x" }, "relation"))).toEqual({
+ promotedAlias: "Parent, inverse=x", multiplicity: "single"
+ });
+ });
+
it("parses a serialized definition back to the same object, for every label type", () => {
for (const labelType of LABEL_TYPES) {
const definition = {
diff --git a/packages/commons/src/lib/promoted_attribute_definition_parser.ts b/packages/commons/src/lib/promoted_attribute_definition_parser.ts
index c51af6468a..70945a6b97 100644
--- a/packages/commons/src/lib/promoted_attribute_definition_parser.ts
+++ b/packages/commons/src/lib/promoted_attribute_definition_parser.ts
@@ -36,6 +36,9 @@ export interface DefinitionObject {
* so a definition written by a newer version degrades to its recognized parts instead of being
* lost.
*
+ * The alias and the select options are stored escaped, see {@link encodeAlias} and
+ * {@link encodeOption}, and are decoded here.
+ *
* @param value the raw attribute value of a `label:`/`relation:` definition attribute.
*/
function parse(value: string): DefinitionObject {
@@ -60,7 +63,8 @@ function parse(value: string): DefinitionObject {
.filter((option) => option !== "")
.map(decodeOption);
} else if (token.startsWith("alias")) {
- defObj.promotedAlias = parameterValue(token);
+ const alias = parameterValue(token);
+ defObj.promotedAlias = alias === undefined ? undefined : decodeAlias(alias);
} else if (token.startsWith("inverse")) {
// An inverse relation is a relation name, so the same characters are dropped as when
// one is written by `serialize`. Definitions reaching us from an import, ETAPI or a
@@ -81,6 +85,9 @@ function parse(value: string): DefinitionObject {
* The multiplicity and the label type are always written out, falling back to the defaults their
* consumers assume (`single` and `text`) rather than to the empty token {@link parse} would reject.
*
+ * The alias and the select options are free text, so they are escaped to keep the token separator
+ * out of the stored value, see {@link encodeAlias} and {@link encodeOption}.
+ *
* @param definition the definition to serialize.
* @param valueType whether the definition describes a label or a relation, which decides between
* writing a label type and writing an inverse relation.
@@ -95,7 +102,7 @@ function serialize(definition: DefinitionObject, valueType: "label" | "relation"
// Not only the promoted field's label: the table view titles its columns with it and the attribute
// list names its values by it, neither of which asks whether the attribute is promoted.
if (definition.promotedAlias) {
- props.push(`alias=${definition.promotedAlias}`);
+ props.push(`alias=${encodeAlias(definition.promotedAlias)}`);
}
props.push(definition.multiplicity ?? "single");
@@ -166,6 +173,21 @@ function decodeOption(option: string): string {
return option.replace(/%2C/gi, ",").replace(/%3B/gi, ";").replace(/%25/gi, "%");
}
+/**
+ * Escapes an alias for storage inside the `alias=` token, where a raw `,` would be cut by the
+ * definition tokenizer. Uses the same substitution as {@link encodeOption}, which explains the
+ * choice; `;` separates nothing in an alias, so it is stored verbatim.
+ */
+function encodeAlias(alias: string): string {
+ // `%` first, so the escape written below is not re-escaped.
+ return alias.replace(/%/g, "%25").replace(/,/g, "%2C");
+}
+
+/** The inverse of {@link encodeAlias}: `%25` last, so a decoded `%` cannot start a triplet. */
+function decodeAlias(alias: string): string {
+ return alias.replace(/%2C/gi, ",").replace(/%25/gi, "%");
+}
+
function isLabelType(token: string): token is LabelType {
return (LABEL_TYPES as readonly string[]).includes(token);
}