From 9eaea2ba9494efd8329bd61e64a0a6c50e4d06db Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 2 Dec 2017 23:54:11 -0500 Subject: [PATCH] fix attribute parsing --- src/index.ts | 12 +++++------- test/preprocess/index.js | 3 ++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index e4353fdff7..081660d3d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,19 +35,17 @@ function defaultOnerror(error: Error) { throw error; } -function parseAttributeValue(value: string | boolean) { - const curated = (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; } diff --git a/test/preprocess/index.js b/test/preprocess/index.js index ea1a519655..e07bc5e718 100644 --- a/test/preprocess/index.js +++ b/test/preprocess/index.js @@ -112,13 +112,14 @@ describe.only('preprocess', () => { it('parses attributes', () => { const source = ` - + `; return svelte.preprocess(source, { style: ({ attributes }) => { assert.deepEqual(attributes, { type: 'text/scss', + 'data-foo': 'bar', bool: true }); }