/**
* This Tokenizer is adapted from htmlparser2 under the MIT License listed at
* https://github.com/fb55/htmlparser2/blob/master/LICENSE
Copyright 2010, 2011, Chris Winberry . All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
import {
EntityDecoder,
DecodingMode,
htmlDecodeTree
} from 'entities/lib/decode.js'
import { ElementNode, Position } from '../ast'
export const enum ParseMode {
BASE,
HTML,
SFC
}
export const enum CharCodes {
Tab = 0x9, // "\t"
NewLine = 0xa, // "\n"
FormFeed = 0xc, // "\f"
CarriageReturn = 0xd, // "\r"
Space = 0x20, // " "
ExclamationMark = 0x21, // "!"
Number = 0x23, // "#"
Amp = 0x26, // "&"
SingleQuote = 0x27, // "'"
DoubleQuote = 0x22, // '"'
Dash = 0x2d, // "-"
Slash = 0x2f, // "/"
Zero = 0x30, // "0"
Nine = 0x39, // "9"
Semi = 0x3b, // ";"
Lt = 0x3c, // "<"
Eq = 0x3d, // "="
Gt = 0x3e, // ">"
Questionmark = 0x3f, // "?"
UpperA = 0x41, // "A"
LowerA = 0x61, // "a"
UpperF = 0x46, // "F"
LowerF = 0x66, // "f"
UpperZ = 0x5a, // "Z"
LowerZ = 0x7a, // "z"
LowerX = 0x78, // "x"
OpeningSquareBracket = 0x5b, // "["
LowerV = 0x76, // "v"
Dot = 0x2e, // "."
Colon = 0x3a, // ":"
At = 0x40, // "@"
LeftSqaure = 91, // "["
RightSquare = 93 // "]"
}
const defaultDelimitersOpen = new Uint8Array([123, 123]) // "{{"
const defaultDelimitersClose = new Uint8Array([125, 125]) // "}}"
/** All the states the tokenizer can be in. */
const enum State {
Text = 1,
Interpolation,
// Tags
BeforeTagName, // After <
InTagName,
InSelfClosingTag,
BeforeClosingTagName,
InClosingTagName,
AfterClosingTagName,
// Attributes
BeforeAttributeName,
InAttributeName,
InDirectiveName,
InDirectiveArg,
InDirectiveDynamicArg,
InDirectiveModifier,
AfterAttributeName,
BeforeAttributeValue,
InAttributeValueDq, // "
InAttributeValueSq, // '
InAttributeValueNq,
// Declarations
BeforeDeclaration, // !
InDeclaration,
// Processing instructions
InProcessingInstruction, // ?
// Comments & CDATA
BeforeComment,
CDATASequence,
InSpecialComment,
InCommentLike,
// Special tags
BeforeSpecialS, // Decide if we deal with `
this.emitCodePoint(cp, consumed)
)
}
public mode = ParseMode.BASE
public reset(): void {
this.state = State.Text
this.mode = ParseMode.BASE
this.buffer = ''
this.sectionStart = 0
this.index = 0
this.baseState = State.Text
this.currentSequence = undefined!
this.newlines.length = 0
this.delimiterOpen = defaultDelimitersOpen
this.delimiterClose = defaultDelimitersClose
}
/**
* Generate Position object with line / column information using recorded
* newline positions. We know the index is always going to be an already
* processed index, so all the newlines up to this index should have been
* recorded.
*/
public getPos(index: number): Position {
let line = 1
let column = index + 1
for (let i = this.newlines.length - 1; i >= 0; i--) {
const newlineIndex = this.newlines[i]
if (index > newlineIndex) {
line = i + 2
column = index - newlineIndex
break
}
}
return {
column,
line,
offset: index
}
}
private stateText(c: number): void {
if (c === CharCodes.Lt) {
if (this.index > this.sectionStart) {
this.cbs.ontext(this.sectionStart, this.index)
}
this.state = State.BeforeTagName
this.sectionStart = this.index
} else if (c === CharCodes.Amp) {
this.startEntity()
} else if (this.matchDelimiter(c, this.delimiterOpen)) {
if (this.index > this.sectionStart) {
this.cbs.ontext(this.sectionStart, this.index)
}
this.state = State.Interpolation
this.sectionStart = this.index
this.index += this.delimiterOpen.length
}
}
public delimiterOpen: Uint8Array = defaultDelimitersOpen
public delimiterClose: Uint8Array = defaultDelimitersClose
private matchDelimiter(c: number, delimiter: Uint8Array): boolean {
if (c === delimiter[0]) {
const l = delimiter.length
for (let i = 1; i < l; i++) {
if (this.buffer.charCodeAt(this.index + i) !== delimiter[i]) {
return false
}
}
return true
}
return false
}
private stateInterpolation(c: number): void {
if (this.matchDelimiter(c, this.delimiterClose)) {
this.index += this.delimiterClose.length
this.cbs.oninterpolation(this.sectionStart, this.index)
this.state = State.Text
this.sectionStart = this.index
this.stateText(this.buffer.charCodeAt(this.index))
}
}
private currentSequence: Uint8Array = undefined!
private sequenceIndex = 0
private stateSpecialStartSequence(c: number): void {
const isEnd = this.sequenceIndex === this.currentSequence.length
const isMatch = isEnd
? // If we are at the end of the sequence, make sure the tag name has ended
isEndOfTagSection(c)
: // Otherwise, do a case-insensitive comparison
(c | 0x20) === this.currentSequence[this.sequenceIndex]
if (!isMatch) {
this.isSpecial = false
} else if (!isEnd) {
this.sequenceIndex++
return
}
this.sequenceIndex = 0
this.state = State.InTagName
this.stateInTagName(c)
}
/** Look for an end tag. For and