@@ -144,11 +144,18 @@ export default class Parser {
144
144
attr . push ( this . currToken ) ;
145
145
this . position ++ ;
146
146
}
147
- if ( this . currToken [ TOKEN . TYPE ] !== tokens . closeSquare ) {
148
- return this . expected ( 'closing square bracket' , this . currToken [ TOKEN . START_POS ] ) ;
147
+
148
+ if ( ! this . currToken ) {
149
+ this . position -- ;
150
+ return this . missingClosingSquareBracket ( ) ;
149
151
}
150
152
151
153
const len = attr . length ;
154
+
155
+ if ( len === 0 ) {
156
+ return this . expected ( 'attribute' , this . currToken [ TOKEN . START_POS ] ) ;
157
+ }
158
+
152
159
const node = {
153
160
source : getSource (
154
161
startingToken [ 1 ] ,
@@ -465,8 +472,7 @@ export default class Parser {
465
472
if ( rawSpace === space ) {
466
473
rawSpace = undefined ;
467
474
}
468
- let result = { space, rawSpace} ;
469
- return result ;
475
+ return { space, rawSpace} ;
470
476
}
471
477
472
478
isNamedCombinator ( position = this . position ) {
@@ -475,30 +481,27 @@ export default class Parser {
475
481
this . tokens [ position + 2 ] && this . tokens [ position + 2 ] [ TOKEN . TYPE ] === tokens . slash ;
476
482
477
483
}
484
+
478
485
namedCombinator ( ) {
479
- if ( this . isNamedCombinator ( ) ) {
480
- let nameRaw = this . content ( this . tokens [ this . position + 1 ] ) ;
481
- let name = unesc ( nameRaw ) . toLowerCase ( ) ;
482
- let raws = { } ;
483
- if ( name !== nameRaw ) {
484
- raws . value = `/${ nameRaw } /` ;
485
- }
486
- let node = new Combinator ( {
487
- value : `/${ name } /` ,
488
- source : getSource (
489
- this . currToken [ TOKEN . START_LINE ] ,
490
- this . currToken [ TOKEN . START_COL ] ,
491
- this . tokens [ this . position + 2 ] [ TOKEN . END_LINE ] ,
492
- this . tokens [ this . position + 2 ] [ TOKEN . END_COL ] ,
493
- ) ,
494
- sourceIndex : this . currToken [ TOKEN . START_POS ] ,
495
- raws,
496
- } ) ;
497
- this . position = this . position + 3 ;
498
- return node ;
499
- } else {
500
- this . unexpected ( ) ;
486
+ let nameRaw = this . content ( this . tokens [ this . position + 1 ] ) ;
487
+ let name = unesc ( nameRaw ) . toLowerCase ( ) ;
488
+ let raws = { } ;
489
+ if ( name !== nameRaw ) {
490
+ raws . value = `/${ nameRaw } /` ;
501
491
}
492
+ let node = new Combinator ( {
493
+ value : `/${ name } /` ,
494
+ source : getSource (
495
+ this . currToken [ TOKEN . START_LINE ] ,
496
+ this . currToken [ TOKEN . START_COL ] ,
497
+ this . tokens [ this . position + 2 ] [ TOKEN . END_LINE ] ,
498
+ this . tokens [ this . position + 2 ] [ TOKEN . END_COL ] ,
499
+ ) ,
500
+ sourceIndex : this . currToken [ TOKEN . START_POS ] ,
501
+ raws,
502
+ } ) ;
503
+ this . position = this . position + 3 ;
504
+ return node ;
502
505
}
503
506
504
507
combinator ( ) {
@@ -620,14 +623,22 @@ export default class Parser {
620
623
} ) ;
621
624
}
622
625
623
- missingParenthesis ( ) {
626
+ missingOpeningParenthesis ( ) {
624
627
return this . expected ( 'opening parenthesis' , this . currToken [ TOKEN . START_POS ] ) ;
625
628
}
626
629
627
- missingSquareBracket ( ) {
630
+ missingClosingParenthesis ( ) {
631
+ return this . expected ( 'closing parenthesis' , this . currToken [ TOKEN . START_POS ] ) ;
632
+ }
633
+
634
+ missingOpeningSquareBracket ( ) {
628
635
return this . expected ( 'opening square bracket' , this . currToken [ TOKEN . START_POS ] ) ;
629
636
}
630
637
638
+ missingClosingSquareBracket ( ) {
639
+ return this . expected ( 'closing square bracket' , this . currToken [ TOKEN . START_POS ] ) ;
640
+ }
641
+
631
642
unexpected ( ) {
632
643
return this . error ( `Unexpected '${ this . content ( ) } '. Escaping special characters with \\ may help.` , this . currToken [ TOKEN . START_POS ] ) ;
633
644
}
@@ -702,6 +713,10 @@ export default class Parser {
702
713
parenValue += this . parseParenthesisToken ( this . currToken ) ;
703
714
this . position ++ ;
704
715
}
716
+ if ( unbalanced ) {
717
+ this . position -- ;
718
+ return this . missingClosingParenthesis ( ) ;
719
+ }
705
720
if ( last ) {
706
721
last . appendToPropertyAndEscape ( "value" , parenValue , parenValue ) ;
707
722
} else {
@@ -718,7 +733,8 @@ export default class Parser {
718
733
}
719
734
}
720
735
if ( unbalanced ) {
721
- return this . expected ( 'closing parenthesis' , this . currToken [ TOKEN . START_POS ] ) ;
736
+ this . position -- ;
737
+ return this . missingClosingParenthesis ( ) ;
722
738
}
723
739
}
724
740
@@ -733,22 +749,13 @@ export default class Parser {
733
749
return this . expected ( [ 'pseudo-class' , 'pseudo-element' ] , this . position - 1 ) ;
734
750
}
735
751
if ( this . currToken [ TOKEN . TYPE ] === tokens . word ) {
736
- this . splitWord ( false , ( first , length ) => {
752
+ this . splitWord ( false , ( first ) => {
737
753
pseudoStr += first ;
738
754
this . newNode ( new Pseudo ( {
739
755
value : pseudoStr ,
740
756
source : getTokenSourceSpan ( startingToken , this . currToken ) ,
741
757
sourceIndex : startingToken [ TOKEN . START_POS ] ,
742
758
} ) ) ;
743
- if (
744
- length > 1 &&
745
- this . nextToken &&
746
- this . nextToken [ TOKEN . TYPE ] === tokens . openParenthesis
747
- ) {
748
- this . error ( 'Misplaced parenthesis.' , {
749
- index : this . nextToken [ TOKEN . START_POS ] ,
750
- } ) ;
751
- }
752
759
} ) ;
753
760
} else {
754
761
return this . expected ( [ 'pseudo-class' , 'pseudo-element' ] , this . currToken [ TOKEN . START_POS ] ) ;
@@ -813,13 +820,6 @@ export default class Parser {
813
820
this . position ++ ;
814
821
let current = this . content ( ) ;
815
822
word += current ;
816
- if ( current . lastIndexOf ( '\\' ) === current . length - 1 ) {
817
- let next = this . nextToken ;
818
- if ( next && next [ TOKEN . TYPE ] === tokens . space ) {
819
- word += this . requiredSpace ( this . content ( next ) ) ;
820
- this . position ++ ;
821
- }
822
- }
823
823
nextToken = this . nextToken ;
824
824
}
825
825
const hasClass = indexesOf ( word , '.' ) . filter ( i => word [ i - 1 ] !== '\\' ) ;
@@ -905,7 +905,7 @@ export default class Parser {
905
905
break ;
906
906
case tokens . closeParenthesis :
907
907
if ( throwOnParenthesis ) {
908
- this . missingParenthesis ( ) ;
908
+ this . missingOpeningParenthesis ( ) ;
909
909
}
910
910
break ;
911
911
case tokens . openSquare :
@@ -938,7 +938,7 @@ export default class Parser {
938
938
break ;
939
939
// These cases throw; no break needed.
940
940
case tokens . closeSquare :
941
- this . missingSquareBracket ( ) ;
941
+ this . missingOpeningSquareBracket ( ) ;
942
942
case tokens . semicolon :
943
943
this . missingBackslash ( ) ;
944
944
default :
0 commit comments