Skip to content

Commit

Permalink
ref: stop using deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeNury committed Jan 25, 2022
1 parent 80660c2 commit e29fbf5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "12"
- "14"

install:
- npm install --global codecov
Expand Down
30 changes: 15 additions & 15 deletions src/extractors.spec.ts
@@ -1,10 +1,10 @@
import { FieldConfig } from './types';
import { extract } from './extractors';
import { parseDOM } from 'htmlparser2';
import { parseDocument } from 'htmlparser2';

describe('Extractors', () => {
it('should work when extract = text', () => {
const nodes = parseDOM(`<h1>Title</h1>`);
const nodes = parseDocument(`<h1>Title</h1>`).children;

const config = {
extractor: { name: 'text', args: [] },
Expand All @@ -16,7 +16,7 @@ describe('Extractors', () => {
expect(result).toEqual('Title');
});
it('should work when extract = text and when selector do not match anything', () => {
const nodes = parseDOM(`<h1>Title</h1>`);
const nodes = parseDocument(`<h1>Title</h1>`).children;

const config = {
extractor: { name: 'text', args: [] },
Expand All @@ -28,7 +28,7 @@ describe('Extractors', () => {
expect(result).toEqual('');
});
it('should work when extract = prop', () => {
const nodes = parseDOM(`<a href="a-super-link">Link</a>`);
const nodes = parseDocument(`<a href="a-super-link">Link</a>`).children;

const config = {
extractor: { name: 'prop', args: ['href'] },
Expand All @@ -40,9 +40,9 @@ describe('Extractors', () => {
expect(result).toEqual('a-super-link');
});
it('should work when extract = html', () => {
const nodes = parseDOM(
const nodes = parseDocument(
`<div><a href="a-super-link">Link</a></div>`,
);
).children;

const config = {
extractor: { name: 'html', args: [] },
Expand All @@ -54,9 +54,9 @@ describe('Extractors', () => {
expect(result).toEqual('<a href="a-super-link">Link</a>');
});
it('should work when extract = html and when selector do not match anything', () => {
const nodes = parseDOM(
const nodes = parseDocument(
`<div><a href="a-super-link">Link</a></div>`,
);
).children;

const config = {
extractor: { name: 'html', args: [] },
Expand All @@ -68,9 +68,9 @@ describe('Extractors', () => {
expect(result).toEqual('');
});
it('should work when extract = outerHtml', () => {
const nodes = parseDOM(
const nodes = parseDocument(
`<div><a href="a-super-link">Link</a></div>`,
);
).children;

const config = {
extractor: { name: 'outerHtml', args: [] },
Expand All @@ -82,9 +82,9 @@ describe('Extractors', () => {
expect(result).toEqual('<div><a href="a-super-link">Link</a></div>');
});
it('should work when extract = outerHtml and when selector do not match anything', () => {
const nodes = parseDOM(
const nodes = parseDocument(
`<div><a href="a-super-link">Link</a></div>`,
);
).children;

const config = {
extractor: { name: 'outerHtml', args: [] },
Expand All @@ -96,7 +96,7 @@ describe('Extractors', () => {
expect(result).toEqual('');
});
it('should work when extract = css', () => {
const nodes = parseDOM(`<div style="color: white"></div>`);
const nodes = parseDocument(`<div style="color: white"></div>`).children;

const config = {
extractor: { name: 'css', args: ['color'] },
Expand All @@ -108,7 +108,7 @@ describe('Extractors', () => {
expect(result).toEqual('white');
});
it('should work when extract = css and no style', () => {
const nodes = parseDOM(`<div></div>`);
const nodes = parseDocument(`<div></div>`).children;

const config = {
extractor: { name: 'css', args: ['color'] },
Expand All @@ -120,7 +120,7 @@ describe('Extractors', () => {
expect(result).toEqual('');
});
it('should throw when extract = not existing', () => {
const nodes = parseDOM(`<h1>Title</h1>`);
const nodes = parseDocument(`<h1>Title</h1>`).children;

const config = {
extractor: { name: 'not exising', args: ['color'] },
Expand Down
6 changes: 3 additions & 3 deletions src/formators.ts
@@ -1,7 +1,7 @@
import { FormatTypes, IPipe } from './types';
import { enumAsString, urlJoin } from './utils';
import { parseDOM } from 'htmlparser2';
import { getText } from 'domutils';
import { parseDocument } from 'htmlparser2';
import { textContent } from 'domutils';

const formattorsMap = {
[FormatTypes.STRING]: ignoreUndefined(formatString),
Expand Down Expand Up @@ -52,7 +52,7 @@ function formatHtmlToText(rawValue: string): string {
.replace(/<p>(.*?)<\/p>/g, (_, match) => `\n${match}\n`)
.replace(/<div>(.*?)<\/div>/g, (_, match) => `\n${match}\n`);

return getText(parseDOM(sanitizedHtml));
return textContent(parseDocument(sanitizedHtml));
}

function formatOneLineString(rawValue: string): string {
Expand Down
10 changes: 5 additions & 5 deletions src/parsers.ts
@@ -1,6 +1,6 @@
import { selectAll } from 'css-select';
import { Element, Node, NodeWithChildren } from "domhandler";
import { parseDOM, ElementType } from 'htmlparser2';
import { parseDocument, ElementType } from 'htmlparser2';

import { parseConfig } from './config-parsers';
import { extract } from './extractors';
Expand All @@ -21,15 +21,15 @@ import {

export function parse<T extends EbriScrapConfig>(html: string, config: T): EbriScrapData<T> {
const parsedConfig = parseConfig(config);
const nodes = parseDOM(html, { decodeEntities: true });
return genericParse(nodes, parsedConfig, null, '');
const doc = parseDocument(html, { decodeEntities: true });
return genericParse(doc.children, parsedConfig, null, '');
}

export function parseWithDebug<T extends EbriScrapConfig>(html: string, config: T): EbriscrapDebugResult<T> {
const parsedConfig = parseConfig(config);
const nodes = parseDOM(html, { decodeEntities: true });
const doc = parseDocument(html, { decodeEntities: true });
const debug: DebugStep[] = [];
const result = genericParse(nodes, parsedConfig, debug, '');
const result = genericParse(doc.children, parsedConfig, debug, '');
return { result, debug: parseDebug(debug) };
}

Expand Down

0 comments on commit e29fbf5

Please sign in to comment.