UNPKG

1.26 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5var Parser = require("fastparse");
6
7var 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, // eat up whitespace
20 ">": "outside", // end of attributes
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
41module.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