Skip to content

Commit

Permalink
fix(linkifyDOM): remove unnecessary DOM mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Oct 10, 2017
1 parent 4ed94bf commit 27819e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
27 changes: 13 additions & 14 deletions add-on/src/lib/linkifyDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* TODO: measure & improve performance
*/

;(function (alreadyLinkified) {
if (alreadyLinkified) {
;(function (alreadyLoaded) {
if (alreadyLoaded) {
return
}

Expand All @@ -21,8 +21,8 @@
}

// linkify lock
window.ipfsLinkifiedDOM = true
window.ipfsLinkifyValidationCache = new Map()
window.ipfsCompanionLinkifiedDOM = true
window.ipfsCompanionLinkifyValidationCache = new Map()

const urlRE = /(?:\s+|^)(\/ip(?:f|n)s\/|dweb:\/ip(?:f|n)s\/|ipns:\/\/|ipfs:\/\/)([^\s+"<>]+)/g

Expand Down Expand Up @@ -86,7 +86,7 @@
while ((node = xpathResult.snapshotItem(i++))) {
const parent = node.parentNode
// Skip if no longer in visible DOM
if (!parent || !document.body.contains(node)) continue
if (!parent || !container.contains(node)) continue
// Skip already linkified nodes
if (parent.className && parent.className.match(/\blinkifiedIpfsAddress\b/)) continue
// Skip styled <pre> -- often highlighted by script.
Expand All @@ -95,11 +95,11 @@
if (parent.isContentEditable) continue
await linkifyTextNode(node)
if (++counter > 10) {
return setTimeout(continuation, 100)
return setTimeout(continuation, 0)
}
}
}
window.requestAnimationFrame(continuation)
setTimeout(continuation, 0)
}

function textToIpfsResource (match) {
Expand All @@ -125,18 +125,18 @@
async function validIpfsResource (path) {
// validation is expensive, caching result improved performance
// on page that have multiple copies of the same path
if (window.ipfsLinkifyValidationCache.has(path)) {
return window.ipfsLinkifyValidationCache.get(path)
if (window.ipfsCompanionLinkifyValidationCache.has(path)) {
return window.ipfsCompanionLinkifyValidationCache.get(path)
}
try {
// Callback wrapped in promise -- Chrome compatibility
const checkResult = await browser.runtime.sendMessage({pubGwUrlForIpfsOrIpnsPath: path})
window.ipfsLinkifyValidationCache.set(path, checkResult.pubGwUrlForIpfsOrIpnsPath)
window.ipfsCompanionLinkifyValidationCache.set(path, checkResult.pubGwUrlForIpfsOrIpnsPath)
} catch (error) {
window.ipfsLinkifyValidationCache.set(path, null)
window.ipfsCompanionLinkifyValidationCache.set(path, null)
console.error('pubGwUrlForIpfsOrIpnsPath.error for ' + path, error)
}
return window.ipfsLinkifyValidationCache.get(path)
return window.ipfsCompanionLinkifyValidationCache.get(path)
}

async function linkifyTextNode (node) {
Expand Down Expand Up @@ -172,7 +172,6 @@
if (span) {
// take the text after the last link
span.appendChild(document.createTextNode(txt.substring(point, txt.length)))
span.normalize()
// replace the original text with the new span
try {
node.parentNode.replaceChild(span, node)
Expand All @@ -184,4 +183,4 @@
}

init()
}(window.ipfsLinkifiedDOM))
}(window.ipfsCompanionLinkifiedDOM))
2 changes: 1 addition & 1 deletion add-on/src/lib/normalizeLinksWithUnhandledProtocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
while ((node = xpathResult.snapshotItem(i++))) {
const parent = node.parentNode
// Skip if no longer in visible DOM
if (!parent || !document.body.contains(node)) continue
if (!parent || !root.contains(node)) continue
normalizeElement(node)
if (++counter > 10) {
return setTimeout(continuation, 0)
Expand Down

0 comments on commit 27819e0

Please sign in to comment.