From 74d15ea877af6302afe7b89483d0030d152b7537 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 1 Jul 2017 15:25:55 -0400 Subject: [PATCH] handle empty attributes --- src/generators/extractSelectors.ts | 10 +++++----- .../_config.js | 3 +++ .../expected.css | 4 ++++ .../expected.html | 2 ++ .../input.html | 10 ++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/css/samples/omit-scoping-attribute-attribute-selector/_config.js create mode 100644 test/css/samples/omit-scoping-attribute-attribute-selector/expected.css create mode 100644 test/css/samples/omit-scoping-attribute-attribute-selector/expected.html create mode 100644 test/css/samples/omit-scoping-attribute-attribute-selector/input.html diff --git a/src/generators/extractSelectors.ts b/src/generators/extractSelectors.ts index 6fdd40933c..25e0fa8a2c 100644 --- a/src/generators/extractSelectors.ts +++ b/src/generators/extractSelectors.ts @@ -114,8 +114,8 @@ function selectorAppliesTo(parts: Node[], node: Node, stack: Node[]): boolean { function classMatches(node: Node, className: string) { const attr = node.attributes.find((attr: Node) => attr.name === 'class'); - if (!attr) return false; - if (isDynamic(attr)) return true; + if (!attr || attr.value === true) return false; + if (isDynamic(attr.value)) return true; const value = attr.value[0].data; @@ -125,7 +125,7 @@ function classMatches(node: Node, className: string) { function attributeMatches(node: Node, selector: Node) { const attr = node.attributes.find((attr: Node) => attr.name === selector.name.name); if (!attr) return false; - if (isDynamic(attr)) return true; + if (attr.value === true || isDynamic(attr.value)) return true; const expectedValue = unquote(selector.value.value); const actualValue = attr.value[0].data; @@ -137,8 +137,8 @@ function attributeMatches(node: Node, selector: Node) { return true; } -function isDynamic(attr: Node) { - return attr.value.length > 1 || attr.value[0].type !== 'Text'; +function isDynamic(value: Node) { + return value.length > 1 || value[0].type !== 'Text'; } function unquote(str: string) { diff --git a/test/css/samples/omit-scoping-attribute-attribute-selector/_config.js b/test/css/samples/omit-scoping-attribute-attribute-selector/_config.js new file mode 100644 index 0000000000..b37866f9b6 --- /dev/null +++ b/test/css/samples/omit-scoping-attribute-attribute-selector/_config.js @@ -0,0 +1,3 @@ +export default { + cascade: false +}; \ No newline at end of file diff --git a/test/css/samples/omit-scoping-attribute-attribute-selector/expected.css b/test/css/samples/omit-scoping-attribute-attribute-selector/expected.css new file mode 100644 index 0000000000..c57bf43bd4 --- /dev/null +++ b/test/css/samples/omit-scoping-attribute-attribute-selector/expected.css @@ -0,0 +1,4 @@ + + [data-foo][svelte-2966013849] { + color: red; + } diff --git a/test/css/samples/omit-scoping-attribute-attribute-selector/expected.html b/test/css/samples/omit-scoping-attribute-attribute-selector/expected.html new file mode 100644 index 0000000000..77ed0898cf --- /dev/null +++ b/test/css/samples/omit-scoping-attribute-attribute-selector/expected.html @@ -0,0 +1,2 @@ +

this is styled

+

this is unstyled

\ No newline at end of file diff --git a/test/css/samples/omit-scoping-attribute-attribute-selector/input.html b/test/css/samples/omit-scoping-attribute-attribute-selector/input.html new file mode 100644 index 0000000000..eba59af199 --- /dev/null +++ b/test/css/samples/omit-scoping-attribute-attribute-selector/input.html @@ -0,0 +1,10 @@ +
+

this is styled

+

this is unstyled

+
+ + \ No newline at end of file