xtd 1.0.0
Loading...
Searching...
No Matches
css_reader.hpp
Go to the documentation of this file.
1
4#pragma once
7#include "../../object.hpp"
8
10namespace xtd {
12 namespace web {
13 namespace css {
14 class css_reader : public object {
15 public:
17
19 css_reader(std::istream& stream) {parse_text(xtd::io::stream_reader(stream).read_to_end());}
20 css_reader(xtd::io::text_reader& text_reader) {parse_text(text_reader.read_to_end());}
21 css_reader(const xtd::string& text) {parse_text(text);}
23
25
27 [[nodiscard]] auto selectors() const noexcept -> const xtd::web::css::selector_dictionary& {return selectors_;}
29
30 private:
31 auto parse_text(const xtd::string& text) -> void {
32 enum class parse_status {
34 key,
35 value
36 };
37 parse_status status = parse_status::selector;
38 xtd::usize start_index = 0;
39 xtd::web::css::selector current_selector;
40 xtd::string current_selector_name;
41 xtd::string current_key;
42 for (xtd::usize index = 0; index < text.length(); index++) {
43 if (text[index] == '/' && text[index + 1] == '*') {
44 // Skip comments...
45 index = text.index_of("*/", index + 2);
46 if (index == text.npos) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::format, "expected end comment");
47 index++;
48 start_index = index + 1;
49 continue;
50 } else if (status == parse_status::selector && text[index] == '{') {
51 current_selector_name = text.substring(start_index, index - start_index).trim();
52 current_selector.name(text.substring(start_index, index - start_index).trim());
53 start_index = index + 1;
54 status = parse_status::key;
55 } else if (status == parse_status::key && text[index] == '}') {
56 selectors_[current_selector_name] = current_selector;
57 current_selector = xtd::web::css::selector();
58 start_index = index + 1;
59 status = parse_status::selector;
60 } else if (status == parse_status::key && text[index] == ':') {
61 current_key = text.substring(start_index, index - start_index).trim().to_lower();
63 start_index = index + 1;
64 status = parse_status::value;
65 } else if (status == parse_status::value && text[index] == ';') {
66 auto value = text.substring(start_index, index - start_index).trim();
68 start_index = index + 1;
69 current_selector.properties()[current_key] = property(value);
70 status = parse_status::key;
71 }
72 }
73 }
74
76 };
77 }
78 }
79}
static auto is_empty(const xtd::basic_string< value_type, traits_type, allocator_type > &string) noexcept -> bool
Definition basic_string.hpp:1249
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Implements a xtd::io::text_reader that reads characters from a byte stream.
Definition stream_reader.hpp:28
Represents a reader that can read a sequential series of characters.
Definition text_reader.hpp:38
virtual auto read_to_end() -> xtd::string
Reads all characters from the current position to the end of the text_reader and returns them as one ...
object()=default
Create a new instance of the ultimate base class object.
Definition property.hpp:14
Definition selector.hpp:12
@ format
The format is not valid.
Definition exception_case.hpp:51
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:13
Contains classes and interfaces that enable browser-server communication. This namespace includes the...
Definition css_reader.hpp:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::object class.
Contains xtd::web::css::selector_dictionary alias.
xtd::collections::generic::sorted_dictionary< xtd::string, xtd::web::css::selector > selector_dictionary
Represents the dictionary of a selector name - selector pair.
Definition selector_dictionary.hpp:13
Contains xtd::io::stream_reader class.