Skip to content

Commit

Permalink
Properly escape HTML when working with content editable elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Perez committed May 29, 2016
1 parent 66a4a93 commit d68ce2c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/content-script-tools/custom-events/workflowy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import string from 'ac-util/string';

export default {
url: new RegExp('https://workflowy\.com.*', 'i'),
// override setvalue
bind: function (window) {
this.setValue = (value) => {
this.elem.innerHTML = value;
this.elem.innerHTML = string.htmlEscape(value);

This comment has been minimized.

Copy link
@danhper

danhper May 29, 2016

Owner

This should fix #16

};
}
};
3 changes: 2 additions & 1 deletion src/handlers/content-editable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BaseHandler from './base';
import string from 'ac-util/string';

class ContentEditableHandler extends BaseHandler {
getValue() {
Expand Down Expand Up @@ -31,7 +32,7 @@ class ContentEditableHandler extends BaseHandler {
if (v.trim().length === 0) {
return '<br>';
}
return '<div>' + v + '</div>';
return '<div>' + string.htmlEscape(v) + '</div>';
}).join('');
this.elem.innerHTML = htmlValue;
super.setValue(value);
Expand Down
12 changes: 12 additions & 0 deletions src/util/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ export default {
return s;
}
return s[0].toUpperCase() + s.slice(1);
},

htmlEscape: function (s) {
if (!s) {
return s;
}
return s
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
};

0 comments on commit d68ce2c

Please sign in to comment.