Skip to content

Commit 6d4b410

Browse files
committed
Minor things
1 parent 099f320 commit 6d4b410

File tree

8 files changed

+40
-41
lines changed

8 files changed

+40
-41
lines changed

__tests__/stylesheet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
INVALID_RULE_INDEX_ERROR,
1010
INVALID_RULE_POSITION_ERROR,
1111
MISSING_RULE_ERROR,
12-
SET_INVALID_KEYFRAME_SELECTOR_ERROR,
1312
SET_INVALID_KEYFRAMES_NAME_ERROR,
13+
SET_INVALID_KEYFRAME_SELECTOR_ERROR,
1414
UPDATE_LOCKED_STYLESHEET_ERROR,
1515
} from '../lib/error.js'
1616
import {
@@ -1131,7 +1131,7 @@ describe('CSS grammar - syntax', () => {
11311131
`)
11321132

11331133
expect(cssRules[0].cssText).toBe(`style { ${declarations}; }`)
1134-
expect(cssRules[1].cssText).toBe(`@media (;) {}`)
1134+
expect(cssRules[1].cssText).toBe('@media (;) {}')
11351135
})
11361136
test('positioned {} block in a declaration value not for a custom property', () => {
11371137
// It is always consumed as a rule

__tests__/value.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ describe('backtracking', () => {
549549
*
550550
* 1. Replacing must only apply once (ie. not after backtracking).
551551
* 2. The list index must backtrack to a location stored in state instead of
552-
* from the index of a component value in the list.
552+
* from the index of a token in the list.
553553
* 3. The list must not be updated with the result of parsing because it
554554
* may be different depending on the context production.
555555
*/

doc/parse/parser.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ A CSS parser must backtrack after failing to match a component value or when the
110110

111111
It must successfully parse `a a b` against `[a | a a] b`, or `a a` against `a | a a`, by backtracking, instead of failing to find a match after the first choice in `a | a a` (greedy parser), or instead of looking ahead or re-ordering `a | a a` to `a a | a` to find the longest match (maximal munch parser).
112112

113-
**Note:** an obvious requirement that follows is that an input component value must not be tagged with the name of a production that was matched before backtracking.
113+
**Note:** an obvious requirement that follows is that an input token must not be tagged with the name of a production that was matched before backtracking.
114114

115-
Backtracking requires saving the index in the input list of component values, before parsing a node. If the tail node fails to be parsed and cannot yield an alternative result, it must be removed from the tree before backtracking again. Any sequence of symbols combined with `|`, `||`, `&&`, or any symbol qualified by a multiplier where `min < max`, yield alternatives.
115+
Backtracking requires saving the index in the input list of tokens, before parsing a node. If the tail node fails to be parsed and cannot yield an alternative result, it must be removed from the tree before backtracking again. Any sequence of symbols combined with `|`, `||`, `&&`, or any symbol qualified by a multiplier where `min < max`, yield alternatives.
116116

117117
One would expect to read combined symbols in the same direction as the input. For example, `a a` would match the first alternative in `[a | a a] a?` (`a?` would not be omitted). But the specifications do not define such priority in alternations (`|`) and arrangements (`||`).
118118

@@ -171,7 +171,7 @@ As an example of a semantic context rule, there must be a whitespace preceding `
171171

172172
A simple solution is to define the nodes representing `+` and `-` with a boolean flag like `requireLeadingWhitespace`.
173173

174-
**Note:** whitespaces are usually optionals between component values, which means that failing to consume a whitespace before matching a component value must not cause a parse failure.
174+
**Note:** whitespaces are usually optionals between tokens, which means that failing to consume a whitespace before matching a component value must not cause a parse failure.
175175

176176
Other semantic rules can be encoded in nodes. For example, math functions must support at least 32 arguments, which means the default value (20) of the `#` multiplier of `<calc-sum>` must be overriden. `#` must also be ignored in the top-level value definition expanded from a property value range (eg. `<'color'>`).
177177

lib/parse/grammar.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,11 @@ function* children(parent, alternative) {
302302
const max = getAlternativeMaxLength(definition, alternative)
303303
let index = Math.max(0, children.length - 1)
304304
while (index < max) {
305-
let node = children[index]
306-
if (node) {
307-
children.pop()
305+
let node
306+
if (children[index]) {
307+
node = children.pop()
308+
} else if (separator && index % 2) {
309+
node = create({ type: 'token', value: separator }, input, context, parent)
308310
} else if (type === 'repetition') {
309311
node = create(alternative[0], input, context, parent)
310312
} else {
@@ -313,12 +315,6 @@ function* children(parent, alternative) {
313315
yield node
314316
children.push(node)
315317
++index
316-
if (separator && index < max) {
317-
const node = create({ type: 'token', value: separator }, input, context, parent)
318-
yield node
319-
children.push(node)
320-
++index
321-
}
322318
}
323319
}
324320

lib/parse/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ function parseGrammar(input, definition, context, strategy = 'backtrack') {
11071107
let match = grammar.parse(root)
11081108
while (!isFailure(match) && !isInputAtEnd(root)) {
11091109
if (strategy === 'greedy') {
1110-
match = error({ message: 'Unexpected remaining component values' })
1110+
match = error({ message: 'Unexpected remaining tokens' })
11111111
break
11121112
}
11131113
if (strategy === 'lazy') {

lib/parse/postprocess.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,16 +1339,16 @@ function getPseudoElementDefinition({ value: [, [, { name, types, value }]] }) {
13391339
}
13401340

13411341
/**
1342-
* @param {string} key
1342+
* @param {string} name
13431343
* @param {object} node
13441344
* @returns {boolean}
13451345
*/
1346-
function isValidPseudoClass(key, node) {
1347-
if (pseudos.userActions.includes(key) || pseudos.logical[key]) {
1346+
function isValidPseudoClass(name, node) {
1347+
if (pseudos.userActions.includes(name) || pseudos.logical[name]) {
13481348
return true
13491349
}
13501350
// :has() nested in :has()
1351-
if (key === 'has' && findFunction(node, node => node.definition.name === 'has')) {
1351+
if (name === 'has' && findFunction(node, node => node.definition.name === 'has')) {
13521352
return false
13531353
}
13541354
// Pseudo-class qualifying pseudo-element
@@ -1358,7 +1358,7 @@ function isValidPseudoClass(key, node) {
13581358
node => node.definition.name === '<subclass-selector>',
13591359
true)
13601360
if (pseudoElement) {
1361-
return getPseudoElementDefinition(pseudoElement).classes?.includes(key)
1361+
return getPseudoElementDefinition(pseudoElement).classes?.includes(name)
13621362
}
13631363
return true
13641364
}
@@ -1409,21 +1409,21 @@ function postParsePseudoClassSelector(selector, node) {
14091409
}
14101410

14111411
/**
1412-
* @param {string} key
1412+
* @param {string} name
14131413
* @param {object} node
14141414
* @returns {boolean}
14151415
*/
1416-
function isValidPseudoElement(key, node) {
1416+
function isValidPseudoElement(name, node) {
14171417
const origin = findSibling(
14181418
node,
14191419
node => node.definition.name === '<pseudo-element-selector>',
14201420
node => node.definition.name === '<coumpound-selector>',
14211421
true)
14221422
if (origin) {
1423-
if (key.startsWith('-webkit-')) {
1423+
if (name.startsWith('-webkit-')) {
14241424
return false
14251425
}
1426-
return getPseudoElementDefinition(origin).elements?.includes(key)
1426+
return getPseudoElementDefinition(origin).elements?.includes(name)
14271427
}
14281428
return true
14291429
}

lib/parse/preprocess.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function error({ definition: { name, value } }) {
3434
* @returns {SyntaxError|null|undefined}
3535
* @see {@link https://drafts.csswg.org/css-values-4/#calc-syntax}
3636
*
37-
* It aborts parsing when the next component value is a calculation sum operator
38-
* that is not preceded by a whitespace.
37+
* It aborts parsing when the next token is a calculation sum operator that is
38+
* not preceded by a whitespace.
3939
*/
4040
function preParseCalcOperator(node) {
4141
const { input } = node
@@ -69,12 +69,14 @@ function preParseCalcValue(node) {
6969
* @see {@link https://drafts.csswg.org/selectors-4/#typedef-combinator}
7070
* @see {@link https://drafts.csswg.org/selectors-4/#pseudo-element-structure}
7171
*
72-
* It aborts parsing a combinator when it follows a selector of a pseudo-element
73-
* that has no internal structure, or when the context only allows a compound
72+
* It aborts parsing a combinator when the context only allows a compound
7473
* selector.
7574
*
76-
* It aborts parsing when the context allows relative selector(s) whereas it is
77-
* already restricted to compound selector(s).
75+
* It aborts parsing when the input is at the back of a pseudo-element with no
76+
* internal structure but is still not at the end of the selector.
77+
*
78+
* It aborts parsing when the grammar wants a relative selector but the context
79+
* only allows a compound selector.
7880
*/
7981
function preParseCombinator(node) {
8082
if (isInputAtEnd(node)) {
@@ -83,7 +85,7 @@ function preParseCombinator(node) {
8385
const pseudoElement = findSibling(
8486
node,
8587
node => node.definition.name === '<pseudo-element-selector>',
86-
({ definition: { name } }) => name === '<compound-selector>' || name?.startsWith('<relative'))
88+
({ definition: { name } }) => name === '<subclass-selector>' || name?.startsWith('<relative'))
8789
if (pseudoElement) {
8890
const { value: [, [, { name, types, value }]] } = pseudoElement
8991
const definition = types[0] === '<function>'
@@ -120,8 +122,8 @@ function hasArbitrarySibling({ definition, parent: { definition: { type, value }
120122
* @see {@link https://drafts.csswg.org/css-variables-2/#funcdef-var}
121123
* @see {@link https://github.com/w3c/csswg-drafts/issues/8387}
122124
*
123-
* It aborts parsing when the next component value is a leading, adjacent, or
124-
* trailing comma (exception excluded).
125+
* It aborts parsing when the next token is a leading, adjacent, or trailing
126+
* comma (exception excluded).
125127
*
126128
* It accepts an omitted value as replacing a comma that would not have been the
127129
* separator of a repeated node.
@@ -185,8 +187,8 @@ function preParseComplexSelectorUnit(node) {
185187
* It aborts parsing a compound selector following another compound selector
186188
* when the combinator is omitted and there is no interleaving whitespace.
187189
*
188-
* It aborts parsing when the next component value is not a pseudo whereas the
189-
* context is a logical pseudo-class compounded to a pseudo-element.
190+
* It aborts parsing when the next token is not a pseudo whereas the context is
191+
* a logical pseudo-class compounded to a pseudo-element.
190192
*/
191193
function preParseCompoundSelector(node) {
192194
if (hasInvalidSiblingCombinator(node)) {
@@ -213,7 +215,7 @@ function preParseCompoundSelector(node) {
213215
* @returns {SyntaxError|undefined}
214216
* @see {@link https://drafts.csswg.org/css-images-4/#funcdef-image-set}
215217
*
216-
* It aborts parsing when the next component value is a nested image set.
218+
* It aborts parsing when the next token is a nested image set.
217219
*/
218220
function preParseImageSet(node) {
219221
const token = node.input.next()
@@ -230,8 +232,8 @@ function preParseImageSet(node) {
230232
* @returns {SyntaxError|undefined}
231233
* @see {@link https://drafts.csswg.org/css-ui-4/#typedef-cursor-url-set}
232234
*
233-
* It aborts parsing when the next component value is not an url or a string
234-
* whereas an url set option is expected.
235+
* It aborts parsing when the next token is not an url or a string whereas an
236+
* url set option is expected.
235237
*/
236238
function preParseImageSetOption(node) {
237239
const token = node.input.next()
@@ -253,8 +255,8 @@ function preParseImageSetOption(node) {
253255
* @returns {null|undefined}
254256
* @see {@link https://drafts.csswg.org/css-values-4/#length-value}
255257
*
256-
* It aborts parsing a length when the next component value is 0 and the context
257-
* allows a number or an integer.
258+
* It aborts parsing a length when the next token is 0 and the context allows a
259+
* number or an integer.
258260
*
259261
* It accepts 0 as replacing 0px.
260262
*/

lib/values/pseudos.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ const highlight = {
237237
* @see {@link https://drafts.csswg.org/css-overflow-5/#selectordef-scroll-marker}
238238
* @see {@link https://drafts.csswg.org/css-overflow-5/#selectordef-scroll-marker-group}
239239
* @see {@link https://drafts.csswg.org/css-position-4/#selectordef-backdrop}
240+
* @see {@link https://drafts.csswg.org/css-pseudo-4/#element-like}
240241
* @see {@link https://drafts.csswg.org/css-pseudo-4/#treelike}
241242
* @see {@link https://drafts.csswg.org/css-shadow-parts-1/#selectordef-part}
242243
*/

0 commit comments

Comments
 (0)