Skip to content

Commit ca66463

Browse files
committed
Add transformation function for html escaping values from
user-provided data sources Signed-off-by: Andy Boughton <[email protected]>
1 parent 896eeb3 commit ca66463

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

NOTICE.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Third-party code
2+
LocusZoom gratefully acknowledges the use of some third-party code, with required license information provided below.
3+
4+
### Underscore.js (text helpers and utility functions)
5+
Copyright (c) 2009-2017 Jeremy Ashkenas, DocumentCloud and Investigative
6+
Reporters & Editors
7+
8+
Permission is hereby granted, free of charge, to any person
9+
obtaining a copy of this software and associated documentation
10+
files (the "Software"), to deal in the Software without
11+
restriction, including without limitation the rights to use,
12+
copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the
14+
Software is furnished to do so, subject to the following
15+
conditions:
16+
17+
The above copyright notice and this permission notice shall be
18+
included in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
OTHER DEALINGS IN THE SOFTWARE.
28+

assets/js/app/Singletons.js

+30
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,36 @@ LocusZoom.TransformationFunctions.add("urlencode", function(str) {
291291
return encodeURIComponent(str);
292292
});
293293

294+
/**
295+
* HTML-escape user entered values for use in constructed HTML fragments
296+
*
297+
* For example, this filter can be used on tooltips with custom HTML display
298+
* @function htmlescape
299+
* @param {String} str HTML-escape the provided value
300+
*/
301+
LocusZoom.TransformationFunctions.add("htmlescape", function(str) {
302+
if ( !str ) {
303+
return "";
304+
}
305+
str = str + "";
306+
307+
return str.replace( /['"<>&`]/g, function( s ) {
308+
switch ( s ) {
309+
case "'":
310+
return "&#039;";
311+
case "\"":
312+
return "&quot;";
313+
case "<":
314+
return "&lt;";
315+
case ">":
316+
return "&gt;";
317+
case "&":
318+
return "&amp;";
319+
case "`":
320+
return "&#x60;";
321+
}
322+
});
323+
});
294324

295325
/**
296326
* Singleton for accessing/storing functions that will convert arbitrary data points to values in a given scale

test/Singletons.js

+9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ describe("LocusZoom Singletons", function(){
100100
});
101101
});
102102
});
103+
104+
describe("htmlescape", function() {
105+
it("should escape characters with special meaning in xml, and ignore others", function() {
106+
assert.equal(
107+
LocusZoom.TransformationFunctions.get("htmlescape")("<script type=\"application/javascript\">alert('yo & ' + `I`)</script>"),
108+
"&lt;script type=&quot;application/javascript&quot;&gt;alert(&#039;yo &amp; &#039; + &#x60;I&#x60;)&lt;/script&gt;"
109+
);
110+
});
111+
});
103112
});
104113

105114
describe("Scale Functions", function() {

0 commit comments

Comments
 (0)