Home Download Buy Blog Forum Support

Cannot insert CDATA into a Snippet

Cannot insert CDATA into a Snippet

Postby floatnspace on Sat Mar 31, 2012 2:54 am

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:

Code: Select all
<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
floatnspace
 
Posts: 3
Joined: Sat Mar 31, 2012 2:49 am

Re: Cannot insert CDATA into a Snippet

Postby jps on Sat Mar 31, 2012 5:04 am

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.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Cannot insert CDATA into a Snippet

Postby floatnspace on Sat Mar 31, 2012 7:45 pm

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
floatnspace
 
Posts: 3
Joined: Sat Mar 31, 2012 2:49 am

Re: Cannot insert CDATA into a Snippet

Postby jps on Sat Mar 31, 2012 10:35 pm

This is not possible, it's a fundamental limitation of XML
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Cannot insert CDATA into a Snippet

Postby agibsonsw on Sat Mar 31, 2012 11:02 pm

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 >.
Last edited by agibsonsw on Sat Mar 31, 2012 11:40 pm, edited 1 time in total.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Cannot insert CDATA into a Snippet

Postby C0D312 on Sat Mar 31, 2012 11:28 pm

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.

:P Teaser.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Cannot insert CDATA into a Snippet

Postby floatnspace on Mon Apr 02, 2012 3:19 am

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? :lol:
floatnspace
 
Posts: 3
Joined: Sat Mar 31, 2012 2:49 am

Re: Cannot insert CDATA into a Snippet

Postby jefflundberg on Fri Feb 01, 2013 4:31 pm

Try this:

Code: Select all
<snippet>
    <content><![CDATA[
<![CDATA[
   ${0:$SELECTION}${1}
${2:]]}>
]]></content>
    <description>JavaScript: CDATA</description>
    <tabTrigger>cdata</tabTrigger>
</snippet>
jefflundberg
 
Posts: 1
Joined: Fri Feb 01, 2013 4:29 pm

Re: Cannot insert CDATA into a Snippet

Postby FichteFoll on Sat Feb 02, 2013 2:53 am

FichteFoll
 
Posts: 406
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: Cannot insert CDATA into a Snippet

Postby AltIvan on Sat Apr 20, 2013 2:42 am

Found a better way:

Code: Select all
<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)

Code: Select all
]]${-1}>
AltIvan
 
Posts: 4
Joined: Mon May 21, 2012 6:51 am


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 23 guests