Photon 1.0.0
Loading...
Searching...
No Matches
html.h
Go to the documentation of this file.
1#ifndef LH_HTML_H
2#define LH_HTML_H
3
4#include <cstdlib>
5#include <cmath>
6#include <cctype>
7#include <cstring>
8#include <algorithm>
9#include <functional>
10#include "os_types.h"
11#include "types.h"
12#include "string_id.h"
13#include "utf8_strings.h"
14#include "background.h"
15#include "borders.h"
16#include "web_color.h"
17#include "media_query.h"
18#include "html_microsyntaxes.h"
19#include "html_tag.h"
20#include "document_container.h"
21#include "document.h"
22
23namespace litehtml
24{
25 const string whitespace = " \n\r\t\f";
26 string& trim(string& s, const string& chars_to_trim = whitespace);
27 string trim(const string& s, const string& chars_to_trim = whitespace);
28 string& lcase(string& s);
29 int value_index(const string& val, const string& strings, int defValue = -1, char delim = ';');
30 string index_value(int index, const string& strings, char delim = ';');
31 bool value_in_list(const string& val, const string& strings, char delim = ';');
32 string::size_type find_close_bracket(const string& s, string::size_type off, char open_b = '(', char close_b = ')');
33 void split_string(const string& str, string_vector& tokens, const string& delims = whitespace, const string& delims_preserve = "", const string& quote = "\"");
34 string_vector split_string(const string& str, const string& delims = whitespace, const string& delims_preserve = "", const string& quote = "\"");
35 void join_string(string& str, const string_vector& tokens, const string& delims);
36 double t_strtod(const char* string, char** endPtr = nullptr);
37 string get_escaped_string(const string& in_str);
38
39 template<typename X, typename A>
40 bool is_one_of(X x, A a)
41 {
42 return x == a;
43 }
44 template<typename X, typename A, typename... AA>
45 bool is_one_of(X x, A a, AA... aa)
46 {
47 return x == a || is_one_of(x, aa...);
48 }
49 template<class T>
50 const T& at(const vector<T>& vec, int index /*may be negative*/)
51 {
52 static T invalid_item; // T's default constructor must create invalid item
53 if (index < 0) index += (int)vec.size();
54 return index >= 0 && index < (int)vec.size() ? vec[index] : invalid_item;
55 }
56 template<class Map, class Key>
57 auto at(const Map& map, Key key)
58 {
59 static typename Map::mapped_type invalid_value; // mapped_type's default constructor must create invalid item
60 auto it = map.find(key);
61 return it != map.end() ? it->second : invalid_value;
62 }
63 template<typename T>
64 vector<T> slice(const vector<T>& vec, int index, int count = -1)
65 {
66 if (count == -1) count = (int)vec.size() - index;
67 return {vec.begin() + index, vec.begin() + index + count};
68 }
69 template<class C> // C == vector or string
70 void remove(C& vec, int index /*may be negative*/, int count = 1)
71 {
72 if (index < 0) index += (int)vec.size();
73
74 if (!(index >= 0 && index < (int)vec.size()))
75 return;
76
77 count = min(count, (int)vec.size() - index);
78 if (count <= 0) return;
79
80 vec.erase(vec.begin() + index, vec.begin() + index + count);
81 }
82 template<class T>
83 void insert(vector<T>& vec, int index, const vector<T>& x)
84 {
85 vec.insert(vec.begin() + index, x.begin(), x.end());
86 }
87 template<class T>
88 vector<T>& operator+=(vector<T>& vec, const vector<T>& x)
89 {
90 vec.insert(vec.end(), x.begin(), x.end());
91 return vec;
92 }
93 template<class C, class T>
94 bool contains(const C& coll, const T& item)
95 {
96 return std::find(coll.begin(), coll.end(), item) != coll.end();
97 }
98 inline bool contains(const string& str, const string& substr)
99 {
100 return str.find(substr) != string::npos;
101 }
102 template<class C> void sort(C& coll) { std::sort(coll.begin(), coll.end()); }
103
104 int t_strcasecmp(const char *s1, const char *s2);
105 int t_strncasecmp(const char *s1, const char *s2, size_t n);
106 inline bool equal_i(const string& s1, const string& s2)
107 {
108 if (s1.size() != s2.size()) return false;
109 return t_strncasecmp(s1.c_str(), s2.c_str(), s1.size()) == 0;
110 }
111 inline bool match(const string& str, int index /*may be negative*/, const string& substr)
112 {
113 if (index < 0) index += (int)str.size();
114 if (index < 0) return false;
115 return str.substr(index, substr.size()) == substr;
116 }
117 inline bool match_i(const string& str, int index /*may be negative*/, const string& substr)
118 {
119 if (index < 0) index += (int)str.size();
120 if (index < 0) return false;
121 return equal_i(str.substr(index, substr.size()), substr);
122 }
123
124 bool is_number(const string& string, bool allow_dot = true);
125
126 // https://infra.spec.whatwg.org/#ascii-whitespace
127 inline bool is_whitespace(int c)
128 {
129 return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
130 }
131
132 inline int t_isalpha(int c)
133 {
134 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
135 }
136 const auto is_letter = t_isalpha;
137
138 inline int t_tolower(int c)
139 {
140 return (c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c);
141 }
142 // https://infra.spec.whatwg.org/#ascii-lowercase
143 inline int lowcase(int c)
144 {
145 return t_tolower(c);
146 }
147 inline string lowcase(string str)
148 {
149 for (char& c : str) c = (char)t_tolower(c);
150 return str;
151 }
152
153 inline int t_isdigit(int c)
154 {
155 return (c >= '0' && c <= '9');
156 }
157 const auto is_digit = t_isdigit;
158
159 inline bool is_hex_digit(int ch) {
160 return is_digit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
161 }
162
163 inline int digit_value(int ch) {
164 return is_digit(ch) ? ch - '0' : lowcase(ch) - 'a' + 10;
165 }
166
167 inline bool is_surrogate(int ch) {
168 return ch >= 0xD800 && ch < 0xE000;
169 }
170
171 inline int round_f(float val)
172 {
173 int int_val = (int) val;
174 if(val - int_val >= 0.5)
175 {
176 int_val++;
177 }
178 return int_val;
179 }
180
181 inline int round_d(double val)
182 {
183 int int_val = (int) val;
184 if(val - int_val >= 0.5)
185 {
186 int_val++;
187 }
188 return int_val;
189 }
190
191 inline float t_strtof(const string& str, char** endPtr = nullptr)
192 {
193 return (float)t_strtod(str.c_str(), endPtr);
194 }
195
196 inline int baseline_align(int line_height, int line_base_line, int height, int baseline)
197 {
198 return (line_height - line_base_line) - (height - baseline);
199 }
200}
201
202#endif // LH_HTML_H
Definition types.h:774
constexpr auto count() -> size_t
Definition core.h:1538
Definition background.h:12
line_height
Definition types.h:367
bool contains(const C &coll, const T &item)
Definition html.h:94
bool match(const string &str, int index, const string &substr)
Definition html.h:111
bool is_hex_digit(int ch)
Definition html.h:159
const auto is_letter
Definition html.h:136
bool match_i(const string &str, int index, const string &substr)
Definition html.h:117
int round_f(float val)
Definition html.h:171
int lowcase(int c)
Definition html.h:143
int t_isdigit(int c)
Definition html.h:153
float t_strtof(const string &str, char **endPtr=nullptr)
Definition html.h:191
const string whitespace
Definition html.h:25
bool value_in_list(const string &val, const string &strings, char delim=';')
Definition html.cpp:113
bool is_whitespace(int c)
Definition html.h:127
string & trim(string &s, const string &chars_to_trim=whitespace)
Definition html.cpp:7
const T & at(const vector< T > &vec, int index)
Definition html.h:50
int t_strncasecmp(const char *s1, const char *s2, size_t n)
Definition html.cpp:222
const auto is_digit
Definition html.h:157
bool equal_i(const string &s1, const string &s2)
Definition html.h:106
int baseline_align(int line_height, int line_base_line, int height, int baseline)
Definition html.h:196
vector< T > slice(const vector< T > &vec, int index, int count=-1)
Definition html.h:64
int t_tolower(int c)
Definition html.h:138
string::size_type find_close_bracket(const string &s, string::size_type off, char open_b='(', char close_b=')')
Definition html.cpp:43
void split_string(const string &str, string_vector &tokens, const string &delims=whitespace, const string &delims_preserve="", const string &quote="\"")
Definition html.cpp:130
string index_value(int index, const string &strings, char delim=';')
Definition html.cpp:63
vector< T > & operator+=(vector< T > &vec, const vector< T > &x)
Definition html.h:88
int value_index(const string &val, const string &strings, int defValue=-1, char delim=';')
Definition html.cpp:76
string & lcase(string &s)
Definition html.cpp:34
string get_escaped_string(const string &in_str)
Definition html.cpp:241
std::vector< string > string_vector
Definition types.h:34
int digit_value(int ch)
Definition html.h:163
bool is_one_of(X x, A a)
Definition html.h:40
bool is_surrogate(int ch)
Definition html.h:167
void insert(vector< T > &vec, int index, const vector< T > &x)
Definition html.h:83
double t_strtod(const char *string, char **endPtr=nullptr)
Definition strtod.cpp:71
int round_d(double val)
Definition html.h:181
bool is_number(const string &string, bool allow_dot=true)
Definition html.cpp:299
int t_strcasecmp(const char *s1, const char *s2)
Definition html.cpp:205
void sort(C &coll)
Definition html.h:102
int t_isalpha(int c)
Definition html.h:132
void join_string(string &str, const string_vector &tokens, const string &delims)
Definition html.cpp:192
void remove(C &vec, int index, int count=1)
Definition html.h:70
s
Definition tag_strings.h:47
a
Definition tag_strings.h:43
map
Definition tag_strings.h:86