Skip to content

Commit

Permalink
chore(util): add reusable escape util
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku authored and philippfromme committed Jun 13, 2019
1 parent 9668e84 commit 0e52034
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/features/bendpoints/Bendpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
toCanvasCoordinates
} from './BendpointUtil';

import cssEscape from 'css.escape';
import {
escapeCSS
} from '../../util/EscapeUtil';

import {
pointsAligned,
Expand Down Expand Up @@ -98,7 +100,7 @@ export default function Bendpoints(
function getBendpointsContainer(element, create) {

var layer = canvas.getLayer('overlays'),
gfx = domQuery('.djs-bendpoints[data-element-id="' + cssEscape(element.id) + '"]', layer);
gfx = domQuery('.djs-bendpoints[data-element-id="' + escapeCSS(element.id) + '"]', layer);

if (!gfx && create) {
gfx = svgCreate('g');
Expand Down
14 changes: 14 additions & 0 deletions lib/util/EscapeUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export {
default as escapeCSS
} from 'css.escape';

var HTML_ESCAPE_MAP = {
'<': '&lt',
'>': '&gt'
};

export function escapeHTML(str) {
return str.replace(/[<>]/g, function(match) {
return HTML_ESCAPE_MAP[match];
});
}
20 changes: 20 additions & 0 deletions test/spec/util/EscapeUtilSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
escapeCSS,
escapeHTML
} from 'lib/util/EscapeUtil';


describe('util/EscapeUtil', function() {

it('escapeCSS', function() {
expect(escapeCSS('..ab')).to.eql('\\.\\.ab');
});


it('escapeHTML', function() {
var htmlStr = '<video src=1 onerror=alert(\'hueh\')>';

expect(escapeHTML(htmlStr)).to.eql('&ltvideo src=1 onerror=alert(\'hueh\')&gt');
});

});

0 comments on commit 0e52034

Please sign in to comment.