text: "use strict";var expect=require("expect.js"),DomUtils=require("htmlparser2").DomUtils,helper=require("../tools/helper.js"),path=require("path"),document=helper.getDocument(path.join(__dirname,"index.html")),CSSselect=helper.CSSselect,location={hash:""};CSSselect.pseudos.target=function(e){return e.attribs&&e.attribs.id===location.hash.substr(1)},CSSselect.pseudos.humanoid=function(e){return CSSselect.is(e,"li:contains(human)")||CSSselect.is(e,"ol:contains(human)")};var frag=helper.getDOM('<div class="d i v"><p id="oooo"><em></em><em id="emem"></em></p></div><p id="sep"><div class="a"><span></span></div></p>'),doc=helper.getDOM('<div id="hsoob"><div class="a b"><div class="d e sib" test="fg" id="booshTest"><p><span id="spanny"></span></p></div><em nopass="copyrighters" rel="copyright booshrs" test="f g" class="sib"></em><span class="h i a sib"></span></div><p class="odd"></p></div><div id="lonelyHsoob"></div>'),el=DomUtils.getElementById("attr-child-boosh",document),pseudos=DomUtils.getElementById("pseudos",document).children.filter(DomUtils.isTag);module.exports={Contexts:{"should be able to pass optional context":function(){expect(CSSselect(".a",document)).to.have.length(3),expect(CSSselect(".a",CSSselect("#boosh",document))).to.have.length(2)},"should not return duplicates from combinators":function(){expect(CSSselect("#boosh,#boosh",document)).to.have.length(1),expect(CSSselect("#boosh,.apples,#boosh",document)).to.have.length(1)},"byId sub-queries within context":function(){expect(CSSselect("#booshTest",CSSselect("#boosh",document))).to.have.length(1),expect(CSSselect(".a.b #booshTest",CSSselect("#boosh",document))).to.have.length(1),expect(CSSselect(".a>#booshTest",CSSselect("#boosh",document))).to.have.length(1),expect(CSSselect(">.a>#booshTest",CSSselect("#boosh",document))).to.have.length(1),expect(CSSselect("#boosh",CSSselect("#booshTest",document)).length).to.not.be.ok(),expect(CSSselect("#boosh",CSSselect("#lonelyBoosh",document)).length).to.not.be.ok()}},"CSS 1":{"get element by id":function(){var e=CSSselect("#boosh",document);expect(e[0]).to.be.ok(),expect(CSSselect("h1",document)[0]).to.be.ok()},"byId sub-queries":function(){expect(CSSselect("#boosh #booshTest",document)).to.have.length(1),expect(CSSselect(".a.b #booshTest",document)).to.have.length(1),expect(CSSselect("#boosh>.a>#booshTest",document)).to.have.length(1),expect(CSSselect(".a>#booshTest",document)).to.have.length(1)},"get elements by class":function(){expect(CSSselect("#boosh .a",document)).to.have.length(2),expect(CSSselect("#boosh div.a",document)[0]).to.be.ok(),expect(CSSselect("#boosh div",document)).to.have.length(2),expect(CSSselect("#boosh span",document)[0]).to.be.ok(),expect(CSSselect("#boosh div div",document)[0]).to.be.ok(),expect(CSSselect("a.odd",document)).to.have.length(1)},combos:function(){expect(CSSselect("#boosh div,#boosh span",document)).to.have.length(3)},"class with dashes":function(){expect(CSSselect(".class-with-dashes",document)).to.have.length(1)},"should ignore comment nodes":function(){expect(CSSselect("#boosh *",document)).to.have.length(4)},"deep messy relationships":function(){expect(CSSselect("div#fixtures > div a",document)).to.have.length(5),expect(CSSselect(".direct-descend > .direct-descend .lvl2",document)).to.have.length(1),expect(CSSselect(".direct-descend > .direct-descend div",document)).to.have.length(1),expect(CSSselect(".direct-descend > .direct-descend div",document)).to.have.length(1),expect(CSSselect("div#fixtures div ~ a div",document)).to.be.empty(),expect(CSSselect(".direct-descend > .direct-descend > .direct-descend ~ .lvl2",document)).to.be.empty()}},"CSS 2":{"get elements by attribute":function(){var e=CSSselect("#boosh div[test]",document)[0],n=DomUtils.getElementById("booshTest",document);expect(e).to.be(n),expect(CSSselect("#boosh div[test=fg]",document)[0]).to.be(n),expect(CSSselect('em[rel~="copyright"]',document)).to.have.length(1),expect(CSSselect('em[nopass~="copyright"]',document)).to.be.empty()},"should not throw error by attribute selector":function(){expect(CSSselect('[foo^="bar"]',document)).to.have.length(1)},"crazy town":function(){var e=DomUtils.getElementById("attr-test3",document);expect(CSSselect('div#attr-test3.found.you[title="whatup duders"]',document)[0]).to.be(e)}},"attribute selectors":{"[attr]":function(){var e=DomUtils.getElementById("attr-test-1",document);expect(CSSselect("#attributes div[unique-test]",document)[0]).to.be(e)},"[attr=val]":function(){var e=DomUtils.getElementById("attr-test-2",document);expect(CSSselect('#attributes div[test="two-foo"]',document)[0]).to.be(e),expect(CSSselect("#attributes div[test='two-foo']",document)[0]).to.be(e),expect(CSSselect("#attributes div[test=two-foo]",document)[0]).to.be(e)},"[attr~=val]":function(){var e=DomUtils.getElementById("attr-test-3",document);expect(CSSselect("#attributes div[test~=three]",document)[0]).to.be(e)},"[attr|=val]":function(){var e=DomUtils.getElementById("attr-test-2",document);expect(CSSselect('#attributes div[test|="two-foo"]',document)[0]).to.be(e),expect(CSSselect("#attributes div[test|=two]",document)[0]).to.be(e)},"[href=#x] special case":function(){var e=DomUtils.getElementById("attr-test-4",document);expect(CSSselect('#attributes a[href="#aname"]',document)[0]).to.be(e)},"[attr^=val]":function(){var e=DomUtils.getElementById("attr-test-2",document);expect(CSSselect("#attributes div[test^=two]",document)[0]).to.be(e)},"[attr$=val]":function(){var e=DomUtils.getElementById("attr-test-2",document);expect(CSSselect("#attributes div[test$=foo]",document)[0]).to.be(e)},"[attr*=val]":function(){var e=DomUtils.getElementById("attr-test-3",document);expect(CSSselect("#attributes div[test*=hree]",document)[0]).to.be(e)},"direct descendants":function(){expect(CSSselect("#direct-descend > .direct-descend",document)).to.have.length(2),expect(CSSselect("#direct-descend > .direct-descend > .lvl2",document)).to.have.length(3)},"sibling elements":function(){expect(CSSselect("#sibling-selector ~ .sibling-selector",document)).to.have.length(2),expect(CSSselect("#sibling-selector ~ div.sibling-selector",document)).to.have.length(2),expect(CSSselect("#sibling-selector + div.sibling-selector",document)).to.have.length(1),expect(CSSselect("#sibling-selector + .sibling-selector",document)).to.have.length(1),expect(CSSselect(".parent .oldest ~ .sibling",document)).to.have.length(4),expect(CSSselect(".parent .middle ~ .sibling",document)).to.have.length(2),expect(CSSselect(".parent .middle ~ h4",document)).to.have.length(1),expect(CSSselect(".parent .middle ~ h4.younger",document)).to.have.length(1),expect(CSSselect(".parent .middle ~ h3",document)).to.be.empty(),expect(CSSselect(".parent .middle ~ h2",document)).to.be.empty(),expect(CSSselect(".parent .youngest ~ .sibling",document)).to.be.empty(),expect(CSSselect(".parent .oldest + .sibling",document)).to.have.length(1),expect(CSSselect(".parent .middle + .sibling",document)).to.have.length(1),expect(CSSselect(".parent .middle + h4",document)).to.have.length(1),expect(CSSselect(".parent .middle + h3",document)).to.be.empty(),expect(CSSselect(".parent .middle + h2",document)).to.be.empty(),expect(CSSselect(".parent .youngest + .sibling",document)).to.be.empty()}},"element-context queries":{"detached fragments":function(){expect(CSSselect(".a span",frag)).to.have.length(1)},"byId sub-queries within detached fragment":function(){expect(CSSselect("#emem",frag)).to.have.length(1),expect(CSSselect(".d.i #emem",frag)).to.have.length(1),expect(CSSselect(".d #oooo #emem",frag)).to.have.length(1),expect(CSSselect("#oooo",CSSselect("#emem",frag)).length).to.not.be.ok(),expect(CSSselect("#sep",CSSselect("#emem",frag)).length).to.not.be.ok()},"forms can be used as contexts":function(){expect(CSSselect("*",CSSselect("form",document)[0])).to.have.length(3)}},tokenizer:{"should not get weird tokens":function(){expect(CSSselect('div .tokens[title="one"]',document)[0]).to.be(DomUtils.getElementById("token-one",document)),expect(CSSselect('div .tokens[title="one two"]',document)[0]).to.be(DomUtils.getElementById("token-two",document)),expect(CSSselect('div .tokens[title="one two three #%"]',document)[0]).to.be(DomUtils.getElementById("token-three",document)),expect(CSSselect("div .tokens[title='one two three #%'] a",document)[0]).to.be(DomUtils.getElementById("token-four",document)),expect(CSSselect('div .tokens[title="one two three #%"] a[href$=foo] div',document)[0]).to.be(DomUtils.getElementById("token-five",document))}},"interesting syntaxes":{"should parse bad selectors":function(){expect(CSSselect("#spaced-tokens    p    em    a",document).length).to.be.ok()}},"order matters":{"the order of elements return matters":function(){function e(e){return e.name.toLowerCase()}var n=CSSselect("#order-matters .order-matters",document);expect(e(n[0])).to.be("p"),expect(e(n[1])).to.be("a"),expect(e(n[2])).to.be("em"),expect(e(n[3])).to.be("b")}},"pseudo-selectors":{":contains":function(){expect(CSSselect("li:contains(humans)",document)).to.have.length(1),expect(CSSselect(":contains(humans)",document)).to.have.length(5),expect(CSSselect("*:contains(humans)",document)).to.have.length(5),expect(CSSselect("ol:contains(humans)",document)).to.have.length(1)},":not":function(){expect(CSSselect(".odd:not(div)",document)).to.have.length(1)},":first-child":function(){expect(CSSselect("#pseudos div:first-child",document)[0]).to.be(pseudos[0]),expect(CSSselect("#pseudos div:first-child",document)).to.have.length(1)},":last-child":function(){var e=DomUtils.getElementsByTagName("div",pseudos);expect(CSSselect("#pseudos div:last-child",document)[0]).to.be(e[e.length-1]),expect(CSSselect("#pseudos div:last-child",document)).to.have.length(1)},'ol > li[attr="boosh"]:last-child':function(){var e=DomUtils.getElementById("attr-child-boosh",document);expect(CSSselect('ol > li[attr="boosh"]:last-child',document)).to.have.length(1),expect(CSSselect('ol > li[attr="boosh"]:last-child',document)[0]).to.be(e)},":nth-child(odd|even|x)":function(){var e=DomUtils.getElementsByTagName("div",pseudos)[1];expect(CSSselect("#pseudos :nth-child(odd)",document)).to.have.length(4),expect(CSSselect("#pseudos div:nth-child(odd)",document)).to.have.length(3),expect(CSSselect("#pseudos div:nth-child(even)",document)).to.have.length(3),expect(CSSselect("#pseudos div:nth-child(2)",document)[0]).to.be(e)},":nth-child(expr)":function(){var e=DomUtils.getElementsByTagName("a",pseudos)[0],n=DomUtils.getElementsByTagName("div",pseudos)[4];expect(CSSselect("#pseudos :nth-child(3n+1)",document)).to.have.length(3),expect(CSSselect("#pseudos :nth-child(+3n-2)",document)).to.have.length(3),expect(CSSselect("#pseudos :nth-child(-n+6)",document)).to.have.length(6),expect(CSSselect("#pseudos :nth-child(-n+5)",document)).to.have.length(5),expect(CSSselect("#pseudos :nth-child(3n+2)",document)[1]).to.be(e),expect(CSSselect("#pseudos :nth-child(3n)",document)[1]).to.be(n)},":nth-last-child(odd|even|x)":function(){var e=DomUtils.getElementsByTagName("div",pseudos)[1];expect(CSSselect("#pseudos :nth-last-child(odd)",document)).to.have.length(4),expect(CSSselect("#pseudos div:nth-last-child(odd)",document)).to.have.length(3),expect(CSSselect("#pseudos div:nth-last-child(even)",document)).to.have.length(3),expect(CSSselect("#pseudos div:nth-last-child(6)",document)[0]).to.be(e)},":nth-last-child(expr)":function(){var e=DomUtils.getElementsByTagName("div",pseudos)[2];expect(CSSselect("#pseudos :nth-last-child(3n+1)",document)).to.have.length(3),expect(CSSselect("#pseudos :nth-last-child(3n-2)",document)).to.have.length(3),expect(CSSselect("#pseudos :nth-last-child(-n+6)",document)).to.have.length(6),expect(CSSselect("#pseudos :nth-last-child(-n+5)",document)).to.have.length(5),expect(CSSselect("#pseudos :nth-last-child(3n+2)",document)[0]).to.be(e)},":nth-of-type(expr)":function(){var e=DomUtils.getElementsByTagName("a",pseudos)[0];expect(CSSselect("#pseudos div:nth-of-type(3n+1)",document)).to.have.length(2),expect(CSSselect("#pseudos a:nth-of-type(3n+1)",document)).to.have.length(1),expect(CSSselect("#pseudos a:nth-of-type(3n+1)",document)[0]).to.be(e),expect(CSSselect("#pseudos a:nth-of-type(3n)",document)).to.be.empty(),expect(CSSselect("#pseudos a:nth-of-type(odd)",document)).to.have.length(1),expect(CSSselect("#pseudos a:nth-of-type(1)",document)).to.have.length(1)},":nth-last-of-type(expr)":function(){var e=DomUtils.getElementsByTagName("div",pseudos)[1];expect(CSSselect("#pseudos div:nth-last-of-type(3n+1)",document)).to.have.length(2),expect(CSSselect("#pseudos a:nth-last-of-type(3n+1)",document)).to.have.length(1),expect(CSSselect("#pseudos div:nth-last-of-type(5)",document)[0]).to.be(e)},":first-of-type":function(){expect(CSSselect("#pseudos a:first-of-type",document)[0]).to.be(DomUtils.getElementsByTagName("a",pseudos)[0]),expect(CSSselect("#pseudos a:first-of-type",document)).to.have.length(1)},":last-of-type":function(){var e=DomUtils.getElementsByTagName("div",pseudos);expect(CSSselect("#pseudos div:last-of-type",document)[0]).to.be(e[e.length-1]),expect(CSSselect("#pseudos div:last-of-type",document)).to.have.length(1)},":only-of-type":function(){expect(CSSselect("#pseudos a:only-of-type",document)[0]).to.be(DomUtils.getElementsByTagName("a",pseudos)[0]),expect(CSSselect("#pseudos a:first-of-type",document)).to.have.length(1)},":target":function(){location.hash="",expect(CSSselect("#pseudos:target",document)).to.be.empty(),location.hash="#pseudos",expect(CSSselect("#pseudos:target",document)).to.have.length(1),location.hash=""},"custom pseudos":function(){expect(CSSselect(":humanoid",document)).to.have.length(2)}},"is()":{"simple selectors":function(){expect(CSSselect.is(el,"li")).to.be.ok(),expect(CSSselect.is(el,"*")).to.be.ok(),expect(CSSselect.is(el,"#attr-child-boosh")).to.be.ok(),expect(CSSselect.is(el,"[attr]")).to.be.ok(),expect(CSSselect.is(el,"[attr=boosh]")).to.be.ok(),expect(CSSselect.is(el,"div")).to.not.be.ok(),expect(CSSselect.is(el,"#foo")).to.not.be.ok(),expect(CSSselect.is(el,"[foo]")).to.not.be.ok(),expect(CSSselect.is(el,"[attr=foo]")).to.not.be.ok()},"selector sequences":function(){expect(CSSselect.is(el,"li#attr-child-boosh[attr=boosh]")).to.be.ok(),expect(CSSselect.is(el,"div#attr-child-boosh[attr=boosh]")).to.not.be.ok()},"selector sequences combinators":function(){expect(CSSselect.is(el,"ol li")).to.be.ok(),expect(CSSselect.is(el,"ol>li")).to.be.ok(),expect(CSSselect.is(el,"ol>li+li")).to.be.ok(),expect(CSSselect.is(el,"ol#list li#attr-child-boosh[attr=boosh]")).to.be.ok(),expect(CSSselect.is(el,"ol#list>li#attr-child-boosh[attr=boosh]")).to.not.be.ok(),expect(CSSselect.is(el,"ol ol li#attr-child-boosh[attr=boosh]")).to.be.ok(),expect(CSSselect.is(CSSselect("#token-four",document)[0],"div#fixtures>div a")).to.be.ok()},pseudos:function(){expect(CSSselect.is(el,"li:contains(hello)")).to.be.ok(),expect(CSSselect.is(el,"li:contains(human)")).to.not.be.ok(),expect(CSSselect.is(CSSselect("#list>li",document)[2],":humanoid")).to.be.ok(),expect(CSSselect.is(CSSselect("#list>li",document)[1],":humanoid")).to.not.be.ok()}},"selecting elements in other documents":{"get element by id":function(){var e=CSSselect("#hsoob",doc);expect(e[0]).to.be.ok()},"get elements by class":function(){expect(CSSselect("#hsoob .a",doc)).to.have.length(2),expect(CSSselect("#hsoob div.a",doc)[0]).to.be.ok(),expect(CSSselect("#hsoob div",doc)).to.have.length(2),expect(CSSselect("#hsoob span",doc)[0]).to.be.ok(),expect(CSSselect("#hsoob div div",doc)[0]).to.be.ok(),expect(CSSselect("p.odd",doc)).to.have.length(1)},"complex selectors":function(){expect(CSSselect(".d ~ .sib",doc)).to.have.length(2),expect(CSSselect(".a .d + .sib",doc)).to.have.length(1),expect(CSSselect("#hsoob > div > .h",doc)).to.have.length(1),expect(CSSselect('.a .d ~ .sib[test="f g"]',doc)).to.have.length(1)},"byId sub-queries":function(){expect(CSSselect("#hsoob #spanny",doc)).to.have.length(1),expect(CSSselect(".a #spanny",doc)).to.have.length(1),expect(CSSselect(".a #booshTest #spanny",doc)).to.have.length(1)},"byId sub-queries within sub-context":function(){expect(CSSselect("#spanny",CSSselect("#hsoob",doc))).to.have.length(1),expect(CSSselect(".a #spanny",CSSselect("#hsoob",doc))).to.have.length(1),expect(CSSselect(".a #booshTest #spanny",CSSselect("#hsoob",doc))).to.have.length(1),expect(CSSselect(".a > #booshTest",CSSselect("#hsoob",doc))).to.have.length(1),expect(CSSselect("#booshTest",CSSselect("#spanny",doc)).length).to.not.be.ok(),expect(CSSselect("#booshTest",CSSselect("#lonelyHsoob",doc)).length).to.not.be.ok()}}};