Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AoT compilation #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"dependencies": {
"zone.js": "~0.7.0",
"core-js": "~2.4.0",
"rxjs": "~5.0.0"
"rxjs": "~5.0.0",
"parse5": "~3.0.0"
},
"devDependencies": {
"gulp": "~3.9.0",
Expand Down
167 changes: 167 additions & 0 deletions src/Parsers/AbstractHTMLParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import {TextParser} from './TextParser';
import {Strings} from '../Util/Strings';


export enum HTMLAttributeType
{
NATIVE,
EXPRESSION,
PROPERTY,
EVENT,
EXPORT,
TEMPLATE,
}


export enum HTMLTokenType
{
T_ELEMENT,
T_STRING,
T_EXPRESSION,
}


export declare interface AttributeToken
{
type: HTMLAttributeType,
name: string,
originalName: string,
value: string,
preventDefault?: boolean,
}


export declare interface StringToken
{
type: HTMLTokenType,
value: string,
parent: ElementToken,
}


export declare interface ElementToken
{
type: HTMLTokenType,
name: string,
attributes: {[name: string]: AttributeToken},
parent: ElementToken,
children: Array<StringToken|ElementToken>,
}


export abstract class AbstractHTMLParser
{


private static TWO_WAY_BINDING_CHANGE = 'change';


protected exports: Array<string> = [];


protected parseAttribute(name: string, value: string): Array<AttributeToken>
{
let type = HTMLAttributeType.NATIVE;
let preventDefault = false;
let match;

if (match = name.match(/^\[\((.+)\)]$/)) {
return [
this.parseAttribute('[' + match[1] + ']', value)[0],
this.parseAttribute('(' + match[1] + '-' + AbstractHTMLParser.TWO_WAY_BINDING_CHANGE + ')!', value + '=$value')[0],
];
}

if (match = name.match(/^\*(.+)/)) {
type = HTMLAttributeType.TEMPLATE;
name = match[1];
} else if (match = name.match(/^#(.+)/)) {
type = HTMLAttributeType.EXPORT;
name = match[1];
} else if (match = name.match(/^\[(.+)]$/)) {
type = HTMLAttributeType.PROPERTY;
name = match[1];
} else if (match = name.match(/^\((.+)\)!?$/)) {
preventDefault = name.slice(-1) === '!';
type = HTMLAttributeType.EVENT;
name = match[1];
}

if (type === HTMLAttributeType.NATIVE) {
let attr = this.parseAttributeValue(value);
type = attr.type;
value = attr.value;
}

let attributes = [];

if (type === HTMLAttributeType.EVENT) {
let events = name.split('|');
for (let i = 0; i < events.length; i++) {
attributes.push({
type: type,
name: Strings.hyphensToCamelCase(events[i]),
originalName: events[i],
value: value,
preventDefault: preventDefault,
});
}

} else {
let attribute: AttributeToken = {
type: type,
name: Strings.hyphensToCamelCase(name),
originalName: name,
value: value,
};

attributes.push(attribute);

if (attribute.type === HTMLAttributeType.EXPORT && this.exports.indexOf(attribute.name) < 0) {
this.exports.push(attribute.name);
}
}

return attributes;
}


protected parseAttributeValue(value: string): {type: HTMLAttributeType, value: string}
{
let tokens = TextParser.parse(value);
let type = HTMLAttributeType.NATIVE;

if (tokens.length === 0) {
// skip

} else if (tokens.length === 1) {
if (tokens[0].type === TextParser.TYPE_BINDING) {
type = HTMLAttributeType.EXPRESSION;
value = tokens[0].value;
}

} else {
let buffer = [];

for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];

if (token.type === TextParser.TYPE_TEXT) {
buffer.push('"' + token.value + '"');

} else if (token.type === TextParser.TYPE_BINDING) {
buffer.push('(' + token.value + ')');
}
}

type = HTMLAttributeType.EXPRESSION;
value = buffer.join('+');
}

return {
type: type,
value: value,
};
}

}
42 changes: 42 additions & 0 deletions src/Parsers/BrowserElementParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {AbstractHTMLParser, ElementToken, AttributeToken, HTMLTokenType, HTMLAttributeType} from './AbstractHTMLParser';


export class BrowserElementParser extends AbstractHTMLParser
{


public parse(element: Element): ElementToken
{
if (element.nodeName.toLowerCase() === 'template') {
throw new Error('BrowserElementParser: can not parse template element.');
}

return {
type: HTMLTokenType.T_ELEMENT,
name: element.nodeName.toLowerCase(),
attributes: this.parseAttributes(element),
parent: null,
children: [],
};
}


private parseAttributes(element: Element): {[name: string]: AttributeToken}
{
let attributes = {};

for (let i = 0; i < element.attributes.length; i++) {
let append = this.parseAttribute(element.attributes[i].name, element.attributes[i].value);
for (let j = 0; j < append.length; j++) {
if (append[j].type === HTMLAttributeType.TEMPLATE) {
throw new Error('BrowserElementParser: can not parse template shortcut attribute "' + append[j].name + '".');
}

attributes[append[j].name] = append[j];
}
}

return <any>attributes;
}

}
Loading