');
var p = document.createElement('p');
p.querySelectorAll('p').should.have.length(0);
p.querySelectorAll('p').should.have.length(0);
};
// This is https://github.com/chjj/zest/issues/22
exports.tildeQSA = function() {
var document = domino.createDocument('
');
var els = document.querySelectorAll('.foo');
els.should.have.length(1);
};
exports.rootQSA = function() {
// the root element should be both first-child and last-child
var document = domino.createDocument('foo');
var html = document.documentElement;
html.matches(':first-child').should.be.true();
html.matches(':last-child').should.be.true();
html.matches(':only-child').should.be.true();
html.matches(':first-of-type').should.be.true();
html.matches(':last-of-type').should.be.true();
html.matches(':nth-child(1)').should.be.true();
html.matches(':nth-child(2)').should.be.false();
html.matches(':nth-last-child(1)').should.be.true();
html.matches(':nth-last-child(2)').should.be.false();
html.matches(':nth-of-type(1)').should.be.true();
html.matches(':nth-of-type(2)').should.be.false();
html.matches(':nth-last-of-type(1)').should.be.true();
html.matches(':nth-last-of-type(2)').should.be.false();
};
exports.escapeQSA = function() {
var document = domino.createDocument('
foo');
// ensure that selector parsing can handle escaped characters
document.querySelectorAll('p\\22\\27p').should.have.length(0);
document.querySelectorAll('\\50').should.have.length(1);
};
exports.gh20 = function() {
var window = createWindow('');
var frag = window.document.createDocumentFragment();
frag.querySelectorAll('p').should.have.length(0);
frag.appendChild(window.document.createElement('p'));
frag.querySelectorAll('p').should.have.length(1);
frag.appendChild(window.document.createElement('p'));
frag.querySelectorAll('p').should.have.length(2);
};
exports.gh22 = function() {
var d=domino.createDocument("
Hello world
Hi
");
d.querySelectorAll('div').should.have.length(1);
d.body.querySelectorAll('div').should.have.length(1);
d.body.querySelectorAll('h1').should.have.length(1);
d.body.querySelectorAll('p').should.have.length(1);
var w=createWindow("
',
``
];
tests.forEach(function(html) {
var d = domino.createDocument(html);
// Verify round-tripping.
d.body.outerHTML.should.equal(html);
});
};
exports.largeAttribute = function() {
var size = 400000;
// work around a performance regression in node 0.4.x - 0.6.x
if (/^v0\.[0-6]\./.test(process.version)) { size = 50000; }
var html = '';
// this should not crash with a stack overflow!
domino.createDocument(html);
};
exports.createTextNodeWithNonString = function() {
var document = domino.createDocument('');
var tests = [
[false, 'false'],
[NaN, 'NaN'],
[123, '123'],
[{}, '[object Object]'],
[[], ''],
[null, 'null'],
[undefined, 'undefined'],
];
for(var i=0; ibar';
var doc = domino.createDocument(html);
var h1 = doc.querySelector('h1');
h1.matches('*[id]').should.equal(false);
h1.matches('*[title]').should.equal(false);
h1.matches('*[lang]').should.equal(false);
h1.matches('*[dir]').should.equal(false);
h1.matches('*[accessKey]').should.equal(false);
h1.matches('*[hidden]').should.equal(false);
h1.matches('*[tabIndex]').should.equal(false);
var h2 = doc.querySelector('h2');
h2.matches('*[id]').should.equal(true);
h2.matches('*[title]').should.equal(true);
h2.matches('*[lang]').should.equal(true);
h2.matches('*[dir]').should.equal(true);
h2.matches('*[accessKey]').should.equal(true);
h2.matches('*[hidden]').should.equal(true);
h2.matches('*[tabIndex]').should.equal(true);
h1.matches('*[matches]').should.equal(false);
h1.matches('*[querySelector]').should.equal(false);
h1.matches('*[isHTML]').should.equal(false);
};
exports.crHandling = function() {
var html = '
\r
';
var doc = domino.createDocument(html);
var div = doc.querySelector('#a');
(div != null).should.be.true(); // jshint ignore:line
// all \r should be converted to \n
div.outerHTML.should.equal('
\n
');
};
exports.eqAttr = function() {
var html = "
";
var doc = domino.createDocument(html);
var div = doc.querySelector('#a');
(div != null).should.be.true(); // jshint ignore:line
div.attributes.length.should.equal(2);
div.attributes.item(1).name.should.equal('=');
div.children.length.should.equal(1);
div.children[0].tagName.should.equal('A=B');
};
exports.tagNameCase = function() {
// See https://github.com/fgnass/domino/pull/41
var impl = domino.createDOMImplementation();
var namespace = 'http://schemas.xmlsoap.org/soap/envelope/';
var qualifiedName = 'Envelope';
var doc = impl.createDocument(namespace, qualifiedName, null);
doc.documentElement.tagName.should.equal(qualifiedName);
};
exports.fastAttributes = function() {
// test the SIMPLETAG/SIMPLEATTR path in HTMLParser
var html = "
<\np>
";
var doc = domino.createDocument(html);
var div = doc.querySelector('#a');
(div != null).should.be.true(); // jshint ignore:line
div.attributes.length.should.equal(3);
div.attributes.item(1).value.should.equal('x "y');
div.attributes.item(2).value.should.equal('a \nb');
div.children.length.should.equal(0);
};
exports.anchorElement = function() {
var html = "!";
var doc = domino.createDocument(html);
var a = doc.querySelector('a');
(a != null).should.be.true(); // jshint ignore:line
a.href.should.equal('http://user:pass@example.com:1234/foo/bar?bat#baz');
a.protocol.should.equal('http:');
a.host.should.equal('example.com:1234');
a.hostname.should.equal('example.com');
a.port.should.equal('1234');
a.pathname.should.equal('/foo/bar');
a.search.should.equal('?bat');
a.hash.should.equal('#baz');
a.username.should.equal('user');
a.password.should.equal('pass');
a.origin.should.equal('http://example.com:1234');
// now try mutating!
a.protocol = 'https:';
a.href.should.equal('https://user:pass@example.com:1234/foo/bar?bat#baz');
a.hostname = 'other.net';
a.href.should.equal('https://user:pass@other.net:1234/foo/bar?bat#baz');
a.port = 5678;
a.href.should.equal('https://user:pass@other.net:5678/foo/bar?bat#baz');
a.pathname = '/blam/';
a.href.should.equal('https://user:pass@other.net:5678/blam/?bat#baz');
a.search = '?bat&banana';
a.href.should.equal('https://user:pass@other.net:5678/blam/?bat&banana#baz');
a.hash = '#oranges';
a.href.should.equal('https://user:pass@other.net:5678/blam/?bat&banana#oranges');
a.username = 'joe';
a.href.should.equal('https://joe:pass@other.net:5678/blam/?bat&banana#oranges');
a.password = 'smith';
a.href.should.equal('https://joe:smith@other.net:5678/blam/?bat&banana#oranges');
};
exports.gh59 = function() {
var html = 'foo';
var doc = domino.createDocument(html);
doc.querySelectorAll('span[style]').should.have.length(1);
doc.querySelectorAll('span[style="display:none"]').should.have.length(1);
doc.querySelectorAll('span[style*="display:none"]').should.have.length(1);
};
exports.duplicateID = function() {
var doc = domino.createDocument('');
var root = doc.documentElement;
function makeElement(name) {
var elt = doc.createElement(name);
elt.setAttribute("id", "x");
return elt;
}
var a = root.appendChild(makeElement("a"));
var b = root.appendChild(makeElement("b"));
var c = root.appendChild(makeElement("c"));
var d = root.appendChild(makeElement("d"));
doc.getElementById("x").should.equal(a);
root.removeChild(a);
doc.getElementById("x").should.equal(b);
root.removeChild(c);
root.removeChild(b);
doc.getElementById("x").should.equal(d);
root.removeChild(d);
(doc.getElementById("x") === null).should.be.true();
};
exports.normalize = function() {
var doc = domino.createDocument('foo');
var span = doc.getElementById("x");
span.appendChild(doc.createTextNode('bar'));
span.outerHTML.should.equal('foobar');
span.normalize();
span.outerHTML.should.equal('foobar');
span.childNodes[1].nodeValue.should.equal('foobar');
};
exports.replaceChild = function() {
var impl = domino.createDOMImplementation();
var doc = impl.createDocument();
var root = doc.appendChild(doc.createElement('root'));
root.outerHTML.should.equal('');
var a = root.appendChild(doc.createElement('a'));
root.outerHTML.should.equal('');
var b = doc.createElement('b');
function capture(f) {
var events = [];
doc._setMutationHandler(function(info) {
events.push(info);
});
f();
doc._setMutationHandler(null);
return events;
}
// Replace with unrooted
capture(function() {
root.replaceChild(b, a).should.equal(a);
}).should.deepEqual([
{
type: 4, // Remove
target: root,
node: a
},
{
type: 6, // Insert
target: root,
node: b
}]);
root.outerHTML.should.equal('');
root.replaceChild(a, b).should.equal(b);
root.outerHTML.should.equal('');
// Move node
var c = doc.createElement('c');
root.appendChild(b);
root.appendChild(c);
capture(function() {
root.replaceChild(c, a);
}).should.deepEqual([
{
type: 4, // Remove
target: root,
node: a
},
{
type: 5, // Move
target: c
}]);
root.outerHTML.should.equal('');
// Replace under unrooted parent
var df = doc.createDocumentFragment();
var d = df.appendChild(doc.createElement('d'));
var e = df.appendChild(doc.createElement('e'));
var f = doc.createElement('f');
df.replaceChild(f, e);
df.serialize().should.equal('');
d = d; // avoid defined-but-not-used warning
// Replace rooted node with document fragment
root.appendChild(a);
root.replaceChild(df, b);
root.outerHTML.should.equal('');
};
exports.contains = function() {
// see https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
var document = createWindow(html).document;
var h1 = document.getElementById('lorem');
h1.contains(null).should.equal(false);
h1.contains(h1).should.equal(true);
h1.childNodes[0].contains(h1).should.equal(false);
h1.contains(h1.childNodes[0]).should.equal(true);
document.body.contains(h1).should.equal(true);
var nodeList = document.querySelectorAll('p');
h1.contains(nodeList[0]).should.equal(false);
document.body.contains(nodeList[0]).should.equal(true);
nodeList[0].contains(nodeList[1]).should.equal(false);
nodeList[1].contains(nodeList[0]).should.equal(false);
};
exports.parseImportant = function() {
var html = '
foo
';
var doc = domino.createDocument(html);
var p = doc.querySelector('p');
p.style.fontFamily.should.equal('sans-serif');
p.style.textDecoration.should.equal('none');
};
exports.gh70 = function() {
var document = domino.createDocument('
');
var t = document.getElementById("row");
t.should.be.an.instanceof(domino.impl.HTMLTemplateElement);
t.childNodes.length.should.equal(0);
t.content.should.be.an.instanceof(domino.impl.DocumentFragment);
t.content.serialize().should.equal("
");
document.querySelectorAll("td").length.should.equal(0);
t.content.querySelectorAll("td").length.should.equal(2);
t.content.ownerDocument.should.not.equal(document);
t.content.querySelectorAll("*").map(function(el) {
el.ownerDocument.should.equal(t.content.ownerDocument);
});
};
exports.template2 = function() {
// Templates go in by default.
var document = domino.createDocument("x');
};
// HTMLTemplateElement.innerHTML
// see https://github.com/w3c/DOM-Parsing/issues/1
exports.template3 = function() {
var document = domino.createDocument();
var t = document.createElement("template");
t.should.be.an.instanceof(domino.impl.HTMLTemplateElement);
t.childNodes.length.should.equal(0);
t.content.should.be.an.instanceof(domino.impl.DocumentFragment);
// This is the key line:
t.innerHTML = '
");
};
exports.fosterParent2 = function() {
var document = domino.createDocument('foster test');
var thead = document.createElement("thead");
// exercise the "no table in open element stack" case in foster parenting
// algorithm.
thead.innerHTML = "