From 71eb40585991da54b42961f9a970fc2107caee38 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 8 Jun 2017 14:53:21 +0100 Subject: [PATCH] Add comments & remove redundant check --- src/HtmlUtils.js | 5 +++++ src/components/views/messages/TextualBody.js | 13 ++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/HtmlUtils.js b/src/HtmlUtils.js index 5e8cfc3755..aec32092ed 100644 --- a/src/HtmlUtils.js +++ b/src/HtmlUtils.js @@ -365,6 +365,11 @@ export function bodyToHtml(content, highlights, opts) { } function addCodeCopyButton(safeBody) { + // Adds 'copy' buttons to pre blocks + // Note that this only manipulates the markup to add the buttons: + // we need to add the event handlers once the nodes are in the DOM + // since we can't save functions in the markup. + // This is done in TextualBody const el = document.createElement("div"); el.innerHTML = safeBody; const codeBlocks = Array.from(el.getElementsByTagName("pre")); diff --git a/src/components/views/messages/TextualBody.js b/src/components/views/messages/TextualBody.js index 45fca566b9..d5a1977cdd 100644 --- a/src/components/views/messages/TextualBody.js +++ b/src/components/views/messages/TextualBody.js @@ -94,14 +94,13 @@ module.exports = React.createClass({ } }, 10); } + // add event handlers to the 'copy code' buttons const buttons = ReactDOM.findDOMNode(this).getElementsByClassName("mx_EventTile_copyButton"); - if (buttons.length > 0) { - for (let i = 0; i < buttons.length; i++) { - buttons[i].onclick = (e) => { - const copyCode = buttons[i].parentNode.getElementsByTagName("code")[0]; - this.copyToClipboard(copyCode.textContent); - }; - } + for (let i = 0; i < buttons.length; i++) { + buttons[i].onclick = (e) => { + const copyCode = buttons[i].parentNode.getElementsByTagName("code")[0]; + this.copyToClipboard(copyCode.textContent); + }; } } },