2016-06-23 12:47:54 -04: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-06-08 19:38:52 -04:00
|
|
|
import {HtmlAst, HtmlAstVisitor, HtmlAttrAst, HtmlCommentAst, HtmlElementAst, HtmlExpansionAst, HtmlExpansionCaseAst, HtmlTextAst, htmlVisitAll} from '@angular/compiler/src/html_ast';
|
2016-06-15 12:37:33 -04:00
|
|
|
import {HtmlParseTreeResult} from '@angular/compiler/src/html_parser';
|
|
|
|
import {ParseLocation} from '@angular/compiler/src/parse_util';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
2016-03-23 16:43:28 -04:00
|
|
|
|
2016-06-17 14:38:24 -04:00
|
|
|
export function humanizeDom(
|
|
|
|
parseResult: HtmlParseTreeResult, addSourceSpan: boolean = false): any[] {
|
2016-03-23 16:43:28 -04:00
|
|
|
if (parseResult.errors.length > 0) {
|
|
|
|
var errorString = parseResult.errors.join('\n');
|
|
|
|
throw new BaseException(`Unexpected parse errors:\n${errorString}`);
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:38:24 -04:00
|
|
|
return humanizeNodes(parseResult.rootNodes, addSourceSpan);
|
2016-03-23 16:43:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function humanizeDomSourceSpans(parseResult: HtmlParseTreeResult): any[] {
|
2016-06-17 14:38:24 -04:00
|
|
|
return humanizeDom(parseResult, true);
|
|
|
|
}
|
2016-03-23 16:43:28 -04:00
|
|
|
|
2016-06-17 14:38:24 -04:00
|
|
|
export function humanizeNodes(nodes: HtmlAst[], addSourceSpan: boolean = false): any[] {
|
|
|
|
var humanizer = new _Humanizer(addSourceSpan);
|
|
|
|
htmlVisitAll(humanizer, nodes);
|
2016-03-23 16:43:28 -04:00
|
|
|
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 14:46:49 -04:00
|
|
|
visitExpansion(ast: HtmlExpansionAst, context: any): any {
|
2016-06-14 20:50:23 -04:00
|
|
|
var res =
|
|
|
|
this._appendContext(ast, [HtmlExpansionAst, ast.switchValue, ast.type, this.elDepth++]);
|
2016-04-12 14:46:49 -04:00
|
|
|
this.result.push(res);
|
|
|
|
htmlVisitAll(this, ast.cases);
|
2016-06-14 20:50:23 -04:00
|
|
|
this.elDepth--;
|
2016-04-12 14:46:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
visitExpansionCase(ast: HtmlExpansionCaseAst, context: any): any {
|
2016-06-14 20:50:23 -04:00
|
|
|
var res = this._appendContext(ast, [HtmlExpansionCaseAst, ast.value, this.elDepth]);
|
2016-04-12 14:46:49 -04:00
|
|
|
this.result.push(res);
|
|
|
|
}
|
|
|
|
|
2016-03-23 16:43:28 -04:00
|
|
|
private _appendContext(ast: HtmlAst, input: any[]): any[] {
|
|
|
|
if (!this.includeSourceSpan) return input;
|
|
|
|
input.push(ast.sourceSpan.toString());
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|