diff --git a/elementQuery.js b/elementQuery.js index 1776c9c..4fb9969 100644 --- a/elementQuery.js +++ b/elementQuery.js @@ -26,7 +26,7 @@ if (selector != "") { var parts; if (!number && !value) { - parts = /^([0-9]*.?[0-9]+)(px|em)$/.exec(pair) + parts = /^([0-9]*.?[0-9]+)(px|em|%)$/.exec(pair) if (parts != null) { number = Number(parts[1]); if (number + "" != "NaN") { @@ -78,7 +78,7 @@ if (selectorText) { - var regex = /(\[(min\-width|max\-width|min\-height|max\-height)\~\=(\'|\")([0-9]*.?[0-9]+)(px|em)(\'|\")\])(\[(min\-width|max\-width|min\-height|max\-height)\~\=(\'|\")([0-9]*.?[0-9]+)(px|em)(\'|\")\])?/gi; + var regex = /(\[(min\-width|max\-width|min\-height|max\-height)\~\=(\'|\")([0-9]*.?[0-9]+)(px|em|%)(\'|\")\])(\[(min\-width|max\-width|min\-height|max\-height)\~\=(\'|\")([0-9]*.?[0-9]+)(px|em|%)(\'|\")\])?/gi; // Split out the full selectors separated by a comma ',' var selectors = selectorText.split(","); @@ -94,7 +94,7 @@ // result[2] = min-width|max-width|min-height|max-height // result[4] = number - // result[5] = px|em + // result[5] = px|em|% // result[7] = has another // Ensure that it contains a valid numeric value to compare against @@ -108,7 +108,7 @@ // Append second half of the selector tail = selectors[i].substring(result.index + result[1].length); if (tail.length > 0) { - + t = tail.indexOf(" "); if (t != 0) { if (t > 0) { @@ -117,7 +117,7 @@ } // Remove any sibling element queries - tail = tail.replace(/(\[(min\-width|max\-width|min\-height|max\-height)\~\=(\'|\")([0-9]*.?[0-9]+)(px|em)(\'|\")\])/gi, ""); + tail = tail.replace(/(\[(min\-width|max\-width|min\-height|max\-height)\~\=(\'|\")([0-9]*.?[0-9]+)(px|em|%)(\'|\")\])/gi, ""); selector += tail; } } @@ -144,7 +144,7 @@ }; var processStyleSheet = function (styleSheet, force) { - + if (cssRules == null) { setCssRules(); } @@ -205,7 +205,7 @@ var val = trim(value); if (val != "") { var cur = clean(element, attr); - + if (cur.indexOf(" " + val + " ") < 0) { // Add the value if its not already there element.setAttribute(attr, trim(cur + val)); @@ -265,7 +265,7 @@ // For each min|max-width|height string for (j in queryData[i]) { - // For each number px|em value pair + // For each number px|em|% value pair for (k in queryData[i][j]) { val = queryData[i][j][k][0]; @@ -277,11 +277,28 @@ /* NOTE: Using offsetWidth/Height so an element can be adjusted when it reaches a specific size. /* For Nested queries scrollWidth/Height or clientWidth/Height may sometime be desired but are not supported. */ - - if ((j == "min-width" && element.offsetWidth >= val) || - (j == "max-width" && element.offsetWidth <= val) || - (j == "min-height" && element.offsetHeight >= val) || - (j == "max-height" && element.offsetHeight <= val)) { + var percentWidth = (( element.offsetWidth / element.parentNode.offsetWidth ) * 100); + var percentHeight = (( element.offsetHeight / element.parentNode.offsetHeight ) * 100); + + if ( (queryData[i][j][k][1] == "px" || queryData[i][j][k][1] == "em" ) && + ( + (j == "min-width" && element.offsetWidth >= val) || + (j == "max-width" && element.offsetWidth <= val) || + (j == "min-height" && element.offsetHeight >= val) || + (j == "max-height" && element.offsetHeight <= val) + ) + ) { + // Add matching attr value + addTo(element, j, k); + } + else if (queryData[i][j][k][1] == "%" && + ( + (j == "min-width" && percentWidth >= val) || + (j == "max-width" && percentWidth <= val) || + (j == "min-height" && percentHeight >= val) || + (j == "max-height" && percentHeight <= val) + ) + ) { // Add matching attr value addTo(element, j, k); } @@ -336,7 +353,7 @@ // For each min|max-width|height string for (j in queryData[i]) { - // For each number px|em value pair + // For each number px|em|% value pair for (k in queryData[i][j]) { if (data[i] === undefined) { @@ -403,4 +420,4 @@ // Return the em value in pixels return value; }; -}(document, document.documentElement)); \ No newline at end of file +}(document, document.documentElement));