Skip to content

Commit d0deedd

Browse files
committed
dedupe skip_whitespace_and_comments stuff
1 parent 409c279 commit d0deedd

File tree

9 files changed

+503
-253
lines changed

9 files changed

+503
-253
lines changed

src/css-node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import {
4646
FLAG_HAS_DECLARATIONS,
4747
} from './arena'
4848

49-
import { CHAR_MINUS_HYPHEN, CHAR_PLUS, is_whitespace, parse_dimension } from './string-utils'
49+
import { CHAR_MINUS_HYPHEN, CHAR_PLUS, is_whitespace } from './string-utils'
50+
import { parse_dimension } from './parse-utils'
5051

5152
// Node type constants (numeric for performance)
5253
export type CSSNodeType =

src/parse-anplusb.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import { Lexer } from './lexer'
88
import { NODE_SELECTOR_NTH, CSSDataArena } from './arena'
99
import { TOKEN_IDENT, TOKEN_NUMBER, TOKEN_DIMENSION, TOKEN_DELIM, type TokenType } from './token-types'
10-
import { CHAR_MINUS_HYPHEN, CHAR_PLUS, is_whitespace as is_whitespace_char } from './string-utils'
10+
import { CHAR_MINUS_HYPHEN, CHAR_PLUS } from './string-utils'
11+
import { skip_whitespace_forward } from './parse-utils'
1112
import { CSSNode } from './css-node'
1213

1314
export class ANplusBParser {
@@ -272,13 +273,7 @@ export class ANplusBParser {
272273
}
273274

274275
private skip_whitespace(): void {
275-
while (this.lexer.pos < this.expr_end) {
276-
const ch = this.source.charCodeAt(this.lexer.pos)
277-
if (is_whitespace_char(ch)) {
278-
this.lexer.pos++
279-
}
280-
break
281-
}
276+
this.lexer.pos = skip_whitespace_forward(this.source, this.lexer.pos, this.expr_end)
282277
}
283278

284279
private create_anplusb_node(

src/parse-atrule-prelude.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import {
2626
TOKEN_FUNCTION,
2727
type TokenType,
2828
} from './token-types'
29-
import { trim_boundaries, str_equals, is_whitespace } from './string-utils'
29+
import { str_equals } from './string-utils'
30+
import { trim_boundaries, skip_whitespace_forward } from './parse-utils'
3031
import { CSSNode } from './css-node'
3132

3233
export class AtRulePreludeParser {
@@ -628,13 +629,7 @@ export class AtRulePreludeParser {
628629

629630
// Helper: Skip whitespace
630631
private skip_whitespace(): void {
631-
while (this.lexer.pos < this.prelude_end) {
632-
let ch = this.source.charCodeAt(this.lexer.pos)
633-
if (!is_whitespace(ch)) {
634-
break
635-
}
636-
this.lexer.pos++
637-
}
632+
this.lexer.pos = skip_whitespace_forward(this.source, this.lexer.pos, this.prelude_end)
638633
}
639634

640635
// Helper: Peek at next token type without consuming

src/parse-selector.ts

Lines changed: 14 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ import {
3939
TOKEN_WHITESPACE,
4040
TOKEN_STRING,
4141
} from './token-types'
42+
import { skip_whitespace_forward, skip_whitespace_and_comments_forward, skip_whitespace_and_comments_backward } from './parse-utils'
4243
import {
43-
is_whitespace as is_whitespace_char,
44+
is_whitespace,
4445
is_vendor_prefixed,
4546
CHAR_PLUS,
4647
CHAR_TILDE,
@@ -53,7 +54,6 @@ import {
5354
CHAR_PIPE,
5455
CHAR_DOLLAR,
5556
CHAR_CARET,
56-
CHAR_FORWARD_SLASH,
5757
CHAR_SINGLE_QUOTE,
5858
CHAR_DOUBLE_QUOTE,
5959
CHAR_COLON,
@@ -365,7 +365,7 @@ export class SelectorParser {
365365
// Skip whitespace and check for combinator
366366
while (this.lexer.pos < this.selector_end) {
367367
let ch = this.source.charCodeAt(this.lexer.pos)
368-
if (is_whitespace_char(ch)) {
368+
if (is_whitespace(ch)) {
369369
has_whitespace = true
370370
this.lexer.pos++
371371
} else {
@@ -392,7 +392,7 @@ export class SelectorParser {
392392
this.lexer.pos = whitespace_start
393393
while (this.lexer.pos < this.selector_end) {
394394
let ch = this.source.charCodeAt(this.lexer.pos)
395-
if (is_whitespace_char(ch)) {
395+
if (is_whitespace(ch)) {
396396
this.lexer.pos++
397397
} else {
398398
break
@@ -474,52 +474,10 @@ export class SelectorParser {
474474
// Parse attribute content to extract name, operator, and value
475475
private parse_attribute_content(node: number, start: number, end: number): void {
476476
// Skip leading whitespace and comments
477-
while (start < end) {
478-
let ch = this.source.charCodeAt(start)
479-
if (is_whitespace_char(ch)) {
480-
start++
481-
continue
482-
}
483-
// Skip comments /*...*/
484-
if (ch === CHAR_FORWARD_SLASH && start + 1 < end && this.source.charCodeAt(start + 1) === CHAR_ASTERISK) {
485-
start += 2 // Skip /*
486-
while (start < end) {
487-
if (
488-
this.source.charCodeAt(start) === CHAR_ASTERISK &&
489-
start + 1 < end &&
490-
this.source.charCodeAt(start + 1) === CHAR_FORWARD_SLASH
491-
) {
492-
start += 2 // Skip */
493-
break
494-
}
495-
start++
496-
}
497-
continue
498-
}
499-
break
500-
}
477+
start = skip_whitespace_and_comments_forward(this.source, start, end)
501478

502479
// Skip trailing whitespace and comments
503-
while (end > start) {
504-
let ch = this.source.charCodeAt(end - 1)
505-
if (is_whitespace_char(ch)) {
506-
end--
507-
continue
508-
}
509-
// Skip comments /*...*/
510-
if (ch === CHAR_FORWARD_SLASH && end >= 2 && this.source.charCodeAt(end - 2) === CHAR_ASTERISK) {
511-
// Find start of comment
512-
let pos = end - 2
513-
while (pos > start && !(this.source.charCodeAt(pos) === CHAR_FORWARD_SLASH && this.source.charCodeAt(pos + 1) === CHAR_ASTERISK)) {
514-
pos--
515-
}
516-
if (pos > start) {
517-
end = pos
518-
continue
519-
}
520-
}
521-
break
522-
}
480+
end = skip_whitespace_and_comments_backward(this.source, end, start)
523481

524482
if (start >= end) return
525483

@@ -534,7 +492,7 @@ export class SelectorParser {
534492
while (name_end < end) {
535493
let ch = this.source.charCodeAt(name_end)
536494
if (
537-
is_whitespace_char(ch) ||
495+
is_whitespace(ch) ||
538496
ch === CHAR_EQUALS /* = */ ||
539497
ch === CHAR_TILDE /* ~ */ ||
540498
ch === CHAR_PIPE /* | */ ||
@@ -554,27 +512,7 @@ export class SelectorParser {
554512
}
555513

556514
// Skip whitespace and comments after name
557-
let pos = name_end
558-
while (pos < end) {
559-
let ch = this.source.charCodeAt(pos)
560-
if (is_whitespace_char(ch)) {
561-
pos++
562-
continue
563-
}
564-
// Skip comments
565-
if (ch === CHAR_FORWARD_SLASH && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_ASTERISK) {
566-
pos += 2
567-
while (pos < end) {
568-
if (this.source.charCodeAt(pos) === CHAR_ASTERISK && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_FORWARD_SLASH) {
569-
pos += 2
570-
break
571-
}
572-
pos++
573-
}
574-
continue
575-
}
576-
break
577-
}
515+
let pos = skip_whitespace_and_comments_forward(this.source, name_end, end)
578516

579517
if (pos >= end) {
580518
// No operator, just [attr]
@@ -616,27 +554,7 @@ export class SelectorParser {
616554
}
617555

618556
// Skip whitespace and comments after operator
619-
pos = operator_end
620-
while (pos < end) {
621-
let ch = this.source.charCodeAt(pos)
622-
if (is_whitespace_char(ch)) {
623-
pos++
624-
continue
625-
}
626-
// Skip comments
627-
if (ch === CHAR_FORWARD_SLASH && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_ASTERISK) {
628-
pos += 2
629-
while (pos < end) {
630-
if (this.source.charCodeAt(pos) === CHAR_ASTERISK && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_FORWARD_SLASH) {
631-
pos += 2
632-
break
633-
}
634-
pos++
635-
}
636-
continue
637-
}
638-
break
639-
}
557+
pos = skip_whitespace_and_comments_forward(this.source, operator_end, end)
640558

641559
if (pos >= end) {
642560
// No value after operator
@@ -671,7 +589,7 @@ export class SelectorParser {
671589
// Unquoted identifier
672590
while (pos < end) {
673591
let c = this.source.charCodeAt(pos)
674-
if (is_whitespace_char(c)) {
592+
if (is_whitespace(c)) {
675593
break
676594
}
677595
pos++
@@ -924,9 +842,7 @@ export class SelectorParser {
924842
// Parse selector list after "of"
925843
let selector_start = of_index + 2 // skip "of"
926844
// Skip whitespace
927-
while (selector_start < end && is_whitespace_char(this.source.charCodeAt(selector_start))) {
928-
selector_start++
929-
}
845+
selector_start = skip_whitespace_forward(this.source, selector_start, end)
930846

931847
// Save current state
932848
let saved_selector_end = this.selector_end
@@ -975,8 +891,8 @@ export class SelectorParser {
975891
for (let i = start; i < end - 1; i++) {
976892
if (this.source.charCodeAt(i) === 0x6f /* o */ && this.source.charCodeAt(i + 1) === 0x66 /* f */) {
977893
// Check it's a word boundary
978-
let before_ok = i === start || is_whitespace_char(this.source.charCodeAt(i - 1))
979-
let after_ok = i + 2 >= end || is_whitespace_char(this.source.charCodeAt(i + 2))
894+
let before_ok = i === start || is_whitespace(this.source.charCodeAt(i - 1))
895+
let after_ok = i + 2 >= end || is_whitespace(this.source.charCodeAt(i + 2))
980896

981897
if (before_ok && after_ok) {
982898
return i
@@ -1050,13 +966,7 @@ export class SelectorParser {
1050966

1051967
// Helper to skip whitespace
1052968
private skip_whitespace(): void {
1053-
while (this.lexer.pos < this.selector_end) {
1054-
let ch = this.source.charCodeAt(this.lexer.pos)
1055-
if (is_whitespace_char(ch)) {
1056-
this.lexer.pos++
1057-
}
1058-
break
1059-
}
969+
this.lexer.pos = skip_whitespace_forward(this.source, this.lexer.pos, this.selector_end)
1060970
}
1061971
}
1062972

0 commit comments

Comments
 (0)