misc cleaned up sanitize inconsistent indentation

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2469 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Bruno Heridet
2013-02-25 12:39:27 +00:00
parent 71586aa5fe
commit 2c1c5da6de

View File

@@ -113,34 +113,36 @@ $.each(svgWhiteList_, function(elt, atts){
// Parameters: // Parameters:
// node - The DOM element to be checked (we'll also check its children) // node - The DOM element to be checked (we'll also check its children)
svgedit.sanitize.sanitizeSvg = function(node) { svgedit.sanitize.sanitizeSvg = function(node) {
// Cleanup text nodes // Cleanup text nodes
if (node.nodeType == 3) { // 3 == TEXT_NODE if (node.nodeType == 3) { // 3 == TEXT_NODE
// Trim whitespace // Trim whitespace
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, ''); node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, '');
// Remove if empty // Remove if empty
if(node.nodeValue.length == 0) node.parentNode.removeChild(node); if (node.nodeValue.length === 0) {
node.parentNode.removeChild(node);
}
} }
// We only care about element nodes. // We only care about element nodes.
// Automatically return for all non-element nodes, // Automatically return for all non-element nodes, such as comments, etc.
// such as comments, etc. if (node.nodeType != 1) { // 1 == ELEMENT_NODE
if (node.nodeType != 1) { // 1 == ELEMENT_NODE return;
return; }
}
var doc = node.ownerDocument; var doc = node.ownerDocument;
var parent = node.parentNode; var parent = node.parentNode;
// can parent ever be null here? I think the root node's parent is the document... // can parent ever be null here? I think the root node's parent is the document...
if (!doc || !parent) return; if (!doc || !parent) {
return;
}
var allowedAttrs = svgWhiteList_[node.nodeName]; var allowedAttrs = svgWhiteList_[node.nodeName];
var allowedAttrsNS = svgWhiteListNS_[node.nodeName]; var allowedAttrsNS = svgWhiteListNS_[node.nodeName];
// if this element is supported, sanitize it // if this element is supported, sanitize it
if (allowedAttrs != undefined) { if (typeof allowedAttrs !== 'undefined') {
var se_attrs = []; var seAttrs = [];
var i = node.attributes.length; var i = node.attributes.length;
while (i--) { while (i--) {
// if the attribute is not in our whitelist, then remove it // if the attribute is not in our whitelist, then remove it
@@ -156,16 +158,16 @@ svgedit.sanitize.sanitizeSvg = function(node) {
{ {
// TODO(codedread): Programmatically add the se: attributes to the NS-aware whitelist. // TODO(codedread): Programmatically add the se: attributes to the NS-aware whitelist.
// Bypassing the whitelist to allow se: prefixes. // Bypassing the whitelist to allow se: prefixes.
// Is there a more appropriate way to do this? // Is there a more appropriate way to do this?
if(attrName.indexOf('se:') == 0) { if (attrName.indexOf('se:') === 0) {
se_attrs.push([attrName, attr.nodeValue]); seAttrs.push([attrName, attr.nodeValue]);
} }
node.removeAttributeNS(attrNsURI, attrLocalName); node.removeAttributeNS(attrNsURI, attrLocalName);
} }
// Add spaces before negative signs where necessary // Add spaces before negative signs where necessary
if(svgedit.browser.isGecko()) { if (svgedit.browser.isGecko()) {
switch ( attrName ) { switch (attrName) {
case 'transform': case 'transform':
case 'gradientTransform': case 'gradientTransform':
case 'patternTransform': case 'patternTransform':
@@ -173,12 +175,12 @@ svgedit.sanitize.sanitizeSvg = function(node) {
node.setAttribute(attrName, val); node.setAttribute(attrName, val);
} }
} }
// For the style attribute, rewrite it in terms of XML presentational attributes // For the style attribute, rewrite it in terms of XML presentational attributes
if (attrName == 'style') { if (attrName == 'style') {
var props = attr.nodeValue.split(';'), var props = attr.nodeValue.split(';'),
p = props.length; p = props.length;
while(p--) { while (p--) {
var nv = props[p].split(':'); var nv = props[p].split(':');
var styleAttrName = $.trim(nv[0]); var styleAttrName = $.trim(nv[0]);
var styleAttrVal = $.trim(nv[1]); var styleAttrVal = $.trim(nv[1]);
@@ -190,17 +192,17 @@ svgedit.sanitize.sanitizeSvg = function(node) {
node.removeAttribute('style'); node.removeAttribute('style');
} }
} }
$.each(se_attrs, function(i, attr) { $.each(seAttrs, function(i, attr) {
node.setAttributeNS(NS.SE, attr[0], attr[1]); node.setAttributeNS(NS.SE, attr[0], attr[1]);
}); });
// for some elements that have a xlink:href, ensure the URI refers to a local element // for some elements that have a xlink:href, ensure the URI refers to a local element
// (but not for links) // (but not for links)
var href = svgedit.utilities.getHref(node); var href = svgedit.utilities.getHref(node);
if(href && if (href &&
['filter', 'linearGradient', 'pattern', ['filter', 'linearGradient', 'pattern',
'radialGradient', 'textPath', 'use'].indexOf(node.nodeName) >= 0) 'radialGradient', 'textPath', 'use'].indexOf(node.nodeName) >= 0)
{ {
// TODO: we simply check if the first character is a #, is this bullet-proof? // TODO: we simply check if the first character is a #, is this bullet-proof?
if (href[0] != '#') { if (href[0] != '#') {
@@ -209,13 +211,13 @@ svgedit.sanitize.sanitizeSvg = function(node) {
node.removeAttributeNS(NS.XLINK, 'href'); node.removeAttributeNS(NS.XLINK, 'href');
} }
} }
// Safari crashes on a <use> without a xlink:href, so we just remove the node here // Safari crashes on a <use> without a xlink:href, so we just remove the node here
if (node.nodeName == 'use' && !svgedit.utilities.getHref(node)) { if (node.nodeName == 'use' && !svgedit.utilities.getHref(node)) {
parent.removeChild(node); parent.removeChild(node);
return; return;
} }
// if the element has attributes pointing to a non-local reference, // if the element has attributes pointing to a non-local reference,
// need to remove the attribute // need to remove the attribute
$.each(['clip-path', 'fill', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke'], function(i, attr) { $.each(['clip-path', 'fill', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke'], function(i, attr) {
var val = node.getAttribute(attr); var val = node.getAttribute(attr);
@@ -228,7 +230,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
} }
} }
}); });
// recurse to children // recurse to children
i = node.childNodes.length; i = node.childNodes.length;
while (i--) { svgedit.sanitize.sanitizeSvg(node.childNodes.item(i)); } while (i--) { svgedit.sanitize.sanitizeSvg(node.childNodes.item(i)); }
@@ -248,9 +250,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
// call sanitizeSvg on each of those children // call sanitizeSvg on each of those children
var i = children.length; var i = children.length;
while (i--) { svgedit.sanitize.sanitizeSvg(children[i]); } while (i--) { svgedit.sanitize.sanitizeSvg(children[i]); }
} }
}; };
})(); })();