Skip to content

Commit 10a965c

Browse files
committed
BREAKING: rename offset to start; add end
1 parent fd42b04 commit 10a965c

File tree

6 files changed

+146
-98
lines changed

6 files changed

+146
-98
lines changed

src/css-node.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ export class CSSNode {
436436
}
437437

438438
// Get start offset in source
439-
get offset(): number {
439+
get start(): number {
440440
return this.arena.get_start_offset(this.index)
441441
}
442442

@@ -445,6 +445,12 @@ export class CSSNode {
445445
return this.arena.get_length(this.index)
446446
}
447447

448+
// Get end offset in source
449+
// End is not stored, must be calculated
450+
get end(): number {
451+
return this.start + this.length
452+
}
453+
448454
// --- Tree Traversal ---
449455

450456
// Get first child node
@@ -663,8 +669,8 @@ export class CSSNode {
663669
while (child) {
664670
if (child.type === COMBINATOR) break
665671

666-
if (start === -1) start = child.offset
667-
end = child.offset + child.length
672+
if (start === -1) start = child.start
673+
end = child.start + child.length
668674

669675
child = child.next_sibling
670676
}
@@ -741,7 +747,7 @@ export class CSSNode {
741747
if (locations) {
742748
plain.line = this.line
743749
plain.column = this.column
744-
plain.offset = this.offset
750+
plain.offset = this.start
745751
plain.length = this.length
746752
}
747753

src/parse-atrule-prelude.test.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ describe('At-Rule Prelude Nodes', () => {
2525
const mediaQuery = atRule.first_child!
2626

2727
expect(mediaQuery.type).toBe(MEDIA_QUERY)
28-
expect(mediaQuery.offset).toBe(7)
28+
expect(mediaQuery.start).toBe(7)
2929
expect(mediaQuery.length).toBe(6)
30+
expect(mediaQuery.end).toBe(13)
3031
})
3132

3233
test('offset and length for media feature', () => {
@@ -36,8 +37,9 @@ describe('At-Rule Prelude Nodes', () => {
3637
const mediaQuery = atRule.first_child!
3738

3839
expect(mediaQuery.type).toBe(MEDIA_QUERY)
39-
expect(mediaQuery.offset).toBe(7)
40+
expect(mediaQuery.start).toBe(7)
4041
expect(mediaQuery.length).toBe(18)
42+
expect(mediaQuery.end).toBe(25)
4143
})
4244

4345
test('offset and length for complex query', () => {
@@ -47,8 +49,9 @@ describe('At-Rule Prelude Nodes', () => {
4749
const mediaQuery = atRule.first_child!
4850

4951
expect(mediaQuery.type).toBe(MEDIA_QUERY)
50-
expect(mediaQuery.offset).toBe(7)
52+
expect(mediaQuery.start).toBe(7)
5153
expect(mediaQuery.length).toBe(29)
54+
expect(mediaQuery.end).toBe(36)
5255
})
5356
})
5457

@@ -61,8 +64,9 @@ describe('At-Rule Prelude Nodes', () => {
6164
const mediaType = mediaQuery.first_child!
6265

6366
expect(mediaType.type).toBe(MEDIA_TYPE)
64-
expect(mediaType.offset).toBe(7)
67+
expect(mediaType.start).toBe(7)
6568
expect(mediaType.length).toBe(6)
69+
expect(mediaType.end).toBe(13)
6670
})
6771
})
6872

@@ -75,8 +79,9 @@ describe('At-Rule Prelude Nodes', () => {
7579
const mediaFeature = mediaQuery.first_child!
7680

7781
expect(mediaFeature.type).toBe(MEDIA_FEATURE)
78-
expect(mediaFeature.offset).toBe(7)
82+
expect(mediaFeature.start).toBe(7)
7983
expect(mediaFeature.length).toBe(18)
84+
expect(mediaFeature.end).toBe(25)
8085
})
8186
})
8287

@@ -88,8 +93,9 @@ describe('At-Rule Prelude Nodes', () => {
8893
const containerQuery = atRule.first_child!
8994

9095
expect(containerQuery.type).toBe(CONTAINER_QUERY)
91-
expect(containerQuery.offset).toBe(11)
96+
expect(containerQuery.start).toBe(11)
9297
expect(containerQuery.length).toBe(18)
98+
expect(containerQuery.end).toBe(29)
9399
})
94100

95101
test('offset and length for named query', () => {
@@ -99,8 +105,9 @@ describe('At-Rule Prelude Nodes', () => {
99105
const containerQuery = atRule.first_child!
100106

101107
expect(containerQuery.type).toBe(CONTAINER_QUERY)
102-
expect(containerQuery.offset).toBe(11)
108+
expect(containerQuery.start).toBe(11)
103109
expect(containerQuery.length).toBe(26)
110+
expect(containerQuery.end).toBe(37)
104111
})
105112
})
106113

@@ -112,8 +119,9 @@ describe('At-Rule Prelude Nodes', () => {
112119
const supportsQuery = atRule.first_child!
113120

114121
expect(supportsQuery.type).toBe(SUPPORTS_QUERY)
115-
expect(supportsQuery.offset).toBe(10)
122+
expect(supportsQuery.start).toBe(10)
116123
expect(supportsQuery.length).toBe(15)
124+
expect(supportsQuery.end).toBe(25)
117125
})
118126
})
119127

@@ -125,8 +133,9 @@ describe('At-Rule Prelude Nodes', () => {
125133
const layerName = atRule.first_child!
126134

127135
expect(layerName.type).toBe(LAYER_NAME)
128-
expect(layerName.offset).toBe(7)
136+
expect(layerName.start).toBe(7)
129137
expect(layerName.length).toBe(9)
138+
expect(layerName.end).toBe(16)
130139
})
131140
})
132141

@@ -138,8 +147,9 @@ describe('At-Rule Prelude Nodes', () => {
138147
const identifier = atRule.first_child!
139148

140149
expect(identifier.type).toBe(IDENTIFIER)
141-
expect(identifier.offset).toBe(11)
150+
expect(identifier.start).toBe(11)
142151
expect(identifier.length).toBe(7)
152+
expect(identifier.end).toBe(18)
143153
})
144154

145155
test('offset and length in @property', () => {
@@ -149,8 +159,9 @@ describe('At-Rule Prelude Nodes', () => {
149159
const identifier = atRule.first_child!
150160

151161
expect(identifier.type).toBe(IDENTIFIER)
152-
expect(identifier.offset).toBe(10)
162+
expect(identifier.start).toBe(10)
153163
expect(identifier.length).toBe(10)
164+
expect(identifier.end).toBe(20)
154165
})
155166
})
156167

@@ -163,8 +174,9 @@ describe('At-Rule Prelude Nodes', () => {
163174
const operator = mediaQuery.children[1]
164175

165176
expect(operator.type).toBe(PRELUDE_OPERATOR)
166-
expect(operator.offset).toBe(14)
177+
expect(operator.start).toBe(14)
167178
expect(operator.length).toBe(3)
179+
expect(operator.end).toBe(17)
168180
})
169181
})
170182

@@ -176,8 +188,9 @@ describe('At-Rule Prelude Nodes', () => {
176188
const url = atRule.first_child!
177189

178190
expect(url.type).toBe(URL)
179-
expect(url.offset).toBe(8)
191+
expect(url.start).toBe(8)
180192
expect(url.length).toBe(17)
193+
expect(url.end).toBe(25)
181194
})
182195

183196
test('offset and length with string', () => {
@@ -187,8 +200,9 @@ describe('At-Rule Prelude Nodes', () => {
187200
const url = atRule.first_child!
188201

189202
expect(url.type).toBe(URL)
190-
expect(url.offset).toBe(8)
203+
expect(url.start).toBe(8)
191204
expect(url.length).toBe(12)
205+
expect(url.end).toBe(20)
192206
})
193207
})
194208
})
@@ -674,30 +688,29 @@ describe('At-Rule Prelude Nodes', () => {
674688
expect(children[0].text).toBe('"styles.css"')
675689
})
676690

691+
it('should have .value property for URL with quoted url() function', () => {
692+
const css = '@import url("example.com");'
693+
const ast = parse(css, { parse_atrule_preludes: true })
694+
const atRule = ast.first_child
695+
const url = atRule?.children[0]
677696

678-
it('should have .value property for URL with quoted url() function', () => {
679-
const css = '@import url("example.com");'
680-
const ast = parse(css, { parse_atrule_preludes: true })
681-
const atRule = ast.first_child
682-
const url = atRule?.children[0]
683-
684-
expect(url?.type).toBe(URL)
685-
expect(url?.text).toBe('url("example.com")')
686-
// URL node in @import returns the content with quotes
687-
expect(url?.value).toBe('"example.com"')
688-
})
697+
expect(url?.type).toBe(URL)
698+
expect(url?.text).toBe('url("example.com")')
699+
// URL node in @import returns the content with quotes
700+
expect(url?.value).toBe('"example.com"')
701+
})
689702

690-
it('should have .value property for URL with quoted string', () => {
691-
const css = '@import "example.com";'
692-
const ast = parse(css, { parse_atrule_preludes: true })
693-
const atRule = ast.first_child
694-
const url = atRule?.children[0]
703+
it('should have .value property for URL with quoted string', () => {
704+
const css = '@import "example.com";'
705+
const ast = parse(css, { parse_atrule_preludes: true })
706+
const atRule = ast.first_child
707+
const url = atRule?.children[0]
695708

696-
expect(url?.type).toBe(URL)
697-
expect(url?.text).toBe('"example.com"')
698-
// URL node in @import returns the string with quotes
699-
expect(url?.value).toBe('"example.com"')
700-
})
709+
expect(url?.type).toBe(URL)
710+
expect(url?.text).toBe('"example.com"')
711+
// URL node in @import returns the string with quotes
712+
expect(url?.value).toBe('"example.com"')
713+
})
701714
it('should parse with anonymous layer', () => {
702715
const css = '@import url("styles.css") layer;'
703716
const ast = parse(css, { parse_atrule_preludes: true })

0 commit comments

Comments
 (0)