utils.js 3.3 KB

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