Last active
August 29, 2015 14:24
-
-
Save techniq/82036ef13598396e0d9b to your computer and use it in GitHub Desktop.
Find all elements with a specific style attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getElementsWithStyle('border.*radius'); | |
getElementsWithStyle('box-shadow'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array.from polyfill | |
if (!Array.from) { | |
Array.from = function (object) { | |
'use strict'; | |
return [].slice.call(object); | |
}; | |
} | |
var styleSheets = Array.from(document.styleSheets); | |
function getElementsWithStyle(styleName) { | |
return styleSheets.reduce(function(results, styleSheet) { | |
if (styleSheet.rules) { | |
var rules = Array.from(styleSheet.rules); | |
rules.forEach(function(rule) { | |
if (rule.type === CSSRule.STYLE_RULE) { | |
var styleDeclarations = Array.from(rule.style); | |
styleDeclarations.forEach(function(styleDeclaration) { | |
if (new RegExp(styleName).test(styleDeclaration)) { | |
if (!(rule.selectorText in results)) { | |
// Only add rule once for each selector for (ex. border-.*-radius => border-top-left-radius, border-top-right-radius, etc) | |
results[rule.selectorText] = Array.from(document.querySelectorAll(rule.selectorText)) | |
} | |
} | |
}); | |
} | |
}) | |
} | |
return results; | |
}, {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment