Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
wants to merge 1 commit into from

10 participants

@DouweM

Fixes #51.

@jlebrech

useful for allowing classes, and href

+1

@3mpire

+1

@DouweM

Any progress on reviewing this?

@krixer

+1

@jlebrech

why is not pulled, free code for goodness sake.

@swiggs

+1

@ratrocket

+1, we definitely need this! :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 parser_rules/advanced.js
@@ -179,6 +179,9 @@ var wysihtml5ParserRules = {
"rename_tag": "div"
},
"a": {
+ "allow_attributes": [
+ "title"
+ ],
"check_attributes": {
"href": "url" // if you compiled master manually then change this from 'url' to 'href'
},
@@ -188,6 +191,9 @@ var wysihtml5ParserRules = {
}
},
"img": {
+ "allow_attributes": [
+ "title"
+ ],
"check_attributes": {
"width": "numbers",
"alt": "alt",
View
3  parser_rules/simple.js
@@ -20,6 +20,9 @@ var wysihtml5ParserRules = {
ol: {},
li: {},
a: {
+ allow_attributes: [
+ "title"
+ ],
set_attributes: {
target: "_blank",
rel: "nofollow"
View
14 src/dom/parse.js
@@ -211,6 +211,7 @@ wysihtml5.dom.parse = (function() {
var attributes = {}, // fresh new set of attributes to set on newNode
setClass = rule.set_class, // classes to set
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
checkAttributes = rule.check_attributes, // check/convert values of attributes
allowedClasses = currentRules.classes,
@@ -230,6 +231,17 @@ wysihtml5.dom.parse = (function() {
if (setAttributes) {
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) {
for (attributeName in checkAttributes) {
@@ -270,7 +282,7 @@ wysihtml5.dom.parse = (function() {
classes = classes.concat(oldClasses.split(WHITE_SPACE_REG_EXP));
}
classesLength = classes.length;
- for (; i<classesLength; i++) {
+ for (i = 0; i<classesLength; i++) {
currentClass = classes[i];
if (allowedClasses[currentClass]) {
newClasses.push(currentClass);
View
5 test/dom/parse_test.js
@@ -79,6 +79,7 @@ if (wysihtml5.browser.supported()) {
var rules = {
tags: {
img: {
+ allow_attributes: [ "title" ],
set_attributes: { alt: "foo", border: "1" },
check_attributes: { src: "url", width: "numbers", height: "numbers", border: "numbers" }
},
@@ -98,13 +99,13 @@ if (wysihtml5.browser.supported()) {
'<h1 id="main-headline" >take this you snorty little sanitizer</h1>' +
'<h2>yes, you!</h2>' +
'<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>',
rules
),
'<h2>take this you snorty little sanitizer</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>'
);
});
Something went wrong with that request. Please try again.