/ src / theme / languages / xml.js
xml.js
  1  /*! `xml` grammar compiled for Highlight.js 11.10.0 */
  2    (function(){
  3      var hljsGrammar = (function () {
  4    'use strict';
  5  
  6    /*
  7    Language: HTML, XML
  8    Website: https://www.w3.org/XML/
  9    Category: common, web
 10    Audit: 2020
 11    */
 12  
 13    /** @type LanguageFn */
 14    function xml(hljs) {
 15      const regex = hljs.regex;
 16      // XML names can have the following additional letters: https://www.w3.org/TR/xml/#NT-NameChar
 17      // OTHER_NAME_CHARS = /[:\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]/;
 18      // Element names start with NAME_START_CHAR followed by optional other Unicode letters, ASCII digits, hyphens, underscores, and periods
 19      // const TAG_NAME_RE = regex.concat(/[A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/, regex.optional(/[A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]*:/), /[A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]*/);;
 20      // const XML_IDENT_RE = /[A-Z_a-z:\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]+/;
 21      // const TAG_NAME_RE = regex.concat(/[A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/, regex.optional(/[A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]*:/), /[A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]*/);
 22      // however, to cater for performance and more Unicode support rely simply on the Unicode letter class
 23      const TAG_NAME_RE = regex.concat(/[\p{L}_]/u, regex.optional(/[\p{L}0-9_.-]*:/u), /[\p{L}0-9_.-]*/u);
 24      const XML_IDENT_RE = /[\p{L}0-9._:-]+/u;
 25      const XML_ENTITIES = {
 26        className: 'symbol',
 27        begin: /&[a-z]+;|&#[0-9]+;|&#x[a-f0-9]+;/
 28      };
 29      const XML_META_KEYWORDS = {
 30        begin: /\s/,
 31        contains: [
 32          {
 33            className: 'keyword',
 34            begin: /#?[a-z_][a-z1-9_-]+/,
 35            illegal: /\n/
 36          }
 37        ]
 38      };
 39      const XML_META_PAR_KEYWORDS = hljs.inherit(XML_META_KEYWORDS, {
 40        begin: /\(/,
 41        end: /\)/
 42      });
 43      const APOS_META_STRING_MODE = hljs.inherit(hljs.APOS_STRING_MODE, { className: 'string' });
 44      const QUOTE_META_STRING_MODE = hljs.inherit(hljs.QUOTE_STRING_MODE, { className: 'string' });
 45      const TAG_INTERNALS = {
 46        endsWithParent: true,
 47        illegal: /</,
 48        relevance: 0,
 49        contains: [
 50          {
 51            className: 'attr',
 52            begin: XML_IDENT_RE,
 53            relevance: 0
 54          },
 55          {
 56            begin: /=\s*/,
 57            relevance: 0,
 58            contains: [
 59              {
 60                className: 'string',
 61                endsParent: true,
 62                variants: [
 63                  {
 64                    begin: /"/,
 65                    end: /"/,
 66                    contains: [ XML_ENTITIES ]
 67                  },
 68                  {
 69                    begin: /'/,
 70                    end: /'/,
 71                    contains: [ XML_ENTITIES ]
 72                  },
 73                  { begin: /[^\s"'=<>`]+/ }
 74                ]
 75              }
 76            ]
 77          }
 78        ]
 79      };
 80      return {
 81        name: 'HTML, XML',
 82        aliases: [
 83          'html',
 84          'xhtml',
 85          'rss',
 86          'atom',
 87          'xjb',
 88          'xsd',
 89          'xsl',
 90          'plist',
 91          'wsf',
 92          'svg'
 93        ],
 94        case_insensitive: true,
 95        unicodeRegex: true,
 96        contains: [
 97          {
 98            className: 'meta',
 99            begin: /<![a-z]/,
100            end: />/,
101            relevance: 10,
102            contains: [
103              XML_META_KEYWORDS,
104              QUOTE_META_STRING_MODE,
105              APOS_META_STRING_MODE,
106              XML_META_PAR_KEYWORDS,
107              {
108                begin: /\[/,
109                end: /\]/,
110                contains: [
111                  {
112                    className: 'meta',
113                    begin: /<![a-z]/,
114                    end: />/,
115                    contains: [
116                      XML_META_KEYWORDS,
117                      XML_META_PAR_KEYWORDS,
118                      QUOTE_META_STRING_MODE,
119                      APOS_META_STRING_MODE
120                    ]
121                  }
122                ]
123              }
124            ]
125          },
126          hljs.COMMENT(
127            /<!--/,
128            /-->/,
129            { relevance: 10 }
130          ),
131          {
132            begin: /<!\[CDATA\[/,
133            end: /\]\]>/,
134            relevance: 10
135          },
136          XML_ENTITIES,
137          // xml processing instructions
138          {
139            className: 'meta',
140            end: /\?>/,
141            variants: [
142              {
143                begin: /<\?xml/,
144                relevance: 10,
145                contains: [
146                  QUOTE_META_STRING_MODE
147                ]
148              },
149              {
150                begin: /<\?[a-z][a-z0-9]+/,
151              }
152            ]
153  
154          },
155          {
156            className: 'tag',
157            /*
158            The lookahead pattern (?=...) ensures that 'begin' only matches
159            '<style' as a single word, followed by a whitespace or an
160            ending bracket.
161            */
162            begin: /<style(?=\s|>)/,
163            end: />/,
164            keywords: { name: 'style' },
165            contains: [ TAG_INTERNALS ],
166            starts: {
167              end: /<\/style>/,
168              returnEnd: true,
169              subLanguage: [
170                'css',
171                'xml'
172              ]
173            }
174          },
175          {
176            className: 'tag',
177            // See the comment in the <style tag about the lookahead pattern
178            begin: /<script(?=\s|>)/,
179            end: />/,
180            keywords: { name: 'script' },
181            contains: [ TAG_INTERNALS ],
182            starts: {
183              end: /<\/script>/,
184              returnEnd: true,
185              subLanguage: [
186                'javascript',
187                'handlebars',
188                'xml'
189              ]
190            }
191          },
192          // we need this for now for jSX
193          {
194            className: 'tag',
195            begin: /<>|<\/>/
196          },
197          // open tag
198          {
199            className: 'tag',
200            begin: regex.concat(
201              /</,
202              regex.lookahead(regex.concat(
203                TAG_NAME_RE,
204                // <tag/>
205                // <tag>
206                // <tag ...
207                regex.either(/\/>/, />/, /\s/)
208              ))
209            ),
210            end: /\/?>/,
211            contains: [
212              {
213                className: 'name',
214                begin: TAG_NAME_RE,
215                relevance: 0,
216                starts: TAG_INTERNALS
217              }
218            ]
219          },
220          // close tag
221          {
222            className: 'tag',
223            begin: regex.concat(
224              /<\//,
225              regex.lookahead(regex.concat(
226                TAG_NAME_RE, />/
227              ))
228            ),
229            contains: [
230              {
231                className: 'name',
232                begin: TAG_NAME_RE,
233                relevance: 0
234              },
235              {
236                begin: />/,
237                relevance: 0,
238                endsParent: true
239              }
240            ]
241          }
242        ]
243      };
244    }
245  
246    return xml;
247  
248  })();
249  
250      hljs.registerLanguage('xml', hljsGrammar);
251    })();