Skip to content

Commit 9eaea2b

Browse files
committed
fix attribute parsing
1 parent 6af51e8 commit 9eaea2b

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ function defaultOnerror(error: Error) {
3535
throw error;
3636
}
3737

38-
function parseAttributeValue(value: string | boolean) {
39-
const curated = (<string>value).replace(/"/ig, '');
40-
if (curated === 'true' || curated === 'false') {
41-
return curated === 'true';
42-
}
43-
return curated;
38+
function parseAttributeValue(value: string) {
39+
return /^['"]/.test(value) ?
40+
value.slice(1, -1) :
41+
value;
4442
}
4543

4644
function parseAttributes(str: string) {
4745
const attrs = {};
4846
str.split(/\s+/).filter(Boolean).forEach(attr => {
4947
const [name, value] = attr.split('=');
50-
attrs[name] = parseAttributeValue(value);
48+
attrs[name] = value ? parseAttributeValue(value) : true;
5149
});
5250
return attrs;
5351
}

test/preprocess/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ describe.only('preprocess', () => {
112112

113113
it('parses attributes', () => {
114114
const source = `
115-
<style type='text/scss' bool></style>
115+
<style type='text/scss' data-foo="bar" bool></style>
116116
`;
117117

118118
return svelte.preprocess(source, {
119119
style: ({ attributes }) => {
120120
assert.deepEqual(attributes, {
121121
type: 'text/scss',
122+
'data-foo': 'bar',
122123
bool: true
123124
});
124125
}

0 commit comments

Comments
 (0)