" and return root element', function () {
const root = parseHTML('
', {
lowerCaseTagName: true
});
const u = undefined;
const div = new HTMLElement('div', {}, '', root, u, u, { lowerCaseTagName: true });
const a = div.appendChild(new HTMLElement('a', {}, '', u, u, u, { lowerCaseTagName: true }));
const img = a.appendChild(new HTMLElement('img', {}, '', u, u, u, { lowerCaseTagName: true }));
const p = div.appendChild(new HTMLElement('p', {}, '', u, u, u, { lowerCaseTagName: true }));
root.firstChild.should.eql(div);
});
it.skip('TODO implement: should use HTMLElement._defaultParseOptions', function () {
// set _defaultParseOptions,
// so we dont have to pass parseOptions to every `new HTMLElement(...)`
// FIXME _defaultParseOptions is not used
should(HTMLElement._defaultParseOptions.lowerCaseTagName).be.undefined();
HTMLElement._defaultParseOptions.lowerCaseTagName = true;
const root = parseHTML('
');
const div = new HTMLElement('div', {}, '', root);
const a = div.appendChild(new HTMLElement('a', {}, ''));
const img = a.appendChild(new HTMLElement('img', {}, ''));
const p = div.appendChild(new HTMLElement('p', {}, ''));
root.firstChild.should.eql(div);
// cleanup? remove key from _defaultParseOptions object?
//HTMLElement._defaultParseOptions.lowerCaseTagName = undefined;
});
it('should deal uppercase', function () {
//should(HTMLElement._defaultParseOptions.comment).be.undefined();
const html = 'SISREG III
" and return root element', function () {
const root = parseHTML('
');
const div = new HTMLElement('div', {}, '', root);
const a = div.appendChild(new HTMLElement('a', {}, ''));
const img = a.appendChild(new HTMLElement('img', {}, ''));
const p = div.appendChild(new HTMLElement('p', {}, ''));
root.firstChild.should.eql(div);
});
it('should parse "
" and return root element without comments', function () {
const root = parseHTML('
');
const div = new HTMLElement('div', {}, '', root);
const a = div.appendChild(new HTMLElement('a', {}, ''));
root.firstChild.should.eql(div);
});
it('should parse "
" and return root element with comments', function () {
const root = parseHTML('
', { comment: true });
const u = undefined;
const div = new HTMLElement('div', {}, '', root, u, u, { comment: true });
const a = div.appendChild(new HTMLElement('a', {}, '', u, u, u, { comment: true }));
const comment = a.appendChild(new CommentNode(' my comment ', u, u, u, u, u, { comment: true }));
root.firstChild.should.eql(div);
});
it('should not parse HTML inside comments', function () {
const root = parseHTML('', { comment: true });
const u = undefined;
const div = new HTMLElement('div', {}, '', root, u, u, { comment: true });
const comment = div.appendChild(new CommentNode('', u, u, u, u, u, { comment: true }));
root.firstChild.should.eql(div);
});
// yarn test:target -g 'should parse HTML comments in *'
it('should parse HTML comments in insertAdjacentHTML', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.insertAdjacentHTML('afterend', '');
root.toString().should.eql('');
div.nextSibling.toString().should.eql('');
});
it('should parse HTML comments in set innerHTML', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.innerHTML = '';
root.toString().should.eql('');
div.firstChild.toString().should.eql('');
});
it('should parse HTML comments in set_content', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.set_content('');
root.toString().should.eql('');
div.firstChild.toString().should.eql('');
});
it('should parse HTML comments in set_content - comment in div', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.set_content('');
root.toString().should.eql('
');
div.firstChild.toString().should.eql('');
});
it('should parse HTML comments NOT in set_content with option comment=false', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.set_content('', { comment: false });
root.toString().should.eql('');
});
it('should parse HTML comments NOT in set_content with option comment=false - comment in div', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.set_content('', { comment: false });
root.toString().should.eql('
');
});
it('should parse HTML comments in replaceWith', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
div.replaceWith('');
root.toString().should.eql('');
});
it('should parse HTML comments in clone', function () {
const root = parseHTML('', { comment: true });
const div = root.querySelector('div');
const clone = div.clone();
clone.toString().should.eql('');
});
it('should parse picture element', function () {
const root = parseHTML('');
const picture = new HTMLElement('picture', {}, '', root);
const source = picture.appendChild(new HTMLElement('source', {}, 'srcset="/images/example-1.jpg 1200w, /images/example-2.jpg 1600w" sizes="100vw"'));
const img = picture.appendChild(new HTMLElement('img', {}, 'src="/images/example.jpg" alt="Example"'));
root.firstChild.should.eql(picture);
});
it('should not extract text in script and style by default', function () {
const root = parseHTML('');
root.firstChild.childNodes.should.be.empty;
root.lastChild.childNodes.should.be.empty;
});
it('should extract text in script and style when ask so', function () {
const root = parseHTML('', {
script: true,
style: true
});
const script = root.firstChild;
const style = root.lastChild;
script.childNodes.should.not.be.empty;
script.childNodes.should.eql([new TextNode('1', script)]);
script.text.should.eql('1');
style.childNodes.should.not.be.empty;
style.childNodes.should.eql([new TextNode('2&', style)]);
style.text.should.eql('2&');
style.rawText.should.eql('2&');
});
it('should be able to parse "html/incomplete-script" file', function () {
const root = parseHTML(fs.readFileSync(__dirname + '/../assets/html/incomplete-script').toString(), {
script: true
});
});
it('should be able to parse namespaces', function () {
const namespacedXML = 'content';
parseHTML(namespacedXML).toString().should.eql(namespacedXML);
});
it('should parse "
.." very fast', function () {
for (let i = 0; i < 100; i++)
parseHTML('
');
});
it('should parse "
.." fast', function () {
for (let i = 0; i < 100; i++)
parseHTML('
', {
lowerCaseTagName: true
});
});
// Test for broken tags.
something
it('should parse "
content
other
" (fix h3, span closing tag) very fast', function () {
const root = parseHTML(fs.readFileSync(__dirname + '/../assets/html/incomplete-script').toString());
});
it('should parse table currect', function () {
this.timeout(4000); // pass test on slow CPUs
const root = parseHTML(fs.readFileSync(__dirname + '/../assets/html/tables.html').toString(), {
script: true
});
const tables = root.querySelectorAll('table');
tables.length.should.eql(3176);
});
});
describe('TextNode', function () {
it('#isWhitespace', function () {
let node = new TextNode('');
node.isWhitespace.should.be.ok;
node = new TextNode(' \t');
node.isWhitespace.should.be.ok;
node = new TextNode(' \t \t');
node.isWhitespace.should.be.ok;
});
it('parse text node', function () {
const result = parseHTML('hello mmstudio');
result.firstChild.should.instanceOf(TextNode);
result.toString().should.eql('hello mmstudio');
});
});
describe('HTMLElement', function () {
describe('#removeWhitespace()', function () {
it('should remove whitespaces while preserving nodes with content', function () {
const root = parseHTML('
\r \n \t
123
');
const textNode = new TextNode(' 123 ');
textNode.rawText = textNode.trimmedRawText;
textNode.rawText.should.eql(' 123 ');
const p = new HTMLElement('p', {}, '', root);
p
.appendChild(new HTMLElement('h5', {}, ''))
.appendChild(textNode);
root.firstChild.removeWhitespace().should.eql(p);
});
it('should preserve legitimate leading/trailing whitespace in TextNode', function () {
parseHTML('
html');
root.structuredText.should.eql('content\ninside\nhtml');
});
it('should not indent blocks that are separated by whitespace-only text (pretty-printed HTML)', function () {
const root = parseHTML(
'
\n
Title
\n
First para
\n
Second bold para
\n
\n
one
\n
two
\n
\n
'
);
root.structuredText.should.eql('Title\nFirst para\nSecond bold para\none\ntwo');
});
it('should not add a leading space to a block that follows a whitespace-only text node', function () {
parseHTML('
a
b
').structuredText.should.eql('a\nb');
});
it('should not leave a trailing space on the line before a ', function () {
parseHTML('
a b
').structuredText.should.eql('a\nb');
});
it('should still keep a single space between inline elements', function () {
parseHTML('
hello wx
').structuredText.should.eql('hello w x');
parseHTML('
x
\nyz').structuredText.should.eql('x\ny z');
});
});
describe('#set_content', function () {
it('set content string', function () {
const root = parseHTML('');
root.childNodes[0].set_content('
');
});
it('set content pre', function () {
const root = parseHTML(``);
const body = root.querySelector("body");
body.set_content(`
this is some preformatted text
`);
root.toString().should.eql('
this is some preformatted text
')
});
});
describe('encode/decode', function () {
it('should decode attributes value', function () {
const root = parseHTML('');
root.firstChild.getAttribute('alt').should.eql(`Verissimo, Ilaria D'Amico: «Sogno una bambina. Buffon mi ha chiesto in moglie tante volte»`);
root.firstChild.attributes.alt.should.eql(`Verissimo, Ilaria D'Amico: «Sogno una bambina. Buffon mi ha chiesto in moglie tante volte»`);
root.firstChild.setAttribute('alt', '«Sogno');
root.firstChild.getAttribute('alt').should.eql('«Sogno');
root.firstChild.rawAttributes.alt.should.eql('«Sogno');
});
it('should not decode text from parseHTML()', function () {
// https://github.com/taoqf/node-html-parser/issues/33
const content = `<p> Not a p tag <br /> at all`;
const root = parseHTML(`
`);
const pNode = root.firstChild;
pNode.text.should.eql(decodedText);
pNode.rawText.should.eql(encodedText);
const textNode = pNode.firstChild;
textNode.text.should.eql(decodedText);
textNode.rawText.should.eql(encodedText);
});
});
describe('#insertAdjacentHTML() should parse and insert childrens', function () {
it('shoud insert children after current node', function () {
const html = '';
const root = parseHTML(html);
const a = root.firstChild;
const b = a.firstChild;
b.insertAdjacentHTML('afterend', '');
a.toString().should.eql('');
});
it('shoud insert children before current node', function () {
const html = '';
const root = parseHTML(html);
const b = root.querySelector('b');
b.insertAdjacentHTML('beforebegin', '');
root.toString().should.eql('');
});
it('shoud append children in current node', function () {
const html = '';
const root = parseHTML(html);
const a = root.firstChild;
a.insertAdjacentHTML('beforeend', '');
a.toString().should.eql('');
a.insertAdjacentHTML('beforeend', '');
a.toString().should.eql('');
});
it('shoud insert children at position 0', function () {
const html = '';
const root = parseHTML(html);
const a = root.firstChild;
a.insertAdjacentHTML('afterbegin', '');
a.toString().should.eql('');
a.insertAdjacentHTML('afterbegin', '');
a.toString().should.eql('');
});
it('shoud insert text child at position 0', function () {
const html = '';
const root = parseHTML(html);
const a = root.firstChild;
a.insertAdjacentHTML('afterbegin', 'abc');
a.toString().should.eql('abc');
});
});
describe('Base insertion operations', () => {
describe('#before', () => {
it('Should insert multiple nodes in order', () => {
const root = parseHTML(``);
root.children[0].before(new HTMLElement('span', {}), new HTMLElement('p', {}));
root.childNodes.length.should.eql(3);
root.childNodes[0].tagName.should.eql('SPAN');
root.childNodes[1].tagName.should.eql('P');
});
it('Should insert strings as TextNode', () => {
const root = parseHTML(``);
root.children[0].before(new HTMLElement('span', {}), 'foobar', new HTMLElement('p', {}));
root.childNodes.length.should.eql(4);
root.childNodes[1].should.be.an.instanceof(TextNode);
root.childNodes[1].text.should.eql('foobar');
});
it('Should set the parent correctly', () => {
const root = parseHTML(``);
root.firstElementChild.before('foobar');
root.firstElementChild.parentNode.should.eql(root);
});
it('Should be removed from previous trees', () => {
const root1 = parseHTML(``);
const root2 = parseHTML(``);
const section1 = root1.firstElementChild;
const section2 = root2.firstElementChild;
const div1 = section1.firstElementChild;
section2.before(div1);
section1.childNodes.length.should.eql(0);
root2.childNodes.length.should.eql(2);
div1.parentNode.should.eql(root2);
});
});
describe('#after', () => {
it('Should insert multiple nodes in order', () => {
const root = parseHTML(``);
root.children[0].after(new HTMLElement('span', {}), 'foobar', new HTMLElement('p', {}));
root.childNodes.length.should.eql(4);
root.childNodes[1].tagName.should.eql('SPAN');
root.childNodes[2].should.be.an.instanceof(TextNode);
root.childNodes[3].tagName.should.eql('P');
});
it('Should set the parent correctly', () => {
const root = parseHTML(``);
root.firstElementChild.after('foobar');
root.lastElementChild.parentNode.should.eql(root);
});
});
describe('#prepend', () => {
it('Should insert multiple nodes in order', () => {
const root = parseHTML(``);
const section = root.firstElementChild;
section.prepend(new HTMLElement('span', {}), 'foobar', new HTMLElement('p', {}));
section.childNodes.length.should.eql(5);
section.childNodes[0].tagName.should.eql('SPAN');
section.childNodes[1].should.be.an.instanceof(TextNode);
section.childNodes[2].tagName.should.eql('P');
});
it('Should set the parent correctly', () => {
const root = parseHTML(``);
const section = root.firstElementChild;
section.prepend('foobar');
section.childNodes[0].parentNode.should.eql(section);
});
});
describe('#append', () => {
it('Should insert multiple nodes in order', () => {
const root = parseHTML(``);
const section = root.firstElementChild;
section.append(new HTMLElement('span', {}), 'foobar', new HTMLElement('p', {}));
section.childNodes.length.should.eql(5);
section.childNodes[2].tagName.should.eql('SPAN');
section.childNodes[3].should.be.an.instanceof(TextNode);
section.childNodes[4].tagName.should.eql('P');
});
it('Should set the parent correctly', () => {
const root = parseHTML(``);
const section = root.firstElementChild;
section.append('foobar');
section.childNodes[0].parentNode.should.eql(section);
});
});
});
describe('#removeChild', function () {
it('should remove child node', function () {
const html = '';
const root = parseHTML(html);
const a = root.firstChild;
const b = a.firstChild;
a.childNodes.length.should.eql(1);
a.removeChild(b);
a.childNodes.length.should.eql(0);
});
it('should not remove child node which does not exist', function () {
const html = '';
const root = parseHTML(html);
const a = root.firstChild;
const b = a.firstChild;
const c = b.firstChild;
a.childNodes.length.should.eql(1);
a.removeChild(c);
a.childNodes.length.should.eql(1);
});
});
describe('#getElementsByTagName', function () {
it('find the divs in proper order', function () {
const root = parseHTML(`
`);
const divs = root.getElementsByTagName('div');
for (const div of divs) {
div.tagName.should.eql('DIV');
}
// the literal appearance order
divs[0].attributes['data-test'].should.eql('1.0');
divs[1].attributes['data-test'].should.eql('1.1');
divs[2].attributes['data-test'].should.eql('1.1.1');
divs[3].attributes['data-test'].should.eql('2.0');
});
// check that really only children are found, no parents or anything
it('only return relevant items', function () {
const root = parseHTML(`
`);
const divs = root.querySelector('#suit').getElementsByTagName('div');
divs.length.should.eql(2);
for (const div of divs) {
div.attributes['data-ignore'].should.eql('false');
}
});
it('return all elements if tagName is *', function () {
const root = parseHTML(`
`);
const items = root.getElementsByTagName('*');
items.length.should.eql(4);
items[0].tagName.should.eql('SECTION');
items[1].tagName.should.eql('DIV');
items[2].tagName.should.eql('SPAN');
items[3].tagName.should.eql('P');
});
it('return an empty array if nothing is found', function () {
const root = parseHTML('');
root.getElementsByTagName('div').length.should.eql(0);
});
it('allow sparse arrays', function () {
const root = parseHTML(`
`);
delete root.querySelector('section').childNodes[1];
root.getElementsByTagName('div').length.should.eql(2);
});
});
describe('#children', () => {
const root = parseHTML(`
Text
foobar
baz
`);
it('All children are `HTMLElement`', () => {
root.children.forEach(child => {
child.should.be.an.instanceof(HTMLElement);
});
});
it('Correct length', () => {
root.children.length.should.eql(3);
});
});
describe('#matches', () => {
it('`HTMLElement` should match CSS selector', () => {
const html = parseHTML(``);
html.firstChild.matches('.example-class').should.be.true();
html.firstChild.matches('#example-id').should.be.true();
html.firstChild.matches('div').should.be.true();
html.firstChild.matches('[data-test="example-data"]').should.be.true();
});
it('`HTMLElement` should not match CSS selector ', () => {
const html = parseHTML(``);
html.firstChild.matches('.no-match').should.be.false();
html.firstChild.matches('#no-match').should.be.false();
html.firstChild.matches('span').should.be.false();
html.firstChild.matches('[data-test="no-match"]').should.be.false();
});
});
});
describe('stringify', function () {
it('#toString()', function () {
const html = '