implement all attribute selector operators

pull/689/head
Rich Harris 8 years ago
parent 3dfe92b54f
commit 45dd99d6db

@ -122,6 +122,15 @@ function classMatches(node: Node, className: string) {
return value.split(' ').indexOf(className) !== -1;
}
const operators = {
'=' : (value: string, flags: string) => new RegExp(`^${value}$`, flags),
'~=': (value: string, flags: string) => new RegExp(`\\b${value}\\b`, flags),
'|=': (value: string, flags: string) => new RegExp(`^${value}(-.+)?$`, flags),
'^=': (value: string, flags: string) => new RegExp(`^${value}`, flags),
'$=': (value: string, flags: string) => new RegExp(`${value}$`, flags),
'*=': (value: string, flags: string) => new RegExp(value, flags)
};
function attributeMatches(node: Node, selector: Node) {
const attr = node.attributes.find((attr: Node) => attr.name === selector.name.name);
if (!attr) return false;
@ -130,15 +139,8 @@ function attributeMatches(node: Node, selector: Node) {
const expectedValue = unquote(selector.value.value);
const actualValue = attr.value[0].data;
if (selector.operator === '=') {
return actualValue === expectedValue;
}
if(selector.operator === '~=') {
return actualValue.split(/\s/).indexOf(expectedValue) !== -1;
}
return true;
const pattern = operators[selector.operator](expectedValue, selector.flags || '');
return pattern.test(actualValue);
}
function isDynamic(value: Node) {

@ -0,0 +1,4 @@
[data-foo*='bar'][svelte-4224841812] {
color: red;
}

@ -0,0 +1,2 @@
<div><p svelte-4224841812="" data-foo="foobarbaz">this is styled</p>
<p data-foo="fooBARbaz">this is unstyled</p></div>

@ -0,0 +1,10 @@
<div>
<p data-foo='foobarbaz'>this is styled</p>
<p data-foo='fooBARbaz'>this is unstyled</p>
</div>
<style>
[data-foo*='bar'] {
color: red;
}
</style>

@ -0,0 +1,2 @@
<div><p svelte-4191913977="" data-foo="BAR">this is styled</p>
<p data-foo="BAZ">this is unstyled</p></div>

@ -0,0 +1,10 @@
<div>
<p data-foo='BAR'>this is styled</p>
<p data-foo='BAZ'>this is unstyled</p>
</div>
<style>
[data-foo='bar' i] {
color: red;
}
</style>

@ -0,0 +1,4 @@
[data-foo|='bar'][svelte-1225676040] {
color: red;
}

@ -0,0 +1,3 @@
<div><p svelte-1225676040="" data-foo="bar">this is styled</p>
<p svelte-1225676040="" data-foo="bar-baz">this is styled</p>
<p data-foo="baz-bar">this is unstyled</p></div>

@ -0,0 +1,11 @@
<div>
<p data-foo='bar'>this is styled</p>
<p data-foo='bar-baz'>this is styled</p>
<p data-foo='baz-bar'>this is unstyled</p>
</div>
<style>
[data-foo|='bar'] {
color: red;
}
</style>

@ -0,0 +1,4 @@
[data-foo^='bar'][svelte-3106767242] {
color: red;
}

@ -0,0 +1,2 @@
<div><p svelte-3106767242="" data-foo="barbaz">this is styled</p>
<p data-foo="bazbar">this is unstyled</p></div>

@ -0,0 +1,10 @@
<div>
<p data-foo='barbaz'>this is styled</p>
<p data-foo='bazbar'>this is unstyled</p>
</div>
<style>
[data-foo^='bar'] {
color: red;
}
</style>

@ -0,0 +1,4 @@
[data-foo$='bar'][svelte-207782622] {
color: red;
}

@ -0,0 +1,2 @@
<div><p data-foo="barbaz">this is unstyled</p>
<p svelte-207782622="" data-foo="bazbar">this is styled</p></div>

@ -0,0 +1,10 @@
<div>
<p data-foo='barbaz'>this is unstyled</p>
<p data-foo='bazbar'>this is styled</p>
</div>
<style>
[data-foo$='bar'] {
color: red;
}
</style>

@ -1,6 +0,0 @@
export default {
cascade: false,
data: {
dynamic: 'whatever'
}
};

@ -1,2 +0,0 @@
<div><p svelte-25731322="" data-foo="whatever">this is styled</p>
<p data-foo="qux baz">this is unstyled</p></div>

@ -1,10 +0,0 @@
<div>
<p data-foo='{{dynamic}}'>this is styled</p>
<p data-foo='qux baz'>this is unstyled</p>
</div>
<style>
[data-foo~='bar'] {
color: red;
}
</style>
Loading…
Cancel
Save