Skip to content

Commit 9ddb1b4

Browse files
Merge pull request #527 from remarkablemark/refactor/typescript
refactor: migrate to TypeScript
2 parents e40ccd8 + 5915b08 commit 9ddb1b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1183
-3120
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ jobs:
2121
- name: Lint JavaScript
2222
run: npm run lint
2323

24-
- name: Test TypeScript declaration files
25-
run: npm run lint:dts
24+
- name: Type check
25+
run: npm run lint:tsc
2626

2727
- name: Run server test
2828
run: npm run test:server
2929

3030
- name: Run module tests
31-
run: npm run test:module
31+
run: npm run test:esm
3232

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

.gitignore

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Build directory
2-
dist
2+
dist/
3+
lib/
34

45
# Logs
56
logs
@@ -13,16 +14,16 @@ pids
1314
*.pid.lock
1415

1516
# Directory for instrumented libs generated by jscoverage/JSCover
16-
lib-cov
17+
lib-cov/
1718

1819
# Coverage directory used by tools like istanbul
19-
coverage
20+
coverage/
2021

2122
# nyc test coverage
22-
.nyc_output
23+
.nyc_output/
2324

2425
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
25-
.grunt
26+
.grunt/
2627

2728
# node-waf configuration
2829
.lock-wscript
@@ -31,11 +32,11 @@ coverage
3132
build/Release
3233

3334
# Dependency directories
34-
node_modules
35-
jspm_packages
35+
node_modules/
36+
jspm_packages/
3637

3738
# Optional npm cache directory
38-
.npm
39+
.npm/
3940

4041
# Optional eslint cache
4142
.eslintcache

.prettierrc.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"arrowParens": "avoid",
3-
"trailingComma": "none",
42
"singleQuote": true
53
}

README.md

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ The parser converts an HTML string to a JavaScript object that describes the DOM
1919
#### Example
2020

2121
```js
22-
const parse = require('html-dom-parser');
22+
import parse from 'html-dom-parser';
23+
2324
parse('<p>Hello, World!</p>');
2425
```
2526

@@ -83,16 +84,16 @@ yarn add html-dom-parser
8384

8485
## Usage
8586

86-
Import the module with ES Modules:
87+
Import with ES Modules:
8788

8889
```js
8990
import parse from 'html-dom-parser';
9091
```
9192

92-
Or require the module with CommonJS:
93+
Require with CommonJS:
9394

9495
```js
95-
const parse = require('html-dom-parser');
96+
const parse = require('html-dom-parser').default;
9697
```
9798

9899
Parse empty string:
@@ -179,60 +180,41 @@ Because the server parser is a wrapper of [htmlparser2](https://github.com/fb55/
179180
* should be combined into a single object like so:
180181
*/
181182
const options = {
182-
/**
183-
* Options for the domhandler class.
184-
* https://github.com/fb55/domhandler/blob/master/src/index.ts#L16
185-
*/
186-
withStartIndices: false,
187-
withEndIndices: false,
188-
xmlMode: false,
189-
/**
190-
* Options for the htmlparser2 class.
191-
* https://github.com/fb55/htmlparser2/blob/master/src/Parser.ts#L104
192-
*/
193-
xmlMode: false, // Will overwrite what is used for the domhandler, otherwise inherited.
194-
decodeEntities: true,
195-
lowerCaseTags: true, // !xmlMode by default
196-
lowerCaseAttributeNames: true, // !xmlMode by default
197-
recognizeCDATA: false, // xmlMode by default
198-
recognizeSelfClosing: false, // xmlMode by default
199-
Tokenizer: Tokenizer
183+
/**
184+
* Options for the domhandler class.
185+
* https://github.com/fb55/domhandler/blob/master/src/index.ts#L16
186+
*/
187+
withStartIndices: false,
188+
withEndIndices: false,
189+
xmlMode: false,
190+
/**
191+
* Options for the htmlparser2 class.
192+
* https://github.com/fb55/htmlparser2/blob/master/src/Parser.ts#L104
193+
*/
194+
xmlMode: false, // Will overwrite what is used for the domhandler, otherwise inherited.
195+
decodeEntities: true,
196+
lowerCaseTags: true, // !xmlMode by default
197+
lowerCaseAttributeNames: true, // !xmlMode by default
198+
recognizeCDATA: false, // xmlMode by default
199+
recognizeSelfClosing: false, // xmlMode by default
200+
Tokenizer: Tokenizer,
200201
};
201202
```
202203

203204
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.
204205

205206
> **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.
206207
207-
## Testing
208-
209-
Run server and client tests:
210-
211-
```sh
212-
npm test
213-
```
214-
215-
Generate HTML coverage report for server tests:
216-
217-
```sh
218-
npx nyc report --reporter=html
219-
```
220-
221-
Lint files:
208+
## Migration
222209

223-
```sh
224-
npm run lint
225-
npm run lint:fix
226-
```
210+
### v5
227211

228-
Test TypeScript declaration file for style and correctness:
212+
Migrated to TypeScript. CommonJS imports require the `.default` key:
229213

230-
```sh
231-
npm run lint:dts
214+
```js
215+
const parse = require('html-dom-parser').default;
232216
```
233217

234-
## Migration
235-
236218
### v4
237219

238220
Upgraded [htmlparser2](https://github.com/fb55/htmlparser2) to v9.

esm/client/html-to-dom.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import ClientParser from '../../lib/client/html-to-dom.js';
2+
3+
export default ClientParser.default;

esm/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import HTMLDOMParser from '../lib/index.js';
2+
3+
export default HTMLDOMParser.default;

esm/server/html-to-dom.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import ServerParser from '../../lib/server/html-to-dom.js';
2+
3+
export default ServerParser.default;

index.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.mjs

Lines changed: 0 additions & 3 deletions
This file was deleted.

karma.conf.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Karma configuration
2-
// https://karma-runner.github.io/5.2/config/configuration-file.html
3-
module.exports = config => {
1+
/**
2+
* @see https://karma-runner.github.io/6.4/config/configuration-file.html
3+
*/
4+
module.exports = (config) => {
45
config.set({
56
// base path that will be used to resolve all patterns (eg. files, exclude)
67
basePath: '',
@@ -16,7 +17,7 @@ module.exports = config => {
1617
'lib/server/utilities.js',
1718
'test/cases/html.js',
1819
'test/client/*.js',
19-
'test/helpers/*.js'
20+
'test/helpers/*.js',
2021
],
2122

2223
// list of files / patterns to exclude
@@ -27,7 +28,7 @@ module.exports = config => {
2728
preprocessors: {
2829
'dist/*.js': ['commonjs'],
2930
'lib/**/*.js': ['commonjs'],
30-
'test/**/*.js': ['commonjs']
31+
'test/**/*.js': ['commonjs'],
3132
},
3233

3334
// test results reporter to use
@@ -74,14 +75,14 @@ module.exports = config => {
7475
client: {
7576
mocha: {
7677
// change Karma's `debug.html` to the Mocha web reporter
77-
reporter: 'html'
78-
}
78+
reporter: 'html',
79+
},
7980
},
8081

8182
// Mocha reporter options
8283
// https://www.npmjs.com/package/karma-mocha-reporter
8384
mochaReporter: {
84-
showDiff: true
85-
}
85+
showDiff: true,
86+
},
8687
});
8788
};

lib/client/constants.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
2-
* SVG elements, unlike HTML elements, are case-sensitive.
2+
* SVG elements are case-sensitive.
33
*
4-
* {@link https://developer.mozilla.org/docs/Web/SVG/Element#SVG_elements_A_to_Z}
4+
* @see https://developer.mozilla.org/docs/Web/SVG/Element#svg_elements_a_to_z
55
*/
6-
export const CASE_SENSITIVE_TAG_NAMES: string[];
6+
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"];
7+
export declare const CASE_SENSITIVE_TAG_NAMES_MAP: Record<string, string>;
8+
//# sourceMappingURL=constants.d.ts.map

lib/client/constants.js

Lines changed: 40 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/client/domparser.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Parses HTML string to DOM nodes.
33
*
44
* @param html - HTML markup.
5-
* @returns - NodeList.
5+
* @returns - DOM nodes.
66
*/
77
export default function domparser(html: string): NodeList;
8+
//# sourceMappingURL=domparser.d.ts.map

0 commit comments

Comments
 (0)