forked from tildeio/htmlbars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
99 lines (82 loc) · 2.73 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { parse } from "./handlebars/compiler/base";
import { Tokenizer } from "./tokenizer";
import EntityParser from "../simple-html-tokenizer/entity-parser";
import fullCharRefs from "../simple-html-tokenizer/char-refs/full";
import nodeHandlers from "./node-handlers";
import tokenHandlers from "./token-handlers";
import * as syntax from "../htmlbars-syntax";
var splitLines;
// IE8 throws away blank pieces when splitting strings with a regex
// So we split using a string instead as appropriate
if ("foo\n\nbar".split(/\n/).length === 2) {
splitLines = function(str) {
var clean = str.replace(/\r\n?/g, '\n');
return clean.split('\n');
};
} else {
splitLines = function(str) {
return str.split(/(?:\r\n?|\n)/g);
};
}
export function preprocess(html, options) {
var ast = (typeof html === 'object') ? html : parse(html);
var combined = new HTMLProcessor(html, options).acceptNode(ast);
if (options && options.plugins && options.plugins.ast) {
for (var i = 0, l = options.plugins.ast.length; i < l; i++) {
var plugin = new options.plugins.ast[i](options);
plugin.syntax = syntax;
combined = plugin.transform(combined);
}
}
return combined;
}
export default preprocess;
function HTMLProcessor(source, options) {
this.options = options || {};
this.elementStack = [];
this.tokenizer = new Tokenizer('', new EntityParser(fullCharRefs));
this.nodeHandlers = nodeHandlers;
this.tokenHandlers = tokenHandlers;
if (typeof source === 'string') {
this.source = splitLines(source);
}
}
HTMLProcessor.prototype.acceptNode = function(node) {
return this.nodeHandlers[node.type].call(this, node);
};
HTMLProcessor.prototype.acceptToken = function(token) {
if (token) {
return this.tokenHandlers[token.type].call(this, token);
}
};
HTMLProcessor.prototype.currentElement = function() {
return this.elementStack[this.elementStack.length - 1];
};
HTMLProcessor.prototype.sourceForMustache = function(mustache) {
var firstLine = mustache.loc.start.line - 1;
var lastLine = mustache.loc.end.line - 1;
var currentLine = firstLine - 1;
var firstColumn = mustache.loc.start.column + 2;
var lastColumn = mustache.loc.end.column - 2;
var string = [];
var line;
if (!this.source) {
return '{{' + mustache.path.id.original + '}}';
}
while (currentLine < lastLine) {
currentLine++;
line = this.source[currentLine];
if (currentLine === firstLine) {
if (firstLine === lastLine) {
string.push(line.slice(firstColumn, lastColumn));
} else {
string.push(line.slice(firstColumn));
}
} else if (currentLine === lastLine) {
string.push(line.slice(0, lastColumn));
} else {
string.push(line);
}
}
return string.join('\n');
};