libhtmlpp 1.0.0
Loading...
Searching...
No Matches
css.h
Go to the documentation of this file.
1/*******************************************************************************
2Copyright (c) 2021, Jan Koester jan.koester@gmx.net
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*******************************************************************************/
27
28#pragma once
29
30#include <string>
31#include <vector>
32#include <map>
33#include <set>
34#include <memory>
35
36namespace libhtmlpp {
37
39 public:
41 CSSProperty(const std::string &name, const std::string &value);
42 CSSProperty(const CSSProperty &prop);
44
45 CSSProperty& operator=(const CSSProperty &prop);
46
47 const std::string& getName() const;
48 void setName(const std::string &name);
49
50 const std::string& getValue() const;
51 void setValue(const std::string &value);
52
53 private:
54 std::string _Name;
55 std::string _Value;
56 };
57
59 public:
61 CSSDeclaration(const CSSDeclaration &decl);
63
65
66 void addProperty(const std::string &name, const std::string &value);
67 void removeProperty(const std::string &name);
68
69 const CSSProperty* getProperty(const std::string &name) const;
70
71 const std::vector<CSSProperty>& getProperties() const;
72
73 std::string serialize() const;
74
75 void parse(const std::string &input);
76 void clear();
77
78 private:
79 std::vector<CSSProperty> _Properties;
80 };
81
82 class CSSRule {
83 public:
84 CSSRule();
85 CSSRule(const std::string &selector);
86 CSSRule(const CSSRule &rule);
87 ~CSSRule();
88
89 CSSRule& operator=(const CSSRule &rule);
90
91 const std::string& getSelector() const;
92 void setSelector(const std::string &selector);
93
95 const CSSDeclaration& getDeclaration() const;
96
97 std::string serialize(bool formatted = false) const;
98
99 private:
100 std::string _Selector;
101 CSSDeclaration _Declaration;
102 };
103
105 public:
107 CSSStyleSheet(const CSSStyleSheet &sheet);
109
111
112 void parse(const std::string &input);
113
114 void addRule(const CSSRule &rule);
115 void removeRule(size_t index);
116
117 const CSSRule* getRule(size_t index) const;
118 size_t getRuleCount() const;
119
120 const std::vector<CSSRule>& getRules() const;
121
122 std::string serialize(bool formatted = false) const;
123 void clear();
124
125 static CSSDeclaration parseInlineStyle(const std::string &style);
126
151 static bool approximateSelectorMatch(const std::string &selector,
152 const std::string &tag,
153 const std::vector<std::string> &classes,
154 const std::string &id);
155
173 void collectApproximateMatches(const std::string &tag,
174 const std::string &cssClass,
175 const std::string &id,
176 std::map<std::string,std::string> &props,
177 std::string &mediaRules,
178 std::set<std::string> &seenMediaBlocks) const;
179
180 private:
181 void _skipWhitespace(const std::string &input, size_t &pos) const;
182 void _skipComment(const std::string &input, size_t &pos) const;
183 std::vector<CSSRule> _Rules;
184 };
185
202 std::string resolveCSSVariables(const std::string &value,
203 const std::map<std::string,std::string> &customProperties);
204
205}
206
void addProperty(const std::string &name, const std::string &value)
Definition css.cpp:295
void removeProperty(const std::string &name)
Definition css.cpp:310
const std::vector< CSSProperty > & getProperties() const
Definition css.cpp:327
std::string serialize() const
Definition css.cpp:331
const CSSProperty * getProperty(const std::string &name) const
Definition css.cpp:319
void parse(const std::string &input)
Definition css.cpp:343
CSSDeclaration & operator=(const CSSDeclaration &decl)
Definition css.cpp:288
void setName(const std::string &name)
Definition css.cpp:274
const std::string & getName() const
Definition css.cpp:273
void setValue(const std::string &value)
Definition css.cpp:277
CSSProperty & operator=(const CSSProperty &prop)
Definition css.cpp:265
const std::string & getValue() const
Definition css.cpp:276
CSSDeclaration & getDeclaration()
Definition css.cpp:431
std::string serialize(bool formatted=false) const
Definition css.cpp:434
void setSelector(const std::string &selector)
Definition css.cpp:429
CSSRule & operator=(const CSSRule &rule)
Definition css.cpp:420
const std::string & getSelector() const
Definition css.cpp:428
void parse(const std::string &input)
Definition css.cpp:484
static bool approximateSelectorMatch(const std::string &selector, const std::string &tag, const std::vector< std::string > &classes, const std::string &id)
Conservative, NOT spec-complete selector match: selector (a single selector, or a comma-separated lis...
Definition css.cpp:632
void collectApproximateMatches(const std::string &tag, const std::string &cssClass, const std::string &id, std::map< std::string, std::string > &props, std::string &mediaRules, std::set< std::string > &seenMediaBlocks) const
Runs every rule in this sheet through approximateSelectorMatch against the element identified by tag/...
Definition css.cpp:659
const std::vector< CSSRule > & getRules() const
Definition css.cpp:608
CSSStyleSheet & operator=(const CSSStyleSheet &sheet)
Definition css.cpp:459
static CSSDeclaration parseInlineStyle(const std::string &style)
Definition css.cpp:626
std::string serialize(bool formatted=false) const
Definition css.cpp:612
const CSSRule * getRule(size_t index) const
Definition css.cpp:599
void addRule(const CSSRule &rule)
Definition css.cpp:589
void removeRule(size_t index)
Definition css.cpp:593
size_t getRuleCount() const
Definition css.cpp:604
Core namespace for the libhtmlpp HTML parsing and printing library.
Definition css.h:36
std::string resolveCSSVariables(const std::string &value, const std::map< std::string, std::string > &customProperties)
Resolves every var(--name) / var(--name, fallback) reference in value using customProperties (custom-...
Definition css.cpp:757