Skip to content

Commit

Permalink
web-html: Implemented before_head state.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Mar 21, 2024
1 parent d8a978b commit 901bd52
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/web/web-html/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,49 @@ void Builder::_handleBeforeHead(Token const &t) {
// Ignore the token.
} else if (t.type == Token::COMMENT) {
// Insert a comment.
_insertAComment();
} else if (t.type == Token::DOCTYPE) {
// Parse error. Ignore the token.
_raise();
} else if (t.type == Token::START_TAG and t.name == "html") {
// Process the token using the rules for the "in body" insertion mode.
_acceptIn(Mode::IN_BODY, t);
} else if (t.type == Token::START_TAG and t.name == "head") {

auto head = _createElementFor(t);
_insertAnHtmlElement(head);
_headElement = head;
} else if (t.type == Token::END_TAG and not(t.name == "head" or t.name == "body" or t.name == "html" or t.name == "br")) {
// ignore
_raise();
} else {
Token headToken;
headToken.type = Token::START_TAG;
headToken.name = String{"head"};
auto head = _createElementFor(headToken);
_insertAnHtmlElement(head);
_headElement = head;
_switchTo(Mode::IN_HEAD);
accept(t);
}
}

// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead
void Builder::_acceptInHead(Token const &t) {
if (t.type == Token::DOCTYPE) {
_raise();
} else if (t.type == Token::COMMENT) {
_insertAComment();
} else if (t.type == Token::CHARACTER and
(t.rune == '\t' or
t.rune == '\n' or
t.rune == '\f' or
t.rune == ' ')) {
// Ignore the token.
} else if (t.type == Token::START_TAG and t) {

} else if (t.type == Token::END_TAG and not(t.name == "head" or t.name == "body" or t.name == "html" or t.name == "br")) {
// ignore
_raise();
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/web/web-html/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct Builder {

void _insertAForeignElement(Token const &t);

void _insertAnHtmlElement();
void _insertAnHtmlElement(Strong<Dom::Element>);

void _insertACharacter();

Expand All @@ -88,10 +88,18 @@ struct Builder {

void _handleInitialMode(Token const &t);

// 13.2.6.4.2 The "before html" insertion mode
// https://html.spec.whatwg.org/multipage/parsing.html#the-before-html-insertion-mode
void _handleBeforeHtml(Token const &t);

// 13.2.6.4.3 The "before head" insertion mode
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-beforehead
void _handleBeforeHead(Token const &t);

// 13.2.6.4.4 The "in head" insertion mode
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead
void _acceptInHead(Token const &t);

void _acceptIn(Mode mode, Token const &t);

void accept(Token const &t);
Expand Down

0 comments on commit 901bd52

Please sign in to comment.