Skip to content

Commit bb77fad

Browse files
authored
fix: trailing whitespace in complex selector (#64)
closes #61
1 parent 65cf732 commit bb77fad

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/parse-selector.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,24 @@ describe('Selector Nodes', () => {
727727
expect(getNodeContent(arena, source, children[1])).toBe('is')
728728
})
729729

730+
it('should parse trailing space in :is() pseudo-class', () => {
731+
const root = parse_selector(':is(a )')
732+
const selector = root.first_child
733+
const pseudo = selector?.first_child!
734+
const [list] = pseudo.children
735+
const [a] = list.children
736+
expect(a.text).toBe('a')
737+
})
738+
739+
it('should parse trailing tab in :is() pseudo-class', () => {
740+
const root = parse_selector(':is(a )')
741+
const selector = root.first_child
742+
const pseudo = selector?.first_child!
743+
const [list] = pseudo.children
744+
const [a] = list.children
745+
expect(a.text).toBe('a')
746+
})
747+
730748
it('should parse :not() pseudo-class', () => {
731749
const { arena, rootNode, source } = parseSelectorInternal('div:not(.disabled)')
732750

src/parse-selector.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
CHAR_FORM_FEED,
6565
CHAR_NEWLINE,
6666
CHAR_SPACE,
67+
CHAR_TAB,
6768
} from './string-utils'
6869
import { ANplusBParser } from './parse-anplusb'
6970
import { CSSNode } from './css-node'
@@ -219,7 +220,10 @@ export class SelectorParser {
219220
// Peek ahead for comma or end
220221
const saved = this.lexer.save_position()
221222
this.skip_whitespace()
222-
if (this.lexer.pos >= this.selector_end) break
223+
if (this.lexer.pos >= this.selector_end) {
224+
this.lexer.restore_position(saved)
225+
break
226+
}
223227

224228
this.lexer.next_token_fast(false)
225229
let token_type = this.lexer.token_type
@@ -417,15 +421,18 @@ export class SelectorParser {
417421
while (this.lexer.pos < this.selector_end) {
418422
let ch = this.source.charCodeAt(this.lexer.pos)
419423
// no calling is_whitespace() because of function call overhead
420-
if (ch === CHAR_SPACE || ch === CHAR_NEWLINE || ch === CHAR_CARRIAGE_RETURN || ch === CHAR_FORM_FEED) {
424+
if (ch === CHAR_SPACE || ch === CHAR_TAB || ch === CHAR_NEWLINE || ch === CHAR_CARRIAGE_RETURN || ch === CHAR_FORM_FEED) {
421425
has_whitespace = true
422426
this.lexer.pos++
423427
} else {
424428
break
425429
}
426430
}
427431

428-
if (this.lexer.pos >= this.selector_end) return null
432+
if (this.lexer.pos >= this.selector_end) {
433+
this.lexer.pos = whitespace_start
434+
return null
435+
}
429436

430437
this.lexer.next_token_fast(false)
431438

@@ -445,7 +452,7 @@ export class SelectorParser {
445452
while (this.lexer.pos < this.selector_end) {
446453
let ch = this.source.charCodeAt(this.lexer.pos)
447454
// no calling is_whitespace() because of function call overhead
448-
if (ch === CHAR_SPACE || ch === CHAR_NEWLINE || ch === CHAR_CARRIAGE_RETURN || ch === CHAR_FORM_FEED) {
455+
if (ch === CHAR_SPACE || ch === CHAR_TAB || ch === CHAR_NEWLINE || ch === CHAR_CARRIAGE_RETURN || ch === CHAR_FORM_FEED) {
449456
this.lexer.pos++
450457
} else {
451458
break

0 commit comments

Comments
 (0)