Skip to content

Commit

Permalink
Merge pull request #800 from sveltejs/gh-798
Browse files Browse the repository at this point in the history
handle unquoted attribute values
  • Loading branch information
Rich-Harris authored Aug 30, 2017
2 parents 835a48b + 3968156 commit 7a9ac95
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/css/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function applySelector(blocks: Block[], node: Node, stack: Node[], toEncapsulate
}

else if (selector.type === 'AttributeSelector') {
if (!attributeMatches(node, selector.name.name, selector.value && unquote(selector.value.value), selector.operator, selector.flags)) return false;
if (!attributeMatches(node, selector.name.name, selector.value && unquote(selector.value), selector.operator, selector.flags)) return false;
}

else if (selector.type === 'TypeSelector') {
Expand Down Expand Up @@ -245,10 +245,13 @@ function isDynamic(value: Node) {
return value.length > 1 || value[0].type !== 'Text';
}

function unquote(str: string) {
function unquote(value: Node) {
if (value.type === 'Identifier') return value.name;
const str = value.value;
if (str[0] === str[str.length - 1] && str[0] === "'" || str[0] === '"') {
return str.slice(1, str.length - 1);
}
return str;
}

class Block {
Expand Down
3 changes: 3 additions & 0 deletions test/css/samples/attribute-selector-unquoted/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
cascade: false
};
1 change: 1 addition & 0 deletions test/css/samples/attribute-selector-unquoted/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[foo=bar][svelte-xyz]{color:red}
7 changes: 7 additions & 0 deletions test/css/samples/attribute-selector-unquoted/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div foo='bar'></div>

<style>
[foo=bar] {
color: red;
}
</style>

0 comments on commit 7a9ac95

Please sign in to comment.