Skip to content

Commit 61be6d5

Browse files
authored
Merge pull request #77 from posthtml/milestone-0.9.1
Milestone 0.9.1
2 parents 6090060 + a03017e commit 61be6d5

File tree

8 files changed

+74
-33
lines changed

8 files changed

+74
-33
lines changed

changelog.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
## <small>0.9.1 (2021-07-26)</small>
2+
3+
* test: dom nest parsing issues #76 ([fdfe82e](https://github.com/posthtml/posthtml-parser/commit/fdfe82e)), closes [#76](https://github.com/posthtml/posthtml-parser/issues/76)
4+
* test: remove only ([78b7fc7](https://github.com/posthtml/posthtml-parser/commit/78b7fc7))
5+
* style: after prettier ([ccbfd69](https://github.com/posthtml/posthtml-parser/commit/ccbfd69))
6+
* style: after prettier ([72f2092](https://github.com/posthtml/posthtml-parser/commit/72f2092))
7+
* build: set object-curly-spacing always ([47dceb6](https://github.com/posthtml/posthtml-parser/commit/47dceb6))
8+
* build(deps): bump handlebars from 4.7.6 to 4.7.7 ([e6bc6cd](https://github.com/posthtml/posthtml-parser/commit/e6bc6cd))
9+
* build(deps): bump hosted-git-info from 2.8.8 to 2.8.9 ([2137591](https://github.com/posthtml/posthtml-parser/commit/2137591))
10+
* build(deps): bump lodash from 4.17.20 to 4.17.21 ([e9b8d04](https://github.com/posthtml/posthtml-parser/commit/e9b8d04))
11+
* build(deps): bump normalize-url from 4.5.0 to 4.5.1 ([9e0d1f9](https://github.com/posthtml/posthtml-parser/commit/9e0d1f9))
12+
* perf: type for replace ([94ebb8d](https://github.com/posthtml/posthtml-parser/commit/94ebb8d))
13+
* Update readme.md ([7081062](https://github.com/posthtml/posthtml-parser/commit/7081062))
14+
15+
16+
117
## 0.9.0 (2021-05-11)
218

19+
* 0.9.0 ([d725e83](https://github.com/posthtml/posthtml-parser/commit/d725e83))
320
* Add description to docs ([d61007b](https://github.com/posthtml/posthtml-parser/commit/d61007b))
421
* Move location tracker to a separate file and refactor into class ([7d6f76b](https://github.com/posthtml/posthtml-parser/commit/7d6f76b))
522
* feat: add optional support for source locations ([6faf50d](https://github.com/posthtml/posthtml-parser/commit/6faf50d))

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-parser",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "Parse HTML/XML to PostHTMLTree",
55
"license": "MIT",
66
"repository": "posthtml/posthtml-parser",

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {Parser, ParserOptions} from 'htmlparser2';
2-
import {Directive, Node, NodeTag, Options, Attributes} from '../types/index.d';
3-
import {LocationTracker} from './location-tracker';
1+
import { Parser, ParserOptions } from 'htmlparser2';
2+
import { Directive, Node, NodeTag, Options, Attributes } from '../types/index.d';
3+
import { LocationTracker } from './location-tracker';
44

55
const defaultOptions: ParserOptions = {
66
lowerCaseTags: false,
@@ -44,7 +44,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
4444
Object.keys(attrs).forEach((key: string) => {
4545
const object: Attributes = {};
4646

47-
object[key] = attrs[key].replace(/&quot;/g, '"');
47+
object[key] = String(attrs[key]).replace(/&quot;/g, '"');
4848
Object.assign(result, object);
4949
});
5050

@@ -95,7 +95,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
9595

9696
function onopentag(tag: string, attrs: Attributes) {
9797
const start = locationTracker.getPosition(parser.startIndex);
98-
const buf: NodeTag = {tag};
98+
const buf: NodeTag = { tag };
9999

100100
if (options.sourceLocations) {
101101
buf.location = {
@@ -167,7 +167,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
167167
onopentag,
168168
onclosetag,
169169
ontext
170-
}, {...defaultOptions, ...options});
170+
}, { ...defaultOptions, ...options });
171171

172172
parser.write(html);
173173
parser.end();

src/location-tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Position} from '../types/index.d';
1+
import { Position } from '../types/index.d';
22

33
export class LocationTracker {
44
private readonly source: string;

test/test-core.spec.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ test('should be parse comment', t => {
1414
});
1515

1616
test('should be parse CDATA', t => {
17-
const tree = parser('<script><![CDATA[console.log(1);]]></script>', {xmlMode: true});
18-
const expected = [{tag: 'script', content: ['console.log(1);']}];
17+
const tree = parser('<script><![CDATA[console.log(1);]]></script>', { xmlMode: true });
18+
const expected = [{ tag: 'script', content: ['console.log(1);'] }];
1919
t.deepEqual(tree, expected);
2020
});
2121

@@ -52,7 +52,7 @@ test.skip('should be parse tag with object in attribute data witchout escape', t
5252
});
5353

5454
test.skip('should be parse tag with object in attribute data escape', t => {
55-
const json = JSON.stringify({button: {checkedView: 'extra'}});
55+
const json = JSON.stringify({ button: { checkedView: 'extra' } });
5656
const html = '<button data-bem="' + json + '"' +
5757
' type="submit"></button>';
5858
const tree = parser(html);
@@ -70,25 +70,25 @@ test.skip('should be parse tag with object in attribute data escape', t => {
7070

7171
test('should be parse isolated comment', t => {
7272
const tree = parser('<div><!--comment--></div>');
73-
const expected = [{tag: 'div', content: ['<!--comment-->']}];
73+
const expected = [{ tag: 'div', content: ['<!--comment-->'] }];
7474
t.deepEqual(tree, expected);
7575
});
7676

7777
test('should be parse comment before text content', t => {
7878
const tree = parser('<div><!--comment-->Text after comment</div>');
79-
const expected = [{tag: 'div', content: ['<!--comment-->', 'Text after comment']}];
79+
const expected = [{ tag: 'div', content: ['<!--comment-->', 'Text after comment'] }];
8080
t.deepEqual(tree, expected);
8181
});
8282

8383
test('should be parse comment after text content', t => {
8484
const tree = parser('<div>Text before comment.<!--comment--></div>');
85-
const expected = [{tag: 'div', content: ['Text before comment.', '<!--comment-->']}];
85+
const expected = [{ tag: 'div', content: ['Text before comment.', '<!--comment-->'] }];
8686
t.deepEqual(tree, expected);
8787
});
8888

8989
test('should be parse comment in the middle of text content', t => {
9090
const tree = parser('<div>Text surrounding <!--comment--> a comment.</div>');
91-
const expected = [{tag: 'div', content: ['Text surrounding ', '<!--comment-->', ' a comment.']}];
91+
const expected = [{ tag: 'div', content: ['Text surrounding ', '<!--comment-->', ' a comment.'] }];
9292
t.deepEqual(tree, expected);
9393
});
9494

@@ -101,7 +101,7 @@ test('should be parse doctype', t => {
101101
test('should be parse directive', t => {
102102
const options = {
103103
directives: [
104-
{name: '?php', start: '<', end: '>'}
104+
{ name: '?php', start: '<', end: '>' }
105105
]
106106
};
107107
const tree = parser('<?php echo "Hello word"; ?>', options);
@@ -112,7 +112,7 @@ test('should be parse directive', t => {
112112
test('should be parse regular expression directive', t => {
113113
const options = {
114114
directives: [
115-
{name: /\?(php|=).*/, start: '<', end: '>'}
115+
{ name: /\?(php|=).*/, start: '<', end: '>' }
116116
]
117117
};
118118
const tree1 = parser('<?php echo "Hello word"; ?>', options);
@@ -127,8 +127,8 @@ test('should be parse regular expression directive', t => {
127127
test('should be parse directives and tag', t => {
128128
const options = {
129129
directives: [
130-
{name: '!doctype', start: '<', end: '>'},
131-
{name: '?php', start: '<', end: '>'}
130+
{ name: '!doctype', start: '<', end: '>' },
131+
{ name: '?php', start: '<', end: '>' }
132132
]
133133
};
134134
const html = '<!doctype html><header><?php echo "Hello word"; ?></header><body>{{%njk test %}}</body>';
@@ -149,20 +149,20 @@ test('should be parse directives and tag', t => {
149149

150150
test('should be parse tag', t => {
151151
const tree = parser('<html></html>');
152-
const expected = [{tag: 'html'}];
152+
const expected = [{ tag: 'html' }];
153153
t.deepEqual(tree, expected);
154154
});
155155

156156
test('should be parse doctype and tag', t => {
157157
const tree = parser('<!doctype html><html></html>');
158-
const expected = ['<!doctype html>', {tag: 'html'}];
158+
const expected = ['<!doctype html>', { tag: 'html' }];
159159
t.deepEqual(tree, expected);
160160
});
161161

162162
test('should be parse tag attrs', t => {
163163
const tree = parser('<div id="id" class="class"></div>');
164164
const expected = [{
165-
tag: 'div', attrs: {id: 'id', class: 'class'}
165+
tag: 'div', attrs: { id: 'id', class: 'class' }
166166
}];
167167
t.deepEqual(tree, expected);
168168
});
@@ -175,30 +175,30 @@ test('should be parse text', t => {
175175

176176
test('should be parse text in content', t => {
177177
const tree = parser('<div>Text</div>');
178-
const expected = [{tag: 'div', content: ['Text']}];
178+
const expected = [{ tag: 'div', content: ['Text'] }];
179179
t.deepEqual(tree, expected);
180180
});
181181

182182
test('should be parse not a single node in tree', t => {
183183
const tree = parser('<span>Text1</span><span>Text2</span>Text3');
184184
const expected = [
185-
{tag: 'span', content: ['Text1']}, {tag: 'span', content: ['Text2']}, 'Text3'
185+
{ tag: 'span', content: ['Text1'] }, { tag: 'span', content: ['Text2'] }, 'Text3'
186186
];
187187
t.deepEqual(tree, expected);
188188
});
189189

190190
test('should be parse not a single node in parent content', t => {
191191
const tree = parser('<div><span>Text1</span><span>Text2</span>Text3</div>');
192192
const expected = [
193-
{tag: 'div', content: [{tag: 'span', content: ['Text1']}, {tag: 'span', content: ['Text2']}, 'Text3']}
193+
{ tag: 'div', content: [{ tag: 'span', content: ['Text1'] }, { tag: 'span', content: ['Text2'] }, 'Text3'] }
194194
];
195195
t.deepEqual(tree, expected);
196196
});
197197

198198
test('should be parse camelCase tag name', t => {
199199
const tree = parser('<mySuperTag></mySuperTag>');
200200
const expected = [
201-
{tag: 'mySuperTag'}
201+
{ tag: 'mySuperTag' }
202202
];
203203
t.deepEqual(tree, expected);
204204
});
@@ -207,7 +207,7 @@ test('should be parse simple contents are split with "<" in comment', t => {
207207
const html = '<a> /* width < 800px */ <hr /> test</a>';
208208
const tree = parser(html);
209209
const expected = [
210-
{tag: 'a', content: [' /* width < 800px */ ', {tag: 'hr'}, ' test']}
210+
{ tag: 'a', content: [' /* width < 800px */ ', { tag: 'hr' }, ' test'] }
211211
];
212212
t.deepEqual(tree, expected);
213213
});
@@ -216,7 +216,7 @@ test('should be parse style contents are split with "<" in comment', t => {
216216
const html = '<style> /* width < 800px */ @media (max-width: 800px) { /* selectors */} </style>';
217217
const tree = parser(html);
218218
const expected = [
219-
{tag: 'style', content: [' /* width < 800px */ @media (max-width: 800px) { /* selectors */} ']}
219+
{ tag: 'style', content: [' /* width < 800px */ @media (max-width: 800px) { /* selectors */} '] }
220220
];
221221
t.deepEqual(tree, expected);
222222
});
@@ -229,7 +229,8 @@ test('should be parse script contents are split with "<" in comment', t => {
229229
tag: 'script',
230230
content: [
231231
' var str = \'hey <form\'; if (!str.match(new RegExp(\'<(form|iframe)\', \'g\'))) { /* ... */ }'
232-
]}
232+
]
233+
}
233234
];
234235
t.deepEqual(tree, expected);
235236
});
@@ -243,7 +244,7 @@ test('should be not converting html entity name', t => {
243244

244245
test('should parse with source locations', t => {
245246
const html = '<h1>Test</h1>\n<p><b>Foo</b></p>';
246-
const tree = parser(html, {sourceLocations: true});
247+
const tree = parser(html, { sourceLocations: true });
247248
const expected = [
248249
{
249250
tag: 'h1',
@@ -292,3 +293,25 @@ test('should parse with source locations', t => {
292293
];
293294
t.deepEqual(tree, expected);
294295
});
296+
297+
test('should parse with input in button', t => {
298+
const html = '<button >Hello <input type="file" ng-hide="true" />PostHtml</button>';
299+
const tree = parser(html, { xmlMode: true });
300+
const expected = [
301+
{
302+
tag: 'button',
303+
content: [
304+
'Hello ',
305+
{
306+
tag: 'input',
307+
attrs: {
308+
type: 'file',
309+
'ng-hide': 'true'
310+
}
311+
},
312+
'PostHtml'
313+
]
314+
}
315+
];
316+
t.deepEqual(tree, expected);
317+
});

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ParserOptions} from 'htmlparser2';
1+
import { ParserOptions } from 'htmlparser2';
22

33
declare const parser: (html: string, options?: Options) => Node[];
44

xo.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
rules: {
44
'@typescript-eslint/prefer-readonly-parameter-types': 'off',
55
'ava/no-skip-test': 'off',
6-
'ava/no-only-test': 'off'
6+
'ava/no-only-test': 'off',
7+
'object-curly-spacing': ['error', 'always']
78
}
89
};

0 commit comments

Comments
 (0)