Skip to content

Commit

Permalink
css+style: Added support for inline styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 20, 2024
1 parent 0b16062 commit 6badeff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/vaev-css/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Opt<Sst> consumeRule(Lexer &lex) {
// NOSPEC: We unified the two functions into one for simplicity
// and added a check for the right curly bracket
// to avoid aving to parsing the input multiple times
Content consumeDeclarationList(Lexer &lex) {
Content consumeDeclarationList(Lexer &lex, bool topLevel) {
Content block;

while (true) {
Expand All @@ -153,7 +153,8 @@ Content consumeDeclarationList(Lexer &lex) {
break;

case Token::END_OF_FILE:
logError("unexpected end of file");
if (not topLevel)
logError("unexpected end of file");
lex.next();
return block;

Expand Down
2 changes: 1 addition & 1 deletion src/vaev-css/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Opt<Sst> consumeRule(Lexer &lex);

Content consumeDeclarationValue(Lexer &lex);

Content consumeDeclarationList(Lexer &lex);
Content consumeDeclarationList(Lexer &lex, bool topLevel = false);

Content consumeDeclarationBlock(Lexer &lex);

Expand Down
4 changes: 2 additions & 2 deletions src/vaev-markup/dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ struct TokenList {
struct Element : public Node {
static constexpr auto TYPE = NodeType::ELEMENT;

Opt<String> id() const {
Opt<Str> id() const {
return this->getAttribute(Html::ID_ATTR);
}

Expand Down Expand Up @@ -385,7 +385,7 @@ struct Element : public Node {
return this->attributes.tryGet(name) != NONE;
}

Opt<String> getAttribute(AttrName name) const {
Opt<Str> getAttribute(AttrName name) const {
auto attr = this->attributes.tryGet(name);
if (attr == NONE)
return NONE;
Expand Down
9 changes: 9 additions & 0 deletions src/vaev-style/computer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "computer.h"
#include <vaev-style/decls.h>

namespace Vaev::Style {

Expand Down Expand Up @@ -48,6 +49,14 @@ Strong<Computed> Computer::computeFor(Computed const &parent, Markup::Element co
}
);

// Get the style attribute if any
auto styleAttr = el.getAttribute(Html::STYLE_ATTR);
StyleRule styleRule{
.selector = UNIVERSAL,
.props = parseDeclarations<StyleProp>(styleAttr ? *styleAttr : ""),
};
matchingRules.pushBack(&styleRule);

// Compute computed style
auto computed = makeStrong<Computed>(Computed::initial());
computed->inherit(parent);
Expand Down
16 changes: 14 additions & 2 deletions src/vaev-style/decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ Res<P> parseDeclaration(Css::Sst const &sst) {
}

template <typename P>
Vec<P> parseDeclarations(Css::Sst const &sst) {
Vec<P> parseDeclarations(Css::Content const &sst) {
Vec<P> res;

for (auto const &item : sst.content) {
for (auto const &item : sst) {
if (item != Css::Sst::DECL) {
logWarn("unexpected item in declaration: {}", item.type);
continue;
Expand All @@ -83,4 +83,16 @@ Vec<P> parseDeclarations(Css::Sst const &sst) {
return res;
}

template <typename P>
Vec<P> parseDeclarations(Css::Sst const &sst) {
return parseDeclarations<P>(sst.content);
}

template <typename P>
Vec<P> parseDeclarations(Str style) {
Css::Lexer lex{style};
auto sst = Css::consumeDeclarationList(lex, true);
return parseDeclarations<P>(sst);
}

} // namespace Vaev::Style

0 comments on commit 6badeff

Please sign in to comment.