mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
move html escape into a util
This commit is contained in:
parent
10c6555d9f
commit
1be0f3189c
4 changed files with 39 additions and 6 deletions
|
@ -3,6 +3,7 @@ define(function (require) {
|
|||
var html = require('text!apps/discover/partials/table.html');
|
||||
var detailsHtml = require('text!apps/discover/partials/row_details.html');
|
||||
var moment = require('moment');
|
||||
var htmlEscape = require('utils/html_escape');
|
||||
|
||||
var _ = require('lodash');
|
||||
var $ = require('jquery');
|
||||
|
@ -284,12 +285,7 @@ define(function (require) {
|
|||
|
||||
|
||||
if (breakWords) {
|
||||
text = text.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/'/g, ''')
|
||||
.replace(/"/g, '"');
|
||||
|
||||
text = htmlEscape(text);
|
||||
var lineSize = 0;
|
||||
var newText = '';
|
||||
for (var i = 0, len = text.length; i < len; i++) {
|
||||
|
|
17
src/kibana/utils/html_escape.js
Normal file
17
src/kibana/utils/html_escape.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
define(function (require) {
|
||||
var _ = require('lodash');
|
||||
var map = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'\'': ''',
|
||||
'"': '"',
|
||||
};
|
||||
|
||||
var regex = new RegExp('[' + _.keys(map).join('') + ']', 'g');
|
||||
return function htmlEscape(text) {
|
||||
return text.replace(regex, function (c) {
|
||||
return map[c];
|
||||
});
|
||||
};
|
||||
});
|
|
@ -102,6 +102,7 @@
|
|||
'specs/utils/versionmath',
|
||||
'specs/utils/routes/index',
|
||||
'specs/utils/sequencer',
|
||||
'specs/utils/html_escape',
|
||||
'specs/courier/search_source/_get_normalized_sort',
|
||||
'specs/factories/base_object',
|
||||
'specs/state_management/state',
|
||||
|
|
19
test/unit/specs/utils/html_escape.js
Normal file
19
test/unit/specs/utils/html_escape.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
define(function (require) {
|
||||
describe('HTML Escape Util', function () {
|
||||
var htmlEscape = require('utils/html_escape');
|
||||
|
||||
it('removes tags by replacing their angle-brackets', function () {
|
||||
expect(htmlEscape('<h1>header</h1>')).to.eql('<h1>header</h1>');
|
||||
});
|
||||
|
||||
it('removes attributes from tags using " and '', function () {
|
||||
expect(htmlEscape('<h1 onclick="alert(\'hi\');">header</h1>'))
|
||||
.to.eql('<h1 onclick="alert('hi');">header</h1>');
|
||||
});
|
||||
|
||||
it('escapes existing html entities by escaping their leading &', function () {
|
||||
expect(htmlEscape('<h1>header</h1>'))
|
||||
.to.eql('&lt;h1&gt;header&lt;/h1&gt;');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue