您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

83 行
2.1 KiB

3 年前
  1. var parse = require('./parse'),
  2. render = require('dom-serializer');
  3. /**
  4. * HTML Tags
  5. */
  6. var tags = { tag: true, script: true, style: true };
  7. /**
  8. * Check if the DOM element is a tag
  9. *
  10. * isTag(type) includes <script> and <style> tags
  11. */
  12. exports.isTag = function(type) {
  13. if (type.type) type = type.type;
  14. return tags[type] || false;
  15. };
  16. /**
  17. * Convert a string to camel case notation.
  18. * @param {String} str String to be converted.
  19. * @return {String} String in camel case notation.
  20. */
  21. exports.camelCase = function(str) {
  22. return str.replace(/[_.-](\w|$)/g, function(_, x) {
  23. return x.toUpperCase();
  24. });
  25. };
  26. /**
  27. * Convert a string from camel case to "CSS case", where word boundaries are
  28. * described by hyphens ("-") and all characters are lower-case.
  29. * @param {String} str String to be converted.
  30. * @return {string} String in "CSS case".
  31. */
  32. exports.cssCase = function(str) {
  33. return str.replace(/[A-Z]/g, '-$&').toLowerCase();
  34. };
  35. /**
  36. * Iterate over each DOM element without creating intermediary Cheerio instances.
  37. *
  38. * This is indented for use internally to avoid otherwise unnecessary memory pressure introduced
  39. * by _make.
  40. */
  41. exports.domEach = function(cheerio, fn) {
  42. var i = 0, len = cheerio.length;
  43. while (i < len && fn.call(cheerio, i, cheerio[i]) !== false) ++i;
  44. return cheerio;
  45. };
  46. /**
  47. * Create a deep copy of the given DOM structure by first rendering it to a
  48. * string and then parsing the resultant markup.
  49. *
  50. * @argument {Object} dom - The htmlparser2-compliant DOM structure
  51. * @argument {Object} options - The parsing/rendering options
  52. */
  53. exports.cloneDom = function(dom, options) {
  54. return parse(render(dom, options), options).children;
  55. };
  56. /*
  57. * A simple way to check for HTML strings or ID strings
  58. */
  59. var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
  60. /*
  61. * Check if string is HTML
  62. */
  63. exports.isHtml = function(str) {
  64. // Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML
  65. if (str.charAt(0) === '<' && str.charAt(str.length - 1) === '>' && str.length >= 3) return true;
  66. // Run the regex
  67. var match = quickExpr.exec(str);
  68. return !!(match && match[1]);
  69. };