react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / nwmatcher / src / modules / nwmatcher-pseudos.js
81144 views/*1* Copyright (C) 2007-2015 Diego Perini2* All rights reserved.3*4* CSS3 pseudo-classes extension for NWMatcher5*6* Added capabilities:7*8* - structural pseudo-classes9*10* :root, :empty,11* :nth-child(), nth-of-type(),12* :nth-last-child(), nth-last-of-type(),13* :first-child, :last-child, :only-child14* :first-of-type, :last-of-type, :only-of-type15*16* - negation, language, target and UI element pseudo-classes17*18* :not(), :target, :lang(), :target19* :link, :visited, :active, :focus, :hover,20* :checked, :disabled, :enabled, :selected21*/2223(function(global) {2425var LINK_NODES = global.Object({26'a': 1, 'A': 1,27'area': 1, 'AREA': 1,28'link': 1, 'LINK': 129}),3031root = document.documentElement,3233contains = 'compareDocumentPosition' in root ?34function(container, element) {35return (container.compareDocumentPosition(element) & 16) == 16;36} : 'contains' in root ?37function(container, element) {38return element.nodeType == 1 && container.contains(element);39} :40function(container, element) {41while ((element = element.parentNode) && element.nodeType == 1) {42if (element === container) return true;43}44return false;45},4647isLink =48function(element) {49return element.getAttribute('href') && LINK_NODES[element.nodeName];50},5152isEmpty =53function(node) {54node = node.firstChild;55while (node) {56if (node.nodeType == 3 || node.nodeName > '@') return false;57node = node.nextSibling;58}59return true;60},6162nthElement =63function(element, last) {64var count = 1, succ = last ? 'nextSibling' : 'previousSibling';65while ((element = element[succ])) {66if (element.nodeName > '@') ++count;67}68return count;69},7071nthOfType =72function(element, last) {73var count = 1, succ = last ? 'nextSibling' : 'previousSibling', type = element.nodeName;74while ((element = element[succ])) {75if (element.nodeName == type) ++count;76}77return count;78};7980NW.Dom.Snapshot['contains'] = contains;8182NW.Dom.Snapshot['isLink'] = isLink;83NW.Dom.Snapshot['isEmpty'] = isEmpty;84NW.Dom.Snapshot['nthOfType'] = nthOfType;85NW.Dom.Snapshot['nthElement'] = nthElement;8687})(this);8889NW.Dom.registerSelector(90'nwmatcher:spseudos',91/^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s*(even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\))?(.*)/i,92(function(global) {9394return function(match, source) {9596var a, n, b, status = true, test, type;9798switch (match[1]) {99100case 'root':101if (match[3])102source = 'if(e===h||s.contains(h,e)){' + source + '}';103else104source = 'if(e===h){' + source + '}';105break;106107case 'empty':108source = 'if(s.isEmpty(e)){' + source + '}';109break;110111default:112if (match[1] && match[2]) {113114if (match[2] == 'n') {115source = 'if(e!==h){' + source + '}';116break;117} else if (match[2] == 'even') {118a = 2;119b = 0;120} else if (match[2] == 'odd') {121a = 2;122b = 1;123} else {124b = ((n = match[2].match(/(-?\d+)$/)) ? global.parseInt(n[1], 10) : 0);125a = ((n = match[2].match(/(-?\d*)n/i)) ? global.parseInt(n[1], 10) : 0);126if (n && n[1] == '-') a = -1;127}128test = a > 1 ?129(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :130'n>=' + b + '&&(n-(' + b + '))%' + a + '==0' : a < -1 ?131(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :132'n<=' + b + '&&(n-(' + b + '))%' + a + '==0' : a === 0 ?133'n==' + b : a == -1 ? 'n<=' + b : 'n>=' + b;134source =135'if(e!==h){' +136'n=s[' + (/-of-type/i.test(match[1]) ? '"nthOfType"' : '"nthElement"') + ']' +137'(e,' + (/last/i.test(match[1]) ? 'true' : 'false') + ');' +138'if(' + test + '){' + source + '}' +139'}';140141} else if (match[1]) {142143a = /first/i.test(match[1]) ? 'previous' : 'next';144n = /only/i.test(match[1]) ? 'previous' : 'next';145b = /first|last/i.test(match[1]);146type = /-of-type/i.test(match[1]) ? '&&n.nodeName!==e.nodeName' : '&&n.nodeName<"@"';147source = 'if(e!==h){' +148( 'n=e;while((n=n.' + a + 'Sibling)' + type + ');if(!n){' + (b ? source :149'n=e;while((n=n.' + n + 'Sibling)' + type + ');if(!n){' + source + '}') + '}' ) + '}';150151} else {152153status = false;154155}156break;157}158159return global.Object({160'source': source,161'status': status162});163164};165166})(this));167168NW.Dom.registerSelector(169'nwmatcher:dpseudos',170/^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(([^()]*|.*)\))?(.*)/i,171(function(global) {172173var doc = global.document,174Config = NW.Dom.Config,175Tokens = NW.Dom.Tokens,176177reTrimSpace = global.RegExp('^\\s+|\\s+$', 'g'),178179reSimpleNot = global.RegExp('^((?!:not)' +180'(' + Tokens.prefixes + '|' + Tokens.identifier +181'|\\([^()]*\\))+|\\[' + Tokens.attributes + '\\])$');182183return function(match, source) {184185var expr, status = true, test;186187switch (match[1].match(/^\w+/)[0]) {188189case 'not':190expr = match[3].replace(reTrimSpace, '');191if (Config.SIMPLENOT && !reSimpleNot.test(expr)) {192NW.Dom.emit('Negation pseudo-class only accepts simple selectors "' + match.join('') + '"');193} else {194if ('compatMode' in doc) {195source = 'if(!' + NW.Dom.compile(expr, '', false) + '(e,s,r,d,h,g)){' + source + '}';196} else {197source = 'if(!s.match(e, "' + expr.replace(/\x22/g, '\\"') + '",g)){' + source +'}';198}199}200break;201202case 'checked':203source = 'if((typeof e.form!=="undefined"&&(/^(?:radio|checkbox)$/i).test(e.type)&&e.checked)' +204(Config.USE_HTML5 ? '||(/^option$/i.test(e.nodeName)&&(e.selected||e.checked))' : '') +205'){' + source + '}';206break;207208case 'disabled':209source = 'if(((typeof e.form!=="undefined"' +210(Config.USE_HTML5 ? '' : '&&!(/^hidden$/i).test(e.type)') +211')||s.isLink(e))&&e.disabled===true){' + source + '}';212break;213214case 'enabled':215source = 'if(((typeof e.form!=="undefined"' +216(Config.USE_HTML5 ? '' : '&&!(/^hidden$/i).test(e.type)') +217')||s.isLink(e))&&e.disabled===false){' + source + '}';218break;219220case 'lang':221test = '';222if (match[2]) test = match[2].substr(0, 2) + '-';223source = 'do{(n=e.lang||"").toLowerCase();' +224'if((n==""&&h.lang=="' + match[2].toLowerCase() + '")||' +225'(n&&(n=="' + match[2].toLowerCase() +226'"||n.substr(0,3)=="' + test.toLowerCase() + '")))' +227'{' + source + 'break;}}while((e=e.parentNode)&&e!==g);';228break;229230case 'target':231source = 'if(e.id==d.location.hash.slice(1)){' + source + '}';232break;233234case 'link':235source = 'if(s.isLink(e)&&!e.visited){' + source + '}';236break;237238case 'visited':239source = 'if(s.isLink(e)&&e.visited){' + source + '}';240break;241242case 'active':243source = 'if(e===d.activeElement){' + source + '}';244break;245246case 'hover':247source = 'if(e===d.hoverElement){' + source + '}';248break;249250case 'focus':251source = 'hasFocus' in doc ?252'if(e===d.activeElement&&d.hasFocus()&&(e.type||e.href||typeof e.tabIndex=="number")){' + source + '}' :253'if(e===d.activeElement&&(e.type||e.href)){' + source + '}';254break;255256case 'selected':257source = 'if(/^option$/i.test(e.nodeName)&&(e.selected||e.checked)){' + source + '}';258break;259260default:261status = false;262break;263}264265return global.Object({266'source': source,267'status': status268});269270};271272})(this));273274275