Skip to content

Commit

Permalink
feat: implement block level elements spec
Browse files Browse the repository at this point in the history
  • Loading branch information
onhate committed Jun 23, 2021
1 parent bcc21a6 commit 044a1bf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
64 changes: 46 additions & 18 deletions src/nodes/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,52 @@ export interface RawAttributes {

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

const kBlockElements = new Map<string, true>();
const kBlockElements = {
address: true,
article: true,
aside: true,
blockquote: true,
br: true,
details: true,
dialog: true,
dd: true,
div: true,
dl: true,
dt: true,
fieldset: true,
figcaption: true,
figure: true,
footer: true,
form: true,
h1: true,
h2: true,
h3: true,
h4: true,
h5: true,
h6: true,
header: true,
hgroup: true,
hr: true,
li: true,
main: true,
nav: true,
ol: true,
p: true,
pre: true,
section: true,
table: true,
td: true,
tr: true,
ul: true
};

function isBlockElement(node: HTMLElement) {
if (node && node.tagName) {
return Boolean(kBlockElements[node.tagName.toLowerCase()]);
}
return false;
}

kBlockElements.set('DIV', true);
kBlockElements.set('div', true);
kBlockElements.set('P', true);
kBlockElements.set('p', true);
// ul: true,
// ol: true,
kBlockElements.set('LI', true);
kBlockElements.set('li', true);
// table: true,
// tr: true,
kBlockElements.set('TD', true);
kBlockElements.set('td', true);
kBlockElements.set('SECTION', true);
kBlockElements.set('section', true);
kBlockElements.set('BR', true);
kBlockElements.set('br', true);

class DOMTokenList {
private _set: Set<string>;
Expand Down Expand Up @@ -245,7 +273,7 @@ export default class HTMLElement extends Node {
const blocks = [currentBlock];
function dfs(node: Node) {
if (node.nodeType === NodeType.ELEMENT_NODE) {
if (kBlockElements.get((node as HTMLElement).rawTagName)) {
if (isBlockElement(node as HTMLElement)) {
if (currentBlock.length > 0) {
blocks.push(currentBlock = []);
}
Expand Down
5 changes: 5 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ describe('HTML Parser', function () {
const root = parseHTML('<span>o<p>a</p><!-- my comment --></span>', { comment: true });
root.structuredText.should.eql('o\na');
});

it('should return correct structured text (block level elements)', function () {
const root = parseHTML('<p>content</p><span><u><h1>inside</h1><i>htm<u>l</u></i></u></span>');
root.structuredText.should.eql('content\ninside\nhtml');
});
});
describe('#set_content', function () {
it('set content string', function () {
Expand Down

0 comments on commit 044a1bf

Please sign in to comment.