版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

data.js 5.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. var _require = require('../helpers'),
  3. calculateTokenCharactersRange = _require.calculateTokenCharactersRange;
  4. var _require2 = require('../constants/token-types'),
  5. TOKEN_TEXT = _require2.TOKEN_TEXT,
  6. TOKEN_COMMENT_START = _require2.TOKEN_COMMENT_START;
  7. var _require3 = require('../constants/tokenizer-contexts'),
  8. OPEN_TAG_START_CONTEXT = _require3.OPEN_TAG_START_CONTEXT,
  9. CLOSE_TAG_CONTEXT = _require3.CLOSE_TAG_CONTEXT,
  10. DOCTYPE_START_CONTEXT = _require3.DOCTYPE_START_CONTEXT,
  11. COMMENT_CONTENT_CONTEXT = _require3.COMMENT_CONTENT_CONTEXT;
  12. var COMMENT_START = '<!--';
  13. function generateTextToken(state) {
  14. var range = calculateTokenCharactersRange(state, {
  15. keepBuffer: false
  16. });
  17. return {
  18. type: TOKEN_TEXT,
  19. content: state.accumulatedContent,
  20. startPosition: range.startPosition,
  21. endPosition: range.endPosition
  22. };
  23. }
  24. function openingCornerBraceWithText(state, tokens) {
  25. if (state.accumulatedContent.length !== 0) {
  26. tokens.push(generateTextToken(state));
  27. }
  28. state.accumulatedContent = state.decisionBuffer;
  29. state.decisionBuffer = '';
  30. state.currentContext = OPEN_TAG_START_CONTEXT;
  31. state.caretPosition++;
  32. }
  33. function openingCornerBraceWithSlash(state, tokens) {
  34. if (state.accumulatedContent.length !== 0) {
  35. tokens.push(generateTextToken(state));
  36. }
  37. state.accumulatedContent = state.decisionBuffer;
  38. state.decisionBuffer = '';
  39. state.currentContext = CLOSE_TAG_CONTEXT;
  40. state.caretPosition++;
  41. }
  42. function doctypeStart(state, tokens) {
  43. if (state.accumulatedContent.length !== 0) {
  44. tokens.push(generateTextToken(state));
  45. }
  46. state.accumulatedContent = state.decisionBuffer;
  47. state.decisionBuffer = '';
  48. state.currentContext = DOCTYPE_START_CONTEXT;
  49. state.caretPosition++;
  50. }
  51. function commentStart(state, tokens) {
  52. if (state.accumulatedContent.length !== 0) {
  53. tokens.push(generateTextToken(state));
  54. }
  55. var commentStartRange = {
  56. startPosition: state.caretPosition - (COMMENT_START.length - 1),
  57. endPosition: state.caretPosition
  58. };
  59. tokens.push({
  60. type: TOKEN_COMMENT_START,
  61. content: state.decisionBuffer,
  62. startPosition: commentStartRange.startPosition,
  63. endPosition: commentStartRange.endPosition
  64. });
  65. state.accumulatedContent = '';
  66. state.decisionBuffer = '';
  67. state.currentContext = COMMENT_CONTENT_CONTEXT;
  68. state.caretPosition++;
  69. }
  70. function handleContentEnd(state, tokens) {
  71. var textContent = state.accumulatedContent + state.decisionBuffer;
  72. if (textContent.length !== 0) {
  73. var range = calculateTokenCharactersRange(state, {
  74. keepBuffer: false
  75. });
  76. tokens.push({
  77. type: TOKEN_TEXT,
  78. content: textContent,
  79. startPosition: range.startPosition,
  80. endPosition: range.endPosition
  81. });
  82. }
  83. }
  84. function isIncompleteDoctype(chars) {
  85. var charsUpperCase = chars.toUpperCase();
  86. return charsUpperCase === '<!' || charsUpperCase === '<!D' || charsUpperCase === '<!DO' || charsUpperCase === '<!DOC' || charsUpperCase === '<!DOCT' || charsUpperCase === '<!DOCTY' || charsUpperCase === '<!DOCTYP';
  87. }
  88. var OPEN_TAG_START_PATTERN = /^<\w/;
  89. function parseSyntax(chars, state, tokens, nextChar, nextNoWhiteChar, fullChars, charIndex) {
  90. if (OPEN_TAG_START_PATTERN.test(chars)) {
  91. return openingCornerBraceWithText(state, tokens);
  92. }
  93. if (chars === '</') {
  94. return openingCornerBraceWithSlash(state, tokens);
  95. } // 这种方法不行,会误伤属性里的<,如<dd class="1<5">1<5</dd> <view>我是打酱油</view>
  96. // if (chars === '<') {
  97. // const restChars = fullChars ? fullChars.slice(charIndex + 1) : ''
  98. // if (nextChar !== '!' && restChars.indexOf('>') == -1) {
  99. // // 1 < 5 后面没有其他的 > ,则 当前 < 不是标签
  100. // } else if (nextChar !== '!' && restChars.indexOf('<') > -1 && restChars.indexOf('<') < restChars.indexOf('>')) {
  101. // // <div> a < 1 </div> 如果 < 后面先出现 < 后出现 > 就代表当前的 < 不是标签
  102. // } else {
  103. // state.caretPosition++
  104. // return
  105. // }
  106. // }
  107. if (chars === '<') {
  108. // <a>{{i<b}}</a> 判断前后片段,如果被{{}}包裹,就不认为是标签
  109. var preChars = fullChars ? fullChars.slice(0, charIndex - 1) : '';
  110. var restChars = fullChars ? fullChars.slice(charIndex + 1) : '';
  111. var restStartIndex = restChars.indexOf('{{');
  112. var restEndIndex = restChars.indexOf('}}');
  113. if (preChars.lastIndexOf('{{') > preChars.lastIndexOf('}}') && restEndIndex > -1 && restStartIndex == -1 || restEndIndex < restStartIndex) {// not open tag
  114. } else {
  115. if (nextNoWhiteChar.match(/[a-z]|[A-Z]|\/|\!|\-/)) {
  116. state.caretPosition++;
  117. return;
  118. }
  119. }
  120. }
  121. if (chars === '<!' || chars === '<!-') {
  122. state.caretPosition++;
  123. return;
  124. }
  125. if (chars === COMMENT_START) {
  126. return commentStart(state, tokens);
  127. }
  128. if (isIncompleteDoctype(chars)) {
  129. state.caretPosition++;
  130. return;
  131. }
  132. if (chars.toUpperCase() === '<!DOCTYPE') {
  133. return doctypeStart(state, tokens);
  134. }
  135. state.accumulatedContent += state.decisionBuffer;
  136. state.decisionBuffer = '';
  137. state.caretPosition++;
  138. }
  139. module.exports = {
  140. parseSyntax: parseSyntax,
  141. handleContentEnd: handleContentEnd
  142. };