Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

css: Add a style sheet struct #733

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions browser/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ std::string element_text(layout::LayoutBox const *element) {
return std::get<dom::Element>(element->node->node).name;
}

std::string stylesheet_to_string(std::vector<css::Rule> const &stylesheet) {
std::string stylesheet_to_string(css::StyleSheet const &stylesheet) {
std::stringstream ss;
for (auto const &rule : stylesheet) {
for (auto const &rule : stylesheet.rules) {
ss << css::to_string(rule) << std::endl;
}
return std::move(ss).str();
Expand Down
4 changes: 2 additions & 2 deletions css/default.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

Expand All @@ -13,7 +13,7 @@ namespace {
#include "css/default_css.h"
} // namespace

std::vector<css::Rule> default_style() {
StyleSheet default_style() {
return css::parse(std::string_view{reinterpret_cast<char const *>(css_default_css), css_default_css_len});
}

Expand Down
8 changes: 3 additions & 5 deletions css/default.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

#ifndef CSS_DEFAULT_H_
#define CSS_DEFAULT_H_

#include "css/rule.h"

#include <vector>
#include "css/style_sheet.h"

namespace css {

std::vector<css::Rule> default_style();
StyleSheet default_style();

} // namespace css

Expand Down
18 changes: 9 additions & 9 deletions css/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ constexpr std::optional<std::string_view> Parser::consume_while(T const &pred) {
return input_.substr(start, pos_ - start);
}

std::vector<css::Rule> Parser::parse_rules() {
std::vector<css::Rule> rules;
StyleSheet Parser::parse_rules() {
StyleSheet style;
bool in_media_query{false};
std::optional<MediaQuery> media_query;

Expand All @@ -255,7 +255,7 @@ std::vector<css::Rule> Parser::parse_rules() {
auto tmp_query = consume_while([](char c) { return c != '{'; });
if (!tmp_query) {
spdlog::error("Eof while looking for end of media-query");
return rules;
return style;
}

if (auto last_char = tmp_query->find_last_not_of(' '); last_char != std::string_view::npos) {
Expand All @@ -276,7 +276,7 @@ std::vector<css::Rule> Parser::parse_rules() {
auto kind = consume_while([](char c) { return c != ' ' && c != '{' && c != '('; });
if (!kind) {
spdlog::error("Eof while looking for end of at-rule");
return rules;
return style;
}

spdlog::warn("Encountered unhandled {} at-rule", *kind);
Expand All @@ -289,7 +289,7 @@ std::vector<css::Rule> Parser::parse_rules() {
while (peek() != '}') {
if (auto rule = parse_rule(); !rule) {
spdlog::error("Eof while looking for end of rule in unknown at-rule");
return rules;
return style;
}

skip_whitespace_and_comments();
Expand All @@ -303,11 +303,11 @@ std::vector<css::Rule> Parser::parse_rules() {
auto rule = parse_rule();
if (!rule) {
spdlog::error("Eof while parsing rule");
return rules;
return style;
}

rules.push_back(*std::move(rule));
rules.back().media_query = media_query;
style.rules.push_back(*std::move(rule));
style.rules.back().media_query = media_query;

skip_whitespace_and_comments();

Expand All @@ -319,7 +319,7 @@ std::vector<css::Rule> Parser::parse_rules() {
}
}

return rules;
return style;
}

constexpr std::optional<char> Parser::peek() const {
Expand Down
6 changes: 3 additions & 3 deletions css/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#include "css/property_id.h"
#include "css/rule.h"
#include "css/style_sheet.h"

#include <concepts>
#include <cstddef>
#include <optional>
#include <string_view>
#include <utility>
#include <vector>

namespace css {

Expand All @@ -25,7 +25,7 @@ class Parser {
public:
explicit Parser(std::string_view input) : input_{input} {}

std::vector<css::Rule> parse_rules();
StyleSheet parse_rules();

private:
std::string_view input_;
Expand Down Expand Up @@ -77,7 +77,7 @@ class Parser {
void expand_font(std::map<PropertyId, std::string> &declarations, std::string_view value) const;
};

inline std::vector<Rule> parse(std::string_view input) {
inline StyleSheet parse(std::string_view input) {
return Parser{input}.parse_rules();
}

Expand Down
Loading