diff --git a/src/ElementQueries.js b/src/ElementQueries.js index 6e7c357..69cfa23 100755 --- a/src/ElementQueries.js +++ b/src/ElementQueries.js @@ -179,19 +179,30 @@ } } - var regex = /,?([^,\n]*?)\[[\s\t]*?(min|max)-(width|height)[\s\t]*?[~$\^]?=[\s\t]*?"([^"]*?)"[\s\t]*?]([^\n\s\{]*?)/mgi; + var queryRegex = /\[(min|max)\-(width|height)[\b\~\|\^\$\*]=\"(\w*)\"/mgi; + /** + * @param {String} css + */ + function extractQuery(selector, css) { + var match; + while (null !== (match = queryRegex.exec(css))) { + if (3 < match.length) { + queueQuery(selector, match[1], match[2], match[3]); + } + } + } + + var selectorRegex = /(?:^|)([\.\#\[\]\-\w]*)\[(?:min|max)\-(?:width|height)[\~\|\^\$\*]?=\"\w*\"\](?:[\s,]|[^\]]+\]?([\.\#\-\w\~\|\^\$\*\=\"]+))/mgi; /** * @param {String} css */ - function extractQuery(css) { + function extractSelector(css) { var match; - var smatch; css = css.replace(/'/g, '"'); - while (null !== (match = regex.exec(css))) { - if (5 < match.length) { - smatch = match[1] || match[5] || smatch; - queueQuery(smatch, match[2], match[3], match[4]); + while (null !== (match = selectorRegex.exec(css))) { + if (1 < match.length) { + extractQuery(match[1] || match[2], match[0]); } } } @@ -207,16 +218,16 @@ if ('string' === typeof rules) { rules = rules.toLowerCase(); if (-1 !== rules.indexOf('min-width') || -1 !== rules.indexOf('max-width')) { - extractQuery(rules); + extractSelector(rules); } } else { for (var i = 0, j = rules.length; i < j; i++) { if (1 === rules[i].type) { selector = rules[i].selectorText || rules[i].cssText; if (-1 !== selector.indexOf('min-height') || -1 !== selector.indexOf('max-height')) { - extractQuery(selector); + extractSelector(selector); }else if(-1 !== selector.indexOf('min-width') || -1 !== selector.indexOf('max-width')) { - extractQuery(selector); + extractSelector(selector); } } else if (4 === rules[i].type) { readRules(rules[i].cssRules || rules[i].rules); @@ -235,7 +246,7 @@ this.withTracking = withTracking; for (var i = 0, j = document.styleSheets.length; i < j; i++) { try { - readRules(document.styleSheets[i].cssText || document.styleSheets[i].cssRules || document.styleSheets[i].rules); + readRules(document.styleSheets[i].cssRules || document.styleSheets[i].rules || document.styleSheets[i].cssText); } catch(e) { if (e.name !== 'SecurityError') { throw e; @@ -304,39 +315,4 @@ ElementQueries.instance.init(ElementQueries.withTracking); }; - var domLoaded = function (callback) { - /* Internet Explorer */ - /*@cc_on - @if (@_win32 || @_win64) - document.write('<\/script>'); - document.getElementById('ieScriptLoad').onreadystatechange = function() { - if (this.readyState == 'complete') { - callback(); - } - }; - @end @*/ - /* Mozilla, Chrome, Opera */ - if (document.addEventListener) { - document.addEventListener('DOMContentLoaded', callback, false); - } - /* Safari, iCab, Konqueror */ - if (/KHTML|WebKit|iCab/i.test(navigator.userAgent)) { - var DOMLoadTimer = setInterval(function () { - if (/loaded|complete/i.test(document.readyState)) { - callback(); - clearInterval(DOMLoadTimer); - } - }, 10); - } - /* Other web browsers */ - window.onload = callback; - }; - - if (window.addEventListener) { - window.addEventListener('load', ElementQueries.init, false); - } else { - window.attachEvent('onload', ElementQueries.init); - } - domLoaded(ElementQueries.init); - })();