stylesheet.js
Summary
Zapatec StyleSheet class definition. Used to manipulate with
style sheets.
Copyright (c) 2004-2006 by Zapatec, Inc.
http://www.zapatec.com
1700 MLK Way, Berkeley, California,
94709, U.S.A.
All rights reserved.
if (typeof Zapatec == 'undefined') {
Zapatec = function() {};
}
Zapatec.StyleSheet = function(bUseLast) {
if (bUseLast) {
if (document.createStyleSheet) {
if (document.styleSheets.length) {
this.styleSheet = document.styleSheets[document.styleSheets.length - 1];
}
} else {
var aStyleSheets = document.getElementsByTagName('style');
if (aStyleSheets.length) {
this.styleSheet = aStyleSheets[aStyleSheets.length - 1];
}
}
}
if (!this.styleSheet) {
if (document.createStyleSheet) {
try {
this.styleSheet = document.createStyleSheet();
} catch(oException) {
this.styleSheet = document.styleSheets[document.styleSheets.length - 1];
};
} else {
this.styleSheet = document.createElement('style');
this.styleSheet.type = 'text/css';
var oHead = document.getElementsByTagName('head')[0];
if (!oHead) {
oHead = document.documentElement;
}
if (oHead) {
oHead.appendChild(this.styleSheet);
}
}
}
};
Zapatec.StyleSheet.prototype.addRule = function(strSelector, strDeclarations) {
if (!this.styleSheet) {
return;
}
if (document.createStyleSheet) {
this.styleSheet.cssText += strSelector + ' { ' + strDeclarations + ' }';
} else {
this.styleSheet.appendChild(
document.createTextNode(strSelector + ' { ' + strDeclarations + ' }')
);
}
};
Zapatec.StyleSheet.prototype.removeRules = function() {
if (!this.styleSheet) {
return;
}
if (document.createStyleSheet) {
var iRules = this.styleSheet.rules.length;
for (var iRule = 0; iRule < iRules; iRule++) {
this.styleSheet.removeRule();
}
} else {
while (this.styleSheet.firstChild) {
this.styleSheet.removeChild(this.styleSheet.firstChild);
}
}
};
Zapatec.StyleSheet.prototype.addParse = function(strStyleSheet) {
var arrClean = [];
var arrTokens = strStyleSheet.split('/*');
for (var iTok = 0; iTok < arrTokens.length; iTok++) {
var arrTails = arrTokens[iTok].split('*/');
arrClean.push(arrTails[arrTails.length - 1]);
}
strStyleSheet = arrClean.join('');
strStyleSheet = strStyleSheet.replace(/@[^{]*;/g, '');
var arrStyles = strStyleSheet.split('}');
for (var iStl = 0; iStl < arrStyles.length; iStl++) {
var arrRules = arrStyles[iStl].split('{');
if (arrRules[0] && arrRules[1]) {
var arrSelectors = arrRules[0].split(',');
for (var iSel = 0; iSel < arrSelectors.length; iSel++) {
this.addRule(arrSelectors[iSel], arrRules[1]);
}
}
}
};
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007