1 |
|
2 |
|
3 |
|
4 |
|
5 | var Parser = require("fastparse");
|
6 |
|
7 | var parser = new Parser({
|
8 | outside: {
|
9 | "<!--.*?-->": true,
|
10 | "<![CDATA[.*?]]>": true,
|
11 | "<[!\\?].*?>": true,
|
12 | "<\/[^>]+>": true,
|
13 | "<([a-zA-Z\\-:]+)\\s*": function(match, tagName) {
|
14 | this.currentTag = tagName;
|
15 | return "inside";
|
16 | }
|
17 | },
|
18 | inside: {
|
19 | "\\s+": true,
|
20 | ">": "outside",
|
21 | "(([a-zA-Z\\-]+)\\s*=\\s*\")([^\"]*)\"": function(match, strUntilValue, name, value, index) {
|
22 | if(!this.isRelevantTagAttr(this.currentTag, name)) return;
|
23 | this.results.push({
|
24 | start: index + strUntilValue.length,
|
25 | length: value.length,
|
26 | value: value
|
27 | });
|
28 | },
|
29 | "(([a-zA-Z\\-]+)\\s*=\\s*)([^\\s>]+)": function(match, strUntilValue, name, value, index) {
|
30 | if(!this.isRelevantTagAttr(this.currentTag, name)) return;
|
31 | this.results.push({
|
32 | start: index + strUntilValue.length,
|
33 | length: value.length,
|
34 | value: value
|
35 | });
|
36 | }
|
37 | }
|
38 | });
|
39 |
|
40 |
|
41 | module.exports = function parse(html, isRelevantTagAttr) {
|
42 | return parser.parse("outside", html, {
|
43 | currentTag: null,
|
44 | results: [],
|
45 | isRelevantTagAttr: isRelevantTagAttr
|
46 | }).results;
|
47 | }; |
\ | No newline at end of file |