-
Notifications
You must be signed in to change notification settings - Fork 15
/
scriptParser.js
46 lines (45 loc) · 1.45 KB
/
scriptParser.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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Andrey Koperskiy @teux
*/
var Parser = require('fastparse');
module.exports = new Parser({
root: {
'<script': function(match, idx) {
this.idx = idx;
return 'scriptTag';
},
'<\\/script[\\s\\n]*>': function(match, idx, len) {
if (this.isTemplate) {
this.scripts.push({
id: this.id, // template name
idx: this.idx, // script begin index
len: idx + len - this.idx, // script including final script tag length
contIdx: this.contIdx, // content begin index
contLen: idx - this.idx - this.contIdx, // content up to final script tag length
});
}
this.isTemplate = this.idx = this.contIdx = this.id = undefined;
},
},
scriptTag: {
'type[\\s\\n]*=[\\s\\n]*[\'"]': 'typeAttr',
'id[\\s\\n]*=[\\s\\n]*[\'"]': 'idAttr',
'>': function(match, idx) {
this.contIdx = idx - this.idx + 1;
return 'root';
},
},
typeAttr: {
'text/ng-template': function() {
this.isTemplate = true;
},
'[\'"]': 'scriptTag',
},
idAttr: {
'[^\'"\\s\\n]+': function(match) {
this.id = match;
},
'[\'"]': 'scriptTag',
},
});