Skip to content

Commit

Permalink
Don't include script and style elements in innerText
Browse files Browse the repository at this point in the history
JSDOM doesn't support `innerText` [1], so our shim was just forwarding
`textContent`. Instead, remove `script` and `style` elements, which
won't do the same thing as in the browser but will hopefully remove most
unwanted content.

Addresses #111

[1] jsdom/jsdom#1245
  • Loading branch information
dstillman committed Feb 16, 2020
1 parent 1927486 commit f0cff95
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/translation/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ Object.defineProperty(Attr.interface.prototype, 'innerText', {
});
var Node = require('jsdom/lib/jsdom/living/generated/Node');
Object.defineProperty(Node.interface.prototype, 'innerText', {
get: function() { return this.textContent },
get: function() {
// innerText in the browser is more sophisticated, but this removes most unwanted content
// https://github.com/jsdom/jsdom/issues/1245#issuecomment-584677454
var el = this.cloneNode(true);
el.querySelectorAll('script,style').forEach(s => s.remove())
return el.textContent
},
set: function(value) { this.textContent = value },
configurable: true,
});
Expand Down

0 comments on commit f0cff95

Please sign in to comment.