Skip to content
This repository was archived by the owner on Jan 8, 2019. It is now read-only.

Commit 82bd053

Browse files
author
Edmundo Alvarez
committed
Escape extractor preview message
jQuery will interpret code included in the message otherwise. Fixes #833.
1 parent 5048fc6 commit 82bd053

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

public/javascripts/extractors.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $(document).ready(function() {
1717

1818
var wizard = $(".xtrc-wizard");
1919
$(".xtrc-wizard-field", wizard).html(field)
20-
$(".xtrc-wizard-example", wizard).html(value);
20+
$(".xtrc-wizard-example", wizard).html(htmlEscape(value));
2121

2222
$("input[name=field]", wizard).val(field)
2323
$("input[name=example]", wizard).val(value);
@@ -113,18 +113,24 @@ $(document).ready(function() {
113113

114114
function highlightMatchResult(result) {
115115
var example = $("#xtrc-example");
116+
var start = result.match.start;
117+
var end = result.match.end;
118+
116119
// Set to original content first, so we can do this multiple times.
117120
example.html($("#xtrc-original-example").html());
118121

119-
var spanStart = "<span class='xtrc-hl'>";
120-
var spanEnd = "</span>";
121-
122-
var start = result.match.start;
123-
var end = result.match.end+spanStart.length;
124-
125122
var exampleContent = $("<div/>").html(example.html()).text(); // ZOMG JS. this is how you unescape HTML entities.
123+
var highlightedElement = $("<span/>").addClass("xtrc-hl");
124+
125+
// We ensure all parts of the example are escaped
126+
var textBeforeHighlight = htmlEscape(exampleContent.slice(0, start));
127+
var highlightedText = htmlEscape(exampleContent.slice(start, end));
128+
var textAfterHighlight = htmlEscape(exampleContent.slice(end));
126129

127-
example.html(exampleContent.splice(start,0,spanStart).splice(end,0,spanEnd));
130+
example.html(textBeforeHighlight);
131+
highlightedElement.html(highlightedText);
132+
example.append(highlightedElement);
133+
example.append(textAfterHighlight);
128134
}
129135

130136
// Add converter button.

0 commit comments

Comments
 (0)