1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
var WebDeveloper = WebDeveloper || {};
WebDeveloper.CSS = WebDeveloper.CSS || {};
// Formats a style property
WebDeveloper.CSS.formatStyleProperty = function(styleProperty)
{
// Switch on the style property
switch(styleProperty)
{
case "margin-bottom-value":
return "margin-bottom";
case "margin-left-value":
return "margin-left";
case "margin-right-value":
return "margin-right";
case "margin-top-value":
return "margin-top";
case "padding-bottom-value":
return "padding-bottom";
case "padding-left-value":
return "padding-left";
case "padding-right-value":
return "padding-right";
case "padding-top-value":
return "padding-top";
case "-x-background-x-position":
return "background-x-position";
case "-x-background-y-position":
return "background-y-position";
}
return styleProperty;
};
// Formats a style value
WebDeveloper.CSS.formatStyleValue = function(styleValue)
{
// If the style value is set
if(styleValue)
{
var rgbRegularExpression = new RegExp("rgb\\((\\d{1,3}),\\s(\\d{1,3}),\\s(\\d{1,3})\\)", "gi");
var styleValueColor = rgbRegularExpression.exec(styleValue);
// If the style value is a color
if(styleValueColor)
{
var blue = parseInt(styleValueColor[3], 10).toString(16);
var green = parseInt(styleValueColor[2], 10).toString(16);
var red = parseInt(styleValueColor[1], 10).toString(16);
// If the blue color is only 1 character long
if(blue.length == 1)
{
blue = "0" + blue;
}
// If the green color is only 1 character long
if(green.length == 1)
{
green = "0" + green;
}
// If the red color is only 1 character long
if(red.length == 1)
{
red = "0" + red;
}
return "#" + red + green + blue;
}
}
return styleValue;
};
// Returns an array of style sheets imported in the given style sheet
WebDeveloper.CSS.getImportedStyleSheets = function(styleSheet)
{
var styleSheets = [];
// If the style sheet is set
if(styleSheet)
{
var cssRules = styleSheet.cssRules;
// If there are CSS rules
if(cssRules)
{
var cssRule = null;
var importedStyleSheet = null;
// Loop through the style sheet rules
for(var i = 0, l = cssRules.length; i < l; i++)
{
cssRule = cssRules[i];
// If this is an import rule
if(cssRule.type == 3)
{
importedStyleSheet = cssRule.styleSheet;
// If this style sheet is valid
if(WebDeveloper.CSS.isValidStyleSheet(importedStyleSheet))
{
styleSheets.push(importedStyleSheet.href);
styleSheets = styleSheets.concat(WebDeveloper.CSS.getImportedStyleSheets(importedStyleSheet));
}
}
}
}
}
return styleSheets;
};
// Returns true if this is an alternate style sheet
WebDeveloper.CSS.isAlternateStyleSheet = function(styleSheet)
{
// If the style sheet is set
if(styleSheet)
{
var ownerNode = styleSheet.ownerNode;
// If the owner node is set
if(ownerNode)
{
// If the owner node is a processing instruction
if(ownerNode.nodeType == Node.PROCESSING_INSTRUCTION_NODE)
{
// If the processing instruction data contains alternate="yes"
if(ownerNode.data.indexOf('alternate="yes"') != -1)
{
return true;
}
}
else if(ownerNode.hasAttribute("rel") && ownerNode.getAttribute("rel").toLowerCase() == "alternate stylesheet")
{
return true;
}
}
}
return false;
};
// Returns true if this style sheet is for this media type
WebDeveloper.CSS.isMediaStyleSheet = function(styleSheet, mediaType)
{
// If the style sheet and media type are set
if(styleSheet && mediaType)
{
var media = styleSheet.media;
var mediaLength = media.length;
var styleSheetMediaType = null;
// If there is no media and the match media type is screen
if(mediaLength === 0 && mediaType == "screen")
{
return true;
}
// Loop through the media
for(var i = 0; i < mediaLength; i++)
{
styleSheetMediaType = media.item(i).toLowerCase();
// If the style sheet media type is all or matches the media type
if(styleSheetMediaType == "all" || styleSheetMediaType == mediaType)
{
return true;
}
}
}
return false;
};
// Returns true if this is a valid rule style
WebDeveloper.CSS.isValidRuleStyle = function(ruleStyles, ruleStyle)
{
// If the rule style is set
if(ruleStyle)
{
// If the rule style is an invalid rule style
if(ruleStyle.indexOf("-moz-") === 0 || ruleStyle.indexOf("-x-") === 0 || ruleStyles.getPropertyValue(ruleStyle).indexOf("-moz-") === 0 || ((ruleStyle.indexOf("-ltr-source") !== 0 || ruleStyle.indexOf("-rtl-source") !== 0) && ruleStyles.getPropertyValue(ruleStyle) === "physical"))
{
return false;
}
return true;
}
return false;
};
// Returns true if this is a valid style sheet
WebDeveloper.CSS.isValidStyleSheet = function(styleSheet)
{
// If the style sheet is set
if(styleSheet)
{
var styleSheetHref = styleSheet.href;
// If the style sheet href is not set or this is not a chrome or data style sheet
if(!styleSheetHref || (styleSheetHref.indexOf("about:") !== 0 && styleSheetHref.indexOf("chrome://") !== 0 && styleSheetHref.indexOf("chrome-extension://") !== 0 && styleSheetHref.indexOf("data:") !== 0 && styleSheetHref.indexOf("resource://") !== 0))
{
return true;
}
}
return false;
};
// Toggles all the style sheets in a document
WebDeveloper.CSS.toggleAllStyleSheets = function(disable, contentDocument)
{
var styleSheet = null;
var styleSheets = contentDocument.styleSheets;
// Loop through the style sheets
for(var i = 0, l = styleSheets.length; i < l; i++)
{
styleSheet = styleSheets[i];
// If this is a valid style sheet and is not an alternate style sheet or style sheets are being disabled
if(WebDeveloper.CSS.isValidStyleSheet(styleSheet) && (!WebDeveloper.CSS.isAlternateStyleSheet(styleSheet) || disable))
{
styleSheet.disabled = disable;
}
}
};
|