Skip to content

Commit fbc173b

Browse files
authored
refactor: Use domutils module directly (#1928)
Simplifies some imports, and allows Cheerio to pull in updates.
1 parent a3b11aa commit fbc173b

File tree

7 files changed

+32
-29
lines changed

7 files changed

+32
-29
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"cheerio-select": "^1.5.0",
3737
"dom-serializer": "^1.3.2",
3838
"domhandler": "^4.2.0",
39+
"domutils": "^2.7.0",
3940
"htmlparser2": "^6.1.0",
4041
"parse5": "^6.0.1",
4142
"parse5-htmlparser2-tree-adapter": "^6.0.1",

src/api/manipulation.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { hasChildren } from 'domhandler';
21
/**
32
* Methods for modifying the DOM structure.
43
*
54
* @module cheerio/manipulation
65
*/
76

8-
import { Node, NodeWithChildren, Element, Text } from 'domhandler';
7+
import { Node, NodeWithChildren, Element, Text, hasChildren } from 'domhandler';
98
import { default as parse, update as updateDOM } from '../parse';
109
import { html as staticHtml, text as staticText } from '../static';
1110
import { domEach, cloneDom, isTag, isHtml, isCheerio } from '../utils';
12-
import { DomUtils } from 'htmlparser2';
11+
import { removeElement } from 'domutils';
1312
import type { Cheerio } from '../cheerio';
1413
import type { BasicAcceptedElems, AcceptedElems } from '../types';
1514

@@ -279,7 +278,7 @@ function _wrap(
279278

280279
const [wrapperDom] = this._makeDomArray(wrap, i < lastIdx);
281280

282-
if (!wrapperDom || !DomUtils.hasChildren(wrapperDom)) continue;
281+
if (!wrapperDom || !hasChildren(wrapperDom)) continue;
283282

284283
let elInsertLocation = wrapperDom;
285284

@@ -587,7 +586,7 @@ export function after<T extends Node>(
587586

588587
return domEach(this, (el, i) => {
589588
const { parent } = el;
590-
if (!DomUtils.hasChildren(el) || !parent) {
589+
if (!hasChildren(el) || !parent) {
591590
return;
592591
}
593592

@@ -701,7 +700,7 @@ export function before<T extends Node>(
701700

702701
return domEach(this, (el, i) => {
703702
const { parent } = el;
704-
if (!DomUtils.hasChildren(el) || !parent) {
703+
if (!hasChildren(el) || !parent) {
705704
return;
706705
}
707706

@@ -807,7 +806,7 @@ export function remove<T extends Node>(
807806
const elems = selector ? this.filter(selector) : this;
808807

809808
domEach(elems, (el) => {
810-
DomUtils.removeElement(el);
809+
removeElement(el);
811810
el.prev = el.next = el.parent = null;
812811
});
813812

@@ -884,7 +883,7 @@ export function replaceWith<T extends Node>(
884883
*/
885884
export function empty<T extends Node>(this: Cheerio<T>): Cheerio<T> {
886885
return domEach(this, (el) => {
887-
if (!DomUtils.hasChildren(el)) return;
886+
if (!hasChildren(el)) return;
888887
el.children.forEach((child) => {
889888
child.next = child.prev = child.parent = null;
890889
});
@@ -923,15 +922,15 @@ export function html<T extends Node>(
923922
): Cheerio<T> | string | null {
924923
if (str === undefined) {
925924
const el = this[0];
926-
if (!el || !DomUtils.hasChildren(el)) return null;
925+
if (!el || !hasChildren(el)) return null;
927926
return staticHtml(el.children, this.options);
928927
}
929928

930929
// Keep main options unchanged
931930
const opts = { ...this.options, context: null as NodeWithChildren | null };
932931

933932
return domEach(this, (el) => {
934-
if (!DomUtils.hasChildren(el)) return;
933+
if (!hasChildren(el)) return;
935934
el.children.forEach((child) => {
936935
child.next = child.prev = child.parent = null;
937936
});
@@ -1000,7 +999,7 @@ export function text<T extends Node>(
1000999

10011000
// Append text node to each selected elements
10021001
return domEach(this, (el) => {
1003-
if (!DomUtils.hasChildren(el)) return;
1002+
if (!hasChildren(el)) return;
10041003
el.children.forEach((child) => {
10051004
child.next = child.prev = child.parent = null;
10061005
});

src/api/traversing.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import type { Cheerio } from '../cheerio';
99
import * as select from 'cheerio-select';
1010
import { domEach, isTag, isCheerio } from '../utils';
1111
import { contains } from '../static';
12-
import { DomUtils } from 'htmlparser2';
12+
import {
13+
getChildren,
14+
getSiblings,
15+
nextElementSibling,
16+
prevElementSibling,
17+
uniqueSort,
18+
} from 'domutils';
1319
import type { FilterFunction, AcceptedFilters } from '../types';
14-
const { uniqueSort } = DomUtils;
1520
const reSiblingSelector = /^\s*[~+]/;
1621

1722
/**
@@ -331,7 +336,7 @@ export function closest<T extends Node>(
331336
* @returns The next nodes.
332337
* @see {@link https://api.jquery.com/next/}
333338
*/
334-
export const next = _singleMatcher((elem) => DomUtils.nextElementSibling(elem));
339+
export const next = _singleMatcher((elem) => nextElementSibling(elem));
335340

336341
/**
337342
* Gets all the following siblings of the first selected element, optionally
@@ -378,7 +383,7 @@ export const nextAll = _matcher((elem) => {
378383
* @see {@link https://api.jquery.com/nextUntil/}
379384
*/
380385
export const nextUntil = _matchUntil(
381-
(el) => DomUtils.nextElementSibling(el),
386+
(el) => nextElementSibling(el),
382387
_removeDuplicates
383388
);
384389

@@ -398,7 +403,7 @@ export const nextUntil = _matchUntil(
398403
* @returns The previous nodes.
399404
* @see {@link https://api.jquery.com/prev/}
400405
*/
401-
export const prev = _singleMatcher((elem) => DomUtils.prevElementSibling(elem));
406+
export const prev = _singleMatcher((elem) => prevElementSibling(elem));
402407

403408
/**
404409
* Gets all the preceding siblings of the first selected element, optionally
@@ -446,7 +451,7 @@ export const prevAll = _matcher((elem) => {
446451
* @see {@link https://api.jquery.com/prevUntil/}
447452
*/
448453
export const prevUntil = _matchUntil(
449-
(el) => DomUtils.prevElementSibling(el),
454+
(el) => prevElementSibling(el),
450455
_removeDuplicates
451456
);
452457

@@ -471,9 +476,7 @@ export const prevUntil = _matchUntil(
471476
*/
472477
export const siblings = _matcher(
473478
(elem) =>
474-
DomUtils.getSiblings(elem).filter(
475-
(el): el is Element => isTag(el) && el !== elem
476-
),
479+
getSiblings(elem).filter((el): el is Element => isTag(el) && el !== elem),
477480
uniqueSort
478481
);
479482

@@ -496,7 +499,7 @@ export const siblings = _matcher(
496499
* @see {@link https://api.jquery.com/children/}
497500
*/
498501
export const children = _matcher(
499-
(elem) => DomUtils.getChildren(elem).filter(isTag),
502+
(elem) => getChildren(elem).filter(isTag),
500503
_removeDuplicates
501504
);
502505

src/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DomUtils } from 'htmlparser2';
1+
import { removeElement } from 'domutils';
22
import { parse as parseWithHtmlparser2 } from './parsers/htmlparser2-adapter';
33
import { parse as parseWithParse5 } from './parsers/parse5-adapter';
44
import {
@@ -70,7 +70,7 @@ export function update(
7070

7171
// Cleanly remove existing nodes from their previous structures.
7272
if (node.parent && node.parent.children !== arr) {
73-
DomUtils.removeElement(node);
73+
removeElement(node);
7474
}
7575

7676
if (parent) {

src/static.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { CheerioAPI, Cheerio } from '.';
2-
import { Node, Document } from 'domhandler';
2+
import { Node, Document, isText, hasChildren } from 'domhandler';
33
import {
44
InternalOptions,
55
CheerioOptions,
66
default as defaultOptions,
77
flatten as flattenOptions,
88
} from './options';
99
import { select } from 'cheerio-select';
10-
import { ElementType, DomUtils } from 'htmlparser2';
10+
import { ElementType } from 'htmlparser2';
1111
import { render as renderWithParse5 } from './parsers/parse5-adapter';
1212
import { render as renderWithHtmlparser2 } from './parsers/htmlparser2-adapter';
1313

@@ -137,9 +137,9 @@ export function text(
137137

138138
for (let i = 0; i < elems.length; i++) {
139139
const elem = elems[i];
140-
if (DomUtils.isText(elem)) ret += elem.data;
140+
if (isText(elem)) ret += elem.data;
141141
else if (
142-
DomUtils.hasChildren(elem) &&
142+
hasChildren(elem) &&
143143
elem.type !== ElementType.Comment &&
144144
elem.type !== ElementType.Script &&
145145
elem.type !== ElementType.Style

src/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DomUtils } from 'htmlparser2';
21
import { Node, cloneNode, Document } from 'domhandler';
32
import type { Cheerio } from './cheerio';
43

@@ -12,7 +11,7 @@ import type { Cheerio } from './cheerio';
1211
* @param type - DOM node to check.
1312
* @returns Whether the node is a tag.
1413
*/
15-
export const { isTag } = DomUtils;
14+
export { isTag } from 'domhandler';
1615

1716
/**
1817
* Checks if an object is a Cheerio instance.

0 commit comments

Comments
 (0)