|
- const { calculateTokenCharactersRange } = require('../helpers')
-
- const {
- TOKEN_TEXT,
- TOKEN_COMMENT_START
- } = require('../constants/token-types')
- const {
- OPEN_TAG_START_CONTEXT,
- CLOSE_TAG_CONTEXT,
- DOCTYPE_START_CONTEXT,
- COMMENT_CONTENT_CONTEXT
- } = require('../constants/tokenizer-contexts')
-
- const COMMENT_START = '<!--'
-
- function generateTextToken (state) {
- const 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))
- }
-
- const 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) {
- const textContent = state.accumulatedContent + state.decisionBuffer
-
- if (textContent.length !== 0) {
- const range = calculateTokenCharactersRange(state, { keepBuffer: false })
-
- tokens.push({
- type: TOKEN_TEXT,
- content: textContent,
- startPosition: range.startPosition,
- endPosition: range.endPosition
- })
- }
- }
-
- function isIncompleteDoctype (chars) {
- const charsUpperCase = chars.toUpperCase()
-
- return (
- charsUpperCase === '<!'
- || charsUpperCase === '<!D'
- || charsUpperCase === '<!DO'
- || charsUpperCase === '<!DOC'
- || charsUpperCase === '<!DOCT'
- || charsUpperCase === '<!DOCTY'
- || charsUpperCase === '<!DOCTYP'
- )
- }
-
- const 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> 判断前后片段,如果被{{}}包裹,就不认为是标签
- const preChars = fullChars ? fullChars.slice(0, charIndex - 1) : ''
- const restChars = fullChars ? fullChars.slice(charIndex + 1) : ''
- const restStartIndex = restChars.indexOf('{{')
- const 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,
- handleContentEnd
- }
|