Skip to content

Commit

Permalink
format attribute selectors (#86)
Browse files Browse the repository at this point in the history
closes #85
  • Loading branch information
bartveneman authored Sep 3, 2024
1 parent ac698b2 commit 9b19174
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ const SPACE = ' '
const EMPTY_STRING = ''
const COLON = ':'
const SEMICOLON = ';'
const QUOTE = '"'
const OPEN_PARENTHESES = '('
const CLOSE_PARENTHESES = ')'
const OPEN_BRACKET = '['
const CLOSE_BRACKET = ']'
const TYPE_ATRULE = 'Atrule'
const TYPE_RULE = 'Rule'
const TYPE_BLOCK = 'Block'
Expand Down Expand Up @@ -210,6 +213,29 @@ export function format(css, { minify = false } = {}) {
}
break
}
case 'AttributeSelector': {
buffer += OPEN_BRACKET
buffer += child.name.name

if (child.matcher && child.value) {
buffer += child.matcher
buffer += QUOTE

if (child.value.type === 'String') {
buffer += child.value.value
} else if (child.value.type === 'Identifier') {
buffer += child.value.name
}
buffer += QUOTE
}

if (child.flags) {
buffer += SPACE + child.flags
}

buffer += CLOSE_BRACKET
break
}
default: {
buffer += substr(child)
break
Expand Down
23 changes: 23 additions & 0 deletions test/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,27 @@ test('formats multiline selectors', () => {
assert.is(actual, expected)
})

test('forces attribute selectors to have quoted values', () => {
let actual = format(`
[title=foo],
[title="bar"],
[title='baz'] {}
`)
let expected = `[title="foo"],
[title="bar"],
[title="baz"] {}`
assert.is(actual, expected)
})

test('adds a space before attribute selector flags', () => {
let actual = format(`
[title="foo" i],
[title="baz"i],
[title=foo i] {}
`)
let expected = `[title="foo" i],
[title="baz" i],
[title="foo" i] {}`
assert.is(actual, expected)
})
test.run()

0 comments on commit 9b19174

Please sign in to comment.