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

Ensure ESM support. Fixes #149 #150

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@
"name": "node-html-parser",
"version": "4.1.4",
"description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.",
"main": "dist/index.js",
"type": "module",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./*": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js"
}
},
"types": "dist/types/index.d.ts",
"scripts": {
"test": "mocha",
"lint": "eslint ./src/*.ts ./src/**/*.ts",
"clean": "del-cli ./dist/",
"ts:cjs": "tsc -m commonjs",
"ts:cjs": "tsc -m commonjs --outDir ./dist/cjs/ --declaration true --declarationDir ./dist/types/ && cpy package.cjs.json ./dist/cjs/ --rename=package.json",
"ts:amd": "tsc -t es5 -m amd -d false --outFile ./dist/main.js",
"ts:esm": "tsc -t es2019 -m esnext -d false --outDir ./dist/esm/",
"build": "npm run lint && npm run clean && npm run ts:cjs && npm run ts:amd && npm run ts:esm",
"dev": "tsc -w & mocha -w ./test/*.js",
"pretest": "tsc -m commonjs",
"pretest": "npm run ts:cjs",
"release": "yarn build && np",
"prepare": "npm run build"
},
Expand Down Expand Up @@ -45,6 +56,7 @@
"@typescript-eslint/parser": "latest",
"blanket": "latest",
"cheerio": "^1.0.0-rc.5",
"cpy-cli": "^3.1.1",
"del-cli": "latest",
"eslint": "latest",
"eslint-config-prettier": "latest",
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { default as CommentNode } from './nodes/comment';
export { default as HTMLElement, Options } from './nodes/html';
export { default as parse, default } from './parse';
export { default as valid } from './valid';
export { default as Node } from './nodes/node';
export { default as TextNode } from './nodes/text';
export { default as NodeType } from './nodes/type';
export { default as CommentNode } from './nodes/comment.js';
export { default as HTMLElement, Options } from './nodes/html.js';
export { default as parse, default } from './parse.js';
export { default as valid } from './valid.js';
export { default as Node } from './nodes/node.js';
export { default as TextNode } from './nodes/text.js';
export { default as NodeType } from './nodes/type.js';
6 changes: 3 additions & 3 deletions src/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Adapter/*, Predicate*/ } from 'css-select/lib/types';
import HTMLElement from './nodes/html';
import Node from './nodes/node';
import NodeType from './nodes/type';
import HTMLElement from './nodes/html.js';
import Node from './nodes/node.js';
import NodeType from './nodes/type.js';

export declare type Predicate = (node: Node) => node is HTMLElement;

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Node from './node';
import NodeType from './type';
import HTMLElement from './html';
import Node from './node.js';
import NodeType from './type.js';
import HTMLElement from './html.js';

export default class CommentNode extends Node {
public constructor(public rawText: string, parentNode: HTMLElement, range?: [ number, number ]) {
Expand Down
12 changes: 6 additions & 6 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import he from 'he';
import { selectAll, selectOne } from 'css-select';
import Node from './node';
import NodeType from './type';
import TextNode from './text';
import Matcher from '../matcher';
import arr_back from '../back';
import CommentNode from './comment';
import Node from './node.js';
import NodeType from './type.js';
import TextNode from './text.js';
import Matcher from '../matcher.js';
import arr_back from '../back.js';
import CommentNode from './comment.js';

// const { decode } = he;

Expand Down
10 changes: 5 additions & 5 deletions src/nodes/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decode, encode } from 'he';
import NodeType from './type';
import HTMLElement from './html';
import he from 'he';
import NodeType from './type.js';
import HTMLElement from './html.js';

/**
* Node Class as base class for TextNode and HTMLElement.
Expand Down Expand Up @@ -28,9 +28,9 @@ export default abstract class Node {
return this.rawText;
}
public get textContent() {
return decode(this.rawText);
return he.decode(this.rawText);
}
public set textContent(val: string) {
this.rawText = encode(val);
this.rawText = he.encode(val);
}
}
10 changes: 5 additions & 5 deletions src/nodes/text.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { decode } from 'he';
import HTMLElement from './html';
import Node from './node';
import NodeType from './type';
import he from 'he';
import HTMLElement from './html.js';
import Node from './node.js';
import NodeType from './type.js';

/**
* TextNode to contain a text element in DOM tree.
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class TextNode extends Node {
* @return {string} text content
*/
public get text() {
return decode(this.rawText);
return he.decode(this.rawText);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { parse as default } from './nodes/html';
export { parse as default } from './nodes/html.js';
2 changes: 1 addition & 1 deletion src/valid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base_parse, Options } from './nodes/html';
import { base_parse, Options } from './nodes/html.js';

/**
* Parses HTML and returns a root element
Expand Down
2 changes: 1 addition & 1 deletion test/100.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('#querySelectorAll', function () {
it('nothing mached', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/106.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');
const fs = require('fs');

describe.skip('Memory leak', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/109.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('self-close tag', function () {
it('should not teat textarea as self-colse tag', async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/112.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse, HTMLElement } = require('../dist');
const { parse, HTMLElement } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/pull/112
describe('pull/112', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/115.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('issue 115', function () {
it('parse html', async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/119.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/pull/112
describe('issue 119 closest', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/135.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('pr 135', function () {
it('shoud not decode text', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/144.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('issue 144', function () {
it('Nested A tags parsed improperly', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/145.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('issue 145', function () {
it('shoud parse attributes right', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/28,59,74.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('issues/28', function () {
it('query with dl > dt', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/41.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/41
describe('#exchangeChild()', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/42.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('issue 42', function () {
it('svg attribute', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/48.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('issue 48', function () {
it('get decoded text', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/51.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe.skip('issue 51', function () {
it('vue: > in attibute value', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/69.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/69
describe('issues/69', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/70.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/70
describe('issues/70', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/84.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/84
describe('#querySelector()', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/85.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/85
describe('#remove()', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/95.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/95
describe('#textContent', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/98.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/98
describe('getAttribute should be case insensitive', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/caseinsensitive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/75
describe('query should be case insensitive', function () {
Expand Down
5 changes: 2 additions & 3 deletions test/compare-node-html-parser.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import benchmark from 'htmlparser-benchmark';
import node_html_parser from '../dist/index.js';
import { parse } from '../dist/esm/index.js';

// const { parse } = node_html_parser;

var bench = benchmark(function (html, callback) {
// parse(html);
node_html_parser.parse(html);
parse(html);
callback();
});

Expand Down
2 changes: 1 addition & 1 deletion test/emptyattribute.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/95
describe('empty attribute', function () {
Expand Down
6 changes: 2 additions & 4 deletions test/html.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const should = require('should');
const fs = require('fs');

const HTMLParser = require('../dist');
const HTMLElement = require('../dist/nodes/html').default;
const TextNode = require('../dist/nodes/text').default;
const CommentNode = require('../dist/nodes/comment').default;
const HTMLParser = require('../dist/cjs/index.js');
const { HTMLElement, TextNode, CommentNode } = HTMLParser;

describe('HTML Parser', function () {
const parseHTML = HTMLParser.parse;
Expand Down
2 changes: 1 addition & 1 deletion test/node-ranges.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse, HTMLElement, TextNode, CommentNode } = require('../dist');
const { parse, HTMLElement, TextNode, CommentNode } = require('../dist/cjs/index.js');
const hp2 = require('htmlparser2')
const mochaEach = require('mocha-each');

Expand Down
3 changes: 3 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
2 changes: 1 addition & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/38
describe('HTML Parser', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/pre.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/77
describe('pre tag', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/quoteattributes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

// https://github.com/taoqf/node-html-parser/issues/62
describe('quote attributes', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/replacewith.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse } = require('../dist');
const { parse } = require('../dist/cjs/index.js');

describe('should parse tag correct', function () {
it('should get attribute with :', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/valid.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');

const { valid, parse } = require('../dist');
const { valid, parse } = require('../dist/cjs/index.js');

describe('parseWithValidation', function () {
// parse with validation tests
Expand Down