Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ Options:
- `parse_values` (default `true`) — parse declaration values into nodes
- `parse_selectors` (default `true`) — parse selectors into nodes
- `parse_atrule_preludes` (default `true`) — parse at-rule preludes into nodes
- `media_feature_range_name_as_child` (default `false`) — also add the feature name of a media range (`width` in `(400px < width < 1000px)`) as an `Identifier` child, so the children read in source order. `name` is set either way. **Deprecated:** this will become the default in 0.20.0 and the option will be removed, so opt in now to prepare
- `on_comment` — called for every comment (`{ start, end, length, line, column }`); comments are never part of the tree

### Sub-parsers
Expand Down
3 changes: 2 additions & 1 deletion src/node-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ describe('type narrowing — compile-time', () => {
(Dimension | Function | Identifier | PreludeOperator)[]
>()

const [op, dim] = range.children
const [name, op, dim] = range.children
expect(name.type_name).toBe('Identifier')
expect(op.type_name).toBe('Operator')
expect(dim.type_name).toBe('Dimension')
})
Expand Down
52 changes: 23 additions & 29 deletions src/parse-atrule-prelude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,20 +765,21 @@ describe('At-Rule Prelude Nodes', () => {

expect(range?.type).toBe(FEATURE_RANGE)
expect(range?.name).toBe('width')
expect(range?.children.length).toBe(2) // dimension + operator
expect(range?.children.length).toBe(3) // name + operator + dimension

// Verify child types
expect(range?.children[0].type).toBe(PRELUDE_OPERATOR) // >=
expect(range?.children[1].type).toBe(DIMENSION) // 400px
expect(range?.children[0].type).toBe(IDENTIFIER) // width
expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // >=
expect(range?.children[2].type).toBe(DIMENSION) // 400px
})

test('should keep a function value whole in range syntax', () => {
const [query] = parse_atrule_prelude('media', '(width >= calc(1px + 2px)) and (color)')
const range = (query as MediaQuery).children[0] as FeatureRange

expect(range.text).toBe('(width >= calc(1px + 2px))')
expect(range.children.map((c) => c.text)).toEqual(['>=', 'calc(1px + 2px)'])
const fn = range.children[1] as Function
expect(range.children.map((c) => c.text)).toEqual(['width', '>=', 'calc(1px + 2px)'])
const fn = range.children[2] as Function
expect(fn.name).toBe('calc')
expect(fn.value).toBe('1px + 2px')
})
Expand All @@ -787,44 +788,34 @@ describe('At-Rule Prelude Nodes', () => {
const [query] = parse_atrule_prelude('media', '(calc(1px) < width < 5px)')
const range = (query as MediaQuery).children[0] as FeatureRange

expect(range.children.map((c) => c.text)).toEqual(['calc(1px)', '<', '<', '5px'])
expect(range.children.map((c) => c.text)).toEqual(['calc(1px)', '<', 'width', '<', '5px'])
})

test('should tokenize a bare = as its own comparator', () => {
const [query] = parse_atrule_prelude('media', '(width=>1000px)')
const range = (query as MediaQuery).children[0] as FeatureRange

expect(range.children.map((c) => c.text)).toEqual(['=', '>', '1000px'])
expect(range.children.map((c) => c.text)).toEqual(['width', '=', '>', '1000px'])
})

test('should tokenize >= and <= as a single comparator', () => {
const [query] = parse_atrule_prelude('media', '(100px<=width>=1000px)')
const range = (query as MediaQuery).children[0] as FeatureRange

expect(range.children.map((c) => c.text)).toEqual(['100px', '<=', '>=', '1000px'])
expect(range.children.map((c) => c.text)).toEqual(['100px', '<=', 'width', '>=', '1000px'])
})

test('should not add the feature name as a child by default', () => {
test('should add the feature name as a child', () => {
const [query] = parse_atrule_prelude('media', '(500px > width > 1000px)')
const range = (query as MediaQuery).children[0] as FeatureRange

expect(range.name).toBe('width')
expect(range.children.map((c) => c.text)).toEqual(['500px', '>', '>', '1000px'])
})

test('should add the feature name as a child with media_feature_range_name_as_child', () => {
const [query] = parse_atrule_prelude('media', '(500px > width > 1000px)', {
media_feature_range_name_as_child: true,
})
const range = (query as MediaQuery).children[0] as FeatureRange

expect(range.name).toBe('width')
expect(range.children.map((c) => c.text)).toEqual(['500px', '>', 'width', '>', '1000px'])
expect(range.children[2].type).toBe(IDENTIFIER)
})

test('should add the feature name as a child via parse() with media_feature_range_name_as_child', () => {
const ast = parse('@media (width >= 400px) {}', { media_feature_range_name_as_child: true })
test('should add the feature name as a child via parse()', () => {
const ast = parse('@media (width >= 400px) {}')
const range = (ast.first_child as Atrule).prelude!.first_child!.first_child as FeatureRange

expect(range.children.map((c) => c.type_name)).toEqual([
Expand All @@ -847,15 +838,16 @@ describe('At-Rule Prelude Nodes', () => {

expect(range?.type).toBe(FEATURE_RANGE)
expect(range?.name).toBe('width')
expect(range?.children.length).toBe(4) // dim, op, op, dim
expect(range?.children.length).toBe(5) // dim, op, name, op, dim

// Verify child types
expect(range?.children[0].type).toBe(DIMENSION) // 50px
expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // <=
expect(range?.children[2].type).toBe(PRELUDE_OPERATOR) // <=
expect(range?.children[2].type).toBe(IDENTIFIER) // width
expect(range?.children[3].type).toBe(PRELUDE_OPERATOR) // <=
expect((range!.children[1] as PreludeOperator).value).toBe('<=')
expect((range!.children[2] as PreludeOperator).value).toBe('<=')
expect(range?.children[3].type).toBe(DIMENSION) // 100px
expect((range!.children[3] as PreludeOperator).value).toBe('<=')
expect(range?.children[4].type).toBe(DIMENSION) // 100px
})

test('should parse range syntax with less-than', () => {
Expand All @@ -871,11 +863,12 @@ describe('At-Rule Prelude Nodes', () => {

expect(range?.type).toBe(FEATURE_RANGE)
expect(range?.name).toBe('width')
expect(range?.children.length).toBe(2)
expect(range?.children.length).toBe(3)

// Verify child types
expect(range?.children[0].type).toBe(DIMENSION) // 400px
expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // <
expect(range?.children[2].type).toBe(IDENTIFIER) // width
expect((range!.children[1] as PreludeOperator).value).toBe('<')
})

Expand All @@ -892,11 +885,12 @@ describe('At-Rule Prelude Nodes', () => {

expect(range?.type).toBe(FEATURE_RANGE)
expect(range?.name).toBe('width')
expect(range?.children.length).toBe(2)
expect(range?.children.length).toBe(3)

// Verify child types
expect(range?.children[0].type).toBe(PRELUDE_OPERATOR) // =
expect(range?.children[1].type).toBe(DIMENSION) // 500px
expect(range?.children[0].type).toBe(IDENTIFIER) // width
expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // =
expect(range?.children[2].type).toBe(DIMENSION) // 500px
})

test('should parse comma-separated media queries', () => {
Expand Down
38 changes: 5 additions & 33 deletions src/parse-atrule-prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ import { ValueNodeParser } from './value-node-parser'
import { ConditionParser } from './parse-condition'
import { SelectorParser } from './parse-selector'

/** @internal */
export interface PreludeParserOptions {
/**
* Defaults to `false`, but this will become the default behavior in 0.20.0.
*
* @deprecated Opt-in only as a migration aid: in 0.20.0 this behavior is always on and the option will be removed.
*/
media_feature_range_name_as_child?: boolean
}

export class AtRulePreludeParser {
private lexer: Lexer
private arena: CSSDataArena
Expand All @@ -63,23 +53,14 @@ export class AtRulePreludeParser {
// Deep-parses the parenthesised arguments of @scope into real SelectorLists
private selector_parser: SelectorParser

constructor(
arena: CSSDataArena,
source: string,
media_feature_range_name_as_child: boolean = false,
) {
constructor(arena: CSSDataArena, source: string) {
this.arena = arena
this.source = source
// Create a lexer instance for prelude parsing
this.lexer = new Lexer(source)
this.prelude_end = 0
this.value_node_parser = new ValueNodeParser(arena, source, media_feature_range_name_as_child)
this.condition_parser = new ConditionParser(
arena,
source,
this.value_node_parser,
media_feature_range_name_as_child,
)
this.value_node_parser = new ValueNodeParser(arena, source)
this.condition_parser = new ConditionParser(arena, source, this.value_node_parser)
this.selector_parser = new SelectorParser(arena, source)
}

Expand Down Expand Up @@ -853,23 +834,14 @@ export class AtRulePreludeParser {
* Parse an at-rule prelude string and return an array of AST nodes
* @param at_rule_name - The name of the at-rule (e.g., "media", "supports", "layer")
* @param prelude - The at-rule prelude to parse (e.g., "(min-width: 768px)", "utilities")
* @param options - Parser options (`media_feature_range_name_as_child`: also add the feature name of a media range as an Identifier child. This will become the default behavior in 0.20.0.)
* @returns An array of CSSNode objects representing the parsed prelude
*/
export function parse_atrule_prelude(
at_rule_name: string,
prelude: string,
options?: PreludeParserOptions,
): AnyNode[] {
export function parse_atrule_prelude(at_rule_name: string, prelude: string): AnyNode[] {
// Create an arena for the prelude nodes
const arena = new CSSDataArena(CSSDataArena.capacity_for_source(prelude.length))

// Create prelude parser
const prelude_parser = new AtRulePreludeParser(
arena,
prelude,
options?.media_feature_range_name_as_child ?? false,
)
const prelude_parser = new AtRulePreludeParser(arena, prelude)

// Parse the entire source as an at-rule prelude
const node_indices = prelude_parser.parse_prelude(at_rule_name, 0, prelude.length)
Expand Down
27 changes: 8 additions & 19 deletions src/parse-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,7 @@ export class ConditionParser {
// doesn't depend on ValueNodeParser/AtRulePreludeParser), so this is safely owned here.
private selector_parser: SelectorParser

// Opt-in: also add a FEATURE_RANGE's feature name as an Identifier child (in source order)
private media_feature_range_name_as_child: boolean

constructor(
arena: CSSDataArena,
source: string,
value_node_parser: ValueNodeParser,
media_feature_range_name_as_child: boolean = false,
) {
this.media_feature_range_name_as_child = media_feature_range_name_as_child
constructor(arena: CSSDataArena, source: string, value_node_parser: ValueNodeParser) {
this.arena = arena
this.source = source
this.lexer = new Lexer(source)
Expand Down Expand Up @@ -312,16 +303,14 @@ export class ConditionParser {
// Feature name
feature_name_start = this.lexer.token_start
feature_name_end = this.lexer.token_end
if (this.media_feature_range_name_as_child) {
const name_first = this.parse_feature_value(feature_name_start, feature_name_end)
if (name_first !== 0) {
if (first_child === 0) {
first_child = name_first
} else {
this.arena.set_next_sibling(last_child, name_first)
}
last_child = this.arena.get_last_sibling(name_first)
const name_first = this.parse_feature_value(feature_name_start, feature_name_end)
if (name_first !== 0) {
if (first_child === 0) {
first_child = name_first
} else {
this.arena.set_next_sibling(last_child, name_first)
}
last_child = this.arena.get_last_sibling(name_first)
}
} else {
// Value (may itself be a short chain, e.g. a single dimension node). A function
Expand Down
11 changes: 2 additions & 9 deletions src/parse-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,10 @@ export class DeclarationParser {
// Reused lookahead snapshot (never nested) to avoid an allocation per declaration
private lookahead_position: LexerPosition = new Lexer('').save_position()

constructor(
arena: CSSDataArena,
source: string,
parse_values: boolean = true,
media_feature_range_name_as_child: boolean = false,
) {
constructor(arena: CSSDataArena, source: string, parse_values: boolean = true) {
this.arena = arena
this.source = source
this.value_parser = parse_values
? new ValueParser(arena, source, media_feature_range_name_as_child)
: null
this.value_parser = parse_values ? new ValueParser(arena, source) : null
}

// Parse a declaration range into a declaration node (standalone use)
Expand Down
17 changes: 6 additions & 11 deletions src/parse-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,11 +1583,8 @@ describe('Value Node Types', () => {
expect(at(decl_x.next_sibling)).toEqual([6, 3])
})

test('with media_feature_range_name_as_child', () => {
const ast = parse(
'div { color: if(media((400px <= width) and (hover)): blue; else: red) }',
{ media_feature_range_name_as_child: true },
)
test('range includes the feature name as a child', () => {
const ast = parse('div { color: if(media((400px <= width) and (hover)): blue; else: red) }')
const decl = (ast.first_child as Rule).block!.first_child as Declaration
const fn = getBranch(decl.value!.first_child as Function, 0)?.first_child as Function
const range = (fn.children[0] as any).first_child
Expand Down Expand Up @@ -1616,15 +1613,13 @@ describe('Value Node Types', () => {
const range = mediaFunc.children[0] as FeatureRange
expect(range.type).toBe(FEATURE_RANGE)
expect(range.name).toBe('width')
expect(range.children.map((c) => c.text)).toEqual(['400px', '<=', '<=', '800px'])
expect(range.children.map((c) => c.text)).toEqual(['400px', '<=', 'width', '<=', '800px'])
expect((range.children[1] as PreludeOperator).value).toBe('<=')
expect((range.children[2] as PreludeOperator).value).toBe('<=')
expect((range.children[3] as PreludeOperator).value).toBe('<=')
})

test('media() range includes the feature name as a child with media_feature_range_name_as_child', () => {
const ast = parse('div { color: if(media(400px <= width): blue; else: red) }', {
media_feature_range_name_as_child: true,
})
test('media() range includes the feature name as a child', () => {
const ast = parse('div { color: if(media(400px <= width): blue; else: red) }')
const decl = (ast.first_child as Rule).block!.first_child as Declaration
const mediaFunc = getBranch(decl.value!.first_child as Function, 0)?.first_child as Function
const range = mediaFunc.children[0] as FeatureRange
Expand Down
8 changes: 2 additions & 6 deletions src/parse-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ export class ValueParser {
private nodes: ValueNodeParser
private arena: CSSDataArena

constructor(
arena: CSSDataArena,
source: string,
media_feature_range_name_as_child: boolean = false,
) {
constructor(arena: CSSDataArena, source: string) {
this.arena = arena
this.nodes = new ValueNodeParser(arena, source, media_feature_range_name_as_child)
this.nodes = new ValueNodeParser(arena, source)
}

// Parse a declaration value range into a VALUE wrapper node
Expand Down
16 changes: 2 additions & 14 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ export interface ParserOptions {
parse_values?: boolean
parse_selectors?: boolean
parse_atrule_preludes?: boolean
/**
* Also add the feature name of a media range, e.g. `width` in `(400px < width)`, as an Identifier child.
* Defaults to `false`, but this will become the default in 0.20.0.
*
* @deprecated Opt-in only as a migration aid: in 0.20.0 this behavior is always on and the option will be removed.
*/
media_feature_range_name_as_child?: boolean
on_comment?: (info: CommentInfo) => void
}

Expand Down Expand Up @@ -129,14 +122,9 @@ export class Parser {
? new SelectorParser(this.arena, source)
: null
this.prelude_parser = this.parse_atrule_preludes_enabled
? new AtRulePreludeParser(this.arena, source, opts.media_feature_range_name_as_child ?? false)
? new AtRulePreludeParser(this.arena, source)
: null
this.declaration_parser = new DeclarationParser(
this.arena,
source,
this.parse_values_enabled,
opts.media_feature_range_name_as_child ?? false,
)
this.declaration_parser = new DeclarationParser(this.arena, source, this.parse_values_enabled)
}

// Get the arena (for internal/advanced use only)
Expand Down
16 changes: 2 additions & 14 deletions src/value-node-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,12 @@ export class ValueNodeParser {
this.arena,
this.source,
new ValueNodeParser(this.arena, this.source),
this.media_feature_range_name_as_child,
)
}
return this._condition_parser
}

private media_feature_range_name_as_child: boolean

constructor(
arena: CSSDataArena,
source: string,
media_feature_range_name_as_child: boolean = false,
) {
this.media_feature_range_name_as_child = media_feature_range_name_as_child
constructor(arena: CSSDataArena, source: string) {
this.arena = arena
this.source = source
this.lexer = new Lexer(source)
Expand Down Expand Up @@ -783,11 +775,7 @@ export class ValueNodeParser {

private get media_prelude_parser(): AtRulePreludeParser {
if (this._media_prelude_parser === null) {
this._media_prelude_parser = new AtRulePreludeParser(
this.arena,
this.source,
this.media_feature_range_name_as_child,
)
this._media_prelude_parser = new AtRulePreludeParser(this.arena, this.source)
}
return this._media_prelude_parser
}
Expand Down
Loading