Skip to content

Commit

Permalink
web-html: More spec comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Mar 19, 2024
1 parent a9e3280 commit 241d4f0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
37 changes: 30 additions & 7 deletions src/web/web-html/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

namespace Web::Html {

void Builder::_switchTo(Mode mode) {
_mode = mode;
}
// 13.2.6 Tree construction
// https://html.spec.whatwg.org/multipage/parsing.html#tree-construction

// 13.2.2 Parse errors
// https://html.spec.whatwg.org/multipage/parsing.html#parse-errors

void Builder::_raise(Str msg) {
logError("{}: {}", toStr(_mode), msg);
}

// https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode
Dom::QuirkMode Builder::_whichQuirkMode(Token const &) {
// NOSPEC: We assume no quirk mode
return Dom::QuirkMode::NO;
void Builder::_switchTo(Mode mode) {
_mode = mode;
}

// https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token
Expand All @@ -26,7 +26,16 @@ Strong<Dom::Element> Builder::_createElementFor(Token const &t) {
return el;
}

// 13.2.6.4 The rules for parsing tokens in HTML content

// 13.2.6.4.1 The "initial" insertion mode
// https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode

Dom::QuirkMode Builder::_whichQuirkMode(Token const &) {
// NOSPEC: We assume no quirk mode
return Dom::QuirkMode::NO;
}

void Builder::_handleInitialMode(Token const &t) {
if (t.type == Token::CHARACTER and
(t.rune == '\t' or
Expand Down Expand Up @@ -87,7 +96,21 @@ void Builder::_handleBeforeHead(Token const &t) {
t.rune == '\n' or
t.rune == '\f' or
t.rune == ' ')) {
// Ignore the token.
} else if (t.type == Token::COMMENT) {
// Insert a comment.
} 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") {

} 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 {
}
}

Expand Down
33 changes: 31 additions & 2 deletions src/web/web-html/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace Web::Html {
MODE(AFTER_AFTER_FRAMESET)

struct Builder {
// 13.2.6 Tree construction
// https://html.spec.whatwg.org/multipage/parsing.html#tree-construction

enum struct Mode {
#define ITER(NAME) NAME,
FOREACH_INSERTION_MODE(ITER)
Expand All @@ -44,19 +47,45 @@ struct Builder {
Lexer _lexer;
Strong<Dom::Document> _document;
Vec<Strong<Dom::Element>> _openElements;
Opt<Strong<Dom::Element>> _headElement;
Opt<Strong<Dom::Element>> _formElement;

Builder(Strong<Dom::Document> document)
: _document(document) {
}

void _switchTo(Mode mode);
// 13.2.2 Parse errors
// https://html.spec.whatwg.org/multipage/parsing.html#parse-errors

void _raise(Str msg = "parse-error");

Dom::QuirkMode _whichQuirkMode(Token const &);
// 13.2.6.1 Creating and inserting nodes
// https://html.spec.whatwg.org/multipage/parsing.html#creating-and-inserting-nodes

void _apropriatePlaceForInsertingANode();

Strong<Dom::Element> _createElementFor(Token const &t);

void _insertAnElementAtTheAdjustedInsertionLocation();

void _insertAForeignElement(Token const &t);

void _insertAnHtmlElement();

void _insertACharacter();

void _insertAComment();

//////////////////////////////////////////

void _switchTo(Mode mode);

// 13.2.6.4 The rules for parsing tokens in HTML content

// 13.2.6.4.1 The "initial" insertion mode
// https://html.spec.whatwg.org/multipage/parsing.html#the-initial-insertion-mode
Dom::QuirkMode _whichQuirkMode(Token const &);

void _handleInitialMode(Token const &t);

void _handleBeforeHtml(Token const &t);
Expand Down

0 comments on commit 241d4f0

Please sign in to comment.