Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rudimentary support for percentage (%) units #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions elementQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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(",");
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand All @@ -144,7 +144,7 @@
};

var processStyleSheet = function (styleSheet, force) {

if (cssRules == null) {
setCssRules();
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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];
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -403,4 +420,4 @@
// Return the em value in pixels
return value;
};
}(document, document.documentElement));
}(document, document.documentElement));