rhymix/modules/editor/skins/xquared/javascripts/validator/W3.js
haneul 7c3b336e41 17223554 : xquared upgrade to 0.7
git-svn-id: http://xe-core.googlecode.com/svn/sandbox@4968 201d5d3c-b55e-5fd7-737f-ddc643e51545
2008-11-24 08:52:19 +00:00

84 lines
2.4 KiB
JavaScript

/**
* @requires Xquared.js
* @requires validator/Base.js
*/
xq.validator.W3 = xq.Class(xq.validator.Base,
/**
* @name xq.validator.W3
* @lends xq.validator.W3.prototype
* @extends xq.validator.Base
* @constructor
*/
{
validateDom: function(element) {
var rdom = xq.rdom.Base.createInstance();
rdom.setRoot(element);
this.removeDangerousElements(element);
rdom.removePlaceHoldersAndEmptyNodes(element);
this.validateFont(element);
},
validateString: function(html) {
try {
html = this.replaceTag(html, "b", "strong");
html = this.replaceTag(html, "i", "em");
html = this.validateStrike(html);
html = this.validateUnderline(html);
html = this.addNbspToEmptyBlocks(html);
html = this.performFullValidation(html);
html = this.insertNewlineBetweenBlockElements(html);
} catch(ignored) {}
return html;
},
invalidateDom: function(element) {
this.invalidateFont(element);
this.invalidateStrikesAndUnderlines(element);
},
invalidateString: function(html) {
html = this.replaceTag(html, "strong", "b");
html = this.replaceTag(html, "em", "i");
html = this.removeComments(html);
html = this.replaceNbspToBr(html);
return html;
},
performFullValidation: function(html) {
html = this.validateSelfClosingTags(html);
html = this.applyWhitelist(html);
if(this.urlValidationMode === 'relative') {
html = this.makeUrlsRelative(html);
} else if(this.urlValidationMode === 'host_relative') {
html = this.makeUrlsHostRelative(html);
} else if(this.urlValidationMode === 'absolute') {
html = this.makeUrlsAbsolute(html);
}
return html;
},
insertNewlineBetweenBlockElements: function(html) {
var blocks = new xq.DomTree().getBlockTags().join("|");
var regex = new RegExp("</(" + blocks + ")>([^\n])", "img");
return html.replace(regex, '</$1>\n$2');
},
addNbspToEmptyBlocks: function(content) {
var blocks = new xq.DomTree().getBlockTags().join("|");
var regex = new RegExp("<(" + blocks + ")>\\s*?</(" + blocks + ")>", "img");
return content.replace(regex, '<$1>&nbsp;</$2>');
},
replaceNbspToBr: function(content) {
var blocks = new xq.DomTree().getBlockTags().join("|");
// Safari replaces &nbsp; into \xA0
var regex = new RegExp("<(" + blocks + ")>(&nbsp;|\xA0)?</(" + blocks + ")>", "img");
var rdom = xq.rdom.Base.createInstance();
return content.replace(regex, '<$1>' + rdom.makePlaceHolderString() + '</$3>');
}
});