Skip to content

Commit

Permalink
Handle HTML special comment in Markdown (new in CommonMark Spec 0.31.2).
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Apr 4, 2024
1 parent c0a97f2 commit 8104d2d
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions scintilla/lexers/LexMarkdown.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,18 @@ bool MarkdownLexer::HighlightAutoLink() {
break;

case SCE_H_COMMENT:
invalid = sc.Match('-', '-', '>');
if (sc.Match('-', '-')) {
const char chNext = sc.styler[sc.currentPos + 2];
if (chNext == '>' || chNext == '?') {
invalid = true;
}
}
break;

case SCE_H_CDATA:
invalid = sc.Match(']', ']', '>');
if (sc.Match(']', ']', '>')) {
invalid = true;
}
break;
}
}
Expand Down Expand Up @@ -1313,6 +1320,13 @@ bool MarkdownLexer::HandleHtmlTag(HtmlTagType tagType) {
if (chNext == '-' && sc.GetRelative(3) == '-') {
sc.SetState(SCE_H_COMMENT);
sc.Advance(3);
// handle empty comment: <!-->, <!--->
// https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-closing-of-empty-comment
if (sc.chNext == '>' || sc.MatchNext('-', '>')) {
sc.Forward((sc.chNext == '>') ? 2 : 3);
sc.SetState(current);
return true;
}
} else if (chNext == '[' && sc.styler.Match(sc.currentPos + 3, "CDATA[")) {
// <![CDATA[ ]]>
sc.SetState(SCE_H_CDATA);
Expand Down Expand Up @@ -2276,11 +2290,18 @@ void ColouriseMarkdownDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int in
break;

case SCE_H_COMMENT:
if (sc.Match('-', '-', '>')) {
lexer.tagState = HtmlTagState::None;
sc.Advance(3);
sc.SetState(lexer.TryTakeOuterStyle());
continue;
if (sc.Match('-', '-')) {
do {
sc.Forward();
} while (sc.ch == '-');
// close HTML comment with --!>
// https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment
if (sc.ch == '>' || sc.Match('!', '>')) {
lexer.tagState = HtmlTagState::None;
sc.Forward((sc.ch == '>') ? 1 : 2);
sc.SetState(lexer.TryTakeOuterStyle());
continue;
}
}
lexer.DetectAutoLink();
break;
Expand Down

0 comments on commit 8104d2d

Please sign in to comment.