Skip to content

Instantly share code, notes, and snippets.

@think49
Created October 17, 2010 08:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/630663 to your computer and use it in GitHub Desktop.
Save think49/630663 to your computer and use it in GitHub Desktop.
matchesCssRule.js : 対象の要素が cssRules で定義済みのセレクタにマッチするとき、true を返します。
// matchesCssRule.js
// Array.forEach(contextObject, callbackfn [, thisArg])
if (typeof Array.forEach !== 'function') {
Array.forEach = (function (forEach) {
return function (contextObject, callbackfn /*, thisArg*/) {
return forEach.call(contextObject, callbackfn, arguments[2]);
};
})(
// [ES5] Array.prototype.forEach ( callbackfn [, thisArg] )
Array.prototype.forEach ||
function (callbackfn /*, thisArg*/) {
var thisArg, O, k, len;
if ('function' !== typeof callbackfn) {
throw new TypeError(callbackfn + ' is not a function');
}
O = Object(this); // ToObject(this)
len = Number(O.length); // ToUint32(lenValue)
thisArg = arguments[1];
if (arguments.length < 2) { // for "strict mode"
thisArg = this;
}
for (k = 0, len = O.length; k < len; k++) {
if (k in O) {
callbackfn.call(thisArg, O[k], k, O);
}
}
}
);
}
// Array.some(contextObject, callbackfn [, thisArg])
if (typeof Array.some !== 'function') {
Array.some = (function (some) {
return function (contextObject, callbackfn /*, thisArg*/) {
return some.call(contextObject, callbackfn, arguments[2]);
};
})(
// [ES5] Array.prototype.some ( callbackfn [ , thisArg ] )
Array.prototype.some ||
function (callbackfn /*, thisArg*/) {
var thisArg, O, testResult, k, len;
if (typeof callbackfn !== 'function') {
throw new TypeError(callbackfn + ' is not a function');
}
O = Object(this); // ToObject(this)
len = Number(O.length); // ToUint32(lenValue)
thisArg = arguments[1];
if (arguments.length < 2) { // for "strict mode"
thisArg = this;
}
for (k = 0; k < len; k++) {
if (k in O) {
testResult = callbackfn.call(thisArg, O[k], k, O);
if (testResult) {
return true;
}
}
}
return false;
}
);
}
// Element.prototype.matchesSelector(selectors [, refNodes])
if (typeof Element === 'function' || typeof Element === 'object') {
(function () {
if (!('matchesSelector' in this) && ('webkitMatchesSelector' in this || 'mozMatchesSelector' in this)) {
this.matchesSelector = this.webkitMatchesSelector || this.mozMatchesSelector;
}
}).call(Element.prototype);
}
// for loop ver.
function matchesCssRule1 (element, property) {
var styleSheets, styleSheet, cssRules, cssRule, result, i, l, j, m;
result = false;
styleSheets = element.ownerDocument.styleSheets;
for (i = 0, l = styleSheets.length; i < l; i++) {
styleSheet = styleSheets[i];
cssRules = styleSheet.cssRules || styleSheet.rules;
for (j = 0, m = cssRules.length; j < m; j++) {
cssRule = cssRules[j];
if (element.matchesSelector(cssRule.selectorText)) { // selector match
if (property) {
if (cssRule.style[property]) { // property match
result = true;
break;
}
} else {
result = true;
break;
}
}
}
if (result) {
break;
}
}
return result;
}
// Array.forEach ver.
function matchesCssRule2 (element, property) {
var forEach, result;
forEach = Array.forEach;
result = false;
forEach(element.ownerDocument.styleSheets, function (styleSheet) {
forEach(styleSheet.cssRules || styleSheet.rules, function (cssRule) {
if (element.matchesSelector(cssRule.selectorText)) { // selector match
if (property) {
if (cssRule.style[property]) { // property match
result = true;
}
} else {
result = true;
}
}
});
});
return result;
}
// Array.some ver.
function matchesCssRule3 (element, property) {
var some = Array.some;
return some(element.ownerDocument.styleSheets, function (styleSheet) {
return some(styleSheet.cssRules || styleSheet.rules, function (cssRule) {
if (element.matchesSelector(cssRule.selectorText)) { // selector match
if (property) {
if (cssRule.style[property]) { // property match
return true;
}
} else {
return true;
}
}
});
});
}
@think49
Copy link
Author

think49 commented Oct 17, 2010

下記URLで解説しています。

matchesCssRule.js
http://vird2002.s8.xrea.com/javascript/matchesCssRule.html


OKWaveで回答しました。

jQueryでheightが指定されていないエレメントを得る方法 | OKWave
http://okwave.jp/qa/q6253844.html


gtlt さんにアドバイスをいただきました。

2010-10-12 - babu_babu_babooのごみ箱
http://d.hatena.ne.jp/babu_babu_baboo/20101012#c1287310758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment