Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Add allow_attributes parser rule option to do just that. #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions parser_rules/advanced.js
Expand Up @@ -179,6 +179,9 @@ var wysihtml5ParserRules = {
"rename_tag": "div" "rename_tag": "div"
}, },
"a": { "a": {
"allow_attributes": [
"title"
],
"check_attributes": { "check_attributes": {
"href": "url" // if you compiled master manually then change this from 'url' to 'href' "href": "url" // if you compiled master manually then change this from 'url' to 'href'
}, },
Expand All @@ -188,6 +191,9 @@ var wysihtml5ParserRules = {
} }
}, },
"img": { "img": {
"allow_attributes": [
"title"
],
"check_attributes": { "check_attributes": {
"width": "numbers", "width": "numbers",
"alt": "alt", "alt": "alt",
Expand Down
3 changes: 3 additions & 0 deletions parser_rules/simple.js
Expand Up @@ -20,6 +20,9 @@ var wysihtml5ParserRules = {
ol: {}, ol: {},
li: {}, li: {},
a: { a: {
allow_attributes: [
"title"
],
set_attributes: { set_attributes: {
target: "_blank", target: "_blank",
rel: "nofollow" rel: "nofollow"
Expand Down
14 changes: 13 additions & 1 deletion src/dom/parse.js
Expand Up @@ -211,6 +211,7 @@ wysihtml5.dom.parse = (function() {
var attributes = {}, // fresh new set of attributes to set on newNode var attributes = {}, // fresh new set of attributes to set on newNode
setClass = rule.set_class, // classes to set setClass = rule.set_class, // classes to set
addClass = rule.add_class, // add classes based on existing attributes addClass = rule.add_class, // add classes based on existing attributes
allowAttributes = rule.allow_attributes, // copy attributes from old to new node
setAttributes = rule.set_attributes, // attributes to set on the current node setAttributes = rule.set_attributes, // attributes to set on the current node
checkAttributes = rule.check_attributes, // check/convert values of attributes checkAttributes = rule.check_attributes, // check/convert values of attributes
allowedClasses = currentRules.classes, allowedClasses = currentRules.classes,
Expand All @@ -230,6 +231,17 @@ wysihtml5.dom.parse = (function() {
if (setAttributes) { if (setAttributes) {
attributes = wysihtml5.lang.object(setAttributes).clone(); attributes = wysihtml5.lang.object(setAttributes).clone();
} }

if (allowAttributes) {
allowAttributesLength = allowAttributes.length;
for (i = 0; i<allowAttributesLength; i++) {
attributeName = allowAttributes[i];
newAttributeValue = _getAttribute(oldNode, attributeName);
if (typeof(newAttributeValue) === "string") {
attributes[attributeName] = newAttributeValue;
}
}
}


if (checkAttributes) { if (checkAttributes) {
for (attributeName in checkAttributes) { for (attributeName in checkAttributes) {
Expand Down Expand Up @@ -270,7 +282,7 @@ wysihtml5.dom.parse = (function() {
classes = classes.concat(oldClasses.split(WHITE_SPACE_REG_EXP)); classes = classes.concat(oldClasses.split(WHITE_SPACE_REG_EXP));
} }
classesLength = classes.length; classesLength = classes.length;
for (; i<classesLength; i++) { for (i = 0; i<classesLength; i++) {
currentClass = classes[i]; currentClass = classes[i];
if (allowedClasses[currentClass]) { if (allowedClasses[currentClass]) {
newClasses.push(currentClass); newClasses.push(currentClass);
Expand Down
5 changes: 3 additions & 2 deletions test/dom/parse_test.js
Expand Up @@ -79,6 +79,7 @@ if (wysihtml5.browser.supported()) {
var rules = { var rules = {
tags: { tags: {
img: { img: {
allow_attributes: [ "title" ],
set_attributes: { alt: "foo", border: "1" }, set_attributes: { alt: "foo", border: "1" },
check_attributes: { src: "url", width: "numbers", height: "numbers", border: "numbers" } check_attributes: { src: "url", width: "numbers", height: "numbers", border: "numbers" }
}, },
Expand All @@ -98,13 +99,13 @@ if (wysihtml5.browser.supported()) {
'<h1 id="main-headline" >take this you snorty little sanitizer</h1>' + '<h1 id="main-headline" >take this you snorty little sanitizer</h1>' +
'<h2>yes, you!</h2>' + '<h2>yes, you!</h2>' +
'<h3>i\'m old and ready to die</h3>' + '<h3>i\'m old and ready to die</h3>' +
'<div><video src="pr0n.avi">foobar</video><img src="http://foo.gif" height="10" width="10"><img src="/foo.gif"></div>' + '<div><video src="pr0n.avi">foobar</video><img src="http://foo.gif" height="10" width="10" title="Hell yeah!"><img src="/foo.gif"></div>' +
'<div><a href="http://www.google.de"></a></div>', '<div><a href="http://www.google.de"></a></div>',
rules rules
), ),
'<h2>take this you snorty little sanitizer</h2>' + '<h2>take this you snorty little sanitizer</h2>' +
'<h2>yes, you!</h2>' + '<h2>yes, you!</h2>' +
'<span><img alt="foo" border="1" src="http://foo.gif" height="10" width="10"><img alt="foo" border="1"></span>' + '<span><img alt="foo" border="1" title="Hell yeah!" src="http://foo.gif" height="10" width="10"><img alt="foo" border="1"></span>' +
'<span><i title=""></i></span>' '<span><i title=""></i></span>'
); );
}); });
Expand Down