From 3215f5a98e96b4caa1cd0fe66f282fab17c29ec9 Mon Sep 17 00:00:00 2001 From: Dmitry Semigradsky Date: Mon, 11 Jan 2021 05:20:51 +0300 Subject: [PATCH] fix: incorrect handling elemental selector without a namespace (#394) --- src/__fixtures__/tests.ts | 28 ++++++++++++++++++++++++++++ src/parse.ts | 4 ++++ src/stringify.ts | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/__fixtures__/tests.ts b/src/__fixtures__/tests.ts index 83f4bf10..740e6dae 100644 --- a/src/__fixtures__/tests.ts +++ b/src/__fixtures__/tests.ts @@ -843,6 +843,19 @@ export const tests: [ ], "star tag namespace", ], + [ + "|bar", + [ + [ + { + name: "bar", + type: "tag", + namespace: "", + }, + ], + ], + "without namespace", + ], [ "*|*", [ @@ -870,6 +883,21 @@ export const tests: [ ], "basic attribute namespace, existential", ], + [ + "[|bar]", + [ + [ + { + action: "exists", + name: "bar", + type: "attribute", + namespace: "", + value: "", + }, + ], + ], + "without namespace, existential", + ], [ "[foo|bar='baz' i]", [ diff --git a/src/parse.ts b/src/parse.ts index 774f40ab..9edfa777 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -391,6 +391,10 @@ function parseSelector( selectorIndex += 1; name = "*"; } else if (reName.test(selector.slice(selectorIndex))) { + if (selector.charAt(selectorIndex) === "|") { + namespace = ""; + selectorIndex += 1; + } name = getName(0); } else { /* diff --git a/src/stringify.ts b/src/stringify.ts index 70e8757e..88626029 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -106,7 +106,7 @@ function getNamespacedName(token: { } function getNamespace(namespace: string | null): string { - return namespace + return namespace !== null ? `${namespace === "*" ? "*" : escapeName(namespace)}|` : ""; }