You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var Parser = require("fastparse");
  6. var processMatch = function(match, strUntilValue, name, value, index) {
  7. if(!this.isRelevantTagAttr(this.currentTag, name)) return;
  8. this.results.push({
  9. start: index + strUntilValue.length,
  10. length: value.length,
  11. value: value
  12. });
  13. };
  14. var parser = new Parser({
  15. outside: {
  16. "<!--.*?-->": true,
  17. "<![CDATA[.*?]]>": true,
  18. "<[!\\?].*?>": true,
  19. "<\/[^>]+>": true,
  20. "<([a-zA-Z\\-:]+)\\s*": function(match, tagName) {
  21. this.currentTag = tagName;
  22. return "inside";
  23. }
  24. },
  25. inside: {
  26. "\\s+": true, // eat up whitespace
  27. ">": "outside", // end of attributes
  28. "(([0-9a-zA-Z\\-:]+)\\s*=\\s*\")([^\"]*)\"": processMatch,
  29. "(([0-9a-zA-Z\\-:]+)\\s*=\\s*\')([^\']*)\'": processMatch,
  30. "(([0-9a-zA-Z\\-:]+)\\s*=\\s*)([^\\s>]+)": processMatch
  31. }
  32. });
  33. module.exports = function parse(html, isRelevantTagAttr) {
  34. return parser.parse("outside", html, {
  35. currentTag: null,
  36. results: [],
  37. isRelevantTagAttr: isRelevantTagAttr
  38. }).results;
  39. };