Skip to content
This repository has been archived by the owner on May 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #165 from masarakki/remove-replaceWith
Browse files Browse the repository at this point in the history
remove-replaceWith
  • Loading branch information
masarakki authored Oct 20, 2017
2 parents 2c4919f + 9cf02bf commit acc3a50
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions app/javascript/mastodon/actions/highlight_keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,23 @@ export function refreshHighlightKeywordsSuccess(keywords) {
};
}

const replaceTextNode = (reg, node) => {
const span = document.createElement('span');
span.innerHTML = escapeHTML(node.textContent)
.replace(reg, '<span class="highlight">$1</span>');
return Array.from(span.childNodes);
};

const replaceHighlight = (reg, node) => {
for (let i = 0; i < node.childNodes.length; i++) {
const target = node.childNodes[i];
const newNode = node.cloneNode();
Array.from(node.childNodes).forEach(target => {
if (target.nodeName === '#text') {
const span = document.createElement('span');
span.innerHTML = escapeHTML(target.textContent)
.replace(reg, '<span class="highlight">$1</span>');
target.replaceWith.apply(target, span.childNodes);
replaceTextNode(reg, target).forEach(n => newNode.appendChild(n));
} else {
replaceHighlight(reg, target);
newNode.appendChild(replaceHighlight(reg, target));
}
}
});
return newNode;
};

export function addHighlight (text, keywords) {
Expand All @@ -113,10 +118,9 @@ export function addHighlight (text, keywords) {

const dom = document.createElement('div');
dom.innerHTML = text;
const newDOM = replaceHighlight(reg, dom);

replaceHighlight(reg, dom);

return dom.innerHTML;
return newDOM.innerHTML;
} else {
return text;
}
Expand Down

0 comments on commit acc3a50

Please sign in to comment.