This rewrite uses DOMDocument rather than manually editing the HTML string.
We drill down to the right text node based on the required offset of the span,
split it in the necessary places and wrap it in the new node. We then don't
need to worry about the new tags causing offset changes, since the DOM model
doesn't count the tags as text.
It should be much more reliable; it's certainly much easier to read and
understand, in my opinion.
This patch also closes a couple of potential issues and one definite issue: we
skip over spans which are invalid (either improperly nested or which end
before they start).
There are commented out throw new \Exception lines where warnings could be
emitted. I don't want to throw exceptions because I still want some rendered
HTML.
This rewrite uses DOMDocument rather than manually editing the HTML string.
We drill down to the right text node based on the required offset of the span, split it in the necessary places and wrap it in the new node. We then don't need to worry about the new tags causing offset changes, since the DOM model doesn't count the tags as text.
It should be much more reliable; it's certainly much easier to read and understand, in my opinion.
This patch also closes a couple of potential issues and one definite issue: we skip over spans which are invalid (either improperly nested or which end before they start).
There are commented out
throw new \Exception
lines where warnings could be emitted. I don't want to throw exceptions because I still want some rendered HTML.