Sublime Forum

Cannot insert CDATA into a Snippet

#1

I can’t figure out a way to insert CDATA inside a ST2 Snippet. I use a lot of snippets for XML, and many of them contain their own CDATA blocks.

For instance, this snippet won’t work, because the CDATA closure inside the Snippet’s own CDATA causes a conflict:

<snippet>
	<content><![CDATA[
<not_working>
  <code><![CDATA[ this cdata block breaks the snippet... ]]></code>
</not_working>
]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<!-- <tabTrigger>hello</tabTrigger> -->
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<!-- <scope>source.python</scope> -->
</snippet>

Is there any way for me to escape my own CDATA blocks inside ST2 Snippets?

Thanks

0 Likes

#2

Nested CDATA sections are not permitted in XML. Your best bet is to remove the outer CDATA section, and escape the characters within as required.

0 Likes

#3

Ugh, okay. I think I’ll probably just use a placeholder like [CDATA] [/CDATA] and then search + replace those placeholders after the snippet is inserted.

Is there a place I can make a feature request to support something like this in future versions? I’m sure I’m not the only one that would like to be able to have CDATA sections in their XML snippets.

Thanks

0 Likes

#4

This is not possible, it’s a fundamental limitation of XML

0 Likes

#5

You probably don’t need to escape every character. Escaping < ! and possibly > may be sufficient so that it is no longer treated as XML. A backslash - e.g. < - escapes characters. Sometimes you need to double-escape \.

If it still causes problems you can instead try the hex code for the characters. \x3c for < \x3e for >.

0 Likes

#6

You could just use a SMART Snippet for this. It doesn’t have the XML limitations. Oh wait… I haven’t release it yet. Nevermind.

:stuck_out_tongue: Teaser.

0 Likes

#7

Yeah, my snippets are all XML and are fairly large, so escaping all the tags and such would be more work than it’s worth.

@C0D312 – Is a SMART Snippet plugin something you’re really working on, or are you just messing with us? :laughing:

0 Likes

#8

Try this:

<snippet>
    <content><![CDATA[
<![CDATA[ 
	${0:$SELECTION}${1}
${2:]]}> 
]]></content>
    <description>JavaScript: CDATA</description>
    <tabTrigger>cdata</tabTrigger>
</snippet>
0 Likes

#9

stackoverflow.com/questions/2236 … ken-in-xml

Had to use this for SaneSnippets (relevant code).

0 Likes

#10

Found a better way:

<snippet> <content><![CDATA[<snippet> <content><![CDATA[${0:snippet...};]]${-1}></content> <tabTrigger>${1:comando...}</tabTrigger> <scope>source.${2:formato...}</scope> <description>${3:descripcion...}</description> </snippet>]]></content> <tabTrigger>sn</tabTrigger> <description>Create snippet</description> </snippet>

So just using ${-1} affter the doble brackets of the end of CDATA it works perfectly (without creating another insertion point)

]]${-1}>
0 Likes