|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- "use strict";
-
- var _require = require('../helpers'),
- calculateTokenCharactersRange = _require.calculateTokenCharactersRange;
-
- var _require2 = require('../constants/token-types'),
- TOKEN_TEXT = _require2.TOKEN_TEXT,
- TOKEN_COMMENT_START = _require2.TOKEN_COMMENT_START;
-
- var _require3 = require('../constants/tokenizer-contexts'),
- OPEN_TAG_START_CONTEXT = _require3.OPEN_TAG_START_CONTEXT,
- CLOSE_TAG_CONTEXT = _require3.CLOSE_TAG_CONTEXT,
- DOCTYPE_START_CONTEXT = _require3.DOCTYPE_START_CONTEXT,
- COMMENT_CONTENT_CONTEXT = _require3.COMMENT_CONTENT_CONTEXT;
-
- var COMMENT_START = '<!--';
-
- function generateTextToken(state) {
- var range = calculateTokenCharactersRange(state, {
- keepBuffer: false
- });
- return {
- type: TOKEN_TEXT,
- content: state.accumulatedContent,
- startPosition: range.startPosition,
- endPosition: range.endPosition
- };
- }
-
- function openingCornerBraceWithText(state, tokens) {
- if (state.accumulatedContent.length !== 0) {
- tokens.push(generateTextToken(state));
- }
-
- state.accumulatedContent = state.decisionBuffer;
- state.decisionBuffer = '';
- state.currentContext = OPEN_TAG_START_CONTEXT;
- state.caretPosition++;
- }
-
- function openingCornerBraceWithSlash(state, tokens) {
- if (state.accumulatedContent.length !== 0) {
- tokens.push(generateTextToken(state));
- }
-
- state.accumulatedContent = state.decisionBuffer;
- state.decisionBuffer = '';
- state.currentContext = CLOSE_TAG_CONTEXT;
- state.caretPosition++;
- }
-
- function doctypeStart(state, tokens) {
- if (state.accumulatedContent.length !== 0) {
- tokens.push(generateTextToken(state));
- }
-
- state.accumulatedContent = state.decisionBuffer;
- state.decisionBuffer = '';
- state.currentContext = DOCTYPE_START_CONTEXT;
- state.caretPosition++;
- }
-
- function commentStart(state, tokens) {
- if (state.accumulatedContent.length !== 0) {
- tokens.push(generateTextToken(state));
- }
-
- var commentStartRange = {
- startPosition: state.caretPosition - (COMMENT_START.length - 1),
- endPosition: state.caretPosition
- };
- tokens.push({
- type: TOKEN_COMMENT_START,
- content: state.decisionBuffer,
- startPosition: commentStartRange.startPosition,
- endPosition: commentStartRange.endPosition
- });
- state.accumulatedContent = '';
- state.decisionBuffer = '';
- state.currentContext = COMMENT_CONTENT_CONTEXT;
- state.caretPosition++;
- }
-
- function handleContentEnd(state, tokens) {
- var textContent = state.accumulatedContent + state.decisionBuffer;
-
- if (textContent.length !== 0) {
- var range = calculateTokenCharactersRange(state, {
- keepBuffer: false
- });
- tokens.push({
- type: TOKEN_TEXT,
- content: textContent,
- startPosition: range.startPosition,
- endPosition: range.endPosition
- });
- }
- }
-
- function isIncompleteDoctype(chars) {
- var charsUpperCase = chars.toUpperCase();
- return charsUpperCase === '<!' || charsUpperCase === '<!D' || charsUpperCase === '<!DO' || charsUpperCase === '<!DOC' || charsUpperCase === '<!DOCT' || charsUpperCase === '<!DOCTY' || charsUpperCase === '<!DOCTYP';
- }
-
- var OPEN_TAG_START_PATTERN = /^<\w/;
-
- function parseSyntax(chars, state, tokens, nextChar, nextNoWhiteChar, fullChars, charIndex) {
- if (OPEN_TAG_START_PATTERN.test(chars)) {
- return openingCornerBraceWithText(state, tokens);
- }
-
- if (chars === '</') {
- return openingCornerBraceWithSlash(state, tokens);
- } // 这种方法不行,会误伤属性里的<,如<dd class="1<5">1<5</dd> <view>我是打酱油</view>
- // if (chars === '<') {
- // const restChars = fullChars ? fullChars.slice(charIndex + 1) : ''
- // if (nextChar !== '!' && restChars.indexOf('>') == -1) {
- // // 1 < 5 后面没有其他的 > ,则 当前 < 不是标签
- // } else if (nextChar !== '!' && restChars.indexOf('<') > -1 && restChars.indexOf('<') < restChars.indexOf('>')) {
- // // <div> a < 1 </div> 如果 < 后面先出现 < 后出现 > 就代表当前的 < 不是标签
- // } else {
- // state.caretPosition++
- // return
- // }
- // }
-
-
- if (chars === '<') {
- // <a>{{i<b}}</a> 判断前后片段,如果被{{}}包裹,就不认为是标签
- var preChars = fullChars ? fullChars.slice(0, charIndex - 1) : '';
- var restChars = fullChars ? fullChars.slice(charIndex + 1) : '';
- var restStartIndex = restChars.indexOf('{{');
- var restEndIndex = restChars.indexOf('}}');
-
- if (preChars.lastIndexOf('{{') > preChars.lastIndexOf('}}') && restEndIndex > -1 && restStartIndex == -1 || restEndIndex < restStartIndex) {// not open tag
- } else {
- if (nextNoWhiteChar.match(/[a-z]|[A-Z]|\/|\!|\-/)) {
- state.caretPosition++;
- return;
- }
- }
- }
-
- if (chars === '<!' || chars === '<!-') {
- state.caretPosition++;
- return;
- }
-
- if (chars === COMMENT_START) {
- return commentStart(state, tokens);
- }
-
- if (isIncompleteDoctype(chars)) {
- state.caretPosition++;
- return;
- }
-
- if (chars.toUpperCase() === '<!DOCTYPE') {
- return doctypeStart(state, tokens);
- }
-
- state.accumulatedContent += state.decisionBuffer;
- state.decisionBuffer = '';
- state.caretPosition++;
- }
-
- module.exports = {
- parseSyntax: parseSyntax,
- handleContentEnd: handleContentEnd
- };
|