diff --git a/angular-input-highlight.coffee b/angular-input-highlight.coffee index 795312a..c3c6b45 100644 --- a/angular-input-highlight.coffee +++ b/angular-input-highlight.coffee @@ -1,4 +1,15 @@ -_countScrollbar = null +_countScrollbar = null +ENTITY_MAP = + "&": "&" + "<": "<" + ">": ">" + '"': '"' + "'": ''' + "/": '/' + +escapeHTMLEntities = (str) -> + String(str).replace /[&<>"'\/]/g, (c) -> ENTITY_MAP[c] + countScrollbar = -> return _countScrollbar if _countScrollbar != null t = document.createElement('textarea'); @@ -64,13 +75,13 @@ angular.module 'input-highlight', [] markers = [] originalText = text - mirror.innerHTML = text + mirror.innerHTML = escapeHTMLEntities(text) mirror.style.width = style.width; canvas.width = mirror.clientWidth canvas.height = mirror.clientHeight for color, re of formatting - mirror.innerHTML = text.replace re, (s) -> + mirror.innerHTML = escapeHTMLEntities(text).replace re, (s) -> "#{s}" containerRect = mirror.getClientRects()[0] diff --git a/angular-input-highlight.js b/angular-input-highlight.js index 6bd7804..10bf8ff 100644 --- a/angular-input-highlight.js +++ b/angular-input-highlight.js @@ -1,9 +1,24 @@ -// Generated by CoffeeScript 1.10.0 +// Generated by CoffeeScript 1.12.3 (function() { - var _countScrollbar, countScrollbar; + var ENTITY_MAP, _countScrollbar, countScrollbar, escapeHTMLEntities; _countScrollbar = null; + ENTITY_MAP = { + "&": "&", + "<": "<", + ">": ">", + '"': '"', + "'": ''', + "/": '/' + }; + + escapeHTMLEntities = function(str) { + return String(str).replace(/[&<>"'\/]/g, function(c) { + return ENTITY_MAP[c]; + }); + }; + countScrollbar = function() { var t; if (_countScrollbar !== null) { @@ -75,13 +90,13 @@ var color, containerRect, coords, data, j, k, len1, len2, marker, markers, offsetX, offsetY, originalText, re, rect, rects, ref, ref1; markers = []; originalText = text; - mirror.innerHTML = text; + mirror.innerHTML = escapeHTMLEntities(text); mirror.style.width = style.width; canvas.width = mirror.clientWidth; canvas.height = mirror.clientHeight; for (color in formatting) { re = formatting[color]; - mirror.innerHTML = text.replace(re, function(s) { + mirror.innerHTML = escapeHTMLEntities(text).replace(re, function(s) { return "" + s + ""; }); containerRect = mirror.getClientRects()[0]; @@ -108,7 +123,7 @@ ctx.fillRect(coords.x, coords.y, coords.width, coords.height); rects.push(coords); } - data.rectsgit = rects; + data.rects = rects; markers.push(data); } }