misc cleaned up sanitize inconsistent indentation
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2469 eee81c28-f429-11dd-99c0-75d572ba1dddmaster
parent
71586aa5fe
commit
2c1c5da6de
|
@ -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':
|
||||||
|
@ -178,7 +180,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
|
||||||
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]);
|
||||||
|
@ -191,16 +193,16 @@ svgedit.sanitize.sanitizeSvg = function(node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$.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] != '#') {
|
||||||
|
@ -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]); }
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue