Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created October 24, 2010 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterdavis/644134 to your computer and use it in GitHub Desktop.
Save walterdavis/644134 to your computer and use it in GitHub Desktop.
function SetCSSAttribute(tag, attributeName, attributeValue) {
// Sets a "CSS" attribute such as "position:absolute" in a tag value
// Passing an attribute value of null removes that attribute entirely
if (tag==null) return;
var tagField = tag['style'];
if (tagField == null){
tag['style'] = '"'+attributeName+':'+attributeValue+'"';
}else{
var tagField = tagField.toString();
var pairs = tagField.slice(1,-1).split(';');
var out = new Array();
if(attributeValue != null && tagField.indexOf(attributeName) < 0) out.push(attributeName+':'+attributeValue);
for(i in pairs){
pairs[i] = pairs[i].replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); //javascript equivalent of trim
if(pairs[i].slice(0,pairs[i].indexOf(':')) == attributeName) {
if(attributeValue != null) out.push(attributeName+':'+attributeValue);
}else{
out.push(pairs[i]);
}
}
tag['style']= fwQuote(out.join('; '));
}
}
function GetCSSAttribute(tag, attributeName) {
// Gets a "CSS" attribute such as "position:absolute" from a tag value
// returns NULL if the attribute can not be found
if (tag==null) return null;
var tagField = tag['style'];
if (tagField == null) {
return null;
}else {
var tagField = tagField.toString();
var pairs = tagField.slice(1,-1).split(';');
for(i in pairs){
pairs[i] = pairs[i].replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); //javascript equivalent of trim
if(pairs[i].slice(0,pairs[i].indexOf(':')) == attributeName) {
return pairs[i].slice(pairs[i].indexOf(':')+1,pairs[i].length);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment