Skip to content

Commit 8379129

Browse files
committed
Serialize <attr-matcher> with no leading and trailing whitespaces
1 parent 095e244 commit 8379129

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/serialize.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ function serializeAttribute({ value }) {
133133
return string
134134
}
135135

136+
/**
137+
* @param {object} selector
138+
* @returns {string}
139+
* @see {@link https://drafts.csswg.org/selectors-4/#typedef-attribute-selector}
140+
*/
141+
function serializeAttributeSelector({ value }) {
142+
if (isList(value)) {
143+
value = value.map(serializeComponentValue)
144+
const modifier = value.pop()
145+
value = list([list(value, ''), modifier])
146+
}
147+
return `[${serializeComponentValue(value)}]`
148+
}
149+
136150
/**
137151
* @param {object[]} arc
138152
* @returns {string}
@@ -1657,6 +1671,8 @@ function serializeComponentValue(value, context) {
16571671
return serializeColorStop(value)
16581672
case '<attr()>':
16591673
return serializeAttribute(value)
1674+
case '<attribute-selector>':
1675+
return serializeAttributeSelector(value)
16601676
case '<arc-command>':
16611677
return serializeArcCommand(value)
16621678
case '<bad-string-token>':

test/value.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,7 @@ describe('<attr-matcher>', () => {
24272427
})
24282428
test('valid', () => {
24292429
assert.valid('<attribute-selector>', '[attr|=value]')
2430+
assert.valid('<attribute-selector>', '[attr |= value i]', '[attr|=value i]')
24302431
})
24312432
})
24322433
describe('<attr-name>', () => {
@@ -4202,7 +4203,7 @@ describe('<selector-list>', () => {
42024203
test('valid', () => {
42034204
const valid = [
42044205
// No invalid whitespace
4205-
['#id.class[ *|attr ^= value ]:hover > [attr=value]::before', '#id.class[*|attr ^= value]:hover > [attr = value]::before'],
4206+
['#id.class[ *|attr ^= value ]:hover > [attr=value]::before', '#id.class[*|attr^=value]:hover > [attr=value]::before'],
42064207
['html|*'],
42074208
['html|a'],
42084209
['*|a'],

0 commit comments

Comments
 (0)