There are two facts that you should know about editor.setData
.
- In some cases it is asynchronous (it depends on the type of editor). That's why it also accepts a callback. Therefore any code that is meant to be executed after
setData()
should be executed in that callback. - It never is asynchronous before editor is ready. In this period (between editor initialization and
instanceReady
event) it works in a different mode - it just caches the set value and ongetData()
it returns exactly that value.
So, as I see on page load you call synchronously setData()
and getData()
- your function works because you get the value you're expecting to get.
But then, when you try to getData()
when editor is already ready you get the HTML parsed, fixed, processed and perhaps differently formatted by CKEditor. I guess that your indexOf()
checks are not enough to handle this. You have to rethink your function - e.g. regexp can help.
What also can help is removing htmlwriter
plugin, which formats HTML in a way which may make it harder for you to work with it. E.g.:
config.removePlugins = 'htmlwriter';