utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isHtml = exports.cloneDom = exports.domEach = exports.cssCase = exports.camelCase = exports.isCheerio = exports.isTag = void 0;
  4. var domhandler_1 = require("domhandler");
  5. /**
  6. * Check if the DOM element is a tag.
  7. *
  8. * `isTag(type)` includes `<script>` and `<style>` tags.
  9. *
  10. * @private
  11. * @category Utils
  12. * @param type - The DOM node to check.
  13. * @returns Whether the node is a tag.
  14. */
  15. var domhandler_2 = require("domhandler");
  16. Object.defineProperty(exports, "isTag", { enumerable: true, get: function () { return domhandler_2.isTag; } });
  17. /**
  18. * Checks if an object is a Cheerio instance.
  19. *
  20. * @category Utils
  21. * @param maybeCheerio - The object to check.
  22. * @returns Whether the object is a Cheerio instance.
  23. */
  24. function isCheerio(maybeCheerio) {
  25. return maybeCheerio.cheerio != null;
  26. }
  27. exports.isCheerio = isCheerio;
  28. /**
  29. * Convert a string to camel case notation.
  30. *
  31. * @private
  32. * @category Utils
  33. * @param str - The string to be converted.
  34. * @returns String in camel case notation.
  35. */
  36. function camelCase(str) {
  37. return str.replace(/[_.-](\w|$)/g, function (_, x) { return x.toUpperCase(); });
  38. }
  39. exports.camelCase = camelCase;
  40. /**
  41. * Convert a string from camel case to "CSS case", where word boundaries are
  42. * described by hyphens ("-") and all characters are lower-case.
  43. *
  44. * @private
  45. * @category Utils
  46. * @param str - The string to be converted.
  47. * @returns String in "CSS case".
  48. */
  49. function cssCase(str) {
  50. return str.replace(/[A-Z]/g, '-$&').toLowerCase();
  51. }
  52. exports.cssCase = cssCase;
  53. /**
  54. * Iterate over each DOM element without creating intermediary Cheerio instances.
  55. *
  56. * This is indented for use internally to avoid otherwise unnecessary memory
  57. * pressure introduced by _make.
  58. *
  59. * @category Utils
  60. * @param array - The array to iterate over.
  61. * @param fn - Function to call.
  62. * @returns The original instance.
  63. */
  64. function domEach(array, fn) {
  65. var len = array.length;
  66. for (var i = 0; i < len; i++)
  67. fn(array[i], i);
  68. return array;
  69. }
  70. exports.domEach = domEach;
  71. /**
  72. * Create a deep copy of the given DOM structure. Sets the parents of the copies
  73. * of the passed nodes to `null`.
  74. *
  75. * @private
  76. * @category Utils
  77. * @param dom - The domhandler-compliant DOM structure.
  78. * @returns - The cloned DOM.
  79. */
  80. function cloneDom(dom) {
  81. var clone = 'length' in dom
  82. ? Array.prototype.map.call(dom, function (el) { return (0, domhandler_1.cloneNode)(el, true); })
  83. : [(0, domhandler_1.cloneNode)(dom, true)];
  84. // Add a root node around the cloned nodes
  85. var root = new domhandler_1.Document(clone);
  86. clone.forEach(function (node) {
  87. node.parent = root;
  88. });
  89. return clone;
  90. }
  91. exports.cloneDom = cloneDom;
  92. var CharacterCodes;
  93. (function (CharacterCodes) {
  94. CharacterCodes[CharacterCodes["LowerA"] = 97] = "LowerA";
  95. CharacterCodes[CharacterCodes["LowerZ"] = 122] = "LowerZ";
  96. CharacterCodes[CharacterCodes["UpperA"] = 65] = "UpperA";
  97. CharacterCodes[CharacterCodes["UpperZ"] = 90] = "UpperZ";
  98. CharacterCodes[CharacterCodes["Exclamation"] = 33] = "Exclamation";
  99. })(CharacterCodes || (CharacterCodes = {}));
  100. /**
  101. * Check if string is HTML.
  102. *
  103. * Tests for a `<` within a string, immediate followed by a letter and
  104. * eventually followed by a `>`.
  105. *
  106. * @private
  107. * @category Utils
  108. * @param str - The string to check.
  109. * @returns Indicates if `str` is HTML.
  110. */
  111. function isHtml(str) {
  112. var tagStart = str.indexOf('<');
  113. if (tagStart < 0 || tagStart > str.length - 3)
  114. return false;
  115. var tagChar = str.charCodeAt(tagStart + 1);
  116. return (((tagChar >= CharacterCodes.LowerA && tagChar <= CharacterCodes.LowerZ) ||
  117. (tagChar >= CharacterCodes.UpperA && tagChar <= CharacterCodes.UpperZ) ||
  118. tagChar === CharacterCodes.Exclamation) &&
  119. str.includes('>', tagStart + 2));
  120. }
  121. exports.isHtml = isHtml;
  122. //# sourceMappingURL=utils.js.map