Skip to content

refactor: migrate to TypeScript #527

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

Merged
merged 1 commit into from
Oct 16, 2023
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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ jobs:
- name: Lint JavaScript
run: npm run lint

- name: Test TypeScript declaration files
run: npm run lint:dts
- name: Type check
run: npm run lint:tsc

- name: Run server test
run: npm run test:server

- name: Run module tests
run: npm run test:module
run: npm run test:esm

- name: Run server test
run: npm run test:server
Expand Down
17 changes: 9 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build directory
dist
dist/
lib/

# Logs
logs
Expand All @@ -13,16 +14,16 @@ pids
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
lib-cov/

# Coverage directory used by tools like istanbul
coverage
coverage/

# nyc test coverage
.nyc_output
.nyc_output/

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
.grunt/

# node-waf configuration
.lock-wscript
Expand All @@ -31,11 +32,11 @@ coverage
build/Release

# Dependency directories
node_modules
jspm_packages
node_modules/
jspm_packages/

# Optional npm cache directory
.npm
.npm/

# Optional eslint cache
.eslintcache
Expand Down
2 changes: 0 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"arrowParens": "avoid",
"trailingComma": "none",
"singleQuote": true
}
74 changes: 28 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ The parser converts an HTML string to a JavaScript object that describes the DOM
#### Example

```js
const parse = require('html-dom-parser');
import parse from 'html-dom-parser';

parse('<p>Hello, World!</p>');
```

Expand Down Expand Up @@ -83,16 +84,16 @@ yarn add html-dom-parser

## Usage

Import the module with ES Modules:
Import with ES Modules:

```js
import parse from 'html-dom-parser';
```

Or require the module with CommonJS:
Require with CommonJS:

```js
const parse = require('html-dom-parser');
const parse = require('html-dom-parser').default;
```

Parse empty string:
Expand Down Expand Up @@ -179,60 +180,41 @@ Because the server parser is a wrapper of [htmlparser2](https://github.com/fb55/
* should be combined into a single object like so:
*/
const options = {
/**
* Options for the domhandler class.
* https://github.com/fb55/domhandler/blob/master/src/index.ts#L16
*/
withStartIndices: false,
withEndIndices: false,
xmlMode: false,
/**
* Options for the htmlparser2 class.
* https://github.com/fb55/htmlparser2/blob/master/src/Parser.ts#L104
*/
xmlMode: false, // Will overwrite what is used for the domhandler, otherwise inherited.
decodeEntities: true,
lowerCaseTags: true, // !xmlMode by default
lowerCaseAttributeNames: true, // !xmlMode by default
recognizeCDATA: false, // xmlMode by default
recognizeSelfClosing: false, // xmlMode by default
Tokenizer: Tokenizer
/**
* Options for the domhandler class.
* https://github.com/fb55/domhandler/blob/master/src/index.ts#L16
*/
withStartIndices: false,
withEndIndices: false,
xmlMode: false,
/**
* Options for the htmlparser2 class.
* https://github.com/fb55/htmlparser2/blob/master/src/Parser.ts#L104
*/
xmlMode: false, // Will overwrite what is used for the domhandler, otherwise inherited.
decodeEntities: true,
lowerCaseTags: true, // !xmlMode by default
lowerCaseAttributeNames: true, // !xmlMode by default
recognizeCDATA: false, // xmlMode by default
recognizeSelfClosing: false, // xmlMode by default
Tokenizer: Tokenizer,
};
```

If you are parsing HTML with SVG code you can set `lowerCaseTags` to `true` without having to enable `xmlMode`. Keep in mind this will return all tag names in camel-case and not the HTML standard of lowercase.

> **Note**: If you are parsing code client-side (in-browser), you can not control the parsing options. Client-side parsing automatically handles returning some HTML tags in camel-case, such as specific SVG elements, but returns all other tags lowercased according to the HTML standard.

## Testing

Run server and client tests:

```sh
npm test
```

Generate HTML coverage report for server tests:

```sh
npx nyc report --reporter=html
```

Lint files:
## Migration

```sh
npm run lint
npm run lint:fix
```
### v5

Test TypeScript declaration file for style and correctness:
Migrated to TypeScript. CommonJS imports require the `.default` key:

```sh
npm run lint:dts
```js
const parse = require('html-dom-parser').default;
```

## Migration

### v4

Upgraded [htmlparser2](https://github.com/fb55/htmlparser2) to v9.
Expand Down
3 changes: 3 additions & 0 deletions esm/client/html-to-dom.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ClientParser from '../../lib/client/html-to-dom.js';

export default ClientParser.default;
3 changes: 3 additions & 0 deletions esm/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import HTMLDOMParser from '../lib/index.js';

export default HTMLDOMParser.default;
3 changes: 3 additions & 0 deletions esm/server/html-to-dom.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ServerParser from '../../lib/server/html-to-dom.js';

export default ServerParser.default;
3 changes: 0 additions & 3 deletions index.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions index.mjs

This file was deleted.

19 changes: 10 additions & 9 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Karma configuration
// https://karma-runner.github.io/5.2/config/configuration-file.html
module.exports = config => {
/**
* @see https://karma-runner.github.io/6.4/config/configuration-file.html
*/
module.exports = (config) => {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
Expand All @@ -16,7 +17,7 @@ module.exports = config => {
'lib/server/utilities.js',
'test/cases/html.js',
'test/client/*.js',
'test/helpers/*.js'
'test/helpers/*.js',
],

// list of files / patterns to exclude
Expand All @@ -27,7 +28,7 @@ module.exports = config => {
preprocessors: {
'dist/*.js': ['commonjs'],
'lib/**/*.js': ['commonjs'],
'test/**/*.js': ['commonjs']
'test/**/*.js': ['commonjs'],
},

// test results reporter to use
Expand Down Expand Up @@ -74,14 +75,14 @@ module.exports = config => {
client: {
mocha: {
// change Karma's `debug.html` to the Mocha web reporter
reporter: 'html'
}
reporter: 'html',
},
},

// Mocha reporter options
// https://www.npmjs.com/package/karma-mocha-reporter
mochaReporter: {
showDiff: true
}
showDiff: true,
},
});
};
8 changes: 5 additions & 3 deletions lib/client/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* SVG elements, unlike HTML elements, are case-sensitive.
* SVG elements are case-sensitive.
*
* {@link https://developer.mozilla.org/docs/Web/SVG/Element#SVG_elements_A_to_Z}
* @see https://developer.mozilla.org/docs/Web/SVG/Element#svg_elements_a_to_z
*/
export const CASE_SENSITIVE_TAG_NAMES: string[];
export declare const CASE_SENSITIVE_TAG_NAMES: readonly ["animateMotion", "animateTransform", "clipPath", "feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence", "foreignObject", "linearGradient", "radialGradient", "textPath"];
export declare const CASE_SENSITIVE_TAG_NAMES_MAP: Record<string, string>;
//# sourceMappingURL=constants.d.ts.map
72 changes: 40 additions & 32 deletions lib/client/constants.js

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

3 changes: 2 additions & 1 deletion lib/client/domparser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Parses HTML string to DOM nodes.
*
* @param html - HTML markup.
* @returns - NodeList.
* @returns - DOM nodes.
*/
export default function domparser(html: string): NodeList;
//# sourceMappingURL=domparser.d.ts.map
Loading