开发者

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">
//<开发者_StackOverflow![CDATA[
$selected$$end$
//]]>
</script>]]></Code>

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


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>


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜