Sublime Forum

[SOLVED] Embedded JS in XML syntax highlighting

#1

Is there any way to make syntax highlighting work with JavaScript code which is embedded into XML?

<file name="admin/view/template/sale/order_list.tpl">
    <operation>
      <search position="before"><![CDATA[<?php echo $footer; ?>]]></search>
      <add><![CDATA[        
<script type="text/javascript">   
$(document).ready(function() {
    setOptions(); // on load
         ]]></add>
    </operation>
</file>
0 Likes

#2

In case all all CDATA tags contain the same syntax, you may use something like …

Packages/User/XML (PHP).sublime-syntax

%YAML 1.2
---
name: XML (PHP)
scope: text.xml.php

contexts:

  main:
    - include: cdata
    - include: scope:text.xml

  cdata:
    - match: (<!\[)(CDATA)(\[)
      scope: meta.tag.sgml.cdata.xml
      captures:
        1: punctuation.definition.tag.begin.xml
        2: keyword.declaration.cdata.xml
        3: punctuation.definition.tag.begin.xml
      embed: scope:embedding.php
      embed_scope: meta.tag.sgml.cdata.xml
      escape: ']]>'
      escape_captures:
        0: meta.tag.sgml.cdata.xml punctuation.definition.tag.end.xml

Note:

  1. scope:embedding.php is embedded as I already saw PHP tags.
  2. You may also use scope:text.html instead if only <script type="text/javascript"> is to be supported.
  3. You may use scope:source.js in case CDATA tags contain JavaScript only.
  4. This solution removes string scope from all CDATA tags.
1 Like

#3

Sounds cool @deathaxe, only problem that I get error:

0 Likes

#4

The filename should have a sublime-syntax extension; your file has an extension of sublime-snippet tacked onto the end of it accidentally.

0 Likes

#5

Sir, thank you very much. It works flawlessly now.

1 Like