Skip to content

Commit

Permalink
Merge pull request #3149 from Conduitry/preprocess-attribute-parsing-fix
Browse files Browse the repository at this point in the history
preprocess: fix handling of attribute values containing `=`
  • Loading branch information
Rich-Harris authored Jul 1, 2019
2 parents 80bf0fd + f78362d commit 0a14009
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ interface Processed {
dependencies?: string[];
}

function parse_attribute_value(value: string) {
return /^['"]/.test(value) ?
value.slice(1, -1) :
value;
}

function parse_attributes(str: string) {
const attrs = {};
str.split(/\s+/).filter(Boolean).forEach(attr => {
const [name, value] = attr.split('=');
attrs[name] = value ? parse_attribute_value(value) : true;
const p = attr.indexOf('=');
if (p === -1) {
attrs[attr] = true;
} else {
attrs[attr.slice(0, p)] = `'"`.includes(attr[p + 1]) ?
attr.slice(p + 2, -1) :
attr.slice(p + 1);
}
});
return attrs;
}
Expand Down
5 changes: 5 additions & 0 deletions test/preprocess/samples/attributes-with-equals/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
preprocess: {
style: ({ attributes }) => attributes.foo && attributes.foo.includes('=') ? { code: '' } : null
}
};
3 changes: 3 additions & 0 deletions test/preprocess/samples/attributes-with-equals/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<style foo="bar=baz">
foo {}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<style foo="bar=baz"></style>

0 comments on commit 0a14009

Please sign in to comment.