-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add htmlSanitizer resolved #609 * Add some jsdoc
- Loading branch information
1 parent
d29dffd
commit 3e42a73
Showing
5 changed files
with
120 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @fileoverview Implements htmlSanitizer | ||
* @author Sungho Kim([email protected]) FE Development Team/NHN Ent. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var util = tui.util; | ||
|
||
var ATTR_WHITE_LIST_RX = /^(src|href|title|data\-+|class|alt|align|style|type|checked)/g; | ||
|
||
/** | ||
* htmlSanitizer | ||
* @api | ||
* @exports htmlSanitizer | ||
* @param {string|Node} html html or Node | ||
* @param {boolean} [needHtmlText] pass true if need html text | ||
* @returns {string|DocumentFragment} result | ||
*/ | ||
function htmlSanitizer(html, needHtmlText) { | ||
var $html = $('<div />'); | ||
|
||
$html.append(html); | ||
|
||
removeUnnecessaryTags($html); | ||
leaveOnlyWhitelistAttribute($html); | ||
|
||
return finalizeHtml($html, needHtmlText); | ||
} | ||
|
||
/** | ||
* Remove unnecessary tags | ||
* @private | ||
* @param {jQuery} $html jQuery instance | ||
*/ | ||
function removeUnnecessaryTags($html) { | ||
$html.find('script, iframe, textarea, form, button, select, .Apple-converted-space').remove(); | ||
} | ||
|
||
/** | ||
* Leave only white list attributes | ||
* @private | ||
* @param {jQuery} $html jQuery instance | ||
*/ | ||
function leaveOnlyWhitelistAttribute($html) { | ||
$html.find('*').each(function(index, node) { | ||
var blacklist = util.toArray(node.attributes).filter(function(attr) { | ||
return !attr.name.match(ATTR_WHITE_LIST_RX); | ||
}); | ||
|
||
util.forEachArray(blacklist, function(attr) { | ||
node.attributes.removeNamedItem(attr.name); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Finalize html result | ||
* @private | ||
* @param {jQuery} $html jQuery instance | ||
* @param {boolean} needHtmlText pass true if need html text | ||
* @returns {string|DocumentFragment} result | ||
*/ | ||
function finalizeHtml($html, needHtmlText) { | ||
var returnValue, frag; | ||
|
||
if (needHtmlText) { | ||
returnValue = $html[0].innerHTML; | ||
} else { | ||
frag = document.createDocumentFragment(); | ||
$(frag).append($html[0].innerHTML); | ||
returnValue = frag; | ||
} | ||
|
||
return returnValue; | ||
} | ||
|
||
module.exports = htmlSanitizer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var htmlSanitizer = require('../src/js/htmlSanitizer'); | ||
|
||
describe('htmlSanitizer', function() { | ||
describe('tags', function() { | ||
it('escape script tags to text', function() { | ||
expect(htmlSanitizer('<script>alert("test");</script>', true)).toEqual(''); | ||
}); | ||
}); | ||
|
||
describe('attributes', function() { | ||
it('Remove all attritube but white list', function() { | ||
var $img; | ||
var html = '<img class="V" title="V" data-custom="V" src="http://www.nhnent.com/renewal/img/ci_nhnent.png" onload="dd=1" />'; | ||
var dom = htmlSanitizer(html); | ||
|
||
$img = $(dom).find('img'); | ||
|
||
expect($img.attr('src')).toBeTruthy(); | ||
expect($img.attr('class')).toBeTruthy(); | ||
expect($img.attr('title')).toBeTruthy(); | ||
expect($img.attr('data-custom')).toBeTruthy(); | ||
|
||
expect($img.attr('onload')).not.toBeDefined(); | ||
}); | ||
}); | ||
}); |