Skip to content

Commit 409c279

Browse files
committed
dedupe a lot of character class stuff
1 parent e785317 commit 409c279

File tree

6 files changed

+104
-76
lines changed

6 files changed

+104
-76
lines changed

src/css-node.ts

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

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

5151
// Node type constants (numeric for performance)
5252
export type CSSNodeType =
@@ -359,12 +359,12 @@ export class CSSNode {
359359
let check_pos = start - 1
360360
while (check_pos >= 0) {
361361
let ch = this.source.charCodeAt(check_pos)
362-
if (ch === 0x20 /* space */ || ch === 0x09 /* tab */ || ch === 0x0a /* \n */ || ch === 0x0d /* \r */) {
362+
if (is_whitespace(ch)) {
363363
check_pos--
364364
continue
365365
}
366366
// Found non-whitespace
367-
if (ch === 0x2d /* - */) {
367+
if (ch === CHAR_MINUS_HYPHEN /* - */) {
368368
// Prepend - to value
369369
value = '-' + value
370370
}
@@ -373,7 +373,7 @@ export class CSSNode {
373373
}
374374

375375
// Strip leading + if present in the token itself
376-
if (value.charCodeAt(0) === 0x2b /* + */) {
376+
if (value.charCodeAt(0) === CHAR_PLUS) {
377377
return value.substring(1)
378378
}
379379
return value

src/parse-anplusb.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
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 { is_whitespace as is_whitespace_char } from './string-utils'
10+
import { CHAR_MINUS_HYPHEN, CHAR_PLUS, is_whitespace as is_whitespace_char } from './string-utils'
1111
import { CSSNode } from './css-node'
1212

1313
export class ANplusBParser {
@@ -67,11 +67,11 @@ export class ANplusBParser {
6767
const second_char = this.lexer.token_end > this.lexer.token_start + 1 ? this.source.charCodeAt(this.lexer.token_start + 1) : 0
6868

6969
// -n, -n+3, -n-5
70-
if (first_char === 0x2d /* - */ && second_char === 0x6e /* n */) {
70+
if (first_char === CHAR_MINUS_HYPHEN /* - */ && second_char === 0x6e /* n */) {
7171
// Check for attached -n-digit pattern
7272
if (this.lexer.token_end > this.lexer.token_start + 2) {
7373
const third_char = this.source.charCodeAt(this.lexer.token_start + 2)
74-
if (third_char === 0x2d /* - */) {
74+
if (third_char === CHAR_MINUS_HYPHEN /* - */) {
7575
// -n-5 pattern
7676
a = '-n'
7777
a_start = this.lexer.token_start
@@ -102,7 +102,7 @@ export class ANplusBParser {
102102
// Check for attached n-digit pattern
103103
if (this.lexer.token_end > this.lexer.token_start + 1) {
104104
const second_char = this.source.charCodeAt(this.lexer.token_start + 1)
105-
if (second_char === 0x2d /* - */) {
105+
if (second_char === CHAR_MINUS_HYPHEN /* - */) {
106106
// n-5 pattern
107107
a = 'n'
108108
a_start = this.lexer.token_start
@@ -133,7 +133,7 @@ export class ANplusBParser {
133133
}
134134

135135
// Handle +n pattern
136-
if (this.lexer.token_type === TOKEN_DELIM && this.source.charCodeAt(this.lexer.token_start) === 0x2b /* + */) {
136+
if (this.lexer.token_type === TOKEN_DELIM && this.source.charCodeAt(this.lexer.token_start) === CHAR_PLUS) {
137137
// Look ahead for 'n'
138138
const saved_pos = this.lexer.pos
139139
this.lexer.next_token_fast(true)
@@ -151,7 +151,7 @@ export class ANplusBParser {
151151
// Check for attached n-digit pattern
152152
if (this.lexer.token_end > this.lexer.token_start + 1) {
153153
const second_char = this.source.charCodeAt(this.lexer.token_start + 1)
154-
if (second_char === 0x2d /* - */) {
154+
if (second_char === CHAR_MINUS_HYPHEN) {
155155
// +n-5 pattern
156156
b = this.source.substring(this.lexer.token_start + 1, this.lexer.token_end)
157157
b_start = this.lexer.token_start + 1
@@ -189,7 +189,7 @@ export class ANplusBParser {
189189
const remainder = token_text.substring(n_index + 1)
190190

191191
// n-5 or n+5 pattern in dimension
192-
if (remainder.charCodeAt(0) === 0x2d /* - */) {
192+
if (remainder.charCodeAt(0) === CHAR_MINUS_HYPHEN /* - */) {
193193
b = remainder
194194
b_start = this.lexer.token_start + n_index + 1
195195
b_end = this.lexer.token_end
@@ -236,16 +236,16 @@ export class ANplusBParser {
236236
if (this.lexer.token_type === TOKEN_DELIM) {
237237
const ch = this.source.charCodeAt(this.lexer.token_start)
238238

239-
if (ch === 0x2b /* + */ || ch === 0x2d /* - */) {
240-
const sign = ch === 0x2d ? '-' : ''
239+
if (ch === CHAR_PLUS || ch === CHAR_MINUS_HYPHEN) {
240+
const sign = ch === CHAR_MINUS_HYPHEN ? '-' : ''
241241
this.skip_whitespace()
242242

243243
this.lexer.next_token_fast(true)
244244

245245
if ((this.lexer.token_type as TokenType) === TOKEN_NUMBER) {
246246
let num_text = this.source.substring(this.lexer.token_start, this.lexer.token_end)
247247
// Remove leading + if present
248-
if (num_text.charCodeAt(0) === 0x2b /* + */) {
248+
if (num_text.charCodeAt(0) === CHAR_PLUS) {
249249
num_text = num_text.substring(1)
250250
}
251251
return sign === '-' ? sign + num_text : num_text
@@ -259,9 +259,9 @@ export class ANplusBParser {
259259
const first_char = num_text.charCodeAt(0)
260260

261261
// If it starts with + or -, it's a signed number
262-
if (first_char === 0x2b /* + */ || first_char === 0x2d /* - */) {
262+
if (first_char === CHAR_PLUS || first_char === CHAR_MINUS_HYPHEN) {
263263
// Remove leading + if present
264-
if (first_char === 0x2b) {
264+
if (first_char === CHAR_PLUS) {
265265
num_text = num_text.substring(1)
266266
}
267267
return num_text
@@ -276,9 +276,8 @@ export class ANplusBParser {
276276
const ch = this.source.charCodeAt(this.lexer.pos)
277277
if (is_whitespace_char(ch)) {
278278
this.lexer.pos++
279-
} else {
280-
break
281279
}
280+
break
282281
}
283282
}
284283

src/parse-atrule-prelude.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
TOKEN_FUNCTION,
2727
type TokenType,
2828
} from './token-types'
29-
import { trim_boundaries, str_equals, CHAR_SPACE, CHAR_TAB, CHAR_NEWLINE, CHAR_CARRIAGE_RETURN, CHAR_FORM_FEED } from './string-utils'
29+
import { trim_boundaries, str_equals, is_whitespace } from './string-utils'
3030
import { CSSNode } from './css-node'
3131

3232
export class AtRulePreludeParser {
@@ -630,7 +630,7 @@ export class AtRulePreludeParser {
630630
private skip_whitespace(): void {
631631
while (this.lexer.pos < this.prelude_end) {
632632
let ch = this.source.charCodeAt(this.lexer.pos)
633-
if (ch !== CHAR_SPACE && ch !== CHAR_TAB && ch !== CHAR_NEWLINE && ch !== CHAR_CARRIAGE_RETURN && ch !== CHAR_FORM_FEED) {
633+
if (!is_whitespace(ch)) {
634634
break
635635
}
636636
this.lexer.pos++

src/parse-selector.ts

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,25 @@ import {
3939
TOKEN_WHITESPACE,
4040
TOKEN_STRING,
4141
} from './token-types'
42-
import { is_whitespace as is_whitespace_char, is_vendor_prefixed } from './string-utils'
42+
import {
43+
is_whitespace as is_whitespace_char,
44+
is_vendor_prefixed,
45+
CHAR_PLUS,
46+
CHAR_TILDE,
47+
CHAR_GREATER_THAN,
48+
CHAR_PERIOD,
49+
CHAR_ASTERISK,
50+
CHAR_AMPERSAND,
51+
is_combinator,
52+
CHAR_EQUALS,
53+
CHAR_PIPE,
54+
CHAR_DOLLAR,
55+
CHAR_CARET,
56+
CHAR_FORWARD_SLASH,
57+
CHAR_SINGLE_QUOTE,
58+
CHAR_DOUBLE_QUOTE,
59+
CHAR_COLON,
60+
} from './string-utils'
4361
import { ANplusBParser } from './parse-anplusb'
4462
import { CSSNode } from './css-node'
4563

@@ -166,7 +184,7 @@ export class SelectorParser {
166184
// Check if token is a combinator
167185
if (token_type === TOKEN_DELIM) {
168186
let ch = this.source.charCodeAt(this.lexer.token_start)
169-
if (ch === 0x3e || ch === 0x2b || ch === 0x7e) {
187+
if (ch === CHAR_GREATER_THAN || ch === CHAR_PLUS || ch === CHAR_TILDE) {
170188
// Found leading combinator (>, +, ~) - this is a relative selector
171189
let combinator = this.create_combinator(this.lexer.token_start, this.lexer.token_end)
172190
components.push(combinator)
@@ -304,13 +322,13 @@ export class SelectorParser {
304322
case TOKEN_DELIM:
305323
// Could be: . (class), * (universal), & (nesting)
306324
let ch = this.source.charCodeAt(start)
307-
if (ch === 0x2e) {
325+
if (ch === CHAR_PERIOD) {
308326
// . - class selector
309327
return this.parse_class_selector(start)
310-
} else if (ch === 0x2a) {
328+
} else if (ch === CHAR_ASTERISK) {
311329
// * - universal selector
312330
return this.create_universal_selector(start, end)
313-
} else if (ch === 0x26) {
331+
} else if (ch === CHAR_AMPERSAND) {
314332
// & - nesting selector
315333
return this.create_nesting_selector(start, end)
316334
}
@@ -362,7 +380,7 @@ export class SelectorParser {
362380
// Check for explicit combinators
363381
if (this.lexer.token_type === TOKEN_DELIM) {
364382
let ch = this.source.charCodeAt(this.lexer.token_start)
365-
if (ch === 0x3e || ch === 0x2b || ch === 0x7e) {
383+
if (is_combinator(ch)) {
366384
// > + ~ (combinator text excludes leading whitespace)
367385
return this.create_combinator(this.lexer.token_start, this.lexer.token_end)
368386
}
@@ -463,10 +481,14 @@ export class SelectorParser {
463481
continue
464482
}
465483
// Skip comments /*...*/
466-
if (ch === 0x2f /* / */ && start + 1 < end && this.source.charCodeAt(start + 1) === 0x2a /* * */) {
484+
if (ch === CHAR_FORWARD_SLASH && start + 1 < end && this.source.charCodeAt(start + 1) === CHAR_ASTERISK) {
467485
start += 2 // Skip /*
468486
while (start < end) {
469-
if (this.source.charCodeAt(start) === 0x2a && start + 1 < end && this.source.charCodeAt(start + 1) === 0x2f) {
487+
if (
488+
this.source.charCodeAt(start) === CHAR_ASTERISK &&
489+
start + 1 < end &&
490+
this.source.charCodeAt(start + 1) === CHAR_FORWARD_SLASH
491+
) {
470492
start += 2 // Skip */
471493
break
472494
}
@@ -485,10 +507,10 @@ export class SelectorParser {
485507
continue
486508
}
487509
// Skip comments /*...*/
488-
if (ch === 0x2f && end >= 2 && this.source.charCodeAt(end - 2) === 0x2a) {
510+
if (ch === CHAR_FORWARD_SLASH && end >= 2 && this.source.charCodeAt(end - 2) === CHAR_ASTERISK) {
489511
// Find start of comment
490512
let pos = end - 2
491-
while (pos > start && !(this.source.charCodeAt(pos) === 0x2f && this.source.charCodeAt(pos + 1) === 0x2a)) {
513+
while (pos > start && !(this.source.charCodeAt(pos) === CHAR_FORWARD_SLASH && this.source.charCodeAt(pos + 1) === CHAR_ASTERISK)) {
492514
pos--
493515
}
494516
if (pos > start) {
@@ -513,12 +535,12 @@ export class SelectorParser {
513535
let ch = this.source.charCodeAt(name_end)
514536
if (
515537
is_whitespace_char(ch) ||
516-
ch === 0x3d /* = */ ||
517-
ch === 0x7e /* ~ */ ||
518-
ch === 0x7c /* | */ ||
519-
ch === 0x5e /* ^ */ ||
520-
ch === 0x24 /* $ */ ||
521-
ch === 0x2a /* * */
538+
ch === CHAR_EQUALS /* = */ ||
539+
ch === CHAR_TILDE /* ~ */ ||
540+
ch === CHAR_PIPE /* | */ ||
541+
ch === CHAR_CARET /* ^ */ ||
542+
ch === CHAR_DOLLAR /* $ */ ||
543+
ch === CHAR_ASTERISK /* * */
522544
) {
523545
break
524546
}
@@ -540,10 +562,10 @@ export class SelectorParser {
540562
continue
541563
}
542564
// Skip comments
543-
if (ch === 0x2f && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x2a) {
565+
if (ch === CHAR_FORWARD_SLASH && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_ASTERISK) {
544566
pos += 2
545567
while (pos < end) {
546-
if (this.source.charCodeAt(pos) === 0x2a && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x2f) {
568+
if (this.source.charCodeAt(pos) === CHAR_ASTERISK && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_FORWARD_SLASH) {
547569
pos += 2
548570
break
549571
}
@@ -563,27 +585,27 @@ export class SelectorParser {
563585
// Parse operator
564586
let ch1 = this.source.charCodeAt(pos)
565587

566-
if (ch1 === 0x3d) {
588+
if (ch1 === CHAR_EQUALS) {
567589
// =
568590
operator_end = pos + 1
569591
this.arena.set_attr_operator(node, ATTR_OPERATOR_EQUAL)
570-
} else if (ch1 === 0x7e && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x3d) {
592+
} else if (ch1 === CHAR_TILDE && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_EQUALS) {
571593
// ~=
572594
operator_end = pos + 2
573595
this.arena.set_attr_operator(node, ATTR_OPERATOR_TILDE_EQUAL)
574-
} else if (ch1 === 0x7c && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x3d) {
596+
} else if (ch1 === CHAR_PIPE && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_EQUALS) {
575597
// |=
576598
operator_end = pos + 2
577599
this.arena.set_attr_operator(node, ATTR_OPERATOR_PIPE_EQUAL)
578-
} else if (ch1 === 0x5e && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x3d) {
600+
} else if (ch1 === CHAR_CARET && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_EQUALS) {
579601
// ^=
580602
operator_end = pos + 2
581603
this.arena.set_attr_operator(node, ATTR_OPERATOR_CARET_EQUAL)
582-
} else if (ch1 === 0x24 && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x3d) {
604+
} else if (ch1 === CHAR_DOLLAR && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_EQUALS) {
583605
// $=
584606
operator_end = pos + 2
585607
this.arena.set_attr_operator(node, ATTR_OPERATOR_DOLLAR_EQUAL)
586-
} else if (ch1 === 0x2a && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x3d) {
608+
} else if (ch1 === CHAR_ASTERISK && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_EQUALS) {
587609
// *=
588610
operator_end = pos + 2
589611
this.arena.set_attr_operator(node, ATTR_OPERATOR_STAR_EQUAL)
@@ -602,10 +624,10 @@ export class SelectorParser {
602624
continue
603625
}
604626
// Skip comments
605-
if (ch === 0x2f && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x2a) {
627+
if (ch === CHAR_FORWARD_SLASH && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_ASTERISK) {
606628
pos += 2
607629
while (pos < end) {
608-
if (this.source.charCodeAt(pos) === 0x2a && pos + 1 < end && this.source.charCodeAt(pos + 1) === 0x2f) {
630+
if (this.source.charCodeAt(pos) === CHAR_ASTERISK && pos + 1 < end && this.source.charCodeAt(pos + 1) === CHAR_FORWARD_SLASH) {
609631
pos += 2
610632
break
611633
}
@@ -625,7 +647,7 @@ export class SelectorParser {
625647
value_start = pos
626648
let ch = this.source.charCodeAt(pos)
627649

628-
if (ch === 0x22 || ch === 0x27) {
650+
if (ch === CHAR_SINGLE_QUOTE || ch === CHAR_DOUBLE_QUOTE) {
629651
// " or '
630652
// Quoted string - find matching quote
631653
let quote = ch
@@ -673,7 +695,7 @@ export class SelectorParser {
673695

674696
// Check for double colon (::)
675697
let is_pseudo_element = false
676-
if (this.lexer.pos < this.selector_end && this.source.charCodeAt(this.lexer.pos) === 0x3a) {
698+
if (this.lexer.pos < this.selector_end && this.source.charCodeAt(this.lexer.pos) === CHAR_COLON) {
677699
is_pseudo_element = true
678700
this.lexer.pos++ // skip second colon
679701
}
@@ -1032,9 +1054,8 @@ export class SelectorParser {
10321054
let ch = this.source.charCodeAt(this.lexer.pos)
10331055
if (is_whitespace_char(ch)) {
10341056
this.lexer.pos++
1035-
} else {
1036-
break
10371057
}
1058+
break
10381059
}
10391060
}
10401061
}

0 commit comments

Comments
 (0)