');
result.valid.should.eql(false);
result.toString().should.eql('
');
})
it('gmail.html should return Object with valid: true', function () {
var result = parseHTML(fs.readFileSync(__dirname + '/html/gmail.html').toString().replace(/<\//gi, '<'));
result.valid.should.eql(false);
})
it('gmail.html should return Object with valid: true', function () {
var result = parseHTML(fs.readFileSync(__dirname + '/html/nice.html').toString().replace(/<\//gi, '<'));
result.valid.should.eql(false);
})
});
describe('TextNode', function () {
describe('#isWhitespace', function () {
var 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;
});
});
describe('HTMLElement', function () {
describe('#removeWhitespace()', function () {
it('should remove whitespaces while preserving nodes with content', function () {
var root = parseHTML('
\r \n \t
123
');
var p = new HTMLElement('p', {}, '');
p.appendChild(new HTMLElement('h5', {}, ''))
.appendChild(new TextNode('123'));
root.firstChild.removeWhitespace().should.eql(p);
});
});
describe('#rawAttributes', function () {
it('should return escaped attributes of the element', function () {
var root = parseHTML('
');
root.firstChild.rawAttributes.should.eql({
'a': '12',
'data-id': '!$$&',
'yAz': '1'
});
});
});
describe('#attributes', function () {
it('should return attributes of the element', function () {
var root = parseHTML('
');
root.firstChild.attributes.should.eql({
'a': '12',
'data-id': '!$$&',
'yAz': '1',
'disabled': '',
'class': ''
});
});
});
describe('#setAttribute', function () {
it('should edit the attributes of the element', function () {
var root = parseHTML('
');
root.firstChild.setAttribute('a', 13);
root.firstChild.attributes.should.eql({
'a': '13',
});
root.firstChild.toString().should.eql('
');
});
it('should add an attribute to the element', function () {
var root = parseHTML('
');
root.firstChild.setAttribute('b', 13);
root.firstChild.attributes.should.eql({
'a': '12',
'b': '13',
});
root.firstChild.toString().should.eql('
');
});
it('should remove an attribute from the element', function () {
var root = parseHTML('
');
root.firstChild.setAttribute('b', null);
root.firstChild.setAttribute('c');
root.firstChild.attributes.should.eql({
'a': '12',
});
root.firstChild.toString().should.eql('
');
});
});
describe('#setAttributes', function () {
it('should return attributes of the element', function () {
var root = parseHTML('
');
root.firstChild.setAttributes({c: 12});
root.firstChild.attributes.should.eql({
'c': '12',
});
root.firstChild.toString().should.eql('
');
});
});
describe('#querySelector()', function () {
it('should return correct elements in DOM tree', function () {
var root = parseHTML('
');
root.querySelector('#id').should.eql(root.firstChild);
root.querySelector('span.a').should.eql(root.firstChild.firstChild.firstChild);
root.querySelector('span.b').should.eql(root.firstChild.firstChild.firstChild);
root.querySelector('span.a.b').should.eql(root.firstChild.firstChild.firstChild);
root.querySelector('#id .b').should.eql(root.firstChild.firstChild.firstChild);
root.querySelector('#id span').should.eql(root.firstChild.firstChild.firstChild);
root.querySelector('[data-id=myid]').should.eql(root.firstChild);
root.querySelector('[data-id="myid"]').should.eql(root.firstChild);
});
});
describe('#querySelectorAll()', function () {
it('should return correct elements in DOM tree', function () {
var root = parseHTML('
');
root.querySelectorAll('#id').should.eql([root.firstChild]);
root.querySelectorAll('span.a').should.eql([root.firstChild.firstChild.firstChild]);
root.querySelectorAll('span.b').should.eql([root.firstChild.firstChild.firstChild]);
root.querySelectorAll('span.a.b').should.eql([root.firstChild.firstChild.firstChild]);
root.querySelectorAll('#id .b').should.eql([root.firstChild.firstChild.firstChild]);
root.querySelectorAll('#id span').should.eql(root.firstChild.firstChild.childNodes);
root.querySelectorAll('#id, #id .b').should.eql([root.firstChild, root.firstChild.firstChild.firstChild]);
});
});
describe('#structuredText', function () {
it('should return correct structured text', function () {
var root = parseHTML('
oa
b
c');
root.structuredText.should.eql('o\na\nb\nc');
});
it('should not return comments in structured text', function () {
var root = parseHTML('
oa
', { comment: true });
root.structuredText.should.eql('o\na');
});
});
describe('#set_content', function () {
it('set content string', function () {
var root = parseHTML('
');
root.childNodes[0].set_content('
abc
bla');
root.toString().should.eql('
');
});
it('set content nodes', function () {
var root = parseHTML('
');
root.childNodes[0].set_content(parseHTML('
abc
bla').childNodes);
root.toString().should.eql('
');
});
it('set content node', function () {
var root = parseHTML('
');
root.childNodes[0].set_content(parseHTML('
abc
bla').childNodes[0]);
root.toString().should.eql('
');
});
it('set content text', function () {
var root = parseHTML('
');
root.childNodes[0].set_content('abc');
root.toString().should.eql('
abc
');
});
});
});
describe('stringify', function () {
it('#toString()', function () {
const html = '
Hello
bbb';
const root = parseHTML(html);
root.toString().should.eql(html)
});
it('#toString() should not return comments by default', function () {
const html = '
';
const result = '
';
const root = parseHTML(html);
root.toString().should.eql(result);
});
it('#toString() should return comments when specified', function () {
const html = '
';
const root = parseHTML(html, { comment: true });
root.toString().should.eql(html);
});
});
describe('Comment Element', function () {
it('comment nodeType should be 8', function () {
var root = parseHTML('', { comment: true });
root.firstChild.nodeType.should.eql(8);
});
});
describe('Custom Element', function () {
it('parse "
" tagName should be "my-widget"', function () {
var root = parseHTML('
');
root.firstChild.tagName.should.eql('my-widget');
});
});
describe('Custom Element multiple dash', function () {
it('parse "
" tagName should be "my-new-widget"', function () {
var root = parseHTML('
');
root.firstChild.tagName.should.eql('my-new-widget');
});
});
});