Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new sanitize method #103

Merged
merged 13 commits into from
Jan 10, 2017
Next Next commit
new sanitize method
Need to fix caret position
  • Loading branch information
khaydarov committed Dec 24, 2016
commit b96572f6fc6cdab5460c039d52c1da18feea088e
518 changes: 414 additions & 104 deletions codex-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion codex-editor.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var codex = (function(codex){
codex.caret = require('./modules/caret');
codex.notifications = require('./modules/notifications');
codex.parser = require('./modules/parser');
codex.sanitizer = require('./modules/sanitizer/sanitize');
};

codex.version = {
Expand Down
4 changes: 2 additions & 2 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

</body>

<script src="./codex-editor.js?v=11"></script>
<link rel="stylesheet" href="./codex-editor.css?v=11">
<script src="./codex-editor.js?v=10"></script>
<link rel="stylesheet" href="./codex-editor.css?v=11000">

<link rel="stylesheet" href="codex-editor.css">

Expand Down
83 changes: 6 additions & 77 deletions modules/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Works with DOM
*
* @author Codex Team
* @version 1.0
* @version 1.1.1
*/

var content = (function(content) {
Expand Down Expand Up @@ -553,84 +553,13 @@ var content = (function(content) {
return;
}

for (var i = 0; i < target.childNodes.length; i++) {
this.dfs(target.childNodes[i]);
}
};

/**
* Clears styles
* @param {Element|Text}
*/
content.clearStyles = function(target) {

var href,
newNode = null,
blockTags = ['P', 'BLOCKQUOTE', 'UL', 'CODE', 'OL', 'LI', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'DIV', 'PRE', 'HEADER', 'SECTION'],
allowedTags = ['P', 'B', 'I', 'A', 'U', 'BR'],
needReplace = !allowedTags.includes(target.tagName),
isDisplayedAsBlock = blockTags.includes(target.tagName);

if (!codex.core.isDomNode(target)){
return target;
}

if (!target.parentNode){
return target;
}

if (needReplace) {

if (isDisplayedAsBlock) {

newNode = document.createElement('P');
newNode.innerHTML = target.innerHTML;
target.parentNode.replaceChild(newNode, target);
target = newNode;

} else {

newNode = document.createTextNode(` ${target.textContent} `);
newNode.textContent = newNode.textContent.replace(/\s{2,}/g, ' ');
target.parentNode.replaceChild(newNode, target);

}
}
console.log(target);

/** keep href attributes of tag A */
if (target.tagName == 'A') {
href = target.getAttribute('href');
}

/** Remove all tags */
while(target.attributes.length > 0) {
target.removeAttribute(target.attributes[0].name);
}
var sanitizer = new codex.sanitizer(codex.sanitizer.Config.BASIC);
var clearHTML = sanitizer.clean_node(codex.content.currentNode.childNodes[0]);

/** return href */
if (href) {
target.setAttribute('href', href);
}

return target;

};

/**
* Depth-first search Algorithm
* returns all childs
* @param {Element}
*/
content.dfs = function(el) {

if (!codex.core.isDomNode(el))
return;

var sanitized = this.clearStyles(el);

for(var i = 0; i < sanitized.childNodes.length; i++) {
this.dfs(sanitized.childNodes[i]);
}
target.innerHTML = "";
target.appendChild(clearHTML);

};

Expand Down
31 changes: 31 additions & 0 deletions modules/sanitizer/config/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var Sanitize = require('../sanitize');

if(!Sanitize.Config) {
Sanitize.Config = {}
}

Sanitize.Config.BASIC = {
elements: [
'a', 'b', 'blockquote', 'br', 'cite', 'code', 'dd', 'dl', 'dt', 'em',
'i', 'li', 'ol', 'p', 'pre', 'q', 'small', 'strike', 'strong', 'sub',
'sup', 'u', 'ul'],

attributes: {
'a' : ['href'],
'blockquote': ['cite'],
'q' : ['cite']
},

add_attributes: {
'a': {'rel': ''}
},

protocols: {
'a' : {'href': ['ftp', 'http', 'https', 'mailto',
Sanitize.RELATIVE]},
'blockquote': {'cite': ['http', 'https', Sanitize.RELATIVE]},
'q' : {'cite': ['http', 'https', Sanitize.RELATIVE]}
}
};

codex.sanitizer = Sanitize;
39 changes: 39 additions & 0 deletions modules/sanitizer/config/relaxed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var Sanitize = require('../sanitize');

if(!Sanitize.Config) {
Sanitize.Config = {}
}

Sanitize.Config.RELAXED = {
elements: [
'a', 'b', 'blockquote', 'br', 'caption', 'cite', 'code', 'col',
'colgroup', 'dd', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'i', 'img', 'li', 'ol', 'p', 'pre', 'q', 'small', 'strike', 'strong',
'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'u',
'ul'],

attributes: {
'a' : ['href', 'title'],
'blockquote': ['cite'],
'col' : ['span', 'width'],
'colgroup' : ['span', 'width'],
'img' : ['align', 'alt', 'height', 'src', 'title', 'width'],
'ol' : ['start', 'type'],
'q' : ['cite'],
'table' : ['summary', 'width'],
'td' : ['abbr', 'axis', 'colspan', 'rowspan', 'width'],
'th' : ['abbr', 'axis', 'colspan', 'rowspan', 'scope',
'width'],
'ul' : ['type']
},

protocols: {
'a' : {'href': ['ftp', 'http', 'https', 'mailto',
Sanitize.RELATIVE]},
'blockquote': {'cite': ['http', 'https', Sanitize.RELATIVE]},
'img' : {'src' : ['http', 'https', Sanitize.RELATIVE]},
'q' : {'cite': ['http', 'https', Sanitize.RELATIVE]}
}
};

codex.sanitizer = Sanitize;
11 changes: 11 additions & 0 deletions modules/sanitizer/config/restricted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var Sanitize = require('../sanitize');

if(!Sanitize.Config) {
Sanitize.Config = {}
}

Sanitize.Config.RESTRICTED = {
elements: ['a', 'b', 'em', 'i', 'strong', 'u']
};

codex.sanitizer = Sanitize;
Loading