handlebars.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. define(["require", "exports"], function (require, exports) {
  6. 'use strict';
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. // Allow for running under nodejs/requirejs in tests
  9. var _monaco = (typeof monaco === 'undefined' ? self.monaco : monaco);
  10. var EMPTY_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'];
  11. exports.conf = {
  12. wordPattern: /(-?\d*\.\d\w*)|([^\`\~\!\@\$\^\&\*\(\)\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s]+)/g,
  13. comments: {
  14. blockComment: ['{{!--', '--}}']
  15. },
  16. brackets: [
  17. ['<!--', '-->'],
  18. ['<', '>'],
  19. ['{{', '}}'],
  20. ['{', '}'],
  21. ['(', ')']
  22. ],
  23. autoClosingPairs: [
  24. { open: '{', close: '}' },
  25. { open: '[', close: ']' },
  26. { open: '(', close: ')' },
  27. { open: '"', close: '"' },
  28. { open: '\'', close: '\'' }
  29. ],
  30. surroundingPairs: [
  31. { open: '<', close: '>' },
  32. { open: '"', close: '"' },
  33. { open: '\'', close: '\'' }
  34. ],
  35. onEnterRules: [
  36. {
  37. beforeText: new RegExp("<(?!(?:" + EMPTY_ELEMENTS.join('|') + "))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$", 'i'),
  38. afterText: /^<\/(\w[\w\d]*)\s*>$/i,
  39. action: { indentAction: _monaco.languages.IndentAction.IndentOutdent }
  40. },
  41. {
  42. beforeText: new RegExp("<(?!(?:" + EMPTY_ELEMENTS.join('|') + "))(\\w[\\w\\d]*)([^/>]*(?!/)>)[^<]*$", 'i'),
  43. action: { indentAction: _monaco.languages.IndentAction.Indent }
  44. }
  45. ],
  46. };
  47. exports.language = {
  48. defaultToken: '',
  49. tokenPostfix: '',
  50. // ignoreCase: true,
  51. // The main tokenizer for our languages
  52. tokenizer: {
  53. root: [
  54. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.root' }],
  55. [/<!DOCTYPE/, 'metatag.html', '@doctype'],
  56. [/<!--/, 'comment.html', '@comment'],
  57. [/(<)(\w+)(\/>)/, ['delimiter.html', 'tag.html', 'delimiter.html']],
  58. [/(<)(script)/, ['delimiter.html', { token: 'tag.html', next: '@script' }]],
  59. [/(<)(style)/, ['delimiter.html', { token: 'tag.html', next: '@style' }]],
  60. [/(<)([:\w]+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  61. [/(<\/)(\w+)/, ['delimiter.html', { token: 'tag.html', next: '@otherTag' }]],
  62. [/</, 'delimiter.html'],
  63. [/\{/, 'delimiter.html'],
  64. [/[^<{]+/] // text
  65. ],
  66. doctype: [
  67. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.comment' }],
  68. [/[^>]+/, 'metatag.content.html'],
  69. [/>/, 'metatag.html', '@pop'],
  70. ],
  71. comment: [
  72. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.comment' }],
  73. [/-->/, 'comment.html', '@pop'],
  74. [/[^-]+/, 'comment.content.html'],
  75. [/./, 'comment.content.html']
  76. ],
  77. otherTag: [
  78. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.otherTag' }],
  79. [/\/?>/, 'delimiter.html', '@pop'],
  80. [/"([^"]*)"/, 'attribute.value'],
  81. [/'([^']*)'/, 'attribute.value'],
  82. [/[\w\-]+/, 'attribute.name'],
  83. [/=/, 'delimiter'],
  84. [/[ \t\r\n]+/],
  85. ],
  86. // -- BEGIN <script> tags handling
  87. // After <script
  88. script: [
  89. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.script' }],
  90. [/type/, 'attribute.name', '@scriptAfterType'],
  91. [/"([^"]*)"/, 'attribute.value'],
  92. [/'([^']*)'/, 'attribute.value'],
  93. [/[\w\-]+/, 'attribute.name'],
  94. [/=/, 'delimiter'],
  95. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.text/javascript', nextEmbedded: 'text/javascript' }],
  96. [/[ \t\r\n]+/],
  97. [/(<\/)(script\s*)(>)/, ['delimiter.html', 'tag.html', { token: 'delimiter.html', next: '@pop' }]]
  98. ],
  99. // After <script ... type
  100. scriptAfterType: [
  101. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.scriptAfterType' }],
  102. [/=/, 'delimiter', '@scriptAfterTypeEquals'],
  103. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.text/javascript', nextEmbedded: 'text/javascript' }],
  104. [/[ \t\r\n]+/],
  105. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  106. ],
  107. // After <script ... type =
  108. scriptAfterTypeEquals: [
  109. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.scriptAfterTypeEquals' }],
  110. [/"([^"]*)"/, { token: 'attribute.value', switchTo: '@scriptWithCustomType.$1' }],
  111. [/'([^']*)'/, { token: 'attribute.value', switchTo: '@scriptWithCustomType.$1' }],
  112. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.text/javascript', nextEmbedded: 'text/javascript' }],
  113. [/[ \t\r\n]+/],
  114. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  115. ],
  116. // After <script ... type = $S2
  117. scriptWithCustomType: [
  118. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.scriptWithCustomType.$S2' }],
  119. [/>/, { token: 'delimiter.html', next: '@scriptEmbedded.$S2', nextEmbedded: '$S2' }],
  120. [/"([^"]*)"/, 'attribute.value'],
  121. [/'([^']*)'/, 'attribute.value'],
  122. [/[\w\-]+/, 'attribute.name'],
  123. [/=/, 'delimiter'],
  124. [/[ \t\r\n]+/],
  125. [/<\/script\s*>/, { token: '@rematch', next: '@pop' }]
  126. ],
  127. scriptEmbedded: [
  128. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInEmbeddedState.scriptEmbedded.$S2', nextEmbedded: '@pop' }],
  129. [/<\/script/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }]
  130. ],
  131. // -- END <script> tags handling
  132. // -- BEGIN <style> tags handling
  133. // After <style
  134. style: [
  135. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.style' }],
  136. [/type/, 'attribute.name', '@styleAfterType'],
  137. [/"([^"]*)"/, 'attribute.value'],
  138. [/'([^']*)'/, 'attribute.value'],
  139. [/[\w\-]+/, 'attribute.name'],
  140. [/=/, 'delimiter'],
  141. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.text/css', nextEmbedded: 'text/css' }],
  142. [/[ \t\r\n]+/],
  143. [/(<\/)(style\s*)(>)/, ['delimiter.html', 'tag.html', { token: 'delimiter.html', next: '@pop' }]]
  144. ],
  145. // After <style ... type
  146. styleAfterType: [
  147. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.styleAfterType' }],
  148. [/=/, 'delimiter', '@styleAfterTypeEquals'],
  149. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.text/css', nextEmbedded: 'text/css' }],
  150. [/[ \t\r\n]+/],
  151. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  152. ],
  153. // After <style ... type =
  154. styleAfterTypeEquals: [
  155. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.styleAfterTypeEquals' }],
  156. [/"([^"]*)"/, { token: 'attribute.value', switchTo: '@styleWithCustomType.$1' }],
  157. [/'([^']*)'/, { token: 'attribute.value', switchTo: '@styleWithCustomType.$1' }],
  158. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.text/css', nextEmbedded: 'text/css' }],
  159. [/[ \t\r\n]+/],
  160. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  161. ],
  162. // After <style ... type = $S2
  163. styleWithCustomType: [
  164. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInSimpleState.styleWithCustomType.$S2' }],
  165. [/>/, { token: 'delimiter.html', next: '@styleEmbedded.$S2', nextEmbedded: '$S2' }],
  166. [/"([^"]*)"/, 'attribute.value'],
  167. [/'([^']*)'/, 'attribute.value'],
  168. [/[\w\-]+/, 'attribute.name'],
  169. [/=/, 'delimiter'],
  170. [/[ \t\r\n]+/],
  171. [/<\/style\s*>/, { token: '@rematch', next: '@pop' }]
  172. ],
  173. styleEmbedded: [
  174. [/\{\{/, { token: '@rematch', switchTo: '@handlebarsInEmbeddedState.styleEmbedded.$S2', nextEmbedded: '@pop' }],
  175. [/<\/style/, { token: '@rematch', next: '@pop', nextEmbedded: '@pop' }]
  176. ],
  177. // -- END <style> tags handling
  178. handlebarsInSimpleState: [
  179. [/\{\{\{?/, 'delimiter.handlebars'],
  180. [/\}\}\}?/, { token: 'delimiter.handlebars', switchTo: '@$S2.$S3' }],
  181. { include: 'handlebarsRoot' }
  182. ],
  183. handlebarsInEmbeddedState: [
  184. [/\{\{\{?/, 'delimiter.handlebars'],
  185. [/\}\}\}?/, { token: 'delimiter.handlebars', switchTo: '@$S2.$S3', nextEmbedded: '$S3' }],
  186. { include: 'handlebarsRoot' }
  187. ],
  188. handlebarsRoot: [
  189. [/"[^"]*"/, 'string.handlebars'],
  190. [/[#/][^\s}]+/, 'keyword.helper.handlebars'],
  191. [/else\b/, 'keyword.helper.handlebars'],
  192. [/[\s]+/],
  193. [/[^}]/, 'variable.parameter.handlebars'],
  194. ],
  195. },
  196. };
  197. });