Skip to content

Commit

Permalink
Added modules/Escaping with the first useful methods.
Browse files Browse the repository at this point in the history
query/VirtualElement now using the escaping.
  • Loading branch information
Kanaye committed Sep 25, 2016
1 parent 3102494 commit cc1e93f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
43 changes: 43 additions & 0 deletions src/modules/Escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
define([],
function () {
var htmlEntityMap = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#x27;',
'/': '&x2F;'
};

var htmlEscapeRegEx = (function () {
var entities = [];
for (var entity in htmlEntityMap) {
entities.push(entity);
}
return new RegExp('(' + entities.join('|') + ')', 'g');
})();

var Escape = {
// moved from modules/escapeRegEx
forRegEx: function escapeRegEx(string) {
return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
},
forHTML: function (value) {
if (!blocks.isString(value)) {
return value;
}
return value.replace(htmlEscapeRegEx, function (entity) {
return htmlEntityMap[entity];
});
},
// This is only valid because jsblocks forces (inserts itself) double quotes for attributes
// don't use this in other cases
forHTMLAttributes: function (value) {
if (blocks.isString(value)) {
return value.replace(/"/g, '&quot;');
}
return value;
}
};
return Escape;
});
7 changes: 4 additions & 3 deletions src/query/VirtualElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ define([
'../core',
'../var/hasOwn',
'../modules/keys',
'../modules/Escape',
'./var/virtualElementIdentity',
'./var/classAttr',
'./var/dataIdAttr',
Expand All @@ -13,7 +14,7 @@ define([
'./dom',
'./Expression',
'./ElementsData'
], function (blocks, hasOwn, keys, virtualElementIdentity, classAttr, dataIdAttr, dataQueryAttr, getClassIndex, setClass, escapeValue, createFragment, dom,
], function (blocks, hasOwn, keys, Escape, virtualElementIdentity, classAttr, dataIdAttr, dataQueryAttr, getClassIndex, setClass, escapeValue, createFragment, dom,
Expression, ElementsData) {

function VirtualElement(tagName) {
Expand Down Expand Up @@ -85,7 +86,7 @@ define([
text: function (text) {
if (arguments.length > 0) {
if (text != null) {
text = escapeValue(text);
text = Escape.forHTML(text);
this.html(text);
}
return this;
Expand Down Expand Up @@ -618,7 +619,7 @@ define([
if (value === '') {
html += ' ' + key;
} else if (value != null) {
html += ' ' + key + '="' + value + '"';
html += ' ' + key + '="' + Escape.forHTMLAttributes(value) + '"';
}
}

Expand Down

0 comments on commit cc1e93f

Please sign in to comment.