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

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

Loading…
Cancel
Save