| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {HtmlAst, HtmlAstVisitor, HtmlAttrAst, HtmlCommentAst, HtmlElementAst, HtmlExpansionAst, HtmlExpansionCaseAst, HtmlTextAst, htmlVisitAll} from '@angular/compiler/src/html_ast'; | 
					
						
							| 
									
										
										
										
											2016-06-15 09:37:33 -07:00
										 |  |  | import {HtmlParseTreeResult} from '@angular/compiler/src/html_parser'; | 
					
						
							|  |  |  | import {ParseLocation} from '@angular/compiler/src/parse_util'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {BaseException} from '../src/facade/exceptions'; | 
					
						
							| 
									
										
										
										
											2016-03-23 13:43:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function humanizeDom(parseResult: HtmlParseTreeResult): any[] { | 
					
						
							|  |  |  |   if (parseResult.errors.length > 0) { | 
					
						
							|  |  |  |     var errorString = parseResult.errors.join('\n'); | 
					
						
							|  |  |  |     throw new BaseException(`Unexpected parse errors:\n${errorString}`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var humanizer = new _Humanizer(false); | 
					
						
							|  |  |  |   htmlVisitAll(humanizer, parseResult.rootNodes); | 
					
						
							|  |  |  |   return humanizer.result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function humanizeDomSourceSpans(parseResult: HtmlParseTreeResult): any[] { | 
					
						
							|  |  |  |   if (parseResult.errors.length > 0) { | 
					
						
							|  |  |  |     var errorString = parseResult.errors.join('\n'); | 
					
						
							|  |  |  |     throw new BaseException(`Unexpected parse errors:\n${errorString}`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var humanizer = new _Humanizer(true); | 
					
						
							|  |  |  |   htmlVisitAll(humanizer, parseResult.rootNodes); | 
					
						
							|  |  |  |   return humanizer.result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function humanizeLineColumn(location: ParseLocation): string { | 
					
						
							|  |  |  |   return `${location.line}:${location.col}`; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _Humanizer implements HtmlAstVisitor { | 
					
						
							|  |  |  |   result: any[] = []; | 
					
						
							|  |  |  |   elDepth: number = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(private includeSourceSpan: boolean){}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitElement(ast: HtmlElementAst, context: any): any { | 
					
						
							|  |  |  |     var res = this._appendContext(ast, [HtmlElementAst, ast.name, this.elDepth++]); | 
					
						
							|  |  |  |     this.result.push(res); | 
					
						
							|  |  |  |     htmlVisitAll(this, ast.attrs); | 
					
						
							|  |  |  |     htmlVisitAll(this, ast.children); | 
					
						
							|  |  |  |     this.elDepth--; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitAttr(ast: HtmlAttrAst, context: any): any { | 
					
						
							|  |  |  |     var res = this._appendContext(ast, [HtmlAttrAst, ast.name, ast.value]); | 
					
						
							|  |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitText(ast: HtmlTextAst, context: any): any { | 
					
						
							|  |  |  |     var res = this._appendContext(ast, [HtmlTextAst, ast.value, this.elDepth]); | 
					
						
							|  |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitComment(ast: HtmlCommentAst, context: any): any { | 
					
						
							|  |  |  |     var res = this._appendContext(ast, [HtmlCommentAst, ast.value, this.elDepth]); | 
					
						
							|  |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 11:46:49 -07:00
										 |  |  |   visitExpansion(ast: HtmlExpansionAst, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-06-14 17:50:23 -07:00
										 |  |  |     var res = | 
					
						
							|  |  |  |         this._appendContext(ast, [HtmlExpansionAst, ast.switchValue, ast.type, this.elDepth++]); | 
					
						
							| 
									
										
										
										
											2016-04-12 11:46:49 -07:00
										 |  |  |     this.result.push(res); | 
					
						
							|  |  |  |     htmlVisitAll(this, ast.cases); | 
					
						
							| 
									
										
										
										
											2016-06-14 17:50:23 -07:00
										 |  |  |     this.elDepth--; | 
					
						
							| 
									
										
										
										
											2016-04-12 11:46:49 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitExpansionCase(ast: HtmlExpansionCaseAst, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-06-14 17:50:23 -07:00
										 |  |  |     var res = this._appendContext(ast, [HtmlExpansionCaseAst, ast.value, this.elDepth]); | 
					
						
							| 
									
										
										
										
											2016-04-12 11:46:49 -07:00
										 |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 13:43:28 -07:00
										 |  |  |   private _appendContext(ast: HtmlAst, input: any[]): any[] { | 
					
						
							|  |  |  |     if (!this.includeSourceSpan) return input; | 
					
						
							|  |  |  |     input.push(ast.sourceSpan.toString()); | 
					
						
							|  |  |  |     return input; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |