|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from "react"; |
| 18 | +import ReactDOM from 'react-dom'; |
| 19 | + |
| 20 | +import PlatformPeg from "../PlatformPeg"; |
| 21 | +import LinkWithTooltip from "../components/views/elements/LinkWithTooltip"; |
| 22 | + |
| 23 | +/** |
| 24 | + * If the platform enabled needsUrlTooltips, recurses depth-first through a DOM tree, adding tooltip previews |
| 25 | + * for link elements. Otherwise, does nothing. |
| 26 | + * |
| 27 | + * @param {Element[]} rootNodes - a list of sibling DOM nodes to traverse to try |
| 28 | + * to add tooltips. |
| 29 | + * @param {Element[]} ignoredNodes: a list of nodes to not recurse into. |
| 30 | + * @param {Element[]} containers: an accumulator of the DOM nodes which contain |
| 31 | + * React components that have been mounted by this function. The initial caller |
| 32 | + * should pass in an empty array to seed the accumulator. |
| 33 | + */ |
| 34 | +export function tooltipifyLinks(rootNodes: ArrayLike<Element>, ignoredNodes: Element[], containers: Element[]) { |
| 35 | + if (!PlatformPeg.get()?.needsUrlTooltips()) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + let node = rootNodes[0]; |
| 40 | + |
| 41 | + while (node) { |
| 42 | + let tooltipified = false; |
| 43 | + |
| 44 | + if (ignoredNodes.indexOf(node) >= 0) { |
| 45 | + node = node.nextSibling as Element; |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + if (node.tagName === "A" && node.getAttribute("href") |
| 50 | + && node.getAttribute("href") !== node.textContent.trim() |
| 51 | + ) { |
| 52 | + const container = document.createElement("span"); |
| 53 | + const href = node.getAttribute("href"); |
| 54 | + |
| 55 | + const tooltip = <LinkWithTooltip tooltip={new URL(href, window.location.href).toString()}> |
| 56 | + <span dangerouslySetInnerHTML={{ __html: node.outerHTML }} /> |
| 57 | + </LinkWithTooltip>; |
| 58 | + |
| 59 | + ReactDOM.render(tooltip, container); |
| 60 | + node.parentNode.replaceChild(container, node); |
| 61 | + containers.push(container); |
| 62 | + tooltipified = true; |
| 63 | + } |
| 64 | + |
| 65 | + if (node.childNodes?.length && !tooltipified) { |
| 66 | + tooltipifyLinks(node.childNodes as NodeListOf<Element>, ignoredNodes, containers); |
| 67 | + } |
| 68 | + |
| 69 | + node = node.nextSibling as Element; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Unmount tooltip containers created by tooltipifyLinks. |
| 75 | + * |
| 76 | + * It's critical to call this after tooltipifyLinks, otherwise |
| 77 | + * tooltips will leak. |
| 78 | + * |
| 79 | + * @param {Element[]} containers - array of tooltip containers to unmount |
| 80 | + */ |
| 81 | +export function unmountTooltips(containers: Element[]) { |
| 82 | + for (const container of containers) { |
| 83 | + ReactDOM.unmountComponentAtNode(container); |
| 84 | + } |
| 85 | +} |
0 commit comments