fix attribute parsing

pull/969/head
Rich Harris 7 years ago
parent 6af51e8f55
commit 9eaea2ba94

@ -35,19 +35,17 @@ function defaultOnerror(error: Error) {
throw error; throw error;
} }
function parseAttributeValue(value: string | boolean) { function parseAttributeValue(value: string) {
const curated = (<string>value).replace(/"/ig, ''); return /^['"]/.test(value) ?
if (curated === 'true' || curated === 'false') { value.slice(1, -1) :
return curated === 'true'; value;
}
return curated;
} }
function parseAttributes(str: string) { function parseAttributes(str: string) {
const attrs = {}; const attrs = {};
str.split(/\s+/).filter(Boolean).forEach(attr => { str.split(/\s+/).filter(Boolean).forEach(attr => {
const [name, value] = attr.split('='); const [name, value] = attr.split('=');
attrs[name] = parseAttributeValue(value); attrs[name] = value ? parseAttributeValue(value) : true;
}); });
return attrs; return attrs;
} }

@ -112,13 +112,14 @@ describe.only('preprocess', () => {
it('parses attributes', () => { it('parses attributes', () => {
const source = ` const source = `
<style type='text/scss' bool></style> <style type='text/scss' data-foo="bar" bool></style>
`; `;
return svelte.preprocess(source, { return svelte.preprocess(source, {
style: ({ attributes }) => { style: ({ attributes }) => {
assert.deepEqual(attributes, { assert.deepEqual(attributes, {
type: 'text/scss', type: 'text/scss',
'data-foo': 'bar',
bool: true bool: true
}); });
} }

Loading…
Cancel
Save