|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { |
|
asciiAlpha, |
|
asciiAlphanumeric, |
|
markdownLineEnding, |
|
markdownLineEndingOrSpace, |
|
markdownSpace |
|
} from 'micromark-util-character' |
|
import {htmlBlockNames, htmlRawNames} from 'micromark-util-html-tag-name' |
|
import {codes} from 'micromark-util-symbol/codes.js' |
|
import {constants} from 'micromark-util-symbol/constants.js' |
|
import {types} from 'micromark-util-symbol/types.js' |
|
import {ok as assert} from 'uvu/assert' |
|
import {blankLine} from './blank-line.js' |
|
|
|
|
|
export const htmlFlow = { |
|
name: 'htmlFlow', |
|
tokenize: tokenizeHtmlFlow, |
|
resolveTo: resolveToHtmlFlow, |
|
concrete: true |
|
} |
|
|
|
|
|
const blankLineBefore = {tokenize: tokenizeBlankLineBefore, partial: true} |
|
const nonLazyContinuationStart = { |
|
tokenize: tokenizeNonLazyContinuationStart, |
|
partial: true |
|
} |
|
|
|
|
|
function resolveToHtmlFlow(events) { |
|
let index = events.length |
|
|
|
while (index--) { |
|
if ( |
|
events[index][0] === 'enter' && |
|
events[index][1].type === types.htmlFlow |
|
) { |
|
break |
|
} |
|
} |
|
|
|
if (index > 1 && events[index - 2][1].type === types.linePrefix) { |
|
|
|
events[index][1].start = events[index - 2][1].start |
|
|
|
events[index + 1][1].start = events[index - 2][1].start |
|
|
|
events.splice(index - 2, 2) |
|
} |
|
|
|
return events |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function tokenizeHtmlFlow(effects, ok, nok) { |
|
const self = this |
|
|
|
let marker |
|
|
|
let closingTag |
|
|
|
let buffer |
|
|
|
let index |
|
|
|
let markerB |
|
|
|
return start |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function start(code) { |
|
|
|
return before(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function before(code) { |
|
assert(code === codes.lessThan, 'expected `<`') |
|
effects.enter(types.htmlFlow) |
|
effects.enter(types.htmlFlowData) |
|
effects.consume(code) |
|
return open |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function open(code) { |
|
if (code === codes.exclamationMark) { |
|
effects.consume(code) |
|
return declarationOpen |
|
} |
|
|
|
if (code === codes.slash) { |
|
effects.consume(code) |
|
closingTag = true |
|
return tagCloseStart |
|
} |
|
|
|
if (code === codes.questionMark) { |
|
effects.consume(code) |
|
marker = constants.htmlInstruction |
|
|
|
|
|
|
|
|
|
|
|
return self.interrupt ? ok : continuationDeclarationInside |
|
} |
|
|
|
|
|
if (asciiAlpha(code)) { |
|
effects.consume(code) |
|
|
|
buffer = String.fromCharCode(code) |
|
return tagName |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function declarationOpen(code) { |
|
if (code === codes.dash) { |
|
effects.consume(code) |
|
marker = constants.htmlComment |
|
return commentOpenInside |
|
} |
|
|
|
if (code === codes.leftSquareBracket) { |
|
effects.consume(code) |
|
marker = constants.htmlCdata |
|
index = 0 |
|
return cdataOpenInside |
|
} |
|
|
|
|
|
if (asciiAlpha(code)) { |
|
effects.consume(code) |
|
marker = constants.htmlDeclaration |
|
|
|
|
|
return self.interrupt ? ok : continuationDeclarationInside |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function commentOpenInside(code) { |
|
if (code === codes.dash) { |
|
effects.consume(code) |
|
|
|
|
|
return self.interrupt ? ok : continuationDeclarationInside |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function cdataOpenInside(code) { |
|
const value = constants.cdataOpeningString |
|
|
|
if (code === value.charCodeAt(index++)) { |
|
effects.consume(code) |
|
|
|
if (index === value.length) { |
|
|
|
|
|
return self.interrupt ? ok : continuation |
|
} |
|
|
|
return cdataOpenInside |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function tagCloseStart(code) { |
|
if (asciiAlpha(code)) { |
|
effects.consume(code) |
|
|
|
buffer = String.fromCharCode(code) |
|
return tagName |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function tagName(code) { |
|
if ( |
|
code === codes.eof || |
|
code === codes.slash || |
|
code === codes.greaterThan || |
|
markdownLineEndingOrSpace(code) |
|
) { |
|
const slash = code === codes.slash |
|
const name = buffer.toLowerCase() |
|
|
|
if (!slash && !closingTag && htmlRawNames.includes(name)) { |
|
marker = constants.htmlRaw |
|
|
|
|
|
return self.interrupt ? ok(code) : continuation(code) |
|
} |
|
|
|
if (htmlBlockNames.includes(buffer.toLowerCase())) { |
|
marker = constants.htmlBasic |
|
|
|
if (slash) { |
|
effects.consume(code) |
|
return basicSelfClosing |
|
} |
|
|
|
|
|
|
|
return self.interrupt ? ok(code) : continuation(code) |
|
} |
|
|
|
marker = constants.htmlComplete |
|
|
|
return self.interrupt && !self.parser.lazy[self.now().line] |
|
? nok(code) |
|
: closingTag |
|
? completeClosingTagAfter(code) |
|
: completeAttributeNameBefore(code) |
|
} |
|
|
|
|
|
if (code === codes.dash || asciiAlphanumeric(code)) { |
|
effects.consume(code) |
|
buffer += String.fromCharCode(code) |
|
return tagName |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function basicSelfClosing(code) { |
|
if (code === codes.greaterThan) { |
|
effects.consume(code) |
|
|
|
|
|
return self.interrupt ? ok : continuation |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeClosingTagAfter(code) { |
|
if (markdownSpace(code)) { |
|
effects.consume(code) |
|
return completeClosingTagAfter |
|
} |
|
|
|
return completeEnd(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeNameBefore(code) { |
|
if (code === codes.slash) { |
|
effects.consume(code) |
|
return completeEnd |
|
} |
|
|
|
|
|
if (code === codes.colon || code === codes.underscore || asciiAlpha(code)) { |
|
effects.consume(code) |
|
return completeAttributeName |
|
} |
|
|
|
if (markdownSpace(code)) { |
|
effects.consume(code) |
|
return completeAttributeNameBefore |
|
} |
|
|
|
return completeEnd(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeName(code) { |
|
|
|
if ( |
|
code === codes.dash || |
|
code === codes.dot || |
|
code === codes.colon || |
|
code === codes.underscore || |
|
asciiAlphanumeric(code) |
|
) { |
|
effects.consume(code) |
|
return completeAttributeName |
|
} |
|
|
|
return completeAttributeNameAfter(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeNameAfter(code) { |
|
if (code === codes.equalsTo) { |
|
effects.consume(code) |
|
return completeAttributeValueBefore |
|
} |
|
|
|
if (markdownSpace(code)) { |
|
effects.consume(code) |
|
return completeAttributeNameAfter |
|
} |
|
|
|
return completeAttributeNameBefore(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeValueBefore(code) { |
|
if ( |
|
code === codes.eof || |
|
code === codes.lessThan || |
|
code === codes.equalsTo || |
|
code === codes.greaterThan || |
|
code === codes.graveAccent |
|
) { |
|
return nok(code) |
|
} |
|
|
|
if (code === codes.quotationMark || code === codes.apostrophe) { |
|
effects.consume(code) |
|
markerB = code |
|
return completeAttributeValueQuoted |
|
} |
|
|
|
if (markdownSpace(code)) { |
|
effects.consume(code) |
|
return completeAttributeValueBefore |
|
} |
|
|
|
return completeAttributeValueUnquoted(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeValueQuoted(code) { |
|
if (code === markerB) { |
|
effects.consume(code) |
|
markerB = null |
|
return completeAttributeValueQuotedAfter |
|
} |
|
|
|
if (code === codes.eof || markdownLineEnding(code)) { |
|
return nok(code) |
|
} |
|
|
|
effects.consume(code) |
|
return completeAttributeValueQuoted |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeValueUnquoted(code) { |
|
if ( |
|
code === codes.eof || |
|
code === codes.quotationMark || |
|
code === codes.apostrophe || |
|
code === codes.slash || |
|
code === codes.lessThan || |
|
code === codes.equalsTo || |
|
code === codes.greaterThan || |
|
code === codes.graveAccent || |
|
markdownLineEndingOrSpace(code) |
|
) { |
|
return completeAttributeNameAfter(code) |
|
} |
|
|
|
effects.consume(code) |
|
return completeAttributeValueUnquoted |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAttributeValueQuotedAfter(code) { |
|
if ( |
|
code === codes.slash || |
|
code === codes.greaterThan || |
|
markdownSpace(code) |
|
) { |
|
return completeAttributeNameBefore(code) |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeEnd(code) { |
|
if (code === codes.greaterThan) { |
|
effects.consume(code) |
|
return completeAfter |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function completeAfter(code) { |
|
if (code === codes.eof || markdownLineEnding(code)) { |
|
|
|
|
|
return continuation(code) |
|
} |
|
|
|
if (markdownSpace(code)) { |
|
effects.consume(code) |
|
return completeAfter |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuation(code) { |
|
if (code === codes.dash && marker === constants.htmlComment) { |
|
effects.consume(code) |
|
return continuationCommentInside |
|
} |
|
|
|
if (code === codes.lessThan && marker === constants.htmlRaw) { |
|
effects.consume(code) |
|
return continuationRawTagOpen |
|
} |
|
|
|
if (code === codes.greaterThan && marker === constants.htmlDeclaration) { |
|
effects.consume(code) |
|
return continuationClose |
|
} |
|
|
|
if (code === codes.questionMark && marker === constants.htmlInstruction) { |
|
effects.consume(code) |
|
return continuationDeclarationInside |
|
} |
|
|
|
if (code === codes.rightSquareBracket && marker === constants.htmlCdata) { |
|
effects.consume(code) |
|
return continuationCdataInside |
|
} |
|
|
|
if ( |
|
markdownLineEnding(code) && |
|
(marker === constants.htmlBasic || marker === constants.htmlComplete) |
|
) { |
|
effects.exit(types.htmlFlowData) |
|
return effects.check( |
|
blankLineBefore, |
|
continuationAfter, |
|
continuationStart |
|
)(code) |
|
} |
|
|
|
if (code === codes.eof || markdownLineEnding(code)) { |
|
effects.exit(types.htmlFlowData) |
|
return continuationStart(code) |
|
} |
|
|
|
effects.consume(code) |
|
return continuation |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationStart(code) { |
|
return effects.check( |
|
nonLazyContinuationStart, |
|
continuationStartNonLazy, |
|
continuationAfter |
|
)(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationStartNonLazy(code) { |
|
assert(markdownLineEnding(code)) |
|
effects.enter(types.lineEnding) |
|
effects.consume(code) |
|
effects.exit(types.lineEnding) |
|
return continuationBefore |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationBefore(code) { |
|
if (code === codes.eof || markdownLineEnding(code)) { |
|
return continuationStart(code) |
|
} |
|
|
|
effects.enter(types.htmlFlowData) |
|
return continuation(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationCommentInside(code) { |
|
if (code === codes.dash) { |
|
effects.consume(code) |
|
return continuationDeclarationInside |
|
} |
|
|
|
return continuation(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationRawTagOpen(code) { |
|
if (code === codes.slash) { |
|
effects.consume(code) |
|
buffer = '' |
|
return continuationRawEndTag |
|
} |
|
|
|
return continuation(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationRawEndTag(code) { |
|
if (code === codes.greaterThan) { |
|
const name = buffer.toLowerCase() |
|
|
|
if (htmlRawNames.includes(name)) { |
|
effects.consume(code) |
|
return continuationClose |
|
} |
|
|
|
return continuation(code) |
|
} |
|
|
|
if (asciiAlpha(code) && buffer.length < constants.htmlRawSizeMax) { |
|
effects.consume(code) |
|
|
|
buffer += String.fromCharCode(code) |
|
return continuationRawEndTag |
|
} |
|
|
|
return continuation(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationCdataInside(code) { |
|
if (code === codes.rightSquareBracket) { |
|
effects.consume(code) |
|
return continuationDeclarationInside |
|
} |
|
|
|
return continuation(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationDeclarationInside(code) { |
|
if (code === codes.greaterThan) { |
|
effects.consume(code) |
|
return continuationClose |
|
} |
|
|
|
|
|
if (code === codes.dash && marker === constants.htmlComment) { |
|
effects.consume(code) |
|
return continuationDeclarationInside |
|
} |
|
|
|
return continuation(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationClose(code) { |
|
if (code === codes.eof || markdownLineEnding(code)) { |
|
effects.exit(types.htmlFlowData) |
|
return continuationAfter(code) |
|
} |
|
|
|
effects.consume(code) |
|
return continuationClose |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function continuationAfter(code) { |
|
effects.exit(types.htmlFlow) |
|
|
|
|
|
|
|
|
|
return ok(code) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function tokenizeNonLazyContinuationStart(effects, ok, nok) { |
|
const self = this |
|
|
|
return start |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function start(code) { |
|
if (markdownLineEnding(code)) { |
|
effects.enter(types.lineEnding) |
|
effects.consume(code) |
|
effects.exit(types.lineEnding) |
|
return after |
|
} |
|
|
|
return nok(code) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function after(code) { |
|
return self.parser.lazy[self.now().line] ? nok(code) : ok(code) |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function tokenizeBlankLineBefore(effects, ok, nok) { |
|
return start |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function start(code) { |
|
assert(markdownLineEnding(code), 'expected a line ending') |
|
effects.enter(types.lineEnding) |
|
effects.consume(code) |
|
effects.exit(types.lineEnding) |
|
return effects.attempt(blankLine, ok, nok) |
|
} |
|
} |
|
|