Skip to content

Commit 044a1bf

Browse files
committed
feat: implement block level elements spec
1 parent bcc21a6 commit 044a1bf

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

src/nodes/html.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,52 @@ export interface RawAttributes {
2929

3030
export type InsertPosition = 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';
3131

32-
const kBlockElements = new Map<string, true>();
32+
const kBlockElements = {
33+
address: true,
34+
article: true,
35+
aside: true,
36+
blockquote: true,
37+
br: true,
38+
details: true,
39+
dialog: true,
40+
dd: true,
41+
div: true,
42+
dl: true,
43+
dt: true,
44+
fieldset: true,
45+
figcaption: true,
46+
figure: true,
47+
footer: true,
48+
form: true,
49+
h1: true,
50+
h2: true,
51+
h3: true,
52+
h4: true,
53+
h5: true,
54+
h6: true,
55+
header: true,
56+
hgroup: true,
57+
hr: true,
58+
li: true,
59+
main: true,
60+
nav: true,
61+
ol: true,
62+
p: true,
63+
pre: true,
64+
section: true,
65+
table: true,
66+
td: true,
67+
tr: true,
68+
ul: true
69+
};
70+
71+
function isBlockElement(node: HTMLElement) {
72+
if (node && node.tagName) {
73+
return Boolean(kBlockElements[node.tagName.toLowerCase()]);
74+
}
75+
return false;
76+
}
3377

34-
kBlockElements.set('DIV', true);
35-
kBlockElements.set('div', true);
36-
kBlockElements.set('P', true);
37-
kBlockElements.set('p', true);
38-
// ul: true,
39-
// ol: true,
40-
kBlockElements.set('LI', true);
41-
kBlockElements.set('li', true);
42-
// table: true,
43-
// tr: true,
44-
kBlockElements.set('TD', true);
45-
kBlockElements.set('td', true);
46-
kBlockElements.set('SECTION', true);
47-
kBlockElements.set('section', true);
48-
kBlockElements.set('BR', true);
49-
kBlockElements.set('br', true);
5078

5179
class DOMTokenList {
5280
private _set: Set<string>;
@@ -245,7 +273,7 @@ export default class HTMLElement extends Node {
245273
const blocks = [currentBlock];
246274
function dfs(node: Node) {
247275
if (node.nodeType === NodeType.ELEMENT_NODE) {
248-
if (kBlockElements.get((node as HTMLElement).rawTagName)) {
276+
if (isBlockElement(node as HTMLElement)) {
249277
if (currentBlock.length > 0) {
250278
blocks.push(currentBlock = []);
251279
}

test/html.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ describe('HTML Parser', function () {
371371
const root = parseHTML('<span>o<p>a</p><!-- my comment --></span>', { comment: true });
372372
root.structuredText.should.eql('o\na');
373373
});
374+
375+
it('should return correct structured text (block level elements)', function () {
376+
const root = parseHTML('<p>content</p><span><u><h1>inside</h1><i>htm<u>l</u></i></u></span>');
377+
root.structuredText.should.eql('content\ninside\nhtml');
378+
});
374379
});
375380
describe('#set_content', function () {
376381
it('set content string', function () {

0 commit comments

Comments
 (0)