In my app, I want the user to be able to select a bunch of text and highlight it, similar to "marker" features in word/libreoffice etc.
I got some limited success with this fragment, but it fails when selecting more complex (= block elements?) content:
let sel = document.getSelection()
let range = sel.getRangeAt(0)
let marker = document.createElement('span')
marker.className = 'highlight'
range.surroundContents(marker)
range.collapse()
So - how do I get from a selection or range to a modified HTML where every text node within is wrapped in a highlight marker? Note that I don't care too much if the structure of the selection needs to be rewritten, as long as the resulting layout stays the same.
A working sample of the above can be found here: http://jsfiddle.net/7g5nb94w/