2015-10-13 03:29:13 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach
|
|
|
|
} from 'angular2/testing_internal';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
|
|
|
|
import {HtmlParser, HtmlParseTreeResult} from 'angular2/src/compiler/html_parser';
|
2015-08-25 18:36:02 -04:00
|
|
|
import {
|
|
|
|
HtmlAst,
|
|
|
|
HtmlAstVisitor,
|
|
|
|
HtmlElementAst,
|
|
|
|
HtmlAttrAst,
|
|
|
|
HtmlTextAst,
|
|
|
|
htmlVisitAll
|
2015-11-05 17:07:57 -05:00
|
|
|
} from 'angular2/src/compiler/html_ast';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-10-07 12:34:21 -04:00
|
|
|
describe('HtmlParser', () => {
|
2015-08-25 18:36:02 -04:00
|
|
|
var parser: HtmlParser;
|
|
|
|
beforeEach(() => { parser = new HtmlParser(); });
|
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
// TODO: add more test cases
|
|
|
|
// TODO: separate tests for source spans from tests for tree parsing
|
|
|
|
// TODO: find a better way to assert the tree structure!
|
|
|
|
// -> maybe with arrays and object hashes!!
|
2015-11-16 17:37:00 -05:00
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
describe('parse', () => {
|
2015-09-11 16:35:46 -04:00
|
|
|
describe('text nodes', () => {
|
|
|
|
it('should parse root level text nodes', () => {
|
2015-10-07 12:34:21 -04:00
|
|
|
expect(humanizeDom(parser.parse('a', 'TestComp'))).toEqual([[HtmlTextAst, 'a']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse text nodes inside regular elements', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<div>a</div>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, 'div'], [HtmlTextAst, 'a']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
it('should parse text nodes inside template elements', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<template>a</template>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, 'template'], [HtmlTextAst, 'a']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
});
|
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
describe('elements', () => {
|
|
|
|
it('should parse root level elements', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<div></div>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, 'div']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse elements inside of regular elements', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<div><span></span></div>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, 'div'], [HtmlElementAst, 'span']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse elements inside of template elements', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<template><span></span></template>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, 'template'], [HtmlElementAst, 'span']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
});
|
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
describe('attributes', () => {
|
2015-11-16 17:36:39 -05:00
|
|
|
it('should parse attributes on regular elements', () => {
|
2015-10-07 12:34:21 -04:00
|
|
|
expect(humanizeDom(parser.parse('<div kEy="v" key2=v2></div>', 'TestComp')))
|
2015-09-11 16:35:46 -04:00
|
|
|
.toEqual([
|
2015-10-07 12:34:21 -04:00
|
|
|
[HtmlElementAst, 'div'],
|
|
|
|
[HtmlAttrAst, 'kEy', 'v'],
|
|
|
|
[HtmlAttrAst, 'key2', 'v2'],
|
2015-09-11 16:35:46 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
it('should parse attributes without values', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<div k></div>', 'TestComp')))
|
|
|
|
.toEqual([[HtmlElementAst, 'div'], [HtmlAttrAst, 'k', '']]);
|
|
|
|
});
|
|
|
|
|
2015-11-06 14:31:03 -05:00
|
|
|
it('should parse attributes on svg elements case sensitive', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<svg viewBox="0"></svg>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, '@svg:svg'], [HtmlAttrAst, 'viewBox', '0']]);
|
2015-10-14 12:04:38 -04:00
|
|
|
});
|
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
it('should parse attributes on template elements', () => {
|
|
|
|
expect(humanizeDom(parser.parse('<template k="v"></template>', 'TestComp')))
|
2015-10-07 12:34:21 -04:00
|
|
|
.toEqual([[HtmlElementAst, 'template'], [HtmlAttrAst, 'k', 'v']]);
|
2015-09-11 16:35:46 -04:00
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
function humanizeDom(parseResult: HtmlParseTreeResult): any[] {
|
|
|
|
// TODO: humanize errors as well!
|
|
|
|
if (parseResult.errors.length > 0) {
|
|
|
|
throw parseResult.errors;
|
|
|
|
}
|
2015-11-16 17:36:39 -05:00
|
|
|
var humanizer = new Humanizer();
|
2015-10-07 12:34:21 -04:00
|
|
|
htmlVisitAll(humanizer, parseResult.rootNodes);
|
2015-08-25 18:36:02 -04:00
|
|
|
return humanizer.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Humanizer implements HtmlAstVisitor {
|
|
|
|
result: any[] = [];
|
2015-10-07 12:34:21 -04:00
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
visitElement(ast: HtmlElementAst, context: any): any {
|
2015-10-07 12:34:21 -04:00
|
|
|
this.result.push([HtmlElementAst, ast.name]);
|
2015-08-25 18:36:02 -04:00
|
|
|
htmlVisitAll(this, ast.attrs);
|
|
|
|
htmlVisitAll(this, ast.children);
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-07 12:34:21 -04:00
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
visitAttr(ast: HtmlAttrAst, context: any): any {
|
2015-10-07 12:34:21 -04:00
|
|
|
this.result.push([HtmlAttrAst, ast.name, ast.value]);
|
2015-08-25 18:36:02 -04:00
|
|
|
return null;
|
|
|
|
}
|
2015-10-07 12:34:21 -04:00
|
|
|
|
2015-09-11 16:35:46 -04:00
|
|
|
visitText(ast: HtmlTextAst, context: any): any {
|
2015-10-07 12:34:21 -04:00
|
|
|
this.result.push([HtmlTextAst, ast.value]);
|
2015-08-25 18:36:02 -04:00
|
|
|
return null;
|
|
|
|
}
|
2015-10-02 10:57:29 -04:00
|
|
|
}
|