90c4e5ffeb673aeb076277d3a2a47eb7b532535db2ff5564a0a3f9a072b8a9cdf886a9927aa91b8498b05b790d0ae925e2fdc705a24deb256a53b3b50790de 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*jshint node:true */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. 'use strict';
  24. var BaseTokenizer = require('../core/tokenizer').Tokenizer;
  25. var BASETOKEN = require('../core/tokenizer').TOKEN;
  26. var Directives = require('../core/directives').Directives;
  27. var TemplatablePattern = require('../core/templatablepattern').TemplatablePattern;
  28. var Pattern = require('../core/pattern').Pattern;
  29. var TOKEN = {
  30. TAG_OPEN: 'TK_TAG_OPEN',
  31. TAG_CLOSE: 'TK_TAG_CLOSE',
  32. ATTRIBUTE: 'TK_ATTRIBUTE',
  33. EQUALS: 'TK_EQUALS',
  34. VALUE: 'TK_VALUE',
  35. COMMENT: 'TK_COMMENT',
  36. TEXT: 'TK_TEXT',
  37. UNKNOWN: 'TK_UNKNOWN',
  38. START: BASETOKEN.START,
  39. RAW: BASETOKEN.RAW,
  40. EOF: BASETOKEN.EOF
  41. };
  42. var directives_core = new Directives(/<\!--/, /-->/);
  43. var Tokenizer = function(input_string, options) {
  44. BaseTokenizer.call(this, input_string, options);
  45. this._current_tag_name = '';
  46. // Words end at whitespace or when a tag starts
  47. // if we are indenting handlebars, they are considered tags
  48. var templatable_reader = new TemplatablePattern(this._input).read_options(this._options);
  49. var pattern_reader = new Pattern(this._input);
  50. this.__patterns = {
  51. word: templatable_reader.until(/[\n\r\t <]/),
  52. single_quote: templatable_reader.until_after(/'/),
  53. double_quote: templatable_reader.until_after(/"/),
  54. attribute: templatable_reader.until(/[\n\r\t =>]|\/>/),
  55. element_name: templatable_reader.until(/[\n\r\t >\/]/),
  56. handlebars_comment: pattern_reader.starting_with(/{{!--/).until_after(/--}}/),
  57. handlebars: pattern_reader.starting_with(/{{/).until_after(/}}/),
  58. handlebars_open: pattern_reader.until(/[\n\r\t }]/),
  59. handlebars_raw_close: pattern_reader.until(/}}/),
  60. comment: pattern_reader.starting_with(/<!--/).until_after(/-->/),
  61. cdata: pattern_reader.starting_with(/<!\[CDATA\[/).until_after(/]]>/),
  62. // https://en.wikipedia.org/wiki/Conditional_comment
  63. conditional_comment: pattern_reader.starting_with(/<!\[/).until_after(/]>/),
  64. processing: pattern_reader.starting_with(/<\?/).until_after(/\?>/)
  65. };
  66. if (this._options.indent_handlebars) {
  67. this.__patterns.word = this.__patterns.word.exclude('handlebars');
  68. }
  69. this._unformatted_content_delimiter = null;
  70. if (this._options.unformatted_content_delimiter) {
  71. var literal_regexp = this._input.get_literal_regexp(this._options.unformatted_content_delimiter);
  72. this.__patterns.unformatted_content_delimiter =
  73. pattern_reader.matching(literal_regexp)
  74. .until_after(literal_regexp);
  75. }
  76. };
  77. Tokenizer.prototype = new BaseTokenizer();
  78. Tokenizer.prototype._is_comment = function(current_token) { // jshint unused:false
  79. return false; //current_token.type === TOKEN.COMMENT || current_token.type === TOKEN.UNKNOWN;
  80. };
  81. Tokenizer.prototype._is_opening = function(current_token) {
  82. return current_token.type === TOKEN.TAG_OPEN;
  83. };
  84. Tokenizer.prototype._is_closing = function(current_token, open_token) {
  85. return current_token.type === TOKEN.TAG_CLOSE &&
  86. (open_token && (
  87. ((current_token.text === '>' || current_token.text === '/>') && open_token.text[0] === '<') ||
  88. (current_token.text === '}}' && open_token.text[0] === '{' && open_token.text[1] === '{')));
  89. };
  90. Tokenizer.prototype._reset = function() {
  91. this._current_tag_name = '';
  92. };
  93. Tokenizer.prototype._get_next_token = function(previous_token, open_token) { // jshint unused:false
  94. var token = null;
  95. this._readWhitespace();
  96. var c = this._input.peek();
  97. if (c === null) {
  98. return this._create_token(TOKEN.EOF, '');
  99. }
  100. token = token || this._read_open_handlebars(c, open_token);
  101. token = token || this._read_attribute(c, previous_token, open_token);
  102. token = token || this._read_close(c, open_token);
  103. token = token || this._read_raw_content(c, previous_token, open_token);
  104. token = token || this._read_content_word(c);
  105. token = token || this._read_comment_or_cdata(c);
  106. token = token || this._read_processing(c);
  107. token = token || this._read_open(c, open_token);
  108. token = token || this._create_token(TOKEN.UNKNOWN, this._input.next());
  109. return token;
  110. };
  111. Tokenizer.prototype._read_comment_or_cdata = function(c) { // jshint unused:false
  112. var token = null;
  113. var resulting_string = null;
  114. var directives = null;
  115. if (c === '<') {
  116. var peek1 = this._input.peek(1);
  117. // We treat all comments as literals, even more than preformatted tags
  118. // we only look for the appropriate closing marker
  119. if (peek1 === '!') {
  120. resulting_string = this.__patterns.comment.read();
  121. // only process directive on html comments
  122. if (resulting_string) {
  123. directives = directives_core.get_directives(resulting_string);
  124. if (directives && directives.ignore === 'start') {
  125. resulting_string += directives_core.readIgnored(this._input);
  126. }
  127. } else {
  128. resulting_string = this.__patterns.cdata.read();
  129. }
  130. }
  131. if (resulting_string) {
  132. token = this._create_token(TOKEN.COMMENT, resulting_string);
  133. token.directives = directives;
  134. }
  135. }
  136. return token;
  137. };
  138. Tokenizer.prototype._read_processing = function(c) { // jshint unused:false
  139. var token = null;
  140. var resulting_string = null;
  141. var directives = null;
  142. if (c === '<') {
  143. var peek1 = this._input.peek(1);
  144. if (peek1 === '!' || peek1 === '?') {
  145. resulting_string = this.__patterns.conditional_comment.read();
  146. resulting_string = resulting_string || this.__patterns.processing.read();
  147. }
  148. if (resulting_string) {
  149. token = this._create_token(TOKEN.COMMENT, resulting_string);
  150. token.directives = directives;
  151. }
  152. }
  153. return token;
  154. };
  155. Tokenizer.prototype._read_open = function(c, open_token) {
  156. var resulting_string = null;
  157. var token = null;
  158. if (!open_token) {
  159. if (c === '<') {
  160. resulting_string = this._input.next();
  161. if (this._input.peek() === '/') {
  162. resulting_string += this._input.next();
  163. }
  164. resulting_string += this.__patterns.element_name.read();
  165. token = this._create_token(TOKEN.TAG_OPEN, resulting_string);
  166. }
  167. }
  168. return token;
  169. };
  170. Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
  171. var resulting_string = null;
  172. var token = null;
  173. if (!open_token) {
  174. if (this._options.indent_handlebars && c === '{' && this._input.peek(1) === '{') {
  175. if (this._input.peek(2) === '!') {
  176. resulting_string = this.__patterns.handlebars_comment.read();
  177. resulting_string = resulting_string || this.__patterns.handlebars.read();
  178. token = this._create_token(TOKEN.COMMENT, resulting_string);
  179. } else {
  180. resulting_string = this.__patterns.handlebars_open.read();
  181. token = this._create_token(TOKEN.TAG_OPEN, resulting_string);
  182. }
  183. }
  184. }
  185. return token;
  186. };
  187. Tokenizer.prototype._read_close = function(c, open_token) {
  188. var resulting_string = null;
  189. var token = null;
  190. if (open_token) {
  191. if (open_token.text[0] === '<' && (c === '>' || (c === '/' && this._input.peek(1) === '>'))) {
  192. resulting_string = this._input.next();
  193. if (c === '/') { // for close tag "/>"
  194. resulting_string += this._input.next();
  195. }
  196. token = this._create_token(TOKEN.TAG_CLOSE, resulting_string);
  197. } else if (open_token.text[0] === '{' && c === '}' && this._input.peek(1) === '}') {
  198. this._input.next();
  199. this._input.next();
  200. token = this._create_token(TOKEN.TAG_CLOSE, '}}');
  201. }
  202. }
  203. return token;
  204. };
  205. Tokenizer.prototype._read_attribute = function(c, previous_token, open_token) {
  206. var token = null;
  207. var resulting_string = '';
  208. if (open_token && open_token.text[0] === '<') {
  209. if (c === '=') {
  210. token = this._create_token(TOKEN.EQUALS, this._input.next());
  211. } else if (c === '"' || c === "'") {
  212. var content = this._input.next();
  213. if (c === '"') {
  214. content += this.__patterns.double_quote.read();
  215. } else {
  216. content += this.__patterns.single_quote.read();
  217. }
  218. token = this._create_token(TOKEN.VALUE, content);
  219. } else {
  220. resulting_string = this.__patterns.attribute.read();
  221. if (resulting_string) {
  222. if (previous_token.type === TOKEN.EQUALS) {
  223. token = this._create_token(TOKEN.VALUE, resulting_string);
  224. } else {
  225. token = this._create_token(TOKEN.ATTRIBUTE, resulting_string);
  226. }
  227. }
  228. }
  229. }
  230. return token;
  231. };
  232. Tokenizer.prototype._is_content_unformatted = function(tag_name) {
  233. // void_elements have no content and so cannot have unformatted content
  234. // script and style tags should always be read as unformatted content
  235. // finally content_unformatted and unformatted element contents are unformatted
  236. return this._options.void_elements.indexOf(tag_name) === -1 &&
  237. (this._options.content_unformatted.indexOf(tag_name) !== -1 ||
  238. this._options.unformatted.indexOf(tag_name) !== -1);
  239. };
  240. Tokenizer.prototype._read_raw_content = function(c, previous_token, open_token) { // jshint unused:false
  241. var resulting_string = '';
  242. if (open_token && open_token.text[0] === '{') {
  243. resulting_string = this.__patterns.handlebars_raw_close.read();
  244. } else if (previous_token.type === TOKEN.TAG_CLOSE &&
  245. previous_token.opened.text[0] === '<' && previous_token.text[0] !== '/') {
  246. // ^^ empty tag has no content
  247. var tag_name = previous_token.opened.text.substr(1).toLowerCase();
  248. if (tag_name === 'script' || tag_name === 'style') {
  249. // Script and style tags are allowed to have comments wrapping their content
  250. // or just have regular content.
  251. var token = this._read_comment_or_cdata(c);
  252. if (token) {
  253. token.type = TOKEN.TEXT;
  254. return token;
  255. }
  256. resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
  257. } else if (this._is_content_unformatted(tag_name)) {
  258. resulting_string = this._input.readUntil(new RegExp('</' + tag_name + '[\\n\\r\\t ]*?>', 'ig'));
  259. }
  260. }
  261. if (resulting_string) {
  262. return this._create_token(TOKEN.TEXT, resulting_string);
  263. }
  264. return null;
  265. };
  266. Tokenizer.prototype._read_content_word = function(c) {
  267. var resulting_string = '';
  268. if (this._options.unformatted_content_delimiter) {
  269. if (c === this._options.unformatted_content_delimiter[0]) {
  270. resulting_string = this.__patterns.unformatted_content_delimiter.read();
  271. }
  272. }
  273. if (!resulting_string) {
  274. resulting_string = this.__patterns.word.read();
  275. }
  276. if (resulting_string) {
  277. return this._create_token(TOKEN.TEXT, resulting_string);
  278. }
  279. };
  280. module.exports.Tokenizer = Tokenizer;
  281. module.exports.TOKEN = TOKEN;