xml_highlight_rules.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  34. var XmlHighlightRules = function(normalize) {
  35. // http://www.w3.org/TR/REC-xml/#NT-NameChar
  36. // NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
  37. // NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
  38. var tagRegex = "[_:a-zA-Z\xc0-\uffff][-_:.a-zA-Z0-9\xc0-\uffff]*";
  39. this.$rules = {
  40. start : [
  41. {token : "string.cdata.xml", regex : "<\\!\\[CDATA\\[", next : "cdata"},
  42. {
  43. token : ["punctuation.xml-decl.xml", "keyword.xml-decl.xml"],
  44. regex : "(<\\?)(xml)(?=[\\s])", next : "xml_decl", caseInsensitive: true
  45. },
  46. {
  47. token : ["punctuation.instruction.xml", "keyword.instruction.xml"],
  48. regex : "(<\\?)(" + tagRegex + ")", next : "processing_instruction",
  49. },
  50. {token : "comment.xml", regex : "<\\!--", next : "comment"},
  51. {
  52. token : ["xml-pe.doctype.xml", "xml-pe.doctype.xml"],
  53. regex : "(<\\!)(DOCTYPE)(?=[\\s])", next : "doctype", caseInsensitive: true
  54. },
  55. {include : "tag"},
  56. {token : "text.end-tag-open.xml", regex: "</"},
  57. {token : "text.tag-open.xml", regex: "<"},
  58. {include : "reference"},
  59. {defaultToken : "text.xml"}
  60. ],
  61. xml_decl : [{
  62. token : "entity.other.attribute-name.decl-attribute-name.xml",
  63. regex : "(?:" + tagRegex + ":)?" + tagRegex + ""
  64. }, {
  65. token : "keyword.operator.decl-attribute-equals.xml",
  66. regex : "="
  67. }, {
  68. include: "whitespace"
  69. }, {
  70. include: "string"
  71. }, {
  72. token : "punctuation.xml-decl.xml",
  73. regex : "\\?>",
  74. next : "start"
  75. }],
  76. processing_instruction : [
  77. {token : "punctuation.instruction.xml", regex : "\\?>", next : "start"},
  78. {defaultToken : "instruction.xml"}
  79. ],
  80. doctype : [
  81. {include : "whitespace"},
  82. {include : "string"},
  83. {token : "xml-pe.doctype.xml", regex : ">", next : "start"},
  84. {token : "xml-pe.xml", regex : "[-_a-zA-Z0-9:]+"},
  85. {token : "punctuation.int-subset", regex : "\\[", push : "int_subset"}
  86. ],
  87. int_subset : [{
  88. token : "text.xml",
  89. regex : "\\s+"
  90. }, {
  91. token: "punctuation.int-subset.xml",
  92. regex: "]",
  93. next: "pop"
  94. }, {
  95. token : ["punctuation.markup-decl.xml", "keyword.markup-decl.xml"],
  96. regex : "(<\\!)(" + tagRegex + ")",
  97. push : [{
  98. token : "text",
  99. regex : "\\s+"
  100. },
  101. {
  102. token : "punctuation.markup-decl.xml",
  103. regex : ">",
  104. next : "pop"
  105. },
  106. {include : "string"}]
  107. }],
  108. cdata : [
  109. {token : "string.cdata.xml", regex : "\\]\\]>", next : "start"},
  110. {token : "text.xml", regex : "\\s+"},
  111. {token : "text.xml", regex : "(?:[^\\]]|\\](?!\\]>))+"}
  112. ],
  113. comment : [
  114. {token : "comment.xml", regex : "-->", next : "start"},
  115. {defaultToken : "comment.xml"}
  116. ],
  117. reference : [{
  118. token : "constant.language.escape.reference.xml",
  119. regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
  120. }],
  121. attr_reference : [{
  122. token : "constant.language.escape.reference.attribute-value.xml",
  123. regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
  124. }],
  125. tag : [{
  126. token : ["meta.tag.punctuation.tag-open.xml", "meta.tag.punctuation.end-tag-open.xml", "meta.tag.tag-name.xml"],
  127. regex : "(?:(<)|(</))((?:" + tagRegex + ":)?" + tagRegex + ")",
  128. next: [
  129. {include : "attributes"},
  130. {token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next : "start"}
  131. ]
  132. }],
  133. tag_whitespace : [
  134. {token : "text.tag-whitespace.xml", regex : "\\s+"}
  135. ],
  136. // for doctype and processing instructions
  137. whitespace : [
  138. {token : "text.whitespace.xml", regex : "\\s+"}
  139. ],
  140. // for doctype and processing instructions
  141. string: [{
  142. token : "string.xml",
  143. regex : "'",
  144. push : [
  145. {token : "string.xml", regex: "'", next: "pop"},
  146. {defaultToken : "string.xml"}
  147. ]
  148. }, {
  149. token : "string.xml",
  150. regex : '"',
  151. push : [
  152. {token : "string.xml", regex: '"', next: "pop"},
  153. {defaultToken : "string.xml"}
  154. ]
  155. }],
  156. attributes: [{
  157. token : "entity.other.attribute-name.xml",
  158. regex : "(?:" + tagRegex + ":)?" + tagRegex + ""
  159. }, {
  160. token : "keyword.operator.attribute-equals.xml",
  161. regex : "="
  162. }, {
  163. include: "tag_whitespace"
  164. }, {
  165. include: "attribute_value"
  166. }],
  167. attribute_value: [{
  168. token : "string.attribute-value.xml",
  169. regex : "'",
  170. push : [
  171. {token : "string.attribute-value.xml", regex: "'", next: "pop"},
  172. {include : "attr_reference"},
  173. {defaultToken : "string.attribute-value.xml"}
  174. ]
  175. }, {
  176. token : "string.attribute-value.xml",
  177. regex : '"',
  178. push : [
  179. {token : "string.attribute-value.xml", regex: '"', next: "pop"},
  180. {include : "attr_reference"},
  181. {defaultToken : "string.attribute-value.xml"}
  182. ]
  183. }]
  184. };
  185. if (this.constructor === XmlHighlightRules)
  186. this.normalizeRules();
  187. };
  188. (function() {
  189. this.embedTagRules = function(HighlightRules, prefix, tag){
  190. this.$rules.tag.unshift({
  191. token : ["meta.tag.punctuation.tag-open.xml", "meta.tag." + tag + ".tag-name.xml"],
  192. regex : "(<)(" + tag + "(?=\\s|>|$))",
  193. next: [
  194. {include : "attributes"},
  195. {token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next : prefix + "start"}
  196. ]
  197. });
  198. this.$rules[tag + "-end"] = [
  199. {include : "attributes"},
  200. {token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next: "start",
  201. onMatch : function(value, currentState, stack) {
  202. stack.splice(0);
  203. return this.token;
  204. }}
  205. ]
  206. this.embedRules(HighlightRules, prefix, [{
  207. token: ["meta.tag.punctuation.end-tag-open.xml", "meta.tag." + tag + ".tag-name.xml"],
  208. regex : "(</)(" + tag + "(?=\\s|>|$))",
  209. next: tag + "-end"
  210. }, {
  211. token: "string.cdata.xml",
  212. regex : "<\\!\\[CDATA\\["
  213. }, {
  214. token: "string.cdata.xml",
  215. regex : "\\]\\]>"
  216. }]);
  217. };
  218. }).call(TextHighlightRules.prototype);
  219. oop.inherits(XmlHighlightRules, TextHighlightRules);
  220. exports.XmlHighlightRules = XmlHighlightRules;
  221. });