Dan Pettersson
Dan Pettersson

Reputation: 743

Can I include CDATA section in a VS 2010 HTML snippet?

When using the HTML snippet "script" in VS 2010 I get:

<script type="text/javascript"> </script>

I would like the snippet to render the following result:

<script type="text/javascript">
//<![CDATA[

//]]>
</script>

I have looked at the .snippet file for this and found the following "Code" element:

    <Code Language="html"><![CDATA[<script type="text/javascript">$selected$$end$</script>]]></Code>

and I realized it might be a bit tricky to get this to work, i.e. this doesn't:

<Code Language="html"><![CDATA[<script type="text/javascript">
//<![CDATA[
$selected$$end$
//]]>
</script>]]></Code>

Does anyone with a bit more XML knowledge than me know if this is doable?

Upvotes: 3

Views: 705

Answers (3)

Trevor Jones
Trevor Jones

Reputation: 21

I found the solution you were looking for in your snippet just do this.

<Code Language="html">

<![CDATA[<script type="text/javascript">
<![CDATA[
$selected$]]$end$>
</script>]]>

</Code>

Upvotes: 2

Joseph Gabriel
Joseph Gabriel

Reputation: 8510

In order to render a CDATA section within a VS snippet Code element, you need to forego the Code element's CDATA section that is normally used and escape the whole content. If you try to encode just the first portion, the snippet engine recognizes the first un-escaped CDATA section, and doesn't render that.

In your case the snippet Code element should look like this (assuming you have a $script$ parameter defined):

<Code Language="html">
    &lt;script type=&quot;text/javascript&quot;&gt;
    //&lt;![CDATA[
      $script$
    //]]&gt;
    &lt;/script&gt;
</Code>

That will produce the following result

<script type="text/javascript">
    //<![CDATA[
        script goes here
    //]]>
</script>

Upvotes: 0

Mark Avenius
Mark Avenius

Reputation: 13947

From w3schools: Nested CDATA sections are not allowed.

But it is an interesting question.

Edit: The following will work from the xml (snippet) side of things, but I'm not sure how the result will be interpreted by a browser:

<Code Language="html"><![CDATA[<script type="text/javascript">
    //<![CDATA[
        $selected$$end$
    //]]/>
</script>]]></Code>

(note the slash before the closing caret)

This will produce the following output:

<script type="text/javascript">
//<![CDATA[

//]]/>
</script>

This may be something you want to experiment with; as I said, I am not certain how it will be treated in the end.

Upvotes: 0

Related Questions