Skip to content

Commit 7b396bb

Browse files
Merge pull request #1386 from remarkablemark/refactor/client
refactor(client): remove exports `formatAttributes` and `CARRIAGE_RETURN`
2 parents 141a31e + b1179a8 commit 7b396bb

5 files changed

Lines changed: 32 additions & 31 deletions

File tree

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ assignees: remarkablemark
2323
Creating a bug demo will help speed up the process of resolving the issue:
2424
2525
* JSFiddle: https://jsfiddle.net/remarkablemark/ff9yg1yz/
26-
* Replit: https://replit.com/@remarkablemark/html-dom-parser
2726
-->
2827

2928
## Environment

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ If you're parsing SVG, you can set `lowerCaseTags` to `true` without having to e
236236
237237
## Migration
238238

239+
### v6
240+
241+
Upgraded [domhandler](https://github.com/fb55/domhandler/releases/tag/v6.0.0) and [htmlparser2](https://github.com/fb55/htmlparser2/releases/tag/v11.0.0). Removed client exports `formatAttributes` and `CARRIAGE_RETURN` constants.
242+
239243
### v5
240244

241245
Migrated to TypeScript. CommonJS imports require the `.default` key:
@@ -246,19 +250,19 @@ const parse = require('html-dom-parser').default;
246250

247251
### v4
248252

249-
Upgraded [htmlparser2](https://github.com/fb55/htmlparser2) to v9.
253+
Upgraded [htmlparser2](https://github.com/fb55/htmlparser2/releases/tag/v9.0.0).
250254

251255
### v3
252256

253-
Upgraded [domhandler](https://github.com/fb55/domhandler) to v5. [Parser options](https://github.com/fb55/htmlparser2/wiki/Parser-options) like `normalizeWhitespace` have been removed.
257+
Upgraded [domhandler](https://github.com/fb55/domhandler/releases/tag/v5.0.0). [Parser options](https://github.com/fb55/htmlparser2/wiki/Parser-options) like `normalizeWhitespace` have been removed.
254258

255259
### v2
256260

257261
Removed Internet Explorer (IE11) support.
258262

259263
### v1
260264

261-
Upgraded `domhandler` to v4 and `htmlparser2` to v6.
265+
Upgraded [domhandler](https://github.com/fb55/domhandler/releases/tag/v4.0.0) and [htmlparser2](https://github.com/fb55/htmlparser2/releases/tag/v6.0.0).
262266

263267
## Release
264268

__tests__/server/client.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// @vitest-environment jsdom
2-
import { CARRIAGE_RETURN_PLACEHOLDER } from '../../src/client/constants';
32
import { formatDOM } from '../../src/client/utilities';
43
import { revertEscapedCharacters } from '../../src/client/utilities';
54
import { escapeSpecialCharacters } from '../../src/client/utilities';
@@ -15,8 +14,8 @@ describe('client utilities', () => {
1514

1615
describe('escapeSpecialCharacters', () => {
1716
it('escapes carriage return characters', () => {
18-
expect(escapeSpecialCharacters('Hello\rWorld')).to.equal(
19-
`Hello${CARRIAGE_RETURN_PLACEHOLDER}World`,
17+
expect(escapeSpecialCharacters('Hello\rWorld')).to.match(
18+
/^Hello__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_\d+__World$/,
2019
);
2120
});
2221

@@ -31,23 +30,30 @@ describe('client utilities', () => {
3130
});
3231

3332
it('handles multiple carriage returns', () => {
34-
expect(escapeSpecialCharacters('Hello\rDear\rWorld')).to.equal(
35-
`Hello${CARRIAGE_RETURN_PLACEHOLDER}Dear${CARRIAGE_RETURN_PLACEHOLDER}World`,
33+
const escaped = escapeSpecialCharacters('Hello\rDear\rWorld');
34+
const placeholders = escaped.match(
35+
/__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_\d+__/g,
3636
);
37+
38+
expect(escaped).to.match(
39+
/^Hello__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_\d+__Dear__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_\d+__World$/,
40+
);
41+
expect(placeholders).to.have.length(2);
42+
expect(placeholders?.[0]).to.equal(placeholders?.[1]);
3743
});
3844

3945
it('only escapes carriage returns', () => {
4046
// `\n` and `\right` should not be affected
41-
expect(escapeSpecialCharacters('Hello\rWorld\n\right')).to.equal(
42-
`Hello${CARRIAGE_RETURN_PLACEHOLDER}World\n${CARRIAGE_RETURN_PLACEHOLDER}ight`,
47+
expect(escapeSpecialCharacters('Hello\rWorld\n\right')).to.match(
48+
/^Hello__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_\d+__World\n__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_\d+__ight$/,
4349
);
4450
});
4551
});
4652

4753
describe('revertEscapedCharacters', () => {
4854
it('reverts escaped carriage return characters', () => {
4955
expect(
50-
revertEscapedCharacters(`Hello${CARRIAGE_RETURN_PLACEHOLDER}World`),
56+
revertEscapedCharacters(escapeSpecialCharacters('Hello\rWorld')),
5157
).to.equal('Hello\rWorld');
5258
});
5359

@@ -63,17 +69,15 @@ describe('client utilities', () => {
6369

6470
it('handles multiple escaped carriage returns', () => {
6571
expect(
66-
revertEscapedCharacters(
67-
`Hello${CARRIAGE_RETURN_PLACEHOLDER}Dear${CARRIAGE_RETURN_PLACEHOLDER}World`,
68-
),
72+
revertEscapedCharacters(escapeSpecialCharacters('Hello\rDear\rWorld')),
6973
).to.equal('Hello\rDear\rWorld');
7074
});
7175

7276
it('only reverts escaped carriage returns', () => {
7377
// `\n` and `\right` should not be affected
7478
expect(
7579
revertEscapedCharacters(
76-
`Hello${CARRIAGE_RETURN_PLACEHOLDER}World\\n\\right`,
80+
escapeSpecialCharacters('Hello\rWorld\\n\\right'),
7781
),
7882
).to.equal('Hello\rWorld\\n\\right');
7983
});

src/client/constants.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,3 @@ export const CASE_SENSITIVE_TAG_NAMES_MAP = CASE_SENSITIVE_TAG_NAMES.reduce<
4343
accumulator[tagName.toLowerCase()] = tagName;
4444
return accumulator;
4545
}, {});
46-
47-
export const CARRIAGE_RETURN = '\r';
48-
export const CARRIAGE_RETURN_REGEX = new RegExp(CARRIAGE_RETURN, 'g');
49-
export const CARRIAGE_RETURN_PLACEHOLDER = `__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_${Date.now().toString()}__`;
50-
export const CARRIAGE_RETURN_PLACEHOLDER_REGEX = new RegExp(
51-
CARRIAGE_RETURN_PLACEHOLDER,
52-
'g',
53-
);

src/client/utilities.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Comment, Element, ProcessingInstruction, Text } from 'domhandler';
22

33
import type { DOMNode } from '../types';
4-
import {
5-
CARRIAGE_RETURN,
4+
import { CASE_SENSITIVE_TAG_NAMES_MAP } from './constants';
5+
6+
const CARRIAGE_RETURN = '\r';
7+
const CARRIAGE_RETURN_REGEX = new RegExp(CARRIAGE_RETURN, 'g');
8+
const CARRIAGE_RETURN_PLACEHOLDER = `__HTML_DOM_PARSER_CARRIAGE_RETURN_PLACEHOLDER_${Date.now().toString()}__`;
9+
const CARRIAGE_RETURN_PLACEHOLDER_REGEX = new RegExp(
610
CARRIAGE_RETURN_PLACEHOLDER,
7-
CARRIAGE_RETURN_PLACEHOLDER_REGEX,
8-
CARRIAGE_RETURN_REGEX,
9-
CASE_SENSITIVE_TAG_NAMES_MAP,
10-
} from './constants';
11+
'g',
12+
);
1113

1214
/**
1315
* Gets case-sensitive tag name.
@@ -25,7 +27,7 @@ function getCaseSensitiveTagName(tagName: string): string | undefined {
2527
* @param attributes - List of attributes.
2628
* @returns - Map of attribute name to value.
2729
*/
28-
export function formatAttributes(attributes: NamedNodeMap) {
30+
function formatAttributes(attributes: NamedNodeMap) {
2931
const map: Record<string, string> = {};
3032
let index = 0;
3133
const attributesLength = attributes.length;

0 commit comments

Comments
 (0)