Advertisement
richarduie

CssParser.js

Mar 15th, 2019
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.49 KB | None | 0 0
  1. function CssParser()
  2. {
  3.     this.getSheetByHref = function(href) {
  4.     // for external style sheet declared in <link> element, get definitions and return CSSStyleSheet object
  5.         var sheet = null;                       // initialize to indicate failure
  6.         var sheets = document.styleSheets;      // get array of all <link> and <style> objects
  7.         var last = sheets.length;               // how many
  8.         for (var i = 0; i < last; i++) {
  9.             var s = sheets[i];                  // current sheet
  10.             var h = s.href;                     // attempt to get href attribute
  11.             // if href matches, parse filename and keep only that part of url
  12.             if (h) h = h.substring(1 + s.href.lastIndexOf('/'));
  13.             if (href == h) {        // if href of this sheet is that requested...
  14.                 sheet = s;          // assign current sheet as CSSStyleSheet object
  15.                 break;              // quit immediately, since target was found
  16.             }
  17.         }
  18.         return sheet;   // return CSSStyleSheet (or null, if search failed)
  19.     };
  20.     this.getRules = function(sheet) {
  21.         var rules = null;       // initialize to indicate failure
  22.         if (null != sheet) {
  23.             // if valid sheet, get rules by browser
  24.             rules = (sheet.cssRules)?sheets.cssRules:sheets.rules
  25.         }
  26.         return rules;   // return rules (or null, if search failed)
  27.     };
  28.     this.getRule = function(selector, sheet) {
  29.         // get rule, if sheet declares one for selector
  30.         var rule = null;                        // initialize to indicate failure
  31.         var rules = getRules(sheet);            // get rules for sheet
  32.         if (null != rules) {
  33.             var last = rules.length;            // how many
  34.             for (var i = 0; i < last; i++) {    // for each rule
  35.                 // if selector matches capture and quit searching
  36.                 if (rules[i].selectorText == selector) rule = rules[i]
  37.                 break;
  38.             }
  39.         }
  40.         return rule;    // return rule (or null, if search failed)
  41.     };
  42.     this.getAttributeValue = function(attribute, selector, sheet) {
  43.         // get value of style attribute, if sheet declares one for selector
  44.         var value = null;       // initialize to indicate failure
  45.         // get value of attribute, if sheet declares one for selector
  46.         var rule = getRule(selector, sheet);
  47.         if (null != rule && rule.style && rule.style.cssText) {
  48.             var attrValuePairs = rule.style.cssText;
  49.             var startIdx = attrValuePairs.lastIndexOf(attribute);
  50.             if (-1 != startIdx) {
  51.                 var avp = attrValuePairs.substring(startIdx)
  52.                 var stopIdx = avp.indexOf(';');
  53.                 if (-1 != stopIdx) {
  54.                     var avp = avp.substring(0, avp.indexOf(stopIdx));
  55.                     if (-1 != avp.indexOf(':')) {
  56.                         value = avp.split(':')[1];
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         return value;   // return rule (or null, if search failed)
  62.     };
  63. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement