Ok, I'm using the CKEditor in a web application. One thing I need to do it set the text in the text area. I've been using the line:
CKEDITOR.instances.setData(html);
...where html is a varible containing HTML.
This works fine in Chrome & Firefox, but not at all in Internet Explorer or Safari.
Can anyone provide an insight as to why, or suggest a work-around?
Many thanks in advance! :-)
Make sure to strip all newlines from the string you pass into setData(). An exception is thrown if you don't, with a message about an unterminated string. The newline characters used by CKEditor are the UNIX-style of \n (in other words, not the DOS version: \r\n).
The newline apparently throws off the parser, making it think that it's the end of the statement.
Also note that if you call getData() to get that value you just set again, CKEditor puts the line breaks and tabs back into it. You'll need to strip them out again if you need to set that value back using setData(). I use a regexp pattern like this to strip out the newlines (and tabs just for completeness):
[\n\t]+
Also make sure that if you use the regular expression to strip them, you need to make sure that the pattern matching will match the \n character (called "single-line" mode in .NET, but I don't know what you're using).
Related
I use browserless.js (headless Chrome) to fetch the html code of a website, and then use a regular expression to find certain image URLs.
One example is the following:
https://vignette.wikia.nocookie.net/moviepedia/images/8/88/Adrien_Brody.jpg/revision/latest/top-crop/width/360/height/450?cb\u003d20141113231800\u0026path-prefix\u003dde
There are unicode characters such as \u003d, which should be decoded (in this case to =). The reason is that I want to include these images in a site, and without decoding some of them cannot be displayed (like that one above, just paste the URL; it gives broken-image.webp).
I have tried lots of things, but nothing works.
JSON.parse(JSON.stringify(...))
String.prototype.normalize()
decodeURIComponent
Curiously, the regular expression for "\u003d" (i.e. "\\u003d" in js) does not match that string above, but "u003d" does.
This is all very weird, and my current guess is that browserless is responsible for some weird formatting behind the scenes. Namely, when I console log the URL and copy paste it somewhere else, every method mentioned above works for decoding.
I hope that someone can help me on this.
Just to mark this one as answered. Thomas replied:
JSON.parse(`"${url}"`)
I have a regular expression testing for numbers(0-9) and/or forward slashes (/). It looks like this:
/^[0-9/]+$/i.test(value)
Now I believe this to be correct, but the eclipse javascript validator disagrees:
Syntax error on token "]", delete this token
I suppose this is because the separator/delimiter is / and eclipse 'thinks' the regex is finished (and therefore a ] would be unexpected).
We can satisfy eclipse by escaping the / like so:
/^[0-9\/]+$/i.test(value)
Note that both versions work for me.
My problem with this is:
As far as I know I do not need to escape the forward slash specifically in that range. It might be situation specific (as in, for javascript it is the used delimiter).
Although they both appear to be working, I'd rather use the 'correct' version because of behaviour in different environments, and, well.. because correct and all :)
Does anyone know what I'm supposed to do? Escape or not? I did not find any reputable site that told me to escape the / in a range, but the Eclipse-validator is probably not completely stupid...
The standard clearly says you can put anything unescaped in a character class except \, ] and newline:
RegularExpressionClassChar ::
RegularExpressionNonTerminator but not ] or \
RegularExpressionBackslashSequence
RegularExpressionNonTerminator ::
SourceCharacter but not LineTerminator
( http://es5.github.com/#x7.8.5 ). No need to escape /.
On the other side, I personally would escape everything when in doubt, just to make less smart parsers happy.
I'm trying to build my regular expression, but I've failed.
I'm trying to get everything after single or double dash (you can try it here):
var regEx = /(?<=[-{1,2}])[^-]\S*/g;
It works just fine, but it selects even if we have 3+ dashes too. I've tried something like /(?<=^[-{1,2}])[^-]\S*/g and /(?<=\b[-{1,2}])[^-]\S*/g, but then it crashes at all.
Thanks in advance.
Unfortunately javascript doesn't support lookbehind
You can use this regex with multiline option
^-{1,2}(?!-)(\S*)
After this you can use group 1 to access the required match..
I always have noticed this, including in versions before as well. About half way through jQuery's compressed version you'll see some regex:
[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^
The error appears to be at ['"]
I'm sure it's not really a syntax error, but all my code editors recognize it as one, which makes development a pain if I try to combine JavaScript files. Does anyone know what's going on here?
It's a character entity in a regular expression, as specified by the square brackets. There are no restrictions on quote characters in them. All that's going on is buggy syntax highlighting. Ask the developer of your editor.
your code editor sucks, this isnt a syntax error if its inside of a regex literal, which i suspect it is.
the code editor you use probably doesnt support regex literals properly, and that its a string , which would cause the error
This is partly related to the age-old unix vs. windows newline LF/CRLF dilemma. I don't love my solution, but I have most of that figured out... (nonetheless any general guidance related to cross-browser newlines appreciated!). FF can send data from html textareas in a way that the newlines are stored in the db consistently with IE (and my Java client). The other clients can read these FF-authored strings newline and all.
My main remaining issue is how to correctly retrieve and display the strings in FF. How can I identify newlines in xml attributes? For example, I have an xhr returning a set of form fields to render via javascript:
<field name="desc" displayname="Description" value="i
am
in
ff" type="string" length="240"> </field>
(I haven't confirmed, but I'm pretty sure those are CRLFs in the value attribute - same as I stored previously...)
This value will get rendered into a textarea.
But when I try to read the attribute in FF,
var value = fieldNode.getAttribute( "value" );
I get value="i am in ff"
I know if i convert the CRLF to \n on the server, FF will work; but i think this may cause problems for our other clients. I'd like to find a client-side and client-specific solution.
If I could detect the newlines, I could substitute in or whatever. I just need a way to detect them.
ps - no responses after more than a month... why am I the only one with this problem?? guess I'll have to figure it out myself :(
Just in case anyone else stumbles on this in the futre...
This thread helped get me on the right track: How to save newlines in XML attribute?
As it turns out, CRLF, CR, and LF should get encoded in proper xml CRLF = & #xD;& #xA;). I was previously not encoding these. IE is tolerant of that, while other browsers are not. E.g., Firefox's XML parser converts any newline character to a space. Now that i am encoding the newlines, all is well...
I know I am a bit late, but is it possible that you could have put attribute value in the tags?
Something like this: (NB the CDATA to allow to use '<' & '>' inside the field tags.)
<field>
<![CDATA[ i <br/>
am <br/>
in <br/>
ff ]]>
</field>
You could then have used some javascript to get the nodeValue:
getElementsByTagName("field")[0].childNodes[0].nodeValue;
NB it can be easier in XML not to use Attributes, but to use elements (see here)