"Format and indent" command introduces javascript errors

Having trouble installing Oxygen? Got a bug to report? Post it all here.
sampablokuper
Posts: 22
Joined: Tue Apr 08, 2008 1:41 pm

"Format and indent" command introduces javascript errors

Post by sampablokuper »

I'm having the difficulty that the "Format and indent" command sometimes alters javascript in a way that breaks the functionality of the code.

Suppose my document is something like this:

Code: Select all


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Foo</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
</head>
<body>
<script type="text/javascript">
deHashRegex = /([\s\S]*?)#/g;
locNoHash = deHashRegex.exec(document.location);
</script>
<p>(Main body content.)</p>
</body>
</html>
If I then use the "format and indent" command, the result is the following:

Code: Select all


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Foo</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
</head>
<body>
<script type="text/javascript">
deHashRegex = /([\s\S] * ?) #/g;
locNoHash = deHashRegex.exec(document.location);</script>
<p>(Main body content.)</p>
</body>
</html>
OK, the XHTML looks great now, but what about the javascript? Well, the deHashRegex regular expression has been corrupted. Because of the whitespace introduced between some its characters, it will no longer work in browsers. Essentially, oXygen's parser has confused a regular expression for a mathematical one, and has put spaces either side of the asterisk accordingly, and has placed a space after the closing parenthesis.

Does anyone have a solution to this, other than commenting out the javascript or putting it in a CDATA block (either of which would mean I wouldn't get syntax highlighting for the javascript any more)?

Many thanks,

Sam
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: "Format and indent" command introduces javascript errors

Post by sorin_ristache »

Hello,

Thank you for reporting this problem. We will fix it in the next version of oXygen.

I think there is no other workaround. You should put the JavaScript code in a CDATA block.


Regards,
Sorin
sampablokuper
Posts: 22
Joined: Tue Apr 08, 2008 1:41 pm

Re: "Format and indent" command introduces javascript errors

Post by sampablokuper »

sorin wrote: Thank you for reporting this problem. We will fix it in the next version of oXygen.

I think there is no other workaround. You should put the JavaScript code in a CDATA block.
Dear Sorin,

Thanks for the quick reply and the advice. I'll use CDATA for the time being.

All best,

Sam
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: "Format and indent" command introduces javascript errors

Post by sorin_ristache »

Hello,

Now you can disable the option Allow formatting embedded JavaScript from Options -> Preferences -> Editor -> Format -> JavaScript and the JavaScript embedded in an XML file will not be formatted anymore.


Regards,
Sorin
Post Reply