Skip to content

Commit

Permalink
fix: handle attribute selectors having double quotes in their value c…
Browse files Browse the repository at this point in the history
…orrectly
  • Loading branch information
ktsn committed May 5, 2018
1 parent 68be320 commit 5f2e950
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/parser/style/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export function genSelector(s: t.Selector): string {
const attrsCodes = s.attributes.map(attr => {
if (attr.operator != null && attr.value != null) {
const suffix = attr.insensitive ? ' i' : ''
return `[${attr.name}${attr.operator}"${attr.value}"${suffix}]`
const quote = attr.value.includes('"') ? "'" : '"'
return `[${attr.name}${attr.operator}${quote}${
attr.value
}${quote}${suffix}]`
} else {
return `[${attr.name}]`
}
Expand Down
13 changes: 13 additions & 0 deletions test/parser/style/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,17 @@ describe('Style codegen', () => {

expect(genStyle(ast)).toBe(expected)
})

it('uses single quote for attribute value when it has double quote', () => {
const ast = createStyle([
rule([
selector({
attributes: [attribute('value', '=', '"quoted"')]
})
])
])
const expected = '[value=\'"quoted"\'] {}'

expect(genStyle(ast)).toBe(expected)
})
})

0 comments on commit 5f2e950

Please sign in to comment.