reorder namespaces (put all W3C ones first); align a few lines; tweak comments for consistency and clarity.

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2379 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Waldir Pimenta 2013-02-13 17:09:12 +00:00
parent f43adb25fa
commit 5dcdc76912
1 changed files with 28 additions and 22 deletions

View File

@ -23,20 +23,20 @@ if (!svgedit.sanitize) {
// Namespace constants // Namespace constants
var svgns = "http://www.w3.org/2000/svg", var svgns = "http://www.w3.org/2000/svg",
xlinkns = "http://www.w3.org/1999/xlink", xlinkns = "http://www.w3.org/1999/xlink",
xmlns = "http://www.w3.org/XML/1998/namespace", xmlns = "http://www.w3.org/XML/1998/namespace",
xmlnsns = "http://www.w3.org/2000/xmlns/", // see http://www.w3.org/TR/REC-xml-names/#xmlReserved xmlnsns = "http://www.w3.org/2000/xmlns/", // see http://www.w3.org/TR/REC-xml-names/#xmlReserved
se_ns = "http://svg-edit.googlecode.com", htmlns = "http://www.w3.org/1999/xhtml",
htmlns = "http://www.w3.org/1999/xhtml", mathns = "http://www.w3.org/1998/Math/MathML",
mathns = "http://www.w3.org/1998/Math/MathML"; se_ns = "http://svg-edit.googlecode.com";
// map namespace URIs to prefixes // map namespace URIs to prefixes
var nsMap_ = {}; var nsMap_ = {};
nsMap_[xlinkns] = 'xlink'; nsMap_[xlinkns] = 'xlink';
nsMap_[xmlns] = 'xml'; nsMap_[xmlns] = 'xml';
nsMap_[xmlnsns] = 'xmlns'; nsMap_[xmlnsns] = 'xmlns';
nsMap_[se_ns] = 'se'; nsMap_[htmlns] = 'xhtml';
nsMap_[htmlns] = 'xhtml'; nsMap_[mathns] = 'mathml';
nsMap_[mathns] = 'mathml'; nsMap_[se_ns] = 'se';
// temporarily expose these // temporarily expose these
svgedit.sanitize.getNSMap = function() { return nsMap_; } svgedit.sanitize.getNSMap = function() { return nsMap_; }
@ -137,17 +137,23 @@ $.each(svgWhiteList_, function(elt,atts){
// It only keeps what is allowed from our whitelist defined above // It only keeps what is allowed from our whitelist defined above
// //
// Parameters: // Parameters:
// node - The DOM element to be checked, will 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) {
// we only care about element nodes // Cleanup text nodes
// automatically return for all comment, etc nodes if (node.nodeType == 3) { // 3 == TEXT_NODE
// for text, we do a whitespace trim // Trim whitespace
if (node.nodeType == 3) {
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, ""); node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, "");
// Remove empty text nodes // Remove if empty
if(!node.nodeValue.length) node.parentNode.removeChild(node); if(node.nodeValue.length == 0) node.parentNode.removeChild(node);
} }
if (node.nodeType != 1) return;
// We only care about element nodes.
// Automatically return for all non-element nodes,
// such as comments, etc.
if (node.nodeType != 1) { // 1 == ELEMENT_NODE
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...
@ -156,7 +162,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
var allowedAttrs = svgWhiteList_[node.nodeName]; var allowedAttrs = svgWhiteList_[node.nodeName];
var allowedAttrsNS = svgWhiteListNS_[node.nodeName]; var allowedAttrsNS = svgWhiteListNS_[node.nodeName];
// if this element is allowed // if this element is supported, sanitize it
if (allowedAttrs != undefined) { if (allowedAttrs != undefined) {
var se_attrs = []; var se_attrs = [];
@ -175,8 +181,8 @@ svgedit.sanitize.sanitizeSvg = function(node) {
!(attrNsURI == xmlnsns && nsMap_[attr.nodeValue]) ) !(attrNsURI == xmlnsns && nsMap_[attr.nodeValue]) )
{ {
// 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. Is there // Bypassing the whitelist to allow se: prefixes.
// 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]); se_attrs.push([attrName, attr.nodeValue]);
} }
@ -194,7 +200,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
} }
} }
// 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;
@ -202,7 +208,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
var nv = props[p].split(":"); var nv = props[p].split(":");
var attrname = $.trim(nv[0]); var attrname = $.trim(nv[0]);
var attrval = $.trim(nv[1]); var attrval = $.trim(nv[1]);
// now check that this attribute is supported // Now check that this attribute is supported
if (allowedAttrs.indexOf(attrname) >= 0) { if (allowedAttrs.indexOf(attrname) >= 0) {
node.setAttribute(attrname, attrval); node.setAttribute(attrname, attrval);
} }
@ -253,7 +259,7 @@ svgedit.sanitize.sanitizeSvg = function(node) {
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)); }
} }
// else, remove this element // else (element not supported), remove it
else { else {
// remove all children from this node and insert them before this node // remove all children from this node and insert them before this node
// FIXME: in the case of animation elements this will hardly ever be correct // FIXME: in the case of animation elements this will hardly ever be correct