Escape Sequences

J

Jonathan Wood

The following code shows the ending </script> tag in a non-string color in
the editor, and the line produces the error "Newline in constant."

string jScript = "<script>document.forms[0].submit();</script>";

Is this a bug? When did "</" become a string escape sequence?

Thanks.
 
C

Cor Ligthert[MVP]

string jScript = "<script>document.forms[0].submit();</script>";

Is this a bug? When did "</" become a string escape sequence?
Why do you not use consequent @"x" when using syntax for other program
languages in textformat?

Seems for me wanting for bugs instead of well done development. Some simple
maintenance done by somebody else can create errors just because you seems
to lazy for that.

Cor
 
P

Peter Duniho

The following code shows the ending </script> tag in a non-string color
in the editor, and the line produces the error "Newline in constant."

string jScript = "<script>document.forms[0].submit();</script>";

Is this a bug? When did "</" become a string escape sequence?

Works fine on my computer. Are you sure that's exactly the string you're
using? Did you copy and paste it directly from your IDE? What IDE and
version are you using?

The error you're seeing usually happens when you do in fact have a newline
in the constant. That is, the literal is actually being split across two
lines. It makes me wonder if your code does in fact look like what you
posted.

Remember: if you don't post _exactly_ what you're using that causes an
error or other problem, there's no way for someone to actually help.

Pete
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
The following code shows the ending </script> tag in a non-string color in
the editor, and the line produces the error "Newline in constant."

string jScript = "<script>document.forms[0].submit();</script>";

Is this a bug? When did "</" become a string escape sequence?

As Peter says, there's no error in the above code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jonathan Wood

Peter,
The following code shows the ending </script> tag in a non-string color
in the editor, and the line produces the error "Newline in constant."

string jScript = "<script>document.forms[0].submit();</script>";

Is this a bug? When did "</" become a string escape sequence?

Works fine on my computer. Are you sure that's exactly the string you're
using? Did you copy and paste it directly from your IDE? What IDE and
version are you using?

I was afraid of that. I recently installed VS2008 Pro and the string above
was definitely copied and pasted from the editor.

Here's a bit more of my code. I guess I should have pointed out this code is
part of an ASP.NET master page, although I didn't expect that would matter:

<script runat="server">

void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();

string jScript = "<script>document.forms[0].submit();</script>";
}
}

// ...

If I change the line to:

string jScript = "<script>document.forms[0].submit();<//script>";

The string is treated as a single string as expected.

This is obviously a bug and, apparently, related to ASP.NET.
The error you're seeing usually happens when you do in fact have a newline
in the constant. That is, the literal is actually being split across two
lines. It makes me wonder if your code does in fact look like what you
posted.

Remember: if you don't post _exactly_ what you're using that causes an
error or other problem, there's no way for someone to actually help.

Yes, I'm not new to programming or, as an MVP, unaware of the issues that
arise trying to help people.
 
P

Peter Duniho

[...]
Here's a bit more of my code. I guess I should have pointed out this
code is part of an ASP.NET master page, although I didn't expect that
would matter:

Ah. Actually, while I don't know much about ASP.NET, just from what you
posted it sure looks to me like it'd matter. The text you posted looks a
lot more like some kind of XML than a C# source file. If so, then it
looks to me as though maybe the editor is doing the right thing (showing
you exactly how the text is going to be interpreted), and the </script>
tag is getting interpreted as XML even though it's in the quotes.

I admit, I haven't done a lot of XML, but my recollection is that it
pretty much ignores all the text between tags, including quotes. So, in
the XML it's going to happily suck up any characters between <script...>
including that one double quote. Then treat the </script> said:
[...]
If I change the line to:

string jScript = "<script>document.forms[0].submit();<//script>";

The string is treated as a single string as expected.

This is obviously a bug and, apparently, related to ASP.NET.

Could be. But if my theory is right, it's just an XML thing.

Barring someone else here who is more of an expert explaining whatever is
going on, I guess the best thing to do is report is as a bug. If it
actually is, great. If not, I would expect the "by design" resolution of
the bug to include an explanation describing why what you're seeing is
intended behavior.

Pete
 
J

Jon Skeet [C# MVP]

Here's a bit more of my code. I guess I should have pointed out this code is
part of an ASP.NET master page, although I didn't expect that would matter:

Yes, you certainly should have done. I'm sure it's an extra level of
escaping that ASP.NET needs so that it doesn't think that your code is
just stopping half way through the string literal.

I don't think it's a bug - after all, ASP.NET has to work out where
This is obviously a bug and, apparently, related to ASP.NET.

Why do you say it's "obviously a bug"? I suspect it's working as
designed, but you're just doing something which is naturally
problematic.

I suspect you can get round it by using "</"+"script>". Alternatively
(and preferrably, IMO) use a code-behind.
 
J

Jonathan Wood

Peter,
Ah. Actually, while I don't know much about ASP.NET, just from what you
posted it sure looks to me like it'd matter. The text you posted looks a
lot more like some kind of XML than a C# source file. If so, then it
looks to me as though maybe the editor is doing the right thing (showing
you exactly how the text is going to be interpreted), and the </script>
tag is getting interpreted as XML even though it's in the quotes.

I don't know how familiar you are with ASP.NET but it is not some sort of
XML. It is code behind the page, which ASP.NET just happens to allow you to
include in the same file as the page.

It is C# code in every respect and there is no limitation on strings just
because the code is place in an ASP.NET page.
Barring someone else here who is more of an expert explaining whatever is
going on, I guess the best thing to do is report is as a bug. If it
actually is, great. If not, I would expect the "by design" resolution of
the bug to include an explanation describing why what you're seeing is
intended behavior.

My guess is it is a bug. But perhaps I should repost the issue in the
ASP.NET group.

Thanks.
 
J

Jonathan Wood

Jon,
Why do you say it's "obviously a bug"? I suspect it's working as
designed, but you're just doing something which is naturally
problematic.

Maybe, but this is true C# here, and I should be able to put any type of
syntax within a string constant. Seems like a bug to me.

Perhaps I'll repost in an ASP.NET forum.

Thanks.
 
P

Peter Duniho

I don't know how familiar you are with ASP.NET but it is not some sort
of XML. It is code behind the page, which ASP.NET just happens to allow
you to include in the same file as the page.

Yes, but what you posted isn't just the C#, it's the page (or at least a
portion of it). And the C# doesn't get interpreted except in the context
of that page. Maybe the page is HTML instead of XML, but the basic
behavior is still applicable. It's not just C#, and you can't ignore the
fact that the C# is part of a larger document.
It is C# code in every respect and there is no limitation on strings
just because the code is place in an ASP.NET page.

It is obviously _not_ "C# code in every respect". The string you posted
works fine in a regular .cs file in a normal project, and the
more-complete code example you posted would definitely _not_ compile in a
regular .cs file in a normal project.

Just because your HTML/XML happens to include C# code, that doesn't mean
it's "C# code in every respect". If anything, I would think your
situation is definitive proof that the ASP.NET code you've got is not "C#
code in every respect". If it were, you wouldn't be having this problem.
My guess is it is a bug. But perhaps I should repost the issue in the
ASP.NET group.

Yes, I think you should. Given that the problem is not a C# problem, but
rather has something to do with the fact that you're doing ASP.NET, I
think that would be a much more appropriate place to get help. I think
you're wrong about it being a bug, but I simply don't know ASP.NET and in
fact I suspect most of the people who read this newsgroup don't. You'll
get much better advice going straight to the people who do this stuff on a
regular basis. :)

Pete
 
L

Liz

I was afraid of that. I recently installed VS2008 Pro and the string above
was definitely copied and pasted from the editor.

Here's a bit more of my code. I guess I should have pointed out this code
is part of an ASP.NET master page, although I didn't expect that would
matter:

<script runat="server">

void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();

string jScript = "<script>document.forms[0].submit();</script>";
}
}


FWIW, I don't get the error either in VS2008 Pro with your code **if I use
code-behind** ..... but if I don't (and it looks like you're not), and
bracket your code between
<script runat="server"> .... </script>, THEN there's the "Newline in
constant" error

<script runat="server">
void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();</script>";
}
}
</script>

looks like a compiler bug to me ... ignoring that your <script></script>
tags are part of a literal here

These are the messages I get:

Warning 1 This end tag has no matching start tag.

Error 2 Newline in constant

Error 3 } expected
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
Maybe, but this is true C# here, and I should be able to put any type of
syntax within a string constant. Seems like a bug to me.

The ASP.NET engine has to work out what piece of code to extract to
Perhaps I'll repost in an ASP.NET forum.

It's certainly an ASP.NET "issue" rather than a C# issue, but I still
don't think it's a bug.
 
J

Jon Skeet [C# MVP]

FWIW, I don't get the error either in VS2008 Pro with your code **if I use
code-behind**

And that's obvious the best solution here - using ASP.NET the way it
was designed to be used.
... but if I don't (and it looks like you're not), and
bracket your code between
<script runat="server"> .... </script>, THEN there's the "Newline in
constant" error

<script runat="server">
void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();</script>";
}
}
</script>

looks like a compiler bug to me ... ignoring that your <script></script>
tags are part of a literal here

Why would it be a compiler bug? The C# compiler has been passed this:

void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();</script>

That piece of code has all the problems which are reported.
 
J

Jonathan Wood

Jon,
The ASP.NET engine has to work out what piece of code to extract to
pass to the C# compiler. It does that by looking for </script>. How
would *you* expect it to do that, in a language-neutral manner?

I don't know how many languages don't use double quotes for string constants
but I wouldn't think very many.
 
J

Jonathan Wood

Right, sound like we see the same thing. Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Liz said:
I was afraid of that. I recently installed VS2008 Pro and the string
above was definitely copied and pasted from the editor.

Here's a bit more of my code. I guess I should have pointed out this code
is part of an ASP.NET master page, although I didn't expect that would
matter:

<script runat="server">

void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();

string jScript = "<script>document.forms[0].submit();</script>";
}
}


FWIW, I don't get the error either in VS2008 Pro with your code **if I use
code-behind** ..... but if I don't (and it looks like you're not), and
bracket your code between
<script runat="server"> .... </script>, THEN there's the "Newline in
constant" error

<script runat="server">
void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();</script>";
}
}
</script>

looks like a compiler bug to me ... ignoring that your <script></script>
tags are part of a literal here

These are the messages I get:

Warning 1 This end tag has no matching start tag.

Error 2 Newline in constant

Error 3 } expected
 
J

Jon Skeet [C# MVP]

Jonathan Wood said:
I don't know how many languages don't use double quotes for string
constants but I wouldn't think very many.

But in order to work out whether or not any particular piece of text is
within a string literal, you have to also know the escaping rules,
verbatim string literals in C# etc.

It's very easy to work around this limitation, and it wouldn't happen
at all if you use a code-behind. I prefer the ASP.NET engine to be
simple (and thus easier to make reliable, maintainable etc) than overly
complicated to overcome a limitation which is so easy to work round on
the developer side.
 
L

Liz

Why would it be a compiler bug? The C# compiler has been passed this:
void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();</script>

That piece of code has all the problems which are reported.


no, it was passed:

string jScript = "<script>document.forms[0].submit();</script>";

why should that generate an error, anymore than:

void myMethod()
{
string s = "I put a } character in here;";
}

should generate an error?
 
J

Jon Skeet [C# MVP]

Liz said:
Why would it be a compiler bug? The C# compiler has been passed this:
void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();</script>

That piece of code has all the problems which are reported.

no, it was passed:

string jScript = "<script>document.forms[0].submit();</script>";

What makes you say that? That wouldn't explain the errors that were
produced, whereas my (corrected below) version does.

I do have a correction, though: I believe it was actually passed:

void Page_Load()
{
if (!Page.IsPostBack)
{
litYear.Text = DateTime.Now.Year.ToString();
string jScript = "<script>document.forms[0].submit();

In other words said:
why should that generate an error, anymore than:

void myMethod()
{
string s = "I put a } character in here;";
}

should generate an error?

That's *just* C# code - the only thing that has to work with it is the
C# compiler. Compare that with the ASP.NET situation: the ASP.NET
engine has to pick out bit of code within <script runat="server"> and
</script> tags, and pass those off to another compiler. It doesn't
understand the contents of the element.

So, without any direct knowledge of the language it's working with, how
should it find out which is a "real" </script> and which is one which
actually just happens to be part of the code?

This is always going to be a problem when embedding code from one
language within another - there has to be a way of detecting where the
embedded code ends. It looks like the ASP.NET engine just looks for the
first </script> after the opening tag, and passes everything between
the two to the appropriate language compiler.

Compared with any other mechanism it would have to use to find the end
of the code block, I'm pretty comfortable with the decision that's been
made.
 
R

Registered User

The following code shows the ending </script> tag in a non-string color in
the editor, and the line produces the error "Newline in constant."

string jScript = "<script>document.forms[0].submit();</script>";

Is this a bug? When did "</" become a string escape sequence?

Most strange.

I don't know how the script is being written to the page but my
preference is to use the Page.ClientScript.RegisterClientScriptBlock
method.

regards
A.G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top