Skip to content

Commit 02a37de

Browse files
authored
fix: make value property work for SupportsQuery and LayerName insid… (#92)
…e `@import`
1 parent 3d58cb1 commit 02a37de

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/parse-atrule-prelude.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ describe('At-Rule Prelude Nodes', () => {
812812
expect(children.length).toBe(2)
813813
expect(children[0].type).toBe(URL)
814814
expect(children[1].type).toBe(MEDIA_QUERY)
815+
expect(children[1].text).toBe('(min-width: 768px)')
815816
})
816817

817818
it('should parse with combined media query', () => {
@@ -834,6 +835,9 @@ describe('At-Rule Prelude Nodes', () => {
834835
expect(children.length).toBe(3)
835836
expect(children[0].type).toBe(URL)
836837
expect(children[1].type).toBe(LAYER_NAME)
838+
expect(children[1].text).toBe('layer(base)')
839+
expect(children[1].value).toBe('base')
840+
expect(children[1].name).toBe('base')
837841
expect(children[2].type).toBe(MEDIA_QUERY)
838842
})
839843

@@ -845,8 +849,13 @@ describe('At-Rule Prelude Nodes', () => {
845849

846850
expect(children.length).toBe(3)
847851
expect(children[0].type).toBe(URL)
852+
expect(children[0].value).toBe('"styles.css"')
848853
expect(children[1].type).toBe(LAYER_NAME)
854+
expect(children[1].value).toBe('base')
855+
expect(children[1].name).toBe('base')
849856
expect(children[2].type).toBe(SUPPORTS_QUERY)
857+
expect(children[2].value).toBe('display: grid')
858+
expect(children[2].text).toBe('supports(display: grid)')
850859
})
851860

852861
it('should parse with supports and media query', () => {
@@ -883,7 +892,8 @@ describe('At-Rule Prelude Nodes', () => {
883892
expect(children.length).toBe(2)
884893
expect(children[0].type).toBe(URL)
885894
expect(children[1].type).toBe(SUPPORTS_QUERY)
886-
expect(children[1].text).toContain('supports(')
895+
expect(children[1].text).toBe('supports((display: grid) and (gap: 1rem))')
896+
expect(children[1].value).toBe('(display: grid) and (gap: 1rem)')
887897
})
888898

889899
it('should preserve prelude text', () => {

src/parse-atrule-prelude.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,11 @@ export class AtRulePreludeParser {
504504
if (content_length > 0) {
505505
let trimmed = trim_boundaries(this.source, content_start, content_start + content_length)
506506
if (trimmed) {
507+
// Set both content fields (for .name) and value fields (for .value)
507508
this.arena.set_content_start_delta(layer_node, trimmed[0] - layer_start)
508509
this.arena.set_content_length(layer_node, trimmed[1] - trimmed[0])
510+
this.arena.set_value_start_delta(layer_node, trimmed[0] - layer_start)
511+
this.arena.set_value_length(layer_node, trimmed[1] - trimmed[0])
509512
}
510513
}
511514

@@ -530,10 +533,12 @@ export class AtRulePreludeParser {
530533
let text = this.source.substring(this.lexer.token_start, this.lexer.token_end - 1) // -1 to exclude '('
531534
if (str_equals('supports', text)) {
532535
let supports_start = this.lexer.token_start
536+
let content_start = this.lexer.token_end // After the opening '('
533537

534538
// Find matching closing parenthesis
535539
let paren_depth = 1
536540
let supports_end = this.lexer.token_end
541+
let content_end = content_start
537542

538543
while (this.lexer.pos < this.prelude_end && paren_depth > 0) {
539544
let tokenType = this.next_token()
@@ -542,6 +547,7 @@ export class AtRulePreludeParser {
542547
} else if (tokenType === TOKEN_RIGHT_PAREN) {
543548
paren_depth--
544549
if (paren_depth === 0) {
550+
content_end = this.lexer.token_start // Before the closing ')'
545551
supports_end = this.lexer.token_end
546552
}
547553
} else if (tokenType === TOKEN_EOF) {
@@ -552,6 +558,13 @@ export class AtRulePreludeParser {
552558
// Create supports node
553559
let supports_node = this.create_node(SUPPORTS_QUERY, supports_start, supports_end)
554560

561+
// Store query content in value fields, trimmed
562+
let trimmed = trim_boundaries(this.source, content_start, content_end)
563+
if (trimmed) {
564+
this.arena.set_value_start_delta(supports_node, trimmed[0] - supports_start)
565+
this.arena.set_value_length(supports_node, trimmed[1] - trimmed[0])
566+
}
567+
555568
return supports_node
556569
}
557570
}

0 commit comments

Comments
 (0)