diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Token.h b/Userland/Libraries/LibWeb/CSS/Parser/Token.h index 9c1d945b684..3c906aa1660 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Token.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Token.h @@ -56,6 +56,12 @@ public: Number, }; + struct Position { + size_t line { 0 }; + size_t column { 0 }; + }; + + Type type() const { return m_type; } bool is(Type type) const { return m_type == type; } StringView ident() const @@ -136,6 +142,9 @@ public: String to_debug_string() const; + Position const& start_position() const { return m_start_position; } + Position const& end_position() const { return m_end_position; } + private: Type m_type { Type::Invalid }; @@ -143,6 +152,9 @@ private: StringBuilder m_unit; HashType m_hash_type { HashType::Unrestricted }; NumberType m_number_type { NumberType::Integer }; + + Position m_start_position; + Position m_end_position; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp index ad6a52bc29d..9c4602606ab 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.cpp @@ -241,7 +241,10 @@ Vector Tokenizer::parse() { Vector tokens; for (;;) { + auto token_start = m_position; auto token = consume_a_token(); + token.m_start_position = token_start; + token.m_end_position = m_position; tokens.append(token); if (token.is(Token::Type::EndOfFile)) { @@ -256,8 +259,18 @@ u32 Tokenizer::next_code_point() return TOKENIZER_EOF; m_prev_utf8_iterator = m_utf8_iterator; ++m_utf8_iterator; - dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Next code_point: {:d}", *m_prev_utf8_iterator); - return *m_prev_utf8_iterator; + auto code_point = *m_prev_utf8_iterator; + + m_prev_position = m_position; + if (is_newline(code_point)) { + m_position.line++; + m_position.column = 0; + } else { + m_position.column++; + } + + dbgln_if(CSS_TOKENIZER_DEBUG, "(Tokenizer) Next code_point: {:d}", code_point); + return code_point; } u32 Tokenizer::peek_code_point(size_t offset) const @@ -579,6 +592,7 @@ void Tokenizer::consume_as_much_whitespace_as_possible() void Tokenizer::reconsume_current_input_code_point() { m_utf8_iterator = m_prev_utf8_iterator; + m_position = m_prev_position; } // https://www.w3.org/TR/css-syntax-3/#consume-numeric-token diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h index a5198bbd4f7..d557bee3dbf 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Tokenizer.h @@ -103,5 +103,7 @@ private: Utf8View m_utf8_view; AK::Utf8CodePointIterator m_utf8_iterator; AK::Utf8CodePointIterator m_prev_utf8_iterator; + Token::Position m_position; + Token::Position m_prev_position; }; }