123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- goog.provide('goog.cssom');
- goog.provide('goog.cssom.CssRuleType');
- goog.require('goog.array');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.cssom.CssRuleType = {
- STYLE: 1,
- IMPORT: 3,
- MEDIA: 4,
- FONT_FACE: 5,
- PAGE: 6,
- NAMESPACE: 7
- };
- goog.cssom.getAllCssText = function(opt_styleSheet) {
- var styleSheet = opt_styleSheet || document.styleSheets;
- return (goog.cssom.getAllCss_(styleSheet, true));
- };
- goog.cssom.getAllCssStyleRules = function(opt_styleSheet) {
- var styleSheet = opt_styleSheet || document.styleSheets;
- return (
- goog.cssom.getAllCss_(styleSheet, false));
- };
- goog.cssom.getCssRulesFromStyleSheet = function(styleSheet) {
- var cssRuleList = null;
- try {
-
-
-
-
-
-
-
- cssRuleList = styleSheet.cssRules || styleSheet.rules ;
- } catch (e) {
-
- if (e.code == 15) {
-
-
-
- e.styleSheet = styleSheet;
- throw e;
- }
- }
- return cssRuleList;
- };
- goog.cssom.getAllCssStyleSheets = function(
- opt_styleSheet, opt_includeDisabled) {
- var styleSheetsOutput = [];
- var styleSheet = opt_styleSheet || document.styleSheets;
- var includeDisabled =
- goog.isDef(opt_includeDisabled) ? opt_includeDisabled : false;
-
- if (styleSheet.imports && styleSheet.imports.length) {
- for (var i = 0, n = styleSheet.imports.length; i < n; i++) {
- goog.array.extend(
- styleSheetsOutput,
- goog.cssom.getAllCssStyleSheets(
- (styleSheet.imports[i])));
- }
- } else if (styleSheet.length) {
-
-
- for (var i = 0, n = styleSheet.length; i < n; i++) {
- goog.array.extend(
- styleSheetsOutput, goog.cssom.getAllCssStyleSheets(
- (styleSheet[i])));
- }
- } else {
-
-
-
- var cssRuleList = goog.cssom.getCssRulesFromStyleSheet(
- (styleSheet));
- if (cssRuleList && cssRuleList.length) {
-
-
-
-
- for (var i = 0, n = cssRuleList.length, cssRule; i < n; i++) {
- cssRule = cssRuleList[i];
-
- if (cssRule.styleSheet) {
- goog.array.extend(
- styleSheetsOutput,
- goog.cssom.getAllCssStyleSheets(cssRule.styleSheet));
- }
- }
- }
- }
-
- if ((styleSheet.type || styleSheet.rules || styleSheet.cssRules) &&
- (!styleSheet.disabled || includeDisabled)) {
- styleSheetsOutput.push(styleSheet);
- }
- return styleSheetsOutput;
- };
- goog.cssom.getCssTextFromCssRule = function(cssRule) {
- var cssText = '';
- if (cssRule.cssText) {
-
- cssText = cssRule.cssText;
- } else if (cssRule.style && cssRule.style.cssText && cssRule.selectorText) {
-
-
-
-
- var styleCssText =
- cssRule.style.cssText
- .replace(/\s*-closure-parent-stylesheet:\s*\[object\];?\s*/gi, '')
- .replace(/\s*-closure-rule-index:\s*[\d]+;?\s*/gi, '');
- var thisCssText = cssRule.selectorText + ' { ' + styleCssText + ' }';
- cssText = thisCssText;
- }
- return cssText;
- };
- goog.cssom.getCssRuleIndexInParentStyleSheet = function(
- cssRule, opt_parentStyleSheet) {
-
- if (cssRule.style && (cssRule.style)['-closure-rule-index']) {
- return ( (cssRule.style))['-closure-rule-index'];
- }
- var parentStyleSheet =
- opt_parentStyleSheet || goog.cssom.getParentStyleSheet(cssRule);
- if (!parentStyleSheet) {
-
-
- throw Error('Cannot find a parentStyleSheet.');
- }
- var cssRuleList = goog.cssom.getCssRulesFromStyleSheet(parentStyleSheet);
- if (cssRuleList && cssRuleList.length) {
- for (var i = 0, n = cssRuleList.length, thisCssRule; i < n; i++) {
- thisCssRule = cssRuleList[i];
- if (thisCssRule == cssRule) {
- return i;
- }
- }
- }
- return -1;
- };
- goog.cssom.getParentStyleSheet = function(cssRule) {
- return cssRule.parentStyleSheet ||
- cssRule.style &&
- ( (cssRule.style))['-closure-parent-stylesheet'];
- };
- goog.cssom.replaceCssRule = function(
- cssRule, cssText, opt_parentStyleSheet, opt_index) {
- var parentStyleSheet =
- opt_parentStyleSheet || goog.cssom.getParentStyleSheet(cssRule);
- if (parentStyleSheet) {
- var index = Number(opt_index) >= 0 ?
- Number(opt_index) :
- goog.cssom.getCssRuleIndexInParentStyleSheet(cssRule, parentStyleSheet);
- if (index >= 0) {
- goog.cssom.removeCssRule(parentStyleSheet, index);
- goog.cssom.addCssRule(parentStyleSheet, cssText, index);
- } else {
- throw Error('Cannot proceed without the index of the cssRule.');
- }
- } else {
- throw Error('Cannot proceed without the parentStyleSheet.');
- }
- };
- goog.cssom.addCssRule = function(cssStyleSheet, cssText, opt_index) {
- var index = opt_index;
- if (index == undefined || index < 0) {
-
-
- var rules = goog.cssom.getCssRulesFromStyleSheet(cssStyleSheet);
- index = rules.length;
- }
- if (cssStyleSheet.insertRule) {
-
- cssStyleSheet.insertRule(cssText, index);
- } else {
-
-
-
-
- var matches = /^([^\{]+)\{([^\{]+)\}/.exec(cssText);
- if (matches.length == 3) {
- var selector = matches[1];
- var style = matches[2];
- cssStyleSheet.addRule(selector, style, index);
- } else {
- throw Error('Your CSSRule appears to be ill-formatted.');
- }
- }
- };
- goog.cssom.removeCssRule = function(cssStyleSheet, index) {
- if (cssStyleSheet.deleteRule) {
-
- cssStyleSheet.deleteRule(index);
- } else {
-
- cssStyleSheet.removeRule(index);
- }
- };
- goog.cssom.addCssText = function(cssText, opt_domHelper) {
- var domHelper = opt_domHelper || goog.dom.getDomHelper();
- var document = domHelper.getDocument();
- var cssNode = domHelper.createElement(goog.dom.TagName.STYLE);
- cssNode.type = 'text/css';
- var head = domHelper.getElementsByTagName(goog.dom.TagName.HEAD)[0];
- head.appendChild(cssNode);
- if (cssNode.styleSheet) {
-
- cssNode.styleSheet.cssText = cssText;
- } else {
-
- var cssTextNode = document.createTextNode(cssText);
- cssNode.appendChild(cssTextNode);
- }
- return cssNode;
- };
- goog.cssom.getFileNameFromStyleSheet = function(styleSheet) {
- var href = styleSheet.href;
-
-
- if (!href) {
- return null;
- }
-
- var matches = /([^\/\?]+)[^\/]*$/.exec(href);
- var filename = matches[1];
- return filename;
- };
- goog.cssom.getAllCss_ = function(styleSheet, isTextOutput) {
- var cssOut = [];
- var styleSheets = goog.cssom.getAllCssStyleSheets(styleSheet);
- for (var i = 0; styleSheet = styleSheets[i]; i++) {
- var cssRuleList = goog.cssom.getCssRulesFromStyleSheet(styleSheet);
- if (cssRuleList && cssRuleList.length) {
- var ruleIndex = 0;
- for (var j = 0, n = cssRuleList.length, cssRule; j < n; j++) {
- cssRule = cssRuleList[j];
-
- if (isTextOutput && !cssRule.href) {
- var res = goog.cssom.getCssTextFromCssRule(cssRule);
- cssOut.push(res);
- } else if (!cssRule.href) {
-
- if (cssRule.style) {
-
-
-
-
-
-
- if (!cssRule.parentStyleSheet) {
- ( (cssRule.style))[
- '-closure-parent-stylesheet'] = styleSheet;
- }
-
-
-
-
-
- ( (cssRule.style))['-closure-rule-index'] =
- isTextOutput ? undefined : ruleIndex;
- }
- cssOut.push(cssRule);
- }
- if (!isTextOutput) {
- ruleIndex++;
- }
- }
- }
- }
- return isTextOutput ? cssOut.join(' ') : cssOut;
- };
|