/
style_sheet.cpp
101 lines (94 loc) · 3.91 KB
/
style_sheet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "style_sheet.hpp"
#include <utility>
#include <map>
#include <iterator>
namespace WretchedCss
{
//#####################################################################################################################
StyleSheet::StyleSheet()
: styles_()
, pxPerEm_(16)
{
}
//---------------------------------------------------------------------------------------------------------------------
StyleSheet::StyleSheet(RuleSet const& ruleSet)
: StyleSheet()
{
for (auto const& i : ruleSet.rules)
styles_.push_back({i});
}
//---------------------------------------------------------------------------------------------------------------------
unsigned long StyleSheet::getPxPerEm() const
{
return pxPerEm_;
}
//---------------------------------------------------------------------------------------------------------------------
void StyleSheet::setPxPerEm(unsigned long pxPerEm)
{
pxPerEm_ = pxPerEm;
}
//---------------------------------------------------------------------------------------------------------------------
StyleSheet::StyleSheet(std::string const& css)
: StyleSheet(RuleSet(css))
{
}
//---------------------------------------------------------------------------------------------------------------------
boost::optional <Style> StyleSheet::getByClass(std::string const& clas)
{
return select(std::string(".") + clas);
}
//---------------------------------------------------------------------------------------------------------------------
boost::optional <Style> StyleSheet::getById(std::string const& id)
{
return select(std::string("#") + id);
}
//---------------------------------------------------------------------------------------------------------------------
boost::optional <Style> StyleSheet::getByType(std::string const& type) const
{
return select(type);
}
//---------------------------------------------------------------------------------------------------------------------
boost::optional <Style> StyleSheet::select(std::string const& selector, bool cascade) const
{
std::multimap <long, std::pair <int, Style> > filtered;
int counter = 0;
for (auto const& i : styles_)
{
long specificity = i.getRule().selector.getHighestSpecificityFor(selector);
if (specificity != -1)
filtered.insert(std::pair <long, std::pair <int, Style>> (specificity, {counter, i}));
++counter;
}
if (filtered.empty())
return boost::none;
Style combined = filtered.rbegin()->second.second;
Style* last = &combined;
for (auto front = filtered.rbegin(), end = filtered.rend(), i = std::next(front); i != end; ++i)
{
last->derive(&i->second.second);
last = &i->second.second;
}
return boost::optional <WretchedCss::Style> {combined.getCombined()};
}
//---------------------------------------------------------------------------------------------------------------------
void StyleSheet::addStyle(Style const& style)
{
styles_.push_back(style);
}
//---------------------------------------------------------------------------------------------------------------------
std::string StyleSheet::toString() const
{
RuleSet set;
for (auto const& i : styles_)
{
set.rules.push_back(i.getRule());
}
return set.toCss();
}
//---------------------------------------------------------------------------------------------------------------------
bool StyleSheet::empty() const
{
return styles_.empty();
}
//#####################################################################################################################
}