Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function CssParser()
- {
- this.getSheetByHref = function(href) {
- // for external style sheet declared in <link> element, get definitions and return CSSStyleSheet object
- var sheet = null; // initialize to indicate failure
- var sheets = document.styleSheets; // get array of all <link> and <style> objects
- var last = sheets.length; // how many
- for (var i = 0; i < last; i++) {
- var s = sheets[i]; // current sheet
- var h = s.href; // attempt to get href attribute
- // if href matches, parse filename and keep only that part of url
- if (h) h = h.substring(1 + s.href.lastIndexOf('/'));
- if (href == h) { // if href of this sheet is that requested...
- sheet = s; // assign current sheet as CSSStyleSheet object
- break; // quit immediately, since target was found
- }
- }
- return sheet; // return CSSStyleSheet (or null, if search failed)
- };
- this.getRules = function(sheet) {
- var rules = null; // initialize to indicate failure
- if (null != sheet) {
- // if valid sheet, get rules by browser
- rules = (sheet.cssRules)?sheets.cssRules:sheets.rules
- }
- return rules; // return rules (or null, if search failed)
- };
- this.getRule = function(selector, sheet) {
- // get rule, if sheet declares one for selector
- var rule = null; // initialize to indicate failure
- var rules = getRules(sheet); // get rules for sheet
- if (null != rules) {
- var last = rules.length; // how many
- for (var i = 0; i < last; i++) { // for each rule
- // if selector matches capture and quit searching
- if (rules[i].selectorText == selector) rule = rules[i]
- break;
- }
- }
- return rule; // return rule (or null, if search failed)
- };
- this.getAttributeValue = function(attribute, selector, sheet) {
- // get value of style attribute, if sheet declares one for selector
- var value = null; // initialize to indicate failure
- // get value of attribute, if sheet declares one for selector
- var rule = getRule(selector, sheet);
- if (null != rule && rule.style && rule.style.cssText) {
- var attrValuePairs = rule.style.cssText;
- var startIdx = attrValuePairs.lastIndexOf(attribute);
- if (-1 != startIdx) {
- var avp = attrValuePairs.substring(startIdx)
- var stopIdx = avp.indexOf(';');
- if (-1 != stopIdx) {
- var avp = avp.substring(0, avp.indexOf(stopIdx));
- if (-1 != avp.indexOf(':')) {
- value = avp.split(':')[1];
- }
- }
- }
- }
- return value; // return rule (or null, if search failed)
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement