File size: 6,288 Bytes
89ce340 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
import { startsWith, endsWith } from 'lodash'
import type { Token } from './types'
import { childlessTags } from './tags'
interface State {
str: string
position: number
tokens: Token[]
}
const jumpPosition = (state: State, end: number) => {
const len = end - state.position
movePositopn(state, len)
}
const movePositopn = (state: State, len: number) => {
state.position = state.position + len
}
const findTextEnd = (str: string, index: number) => {
const isEnd = false
while (!isEnd) {
const textEnd = str.indexOf('<', index)
if (textEnd === -1) {
return textEnd
}
const char = str.charAt(textEnd + 1)
if (char === '/' || char === '!' || /[A-Za-z0-9]/.test(char)) {
return textEnd
}
index = textEnd + 1
}
return -1
}
const lexText = (state: State) => {
const { str } = state
let textEnd = findTextEnd(str, state.position)
if (textEnd === state.position) return
if (textEnd === -1) {
textEnd = str.length
}
const content = str.slice(state.position, textEnd)
jumpPosition(state, textEnd)
state.tokens.push({
type: 'text',
content,
})
}
const lexComment = (state: State) => {
const { str } = state
movePositopn(state, 4)
let contentEnd = str.indexOf('-->', state.position)
let commentEnd = contentEnd + 3
if (contentEnd === -1) {
contentEnd = commentEnd = str.length
}
const content = str.slice(state.position, contentEnd)
jumpPosition(state, commentEnd)
state.tokens.push({
type: 'comment',
content,
})
}
const lexTagName = (state: State) => {
const { str } = state
const len = str.length
let start = state.position
while (start < len) {
const char = str.charAt(start)
const isTagChar = !(/\s/.test(char) || char === '/' || char === '>')
if (isTagChar) break
start++
}
let end = start + 1
while (end < len) {
const char = str.charAt(end)
const isTagChar = !(/\s/.test(char) || char === '/' || char === '>')
if (!isTagChar) break
end++
}
jumpPosition(state, end)
const tagName = str.slice(start, end)
state.tokens.push({
type: 'tag',
content: tagName
})
return tagName
}
const lexTagAttributes = (state: State) => {
const { str, tokens } = state
let cursor = state.position
let quote = null
let wordBegin = cursor
const words = []
const len = str.length
while (cursor < len) {
const char = str.charAt(cursor)
if (quote) {
const isQuoteEnd = char === quote
if (isQuoteEnd) quote = null
cursor++
continue
}
const isTagEnd = char === '/' || char === '>'
if (isTagEnd) {
if (cursor !== wordBegin) words.push(str.slice(wordBegin, cursor))
break
}
const isWordEnd = /\s/.test(char)
if (isWordEnd) {
if (cursor !== wordBegin) words.push(str.slice(wordBegin, cursor))
wordBegin = cursor + 1
cursor++
continue
}
const isQuoteStart = char === '\'' || char === '"'
if (isQuoteStart) {
quote = char
cursor++
continue
}
cursor++
}
jumpPosition(state, cursor)
const type = 'attribute'
for (let i = 0; i < words.length; i++) {
const word = words[i]
const isNotPair = word.indexOf('=') === -1
if (isNotPair) {
const secondWord = words[i + 1]
if (secondWord && startsWith(secondWord, '=')) {
if (secondWord.length > 1) {
const newWord = word + secondWord
tokens.push({ type, content: newWord })
i += 1
continue
}
const thirdWord = words[i + 2]
i += 1
if (thirdWord) {
const newWord = word + '=' + thirdWord
tokens.push({ type, content: newWord })
i += 1
continue
}
}
}
if (endsWith(word, '=')) {
const secondWord = words[i + 1]
if (secondWord && secondWord.indexOf('=') === -1) {
const newWord = word + secondWord
tokens.push({ type, content: newWord })
i += 1
continue
}
const newWord = word.slice(0, -1)
tokens.push({ type, content: newWord })
continue
}
tokens.push({ type, content: word })
}
}
const lexSkipTag = (tagName: string, state: State) => {
const { str, tokens } = state
const safeTagName = tagName.toLowerCase()
const len = str.length
let index = state.position
while (index < len) {
const nextTag = str.indexOf('</', index)
if (nextTag === -1) {
lexText(state)
break
}
const tagState = {
str,
position: state.position,
tokens: [],
}
jumpPosition(tagState, nextTag)
const name = lexTag(tagState)
if (safeTagName !== name.toLowerCase()) {
index = tagState.position
continue
}
if (nextTag !== state.position) {
const textStart = state.position
jumpPosition(state, nextTag)
tokens.push({
type: 'text',
content: str.slice(textStart, nextTag),
})
}
tokens.push(...tagState.tokens)
jumpPosition(state, tagState.position)
break
}
}
const lexTag = (state: State) => {
const { str } = state
const secondChar = str.charAt(state.position + 1)
const tagStartClose = secondChar === '/'
movePositopn(state, tagStartClose ? 2 : 1)
state.tokens.push({
type: 'tag-start',
close: tagStartClose,
})
const tagName = lexTagName(state)
lexTagAttributes(state)
const firstChar = str.charAt(state.position)
const tagEndClose = firstChar === '/'
movePositopn(state, tagEndClose ? 2 : 1)
state.tokens.push({
type: 'tag-end',
close: tagEndClose,
})
return tagName
}
const lex = (state: State) => {
const str = state.str
const len = str.length
while (state.position < len) {
const start = state.position
lexText(state)
if (state.position === start) {
const isComment = startsWith(str, '!--', start + 1)
if (isComment) lexComment(state)
else {
const tagName = lexTag(state)
const safeTag = tagName.toLowerCase()
if (childlessTags.includes(safeTag)) lexSkipTag(tagName, state)
}
}
}
}
export const lexer = (str: string): Token[] => {
const state = {
str,
position: 0,
tokens: [],
}
lex(state)
return state.tokens
} |