Skip to content

Commit

Permalink
fix: run linkify checks on entire parent tree
Browse files Browse the repository at this point in the history
A lot of web editors create elaborate DOM trees and it is not enough to
check if parent is safe for linkification.

This change walks back over entire parent tree to ensure none of parents
has a no-go flag for linkification (eg. HTMLElement.contentEditable property)
  • Loading branch information
lidel committed Jul 14, 2018
1 parent e19f231 commit a5b047d
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions add-on/src/contentScripts/linkifyDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,28 @@ const PQueue = require('p-queue')
return window.ipfsCompanionLinkifyValidationCache.get(path)
}

async function linkifyTextNode (node) {
const parent = node.parentNode
function isParentTreeSafe (node) {
let parent = node.parentNode
// Skip if no longer in visible DOM
if (!parent) return
// Skip already linkified nodes
if (parent.className && parent.className.match(/\blinkifiedIpfsAddress\b/)) return
// Skip styled <pre> -- often highlighted by script.
if (parent.tagName === 'PRE' && parent.className) return
// Skip forms, textareas
if (parent.isContentEditable) return
if (!parent) return false
// Walk back over parent tree and check each of them
while (parent) {
// Skip forms, textareas
if (parent.isContentEditable) return false
// Skip already linkified nodes
if (parent.className && parent.className.match(/\blinkifiedIpfsAddress\b/)) return false
// Skip styled <pre> -- often highlighted by scripts
if (parent.tagName === 'PRE' && parent.className) return false
// Skip if no longer in visible DOM
if (!(parent instanceof HTMLDocument) && !parent.parentNode) return false
parent = parent.parentNode
}
return true
}

async function linkifyTextNode (node) {
// Skip if node belongs to a parent from unsafe tree
if (!isParentTreeSafe(node)) return

let link
let match
Expand Down

0 comments on commit a5b047d

Please sign in to comment.