Skip to content
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit 1876911

Browse files
digitalcraftyowainwright
authored andcommitted
Use createTextNode() to avoid possible XSS (#145)
* Use createTextNode() to avoid possible XSS For reference: https://stackoverflow.com/questions/476821/is-a-dom-text-node-guaranteed-to-not-be-interpreted-as-html For XSS Example: https://jsfiddle.net/32795mpy/ * delete whitespace for CI * update variable names "hiddenText" => "shavedText" "wrapper" => "elWithShavedText"
1 parent 0e0365d commit 1876911

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/shave.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ export default function shave (target, maxHeight, opts = {}) {
5757
el.insertAdjacentHTML('beforeend', charHtml)
5858
const diff = spaces ? ` ${words.slice(max).join(' ')}` : words.slice(max)
5959

60-
el.insertAdjacentHTML(
61-
'beforeend',
62-
`<span class="${classname}" style="display:none;">${diff}</span>`,
63-
)
60+
// https://stackoverflow.com/questions/476821/is-a-dom-text-node-guaranteed-to-not-be-interpreted-as-html
61+
const shavedText = document.createTextNode(diff)
62+
const elWithShavedText = document.createElement('span')
63+
elWithShavedText.classList.add(classname)
64+
elWithShavedText.style.display = 'none'
65+
elWithShavedText.appendChild(shavedText)
66+
el.insertAdjacentElement('beforeend', elWithShavedText)
6467

6568
styles.height = heightStyle
6669
styles.maxHeight = maxHeightStyle

0 commit comments

Comments
 (0)