Skip to content

Commit

Permalink
fix: isElement function for marker from other ownerDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
meinradandermatt committed Nov 17, 2021
1 parent 3d848c4 commit 71d6d7f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions spec/highlighting.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ describe('Highlighting', function () {
const matches = textSearch.findMatches(blockText, [text])
expect(matches).to.equal(undefined)
})

it('does not go into an endless loop without a html marker node', function () {
const blockText = 'Mehr als 90 Prozent der Fälle in Grossbritannien in den letzten vier Wochen gehen auf die Delta-Variante zurück. Anders als bei vorangegangenen Wellen scheinen sich jedoch die Fallzahlen von den Todesfällen und Hospitalisierungen zu entkoppeln.'
const text = ''
const textSearch = new WordHighlighter('something', 'text')
const matches = textSearch.findMatches(blockText, [text])
expect(matches).to.equal(undefined)
})

it('handle the marker with other ownerdocument correctly', function () {
const blockText = 'Mehr als 90 Prozent'
const text = 'Mehr als 90 Prozent'
const ifrm = window.document.createElement('iframe')
window.document.body.append(ifrm)
const markerNode = createElement('<span class="highlight"></span>', ifrm.contentWindow)
const textSearch = new WordHighlighter(markerNode, 'text')
const matches = textSearch.findMatches(blockText, [text])
expect(matches[0].match).to.equal(text)
})
})

describe('highlightSupport', function () {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/highlighting/text-highlighting.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default class WordHighlighting {

isElement (obj) {
try {
// Using W3 DOM2 (works for FF, Opera and Chrome)
return obj instanceof HTMLElement
if (!obj) return false
return obj instanceof obj.ownerDocument?.defaultView.HTMLElement
} catch (e) {
// Browsers not supporting W3 DOM2 don't have HTMLElement and
// an exception is thrown and we end up here. Testing some
Expand Down

0 comments on commit 71d6d7f

Please sign in to comment.