Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use domutils module directly #1928

Merged
merged 2 commits into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cheerio-select": "^1.5.0",
"dom-serializer": "^1.3.2",
"domhandler": "^4.2.0",
"domutils": "^2.7.0",
"htmlparser2": "^6.1.0",
"parse5": "^6.0.1",
"parse5-htmlparser2-tree-adapter": "^6.0.1",
Expand Down
21 changes: 10 additions & 11 deletions src/api/manipulation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { hasChildren } from 'domhandler';
/**
* Methods for modifying the DOM structure.
*
* @module cheerio/manipulation
*/

import { Node, NodeWithChildren, Element, Text } from 'domhandler';
import { Node, NodeWithChildren, Element, Text, hasChildren } from 'domhandler';
import { default as parse, update as updateDOM } from '../parse';
import { html as staticHtml, text as staticText } from '../static';
import { domEach, cloneDom, isTag, isHtml, isCheerio } from '../utils';
import { DomUtils } from 'htmlparser2';
import { removeElement } from 'domutils';
import type { Cheerio } from '../cheerio';
import type { BasicAcceptedElems, AcceptedElems } from '../types';

Expand Down Expand Up @@ -279,7 +278,7 @@ function _wrap(

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

if (!wrapperDom || !DomUtils.hasChildren(wrapperDom)) continue;
if (!wrapperDom || !hasChildren(wrapperDom)) continue;

let elInsertLocation = wrapperDom;

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

return domEach(this, (el, i) => {
const { parent } = el;
if (!DomUtils.hasChildren(el) || !parent) {
if (!hasChildren(el) || !parent) {
return;
}

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

return domEach(this, (el, i) => {
const { parent } = el;
if (!DomUtils.hasChildren(el) || !parent) {
if (!hasChildren(el) || !parent) {
return;
}

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

domEach(elems, (el) => {
DomUtils.removeElement(el);
removeElement(el);
el.prev = el.next = el.parent = null;
});

Expand Down Expand Up @@ -884,7 +883,7 @@ export function replaceWith<T extends Node>(
*/
export function empty<T extends Node>(this: Cheerio<T>): Cheerio<T> {
return domEach(this, (el) => {
if (!DomUtils.hasChildren(el)) return;
if (!hasChildren(el)) return;
el.children.forEach((child) => {
child.next = child.prev = child.parent = null;
});
Expand Down Expand Up @@ -923,15 +922,15 @@ export function html<T extends Node>(
): Cheerio<T> | string | null {
if (str === undefined) {
const el = this[0];
if (!el || !DomUtils.hasChildren(el)) return null;
if (!el || !hasChildren(el)) return null;
return staticHtml(el.children, this.options);
}

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

return domEach(this, (el) => {
if (!DomUtils.hasChildren(el)) return;
if (!hasChildren(el)) return;
el.children.forEach((child) => {
child.next = child.prev = child.parent = null;
});
Expand Down Expand Up @@ -1000,7 +999,7 @@ export function text<T extends Node>(

// Append text node to each selected elements
return domEach(this, (el) => {
if (!DomUtils.hasChildren(el)) return;
if (!hasChildren(el)) return;
el.children.forEach((child) => {
child.next = child.prev = child.parent = null;
});
Expand Down
23 changes: 13 additions & 10 deletions src/api/traversing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import type { Cheerio } from '../cheerio';
import * as select from 'cheerio-select';
import { domEach, isTag, isCheerio } from '../utils';
import { contains } from '../static';
import { DomUtils } from 'htmlparser2';
import {
getChildren,
getSiblings,
nextElementSibling,
prevElementSibling,
uniqueSort,
} from 'domutils';
import type { FilterFunction, AcceptedFilters } from '../types';
const { uniqueSort } = DomUtils;
const reSiblingSelector = /^\s*[~+]/;

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

/**
* Gets all the following siblings of the first selected element, optionally
Expand Down Expand Up @@ -378,7 +383,7 @@ export const nextAll = _matcher((elem) => {
* @see {@link https://api.jquery.com/nextUntil/}
*/
export const nextUntil = _matchUntil(
(el) => DomUtils.nextElementSibling(el),
(el) => nextElementSibling(el),
_removeDuplicates
);

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

/**
* Gets all the preceding siblings of the first selected element, optionally
Expand Down Expand Up @@ -446,7 +451,7 @@ export const prevAll = _matcher((elem) => {
* @see {@link https://api.jquery.com/prevUntil/}
*/
export const prevUntil = _matchUntil(
(el) => DomUtils.prevElementSibling(el),
(el) => prevElementSibling(el),
_removeDuplicates
);

Expand All @@ -471,9 +476,7 @@ export const prevUntil = _matchUntil(
*/
export const siblings = _matcher(
(elem) =>
DomUtils.getSiblings(elem).filter(
(el): el is Element => isTag(el) && el !== elem
),
getSiblings(elem).filter((el): el is Element => isTag(el) && el !== elem),
uniqueSort
);

Expand All @@ -496,7 +499,7 @@ export const siblings = _matcher(
* @see {@link https://api.jquery.com/children/}
*/
export const children = _matcher(
(elem) => DomUtils.getChildren(elem).filter(isTag),
(elem) => getChildren(elem).filter(isTag),
_removeDuplicates
);

Expand Down
4 changes: 2 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DomUtils } from 'htmlparser2';
import { removeElement } from 'domutils';
import { parse as parseWithHtmlparser2 } from './parsers/htmlparser2-adapter';
import { parse as parseWithParse5 } from './parsers/parse5-adapter';
import {
Expand Down Expand Up @@ -70,7 +70,7 @@ export function update(

// Cleanly remove existing nodes from their previous structures.
if (node.parent && node.parent.children !== arr) {
DomUtils.removeElement(node);
removeElement(node);
}

if (parent) {
Expand Down
8 changes: 4 additions & 4 deletions src/static.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { CheerioAPI, Cheerio } from '.';
import { Node, Document } from 'domhandler';
import { Node, Document, isText, hasChildren } from 'domhandler';
import {
InternalOptions,
CheerioOptions,
default as defaultOptions,
flatten as flattenOptions,
} from './options';
import { select } from 'cheerio-select';
import { ElementType, DomUtils } from 'htmlparser2';
import { ElementType } from 'htmlparser2';
import { render as renderWithParse5 } from './parsers/parse5-adapter';
import { render as renderWithHtmlparser2 } from './parsers/htmlparser2-adapter';

Expand Down Expand Up @@ -137,9 +137,9 @@ export function text(

for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
if (DomUtils.isText(elem)) ret += elem.data;
if (isText(elem)) ret += elem.data;
else if (
DomUtils.hasChildren(elem) &&
hasChildren(elem) &&
elem.type !== ElementType.Comment &&
elem.type !== ElementType.Script &&
elem.type !== ElementType.Style
Expand Down
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DomUtils } from 'htmlparser2';
import { Node, cloneNode, Document } from 'domhandler';
import type { Cheerio } from './cheerio';

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

/**
* Checks if an object is a Cheerio instance.
Expand Down