If you want to remove the <font>
element with the specific color
attribute of #0000ff
, then you can simply search for the element iteratively within the selectionContents
node, and then remove the node if it matches your criteria:
var fontEls = selectionContents.querySelectorAll('font');
fontEls.forEach(function(fontEl) {
if (fontEl.color === '#0000ff')
fontEl.remove();
});
See proof-of-concept below (and check your console):
document.querySelector('button').addEventListener('click', function() {
var range = window.getSelection().getRangeAt(0);
var selectionContents = range.cloneContents();
var fontEls = selectionContents.querySelectorAll('font');
fontEls.forEach(function(fontEl) {
if (fontEl.color === '#0000ff')
fontEl.remove();
});
console.log(selectionContents);
});
<div>
There, <b>Samuel<font color="#0000ff"> Smith</font><font color="#ff0000"> Smith</font><font color="#00ff00"> Smith</font></b>
</div>
<button type="button">Get selection</button>