| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-01 12:19:09 -07:00
										 |  |  | import * as html from '../../src/ml_parser/ast'; | 
					
						
							|  |  |  | import {ParseTreeResult} from '../../src/ml_parser/html_parser'; | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | import {ParseLocation} from '../../src/parse_util'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function humanizeDom(parseResult: ParseTreeResult, addSourceSpan: boolean = false): any[] { | 
					
						
							|  |  |  |   if (parseResult.errors.length > 0) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const errorString = parseResult.errors.join('\n'); | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |     throw new Error(`Unexpected parse errors:\n${errorString}`); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return humanizeNodes(parseResult.rootNodes, addSourceSpan); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function humanizeDomSourceSpans(parseResult: ParseTreeResult): any[] { | 
					
						
							|  |  |  |   return humanizeDom(parseResult, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function humanizeNodes(nodes: html.Node[], addSourceSpan: boolean = false): any[] { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const humanizer = new _Humanizer(addSourceSpan); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   html.visitAll(humanizer, nodes); | 
					
						
							|  |  |  |   return humanizer.result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function humanizeLineColumn(location: ParseLocation): string { | 
					
						
							|  |  |  |   return `${location.line}:${location.col}`; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class _Humanizer implements html.Visitor { | 
					
						
							|  |  |  |   result: any[] = []; | 
					
						
							|  |  |  |   elDepth: number = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 19:51:03 +02:00
										 |  |  |   constructor(private includeSourceSpan: boolean) {} | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   visitElement(element: html.Element, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const res = this._appendContext(element, [html.Element, element.name, this.elDepth++]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |     this.result.push(res); | 
					
						
							|  |  |  |     html.visitAll(this, element.attrs); | 
					
						
							|  |  |  |     html.visitAll(this, element.children); | 
					
						
							|  |  |  |     this.elDepth--; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitAttribute(attribute: html.Attribute, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const res = this._appendContext(attribute, [html.Attribute, attribute.name, attribute.value]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitText(text: html.Text, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const res = this._appendContext(text, [html.Text, text.value, this.elDepth]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitComment(comment: html.Comment, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const res = this._appendContext(comment, [html.Comment, comment.value, this.elDepth]); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitExpansion(expansion: html.Expansion, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const res = this._appendContext( | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expansion, [html.Expansion, expansion.switchValue, expansion.type, this.elDepth++]); | 
					
						
							|  |  |  |     this.result.push(res); | 
					
						
							|  |  |  |     html.visitAll(this, expansion.cases); | 
					
						
							|  |  |  |     this.elDepth--; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   visitExpansionCase(expansionCase: html.ExpansionCase, context: any): any { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const res = | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         this._appendContext(expansionCase, [html.ExpansionCase, expansionCase.value, this.elDepth]); | 
					
						
							|  |  |  |     this.result.push(res); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private _appendContext(ast: html.Node, input: any[]): any[] { | 
					
						
							|  |  |  |     if (!this.includeSourceSpan) return input; | 
					
						
							| 
									
										
										
										
											2017-03-24 09:59:58 -07:00
										 |  |  |     input.push(ast.sourceSpan !.toString()); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |     return input; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |