@@ -20,9 +20,8 @@ import {
2020 HtmlCommentAst ,
2121 htmlVisitAll
2222} from 'angular2/src/compiler/html_ast' ;
23- import { ParseError , ParseLocation , ParseSourceSpan } from 'angular2/src/compiler/parse_util' ;
24-
25- import { BaseException } from 'angular2/src/facade/exceptions' ;
23+ import { ParseError , ParseLocation } from 'angular2/src/compiler/parse_util' ;
24+ import { humanizeDom , humanizeDomSourceSpans , humanizeLineColumn } from './html_ast_spec_utils' ;
2625
2726export function main ( ) {
2827 describe ( 'HtmlParser' , ( ) => {
@@ -51,6 +50,7 @@ export function main() {
5150 } ) ;
5251 } ) ;
5352
53+
5454 describe ( 'elements' , ( ) => {
5555 it ( 'should parse root level elements' , ( ) => {
5656 expect ( humanizeDom ( parser . parse ( '<div></div>' , 'TestComp' ) ) )
@@ -253,6 +253,16 @@ export function main() {
253253 [ HtmlTextAst , '\na\n' , 1 , '\na\n' ] ,
254254 ] ) ;
255255 } ) ;
256+
257+ it ( 'should set the start and end source spans' , ( ) => {
258+ let node = < HtmlElementAst > parser . parse ( '<div>a</div>' , 'TestComp' ) . rootNodes [ 0 ] ;
259+
260+ expect ( node . startSourceSpan . start . offset ) . toEqual ( 0 ) ;
261+ expect ( node . startSourceSpan . end . offset ) . toEqual ( 5 ) ;
262+
263+ expect ( node . endSourceSpan . start . offset ) . toEqual ( 6 ) ;
264+ expect ( node . endSourceSpan . end . offset ) . toEqual ( 12 ) ;
265+ } ) ;
256266 } ) ;
257267
258268 describe ( 'errors' , ( ) => {
@@ -299,33 +309,7 @@ export function main() {
299309 } ) ;
300310}
301311
302- function humanizeDom ( parseResult : HtmlParseTreeResult ) : any [ ] {
303- if ( parseResult . errors . length > 0 ) {
304- var errorString = parseResult . errors . join ( '\n' ) ;
305- throw new BaseException ( `Unexpected parse errors:\n${ errorString } ` ) ;
306- }
307-
308- var humanizer = new Humanizer ( false ) ;
309- htmlVisitAll ( humanizer , parseResult . rootNodes ) ;
310- return humanizer . result ;
311- }
312-
313- function humanizeDomSourceSpans ( parseResult : HtmlParseTreeResult ) : any [ ] {
314- if ( parseResult . errors . length > 0 ) {
315- var errorString = parseResult . errors . join ( '\n' ) ;
316- throw new BaseException ( `Unexpected parse errors:\n${ errorString } ` ) ;
317- }
318-
319- var humanizer = new Humanizer ( true ) ;
320- htmlVisitAll ( humanizer , parseResult . rootNodes ) ;
321- return humanizer . result ;
322- }
323-
324- function humanizeLineColumn ( location : ParseLocation ) : string {
325- return `${ location . line } :${ location . col } ` ;
326- }
327-
328- function humanizeErrors ( errors : ParseError [ ] ) : any [ ] {
312+ export function humanizeErrors ( errors : ParseError [ ] ) : any [ ] {
329313 return errors . map ( error => {
330314 if ( error instanceof HtmlTreeError ) {
331315 // Parser errors
@@ -334,44 +318,4 @@ function humanizeErrors(errors: ParseError[]): any[] {
334318 // Tokenizer errors
335319 return [ ( < any > error ) . tokenType , error . msg , humanizeLineColumn ( error . span . start ) ] ;
336320 } ) ;
337- }
338-
339- class Humanizer implements HtmlAstVisitor {
340- result : any [ ] = [ ] ;
341- elDepth : number = 0 ;
342-
343- constructor ( private includeSourceSpan : boolean ) { } ;
344-
345- visitElement ( ast : HtmlElementAst , context : any ) : any {
346- var res = this . _appendContext ( ast , [ HtmlElementAst , ast . name , this . elDepth ++ ] ) ;
347- this . result . push ( res ) ;
348- htmlVisitAll ( this , ast . attrs ) ;
349- htmlVisitAll ( this , ast . children ) ;
350- this . elDepth -- ;
351- return null ;
352- }
353-
354- visitAttr ( ast : HtmlAttrAst , context : any ) : any {
355- var res = this . _appendContext ( ast , [ HtmlAttrAst , ast . name , ast . value ] ) ;
356- this . result . push ( res ) ;
357- return null ;
358- }
359-
360- visitText ( ast : HtmlTextAst , context : any ) : any {
361- var res = this . _appendContext ( ast , [ HtmlTextAst , ast . value , this . elDepth ] ) ;
362- this . result . push ( res ) ;
363- return null ;
364- }
365-
366- visitComment ( ast : HtmlCommentAst , context : any ) : any {
367- var res = this . _appendContext ( ast , [ HtmlCommentAst , ast . value , this . elDepth ] ) ;
368- this . result . push ( res ) ;
369- return null ;
370- }
371-
372- private _appendContext ( ast : HtmlAst , input : any [ ] ) : any [ ] {
373- if ( ! this . includeSourceSpan ) return input ;
374- input . push ( ast . sourceSpan . toString ( ) ) ;
375- return input ;
376- }
377- }
321+ }
0 commit comments