I’m working in a project I was requested to make and it must work in other browsers the same way as expected in IE8.
This is a system used to translate HTML into BBCode and vice-versa.
My current problem is to translate HTML into BBCode (BBCode into HTML is solved (I hope)).
I have, as an example, this kind of input to translate (this is a spoiler)
<DL contenteditable=false><DT>Spoiler:</DT><DD class=codebox contenteditable=true>someTextToBeSpoiled</DD></DL>
if it has multiple lines it’s even worse!
<DL contenteditable=false><DT>Spoiler:</DT><DD class=codebox contenteditable=true>Line1</DD><DD class=codebox contenteditable=true>Line2</DD><DD class=codebox contenteditable=true>line3</DD></DL>
What ever code I must use for this translation it must be (kinda) rewritable into a generalist code that works for almost any HTML structure so is supposed to work with this specific code for the spoiler tag the same way it works with any other code. I do know where the data is in each one of them.
To try to solve this for ie8 (for others I use a better solution using methods not available in ie8 and, BTW, they solve it quite fast).
` var theOuterHTML = document.createElement(‘div’);
theOuterHTML.innerHTML = “
” + “” + “”;
var searchContent = regexEscape(theOuterHTML.innerHTML).replace("<!\\-\\- !!@here1!!# \\-\\->", "(.*?)")
searchContent = searchContent.replace("\n", "");
new RegExp(searchContent, "gm").exec(param1['context'].outerHTML)[1]
`
The problem is that the var searchContent gets the following after all that code (replace is there but seem to do nothing):
<DL contenteditable=false> (yes, with those weird new lines with no reason to exist)
<DT>Spoiler:</DT>
<DD class=codebox contenteditable=true>someTextToBeSpoiled</DD></DL>
The replace seem to do nothing about those new lines and adding new lines into the input is not possible (due to the unpredictability of ie8).
The end text that should exist is:
[spoiler]someTextToBeSpoiled[/spoiler]
How can such thing be solved? I’ve been puzzling in this all day 🙁
This becomes quite hackish but I believe I found a possible solution.
The problem with IE8 is that is adds \r\n after some tags for a reason I just don’t understand
Unlike what it says in the spec, if you use
.replace("\n", "")this is not a global replace, ie8 does.replace(/\n/, "")instead of.replace(/\n/gm, ""). Also, because it adds a \r\n and not just \n, that replacement is not enough, that’s what has mostly been puzzling me. In order to get a proper replacement you need.replace(/\r\n/mg, "")(no need for the pipe character, it’s not an OR it’s the \r followed by the \n).In the end, this may and probably is hackish but that’s the way to deal with IE8 and before, as far as I know….
By using
.replace(/\r\n/mg, "")I was able to get a match in the exec() method.