| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876 |
- /**
- * @import {
- * Code,
- * Construct,
- * Resolver,
- * State,
- * TokenizeContext,
- * Tokenizer
- * } from 'micromark-util-types'
- */
- import { asciiAlphanumeric, asciiAlpha, markdownLineEndingOrSpace, markdownLineEnding, markdownSpace } from 'micromark-util-character';
- import { htmlBlockNames, htmlRawNames } from 'micromark-util-html-tag-name';
- import { blankLine } from './blank-line.js';
- /** @type {Construct} */
- export const htmlFlow = {
- concrete: true,
- name: 'htmlFlow',
- resolveTo: resolveToHtmlFlow,
- tokenize: tokenizeHtmlFlow
- };
- /** @type {Construct} */
- const blankLineBefore = {
- partial: true,
- tokenize: tokenizeBlankLineBefore
- };
- const nonLazyContinuationStart = {
- partial: true,
- tokenize: tokenizeNonLazyContinuationStart
- };
- /** @type {Resolver} */
- function resolveToHtmlFlow(events) {
- let index = events.length;
- while (index--) {
- if (events[index][0] === 'enter' && events[index][1].type === "htmlFlow") {
- break;
- }
- }
- if (index > 1 && events[index - 2][1].type === "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}
- * Context.
- * @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) {
- effects.enter("htmlFlow");
- effects.enter("htmlFlowData");
- effects.consume(code);
- return open;
- }
- /**
- * After `<`, at tag name or other stuff.
- *
- * ```markdown
- * > | <x />
- * ^
- * > | <!doctype>
- * ^
- * > | <!--xxx-->
- * ^
- * ```
- *
- * @type {State}
- */
- function open(code) {
- if (code === 33) {
- effects.consume(code);
- return declarationOpen;
- }
- if (code === 47) {
- effects.consume(code);
- closingTag = true;
- return tagCloseStart;
- }
- if (code === 63) {
- effects.consume(code);
- marker = 3;
- // 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)) {
- // Always the case.
- effects.consume(code);
- 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 === 45) {
- effects.consume(code);
- marker = 2;
- return commentOpenInside;
- }
- if (code === 91) {
- effects.consume(code);
- marker = 5;
- index = 0;
- return cdataOpenInside;
- }
- // ASCII alphabetical.
- if (asciiAlpha(code)) {
- effects.consume(code);
- marker = 4;
- // // 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 === 45) {
- 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 = "CDATA[";
- 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)) {
- // Always the case.
- effects.consume(code);
- buffer = String.fromCharCode(code);
- return tagName;
- }
- return nok(code);
- }
- /**
- * In tag name.
- *
- * ```markdown
- * > | <ab>
- * ^^
- * > | </ab>
- * ^^
- * ```
- *
- * @type {State}
- */
- function tagName(code) {
- if (code === null || code === 47 || code === 62 || markdownLineEndingOrSpace(code)) {
- const slash = code === 47;
- const name = buffer.toLowerCase();
- if (!slash && !closingTag && htmlRawNames.includes(name)) {
- marker = 1;
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok(code) : continuation(code);
- }
- if (htmlBlockNames.includes(buffer.toLowerCase())) {
- marker = 6;
- if (slash) {
- effects.consume(code);
- return basicSelfClosing;
- }
- // // Do not form containers.
- // tokenizer.concrete = true
- return self.interrupt ? ok(code) : continuation(code);
- }
- marker = 7;
- // 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 === 45 || 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 === 62) {
- 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 === 47) {
- effects.consume(code);
- return completeEnd;
- }
- // ASCII alphanumerical and `:` and `_`.
- if (code === 58 || code === 95 || 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 === 45 || code === 46 || code === 58 || code === 95 || 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 === 61) {
- 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 === null || code === 60 || code === 61 || code === 62 || code === 96) {
- return nok(code);
- }
- if (code === 34 || code === 39) {
- 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 === null || 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 === null || code === 34 || code === 39 || code === 47 || code === 60 || code === 61 || code === 62 || code === 96 || 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 === 47 || code === 62 || 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 === 62) {
- effects.consume(code);
- return completeAfter;
- }
- return nok(code);
- }
- /**
- * After `>` in a complete tag.
- *
- * ```markdown
- * > | <x>
- * ^
- * ```
- *
- * @type {State}
- */
- function completeAfter(code) {
- if (code === null || 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 === 45 && marker === 2) {
- effects.consume(code);
- return continuationCommentInside;
- }
- if (code === 60 && marker === 1) {
- effects.consume(code);
- return continuationRawTagOpen;
- }
- if (code === 62 && marker === 4) {
- effects.consume(code);
- return continuationClose;
- }
- if (code === 63 && marker === 3) {
- effects.consume(code);
- return continuationDeclarationInside;
- }
- if (code === 93 && marker === 5) {
- effects.consume(code);
- return continuationCdataInside;
- }
- if (markdownLineEnding(code) && (marker === 6 || marker === 7)) {
- effects.exit("htmlFlowData");
- return effects.check(blankLineBefore, continuationAfter, continuationStart)(code);
- }
- if (code === null || markdownLineEnding(code)) {
- effects.exit("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) {
- effects.enter("lineEnding");
- effects.consume(code);
- effects.exit("lineEnding");
- return continuationBefore;
- }
- /**
- * In continuation, before non-lazy content.
- *
- * ```markdown
- * | <x>
- * > | asd
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationBefore(code) {
- if (code === null || markdownLineEnding(code)) {
- return continuationStart(code);
- }
- effects.enter("htmlFlowData");
- return continuation(code);
- }
- /**
- * In comment continuation, after one `-`, expecting another.
- *
- * ```markdown
- * > | <!--xxx-->
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationCommentInside(code) {
- if (code === 45) {
- 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 === 47) {
- 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 === 62) {
- const name = buffer.toLowerCase();
- if (htmlRawNames.includes(name)) {
- effects.consume(code);
- return continuationClose;
- }
- return continuation(code);
- }
- if (asciiAlpha(code) && buffer.length < 8) {
- // Always the case.
- effects.consume(code);
- buffer += String.fromCharCode(code);
- return continuationRawEndTag;
- }
- return continuation(code);
- }
- /**
- * In cdata continuation, after `]`, expecting `]>`.
- *
- * ```markdown
- * > | <![CDATA[>&<]]>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationCdataInside(code) {
- if (code === 93) {
- 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 === 62) {
- effects.consume(code);
- return continuationClose;
- }
- // More dashes.
- if (code === 45 && marker === 2) {
- 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 === null || markdownLineEnding(code)) {
- effects.exit("htmlFlowData");
- return continuationAfter(code);
- }
- effects.consume(code);
- return continuationClose;
- }
- /**
- * Done.
- *
- * ```markdown
- * > | <!doctype>
- * ^
- * ```
- *
- * @type {State}
- */
- function continuationAfter(code) {
- effects.exit("htmlFlow");
- // // Feel free to interrupt.
- // tokenizer.interrupt = false
- // // No longer concrete.
- // tokenizer.concrete = false
- return ok(code);
- }
- }
- /**
- * @this {TokenizeContext}
- * Context.
- * @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("lineEnding");
- effects.consume(code);
- effects.exit("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}
- * Context.
- * @type {Tokenizer}
- */
- function tokenizeBlankLineBefore(effects, ok, nok) {
- return start;
- /**
- * Before eol, expecting blank line.
- *
- * ```markdown
- * > | <div>
- * ^
- * |
- * ```
- *
- * @type {State}
- */
- function start(code) {
- effects.enter("lineEnding");
- effects.consume(code);
- effects.exit("lineEnding");
- return effects.attempt(blankLine, ok, nok);
- }
- }
|