123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984 |
- /**
- * @typedef {import('micromark-util-types').Code} Code
- * @typedef {import('micromark-util-types').Construct} Construct
- * @typedef {import('micromark-util-types').Resolver} Resolver
- * @typedef {import('micromark-util-types').State} State
- * @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
- * @typedef {import('micromark-util-types').Tokenizer} Tokenizer
- */
- import {
- asciiAlpha,
- asciiAlphanumeric,
- markdownLineEnding,
- markdownLineEndingOrSpace,
- markdownSpace
- } from 'micromark-util-character'
- import {htmlBlockNames, htmlRawNames} from 'micromark-util-html-tag-name'
- import {codes, constants, types} from 'micromark-util-symbol'
- import {ok as assert} from 'devlop'
- import {blankLine} from './blank-line.js'
- /** @type {Construct} */
- export const htmlFlow = {
- name: 'htmlFlow',
- tokenize: tokenizeHtmlFlow,
- resolveTo: resolveToHtmlFlow,
- concrete: true
- }
- /** @type {Construct} */
- const blankLineBefore = {tokenize: tokenizeBlankLineBefore, partial: true}
- const nonLazyContinuationStart = {
- tokenize: tokenizeNonLazyContinuationStart,
- partial: true
- }
- /** @type {Resolver} */
- 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) {
- // Add the prefix start to the HTML token.
- events[index][1].start = events[index - 2][1].start
- // Add the prefix start to the HTML line token.
- events[index + 1][1].start = events[index - 2][1].start
- // Remove the line prefix.
- events.splice(index - 2, 2)
- }
- return events
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeHtmlFlow(effects, ok, nok) {
- const self = this
- /** @type {number} */
- let marker
- /** @type {boolean} */
- let closingTag
- /** @type {string} */
- let buffer
- /** @type {number} */
- let index
- /** @type {Code} */
- let markerB
- return start
- /**
- * Start of HTML (flow).
- *
- * ```markdown
- * > | <x />
- * ^
- * ```
- *
- * @type {State}
- */
- function start(code) {
- // To do: parse indent like `markdown-rs`.
- return before(code)
- }
- /**
- * At `<`, after optional whitespace.
- *
- * ```markdown
- * > | <x />
- * ^
- * ```
- *
- * @type {State}
- */
- function before(code) {
- assert(code === codes.lessThan, 'expected `<`')
- effects.enter(types.htmlFlow)
- effects.enter(types.htmlFlowData)
- effects.consume(code)
- return open
- }
- /**
- * After `<`, at tag name or other stuff.
- *
- * ```markdown
- * > | <x />
- * ^
- * > | <!doctype>
- * ^
- * > | <!--xxx-->
- * ^
- * ```
- *
- * @type {State}
- */
- 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
- // To do:
- // tokenizer.concrete = true
- // To do: use `markdown-rs` style interrupt.
- // While we’re in an instruction instead of a declaration, we’re on a `?`
- // right now, so we do need to search for `>`, similar to declarations.
- return self.interrupt ? ok : continuationDeclarationInside
- }
- // ASCII alphabetical.
- if (asciiAlpha(code)) {
- effects.consume(code)
- // @ts-expect-error: not null.
- buffer = String.fromCharCode(code)
- return tagName
- }
- return nok(code)
- }
- /**
- * After `<!`, at declaration, comment, or CDATA.
- *
- * ```markdown
- * > | <!doctype>
- * ^
- * > | <!--xxx-->
- * ^
- * > | <![CDATA[>&<]]>
- * ^
- * ```
- *
- * @type {State}
- */
- 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
- }
- // ASCII alphabetical.
- if (asciiAlpha(code)) {
- effects.consume(code)
- marker = constants.htmlDeclaration
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok : continuationDeclarationInside
- }
- return nok(code)
- }
- /**
- * After `<!-`, inside a comment, at another `-`.
- *
- * ```markdown
- * > | <!--xxx-->
- * ^
- * ```
- *
- * @type {State}
- */
- function commentOpenInside(code) {
- if (code === codes.dash) {
- effects.consume(code)
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok : continuationDeclarationInside
- }
- return nok(code)
- }
- /**
- * After `<![`, inside CDATA, expecting `CDATA[`.
- *
- * ```markdown
- * > | <![CDATA[>&<]]>
- * ^^^^^^
- * ```
- *
- * @type {State}
- */
- function cdataOpenInside(code) {
- const value = constants.cdataOpeningString
- if (code === value.charCodeAt(index++)) {
- effects.consume(code)
- if (index === value.length) {
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok : continuation
- }
- return cdataOpenInside
- }
- return nok(code)
- }
- /**
- * After `</`, in closing tag, at tag name.
- *
- * ```markdown
- * > | </x>
- * ^
- * ```
- *
- * @type {State}
- */
- function tagCloseStart(code) {
- if (asciiAlpha(code)) {
- effects.consume(code)
- // @ts-expect-error: not null.
- buffer = String.fromCharCode(code)
- return tagName
- }
- return nok(code)
- }
- /**
- * In tag name.
- *
- * ```markdown
- * > | <ab>
- * ^^
- * > | </ab>
- * ^^
- * ```
- *
- * @type {State}
- */
- 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
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok(code) : continuation(code)
- }
- if (htmlBlockNames.includes(buffer.toLowerCase())) {
- marker = constants.htmlBasic
- if (slash) {
- effects.consume(code)
- return basicSelfClosing
- }
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok(code) : continuation(code)
- }
- marker = constants.htmlComplete
- // Do not support complete HTML when interrupting.
- return self.interrupt && !self.parser.lazy[self.now().line]
- ? nok(code)
- : closingTag
- ? completeClosingTagAfter(code)
- : completeAttributeNameBefore(code)
- }
- // ASCII alphanumerical and `-`.
- if (code === codes.dash || asciiAlphanumeric(code)) {
- effects.consume(code)
- buffer += String.fromCharCode(code)
- return tagName
- }
- return nok(code)
- }
- /**
- * After closing slash of a basic tag name.
- *
- * ```markdown
- * > | <div/>
- * ^
- * ```
- *
- * @type {State}
- */
- function basicSelfClosing(code) {
- if (code === codes.greaterThan) {
- effects.consume(code)
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok : continuation
- }
- return nok(code)
- }
- /**
- * After closing slash of a complete tag name.
- *
- * ```markdown
- * > | <x/>
- * ^
- * ```
- *
- * @type {State}
- */
- function completeClosingTagAfter(code) {
- if (markdownSpace(code)) {
- effects.consume(code)
- return completeClosingTagAfter
- }
- return completeEnd(code)
- }
- /**
- * At an attribute name.
- *
- * At first, this state is used after a complete tag name, after whitespace,
- * where it expects optional attributes or the end of the tag.
- * It is also reused after attributes, when expecting more optional
- * attributes.
- *
- * ```markdown
- * > | <a />
- * ^
- * > | <a :b>
- * ^
- * > | <a _b>
- * ^
- * > | <a b>
- * ^
- * > | <a >
- * ^
- * ```
- *
- * @type {State}
- */
- function completeAttributeNameBefore(code) {
- if (code === codes.slash) {
- effects.consume(code)
- return completeEnd
- }
- // ASCII alphanumerical and `:` and `_`.
- 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)
- }
- /**
- * In attribute name.
- *
- * ```markdown
- * > | <a :b>
- * ^
- * > | <a _b>
- * ^
- * > | <a b>
- * ^
- * ```
- *
- * @type {State}
- */
- function completeAttributeName(code) {
- // ASCII alphanumerical and `-`, `.`, `:`, and `_`.
- if (
- code === codes.dash ||
- code === codes.dot ||
- code === codes.colon ||
- code === codes.underscore ||
- asciiAlphanumeric(code)
- ) {
- effects.consume(code)
- return completeAttributeName
- }
- return completeAttributeNameAfter(code)
- }
- /**
- * After attribute name, at an optional initializer, the end of the tag, or
- * whitespace.
- *
- * ```markdown
- * > | <a b>
- * ^
- * > | <a b=c>
- * ^
- * ```
- *
- * @type {State}
- */
- function completeAttributeNameAfter(code) {
- if (code === codes.equalsTo) {
- effects.consume(code)
- return completeAttributeValueBefore
- }
- if (markdownSpace(code)) {
- effects.consume(code)
- return completeAttributeNameAfter
- }
- return completeAttributeNameBefore(code)
- }
- /**
- * Before unquoted, double quoted, or single quoted attribute value, allowing
- * whitespace.
- *
- * ```markdown
- * > | <a b=c>
- * ^
- * > | <a b="c">
- * ^
- * ```
- *
- * @type {State}
- */
- 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)
- }
- /**
- * In double or single quoted attribute value.
- *
- * ```markdown
- * > | <a b="c">
- * ^
- * > | <a b='c'>
- * ^
- * ```
- *
- * @type {State}
- */
- 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
- }
- /**
- * In unquoted attribute value.
- *
- * ```markdown
- * > | <a b=c>
- * ^
- * ```
- *
- * @type {State}
- */
- 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
- }
- /**
- * After double or single quoted attribute value, before whitespace or the
- * end of the tag.
- *
- * ```markdown
- * > | <a b="c">
- * ^
- * ```
- *
- * @type {State}
- */
- function completeAttributeValueQuotedAfter(code) {
- if (
- code === codes.slash ||
- code === codes.greaterThan ||
- markdownSpace(code)
- ) {
- return completeAttributeNameBefore(code)
- }
- return nok(code)
- }
- /**
- * In certain circumstances of a complete tag where only an `>` is allowed.
- *
- * ```markdown
- * > | <a b="c">
- * ^
- * ```
- *
- * @type {State}
- */
- function completeEnd(code) {
- if (code === codes.greaterThan) {
- effects.consume(code)
- return completeAfter
- }
- return nok(code)
- }
- /**
- * After `>` in a complete tag.
- *
- * ```markdown
- * > | <x>
- * ^
- * ```
- *
- * @type {State}
- */
- function completeAfter(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- // // Do not form containers.
- // tokenizer.concrete = true
- return continuation(code)
- }
- if (markdownSpace(code)) {
- effects.consume(code)
- return completeAfter
- }
- return nok(code)
- }
- /**
- * In continuation of any HTML kind.
- *
- * ```markdown
- * > | <!--xxx-->
- * ^
- * ```
- *
- * @type {State}
- */
- 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
- }
- /**
- * In continuation, at eol.
- *
- * ```markdown
- * > | <x>
- * ^
- * | asd
- * ```
- *
- * @type {State}
- */
- function continuationStart(code) {
- return effects.check(
- nonLazyContinuationStart,
- continuationStartNonLazy,
- continuationAfter
- )(code)
- }
- /**
- * In continuation, at eol, before non-lazy content.
- *
- * ```markdown
- * > | <x>
- * ^
- * | asd
- * ```
- *
- * @type {State}
- */
- function continuationStartNonLazy(code) {
- assert(markdownLineEnding(code))
- effects.enter(types.lineEnding)
- effects.consume(code)
- effects.exit(types.lineEnding)
- return continuationBefore
- }
- /**
- * In continuation, before non-lazy content.
- *
- * ```markdown
- * | <x>
- * > | asd
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationBefore(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- return continuationStart(code)
- }
- effects.enter(types.htmlFlowData)
- return continuation(code)
- }
- /**
- * In comment continuation, after one `-`, expecting another.
- *
- * ```markdown
- * > | <!--xxx-->
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationCommentInside(code) {
- if (code === codes.dash) {
- effects.consume(code)
- return continuationDeclarationInside
- }
- return continuation(code)
- }
- /**
- * In raw continuation, after `<`, at `/`.
- *
- * ```markdown
- * > | <script>console.log(1)</script>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationRawTagOpen(code) {
- if (code === codes.slash) {
- effects.consume(code)
- buffer = ''
- return continuationRawEndTag
- }
- return continuation(code)
- }
- /**
- * In raw continuation, after `</`, in a raw tag name.
- *
- * ```markdown
- * > | <script>console.log(1)</script>
- * ^^^^^^
- * ```
- *
- * @type {State}
- */
- 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)
- // @ts-expect-error: not null.
- buffer += String.fromCharCode(code)
- return continuationRawEndTag
- }
- return continuation(code)
- }
- /**
- * In cdata continuation, after `]`, expecting `]>`.
- *
- * ```markdown
- * > | <![CDATA[>&<]]>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationCdataInside(code) {
- if (code === codes.rightSquareBracket) {
- effects.consume(code)
- return continuationDeclarationInside
- }
- return continuation(code)
- }
- /**
- * In declaration or instruction continuation, at `>`.
- *
- * ```markdown
- * > | <!-->
- * ^
- * > | <?>
- * ^
- * > | <!q>
- * ^
- * > | <!--ab-->
- * ^
- * > | <![CDATA[>&<]]>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationDeclarationInside(code) {
- if (code === codes.greaterThan) {
- effects.consume(code)
- return continuationClose
- }
- // More dashes.
- if (code === codes.dash && marker === constants.htmlComment) {
- effects.consume(code)
- return continuationDeclarationInside
- }
- return continuation(code)
- }
- /**
- * In closed continuation: everything we get until the eol/eof is part of it.
- *
- * ```markdown
- * > | <!doctype>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationClose(code) {
- if (code === codes.eof || markdownLineEnding(code)) {
- effects.exit(types.htmlFlowData)
- return continuationAfter(code)
- }
- effects.consume(code)
- return continuationClose
- }
- /**
- * Done.
- *
- * ```markdown
- * > | <!doctype>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationAfter(code) {
- effects.exit(types.htmlFlow)
- // // Feel free to interrupt.
- // tokenizer.interrupt = false
- // // No longer concrete.
- // tokenizer.concrete = false
- return ok(code)
- }
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeNonLazyContinuationStart(effects, ok, nok) {
- const self = this
- return start
- /**
- * At eol, before continuation.
- *
- * ```markdown
- * > | * ```js
- * ^
- * | b
- * ```
- *
- * @type {State}
- */
- function start(code) {
- if (markdownLineEnding(code)) {
- effects.enter(types.lineEnding)
- effects.consume(code)
- effects.exit(types.lineEnding)
- return after
- }
- return nok(code)
- }
- /**
- * A continuation.
- *
- * ```markdown
- * | * ```js
- * > | b
- * ^
- * ```
- *
- * @type {State}
- */
- function after(code) {
- return self.parser.lazy[self.now().line] ? nok(code) : ok(code)
- }
- }
- /**
- * @this {TokenizeContext}
- * @type {Tokenizer}
- */
- function tokenizeBlankLineBefore(effects, ok, nok) {
- return start
- /**
- * Before eol, expecting blank line.
- *
- * ```markdown
- * > | <div>
- * ^
- * |
- * ```
- *
- * @type {State}
- */
- 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)
- }
- }
|