@@ -34,8 +34,8 @@ export class HtmlToken {
3434}
3535
3636export class HtmlTokenError extends ParseError {
37- constructor ( errorMsg : string , public tokenType : HtmlTokenType , location : ParseLocation ) {
38- super ( location , errorMsg ) ;
37+ constructor ( errorMsg : string , public tokenType : HtmlTokenType , span : ParseSourceSpan ) {
38+ super ( span , errorMsg ) ;
3939 }
4040}
4141
@@ -125,7 +125,8 @@ class _HtmlTokenizer {
125125
126126 private _processCarriageReturns ( content : string ) : string {
127127 // http://www.w3.org/TR/html5/syntax.html#preprocessing-the-input-stream
128- // In order to keep the original position in the source, we can not pre-process it.
128+ // In order to keep the original position in the source, we can not
129+ // pre-process it.
129130 // Instead CRs are processed right before instantiating the tokens.
130131 return StringWrapper . replaceAll ( content , CR_OR_CRLF_REGEXP , '\n' ) ;
131132 }
@@ -168,6 +169,16 @@ class _HtmlTokenizer {
168169 return new ParseLocation ( this . file , this . index , this . line , this . column ) ;
169170 }
170171
172+ private _getSpan ( start ?: ParseLocation , end ?: ParseLocation ) : ParseSourceSpan {
173+ if ( isBlank ( start ) ) {
174+ start = this . _getLocation ( ) ;
175+ }
176+ if ( isBlank ( end ) ) {
177+ end = this . _getLocation ( ) ;
178+ }
179+ return new ParseSourceSpan ( start , end ) ;
180+ }
181+
171182 private _beginToken ( type : HtmlTokenType , start : ParseLocation = null ) {
172183 if ( isBlank ( start ) ) {
173184 start = this . _getLocation ( ) ;
@@ -188,16 +199,16 @@ class _HtmlTokenizer {
188199 return token ;
189200 }
190201
191- private _createError ( msg : string , position : ParseLocation ) : ControlFlowError {
192- var error = new HtmlTokenError ( msg , this . currentTokenType , position ) ;
202+ private _createError ( msg : string , span : ParseSourceSpan ) : ControlFlowError {
203+ var error = new HtmlTokenError ( msg , this . currentTokenType , span ) ;
193204 this . currentTokenStart = null ;
194205 this . currentTokenType = null ;
195206 return new ControlFlowError ( error ) ;
196207 }
197208
198209 private _advance ( ) {
199210 if ( this . index >= this . length ) {
200- throw this . _createError ( unexpectedCharacterErrorMsg ( $EOF ) , this . _getLocation ( ) ) ;
211+ throw this . _createError ( unexpectedCharacterErrorMsg ( $EOF ) , this . _getSpan ( ) ) ;
201212 }
202213 if ( this . peek === $LF ) {
203214 this . line ++ ;
@@ -228,7 +239,8 @@ class _HtmlTokenizer {
228239 private _requireCharCode ( charCode : number ) {
229240 var location = this . _getLocation ( ) ;
230241 if ( ! this . _attemptCharCode ( charCode ) ) {
231- throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , location ) ;
242+ throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) ,
243+ this . _getSpan ( location , location ) ) ;
232244 }
233245 }
234246
@@ -253,7 +265,7 @@ class _HtmlTokenizer {
253265 private _requireStr ( chars : string ) {
254266 var location = this . _getLocation ( ) ;
255267 if ( ! this . _attemptStr ( chars ) ) {
256- throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , location ) ;
268+ throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , this . _getSpan ( location ) ) ;
257269 }
258270 }
259271
@@ -267,7 +279,7 @@ class _HtmlTokenizer {
267279 var start = this . _getLocation ( ) ;
268280 this . _attemptCharCodeUntilFn ( predicate ) ;
269281 if ( this . index - start . offset < len ) {
270- throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , start ) ;
282+ throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , this . _getSpan ( start , start ) ) ;
271283 }
272284 }
273285
@@ -295,7 +307,7 @@ class _HtmlTokenizer {
295307 let numberStart = this . _getLocation ( ) . offset ;
296308 this . _attemptCharCodeUntilFn ( isDigitEntityEnd ) ;
297309 if ( this . peek != $SEMICOLON ) {
298- throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , this . _getLocation ( ) ) ;
310+ throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , this . _getSpan ( ) ) ;
299311 }
300312 this . _advance ( ) ;
301313 let strNum = this . input . substring ( numberStart , this . index - 1 ) ;
@@ -304,7 +316,7 @@ class _HtmlTokenizer {
304316 return StringWrapper . fromCharCode ( charCode ) ;
305317 } catch ( e ) {
306318 let entity = this . input . substring ( start . offset + 1 , this . index - 1 ) ;
307- throw this . _createError ( unknownEntityErrorMsg ( entity ) , start ) ;
319+ throw this . _createError ( unknownEntityErrorMsg ( entity ) , this . _getSpan ( start ) ) ;
308320 }
309321 } else {
310322 let startPosition = this . _savePosition ( ) ;
@@ -317,7 +329,7 @@ class _HtmlTokenizer {
317329 let name = this . input . substring ( start . offset + 1 , this . index - 1 ) ;
318330 let char = NAMED_ENTITIES [ name ] ;
319331 if ( isBlank ( char ) ) {
320- throw this . _createError ( unknownEntityErrorMsg ( name ) , start ) ;
332+ throw this . _createError ( unknownEntityErrorMsg ( name ) , this . _getSpan ( start ) ) ;
321333 }
322334 return char ;
323335 }
@@ -394,7 +406,7 @@ class _HtmlTokenizer {
394406 let lowercaseTagName ;
395407 try {
396408 if ( ! isAsciiLetter ( this . peek ) ) {
397- throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , this . _getLocation ( ) ) ;
409+ throw this . _createError ( unexpectedCharacterErrorMsg ( this . peek ) , this . _getSpan ( ) ) ;
398410 }
399411 var nameStart = this . index ;
400412 this . _consumeTagOpenStart ( start ) ;
0 commit comments