libhtmlpp 1.0.0
Loading...
Searching...
No Matches
html.h
Go to the documentation of this file.
1
9/*******************************************************************************
10Copyright (c) 2014, Jan Koester jan.koester@gmx.net
11All rights reserved.
12
13Redistribution and use in source and binary forms, with or without
14modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of the <organization> nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*******************************************************************************/
35
36#include <sys/types.h>
37
38#include <string>
39#include <cstring>
40#include <vector>
41#include <stack>
42#include <memory>
43
44#pragma once
50namespace libhtmlpp {
54 class DocElements;
58 class HtmlElement;
62 class HtmlString;
63
76 class Element {
77 public:
78 virtual ~Element();
79
80 Element(const Element &el);
81
82 void insertAfter(Element* el);
83 void insertBefore(Element* el);
84
85 Element& operator=(const Element &hel);
86 Element& operator=(const Element *hel);
87
88 Element* nextElement() const;
89 Element* prevElement() const;
90
91 virtual void remove(Element* el);
92
93 virtual int getType() const=0;
94 protected:
95
96 Element();
97
98 std::unique_ptr<Element> _nextElement;
100
101 friend class HtmlElement;
102 friend class TextElement;
103 friend class HtmlString;
104 friend void print(const Element& element, HtmlString &output,bool formated);
105 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
106 };
107
108 class HtmlElement : public Element {
109 public:
110 HtmlElement();
111 HtmlElement(const std::string &tag);
112 HtmlElement(const HtmlElement &hel);
113 HtmlElement(const HtmlElement *hel);
114 ~HtmlElement();
115
116 HtmlElement& operator=(const HtmlElement &hel);
117 HtmlElement& operator=(const HtmlElement *hel);
118
119 bool operator==(const HtmlElement *hel);
120 bool operator==(const HtmlElement &hel);
121
122 void setAttribute(const std::string &name, const std::string &value);
123
124 void setIntAttribute(const std::string &name, int value);
125
126 const std::string getAtributte(const std::string &name) const;
127
128 int getIntAtributte(const std::string &name) const;
129
130 void insertChild(const Element* el);
131 void insertChild(const Element& el);
132 void appendChild(const Element* el);
133 void appendChild(const Element& el);
134
135 void setTagname(const std::string &name);
136 const std::string getTagname() const;
137
138 HtmlElement *getElementbyID(const std::string &id) const;
139 HtmlElement *getElementbyTag(const std::string &tag) const;
140
141 Element* firstChild() const;
142
143 struct Attributes {
144 Attributes();
145 ~Attributes();
146
147 const std::string getKey() const;
148 const std::string getValue() const;
149 Attributes* nextAttribute() const;
150
151 private:
152 std::vector<char> _Key;
153 std::vector<char> _Value;
154 std::unique_ptr<Attributes> _nextAttr;
155
156 friend class HtmlElement;
157 friend void print(const Element& element, HtmlString &output,bool formated);
158 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
159 };
160
161 const Attributes* firstAttribute() const;
162
163 int getType() const;
164 void remove(Element* el);
165
166 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
167 protected:
168
169 std::unique_ptr<Element> _childElement=nullptr;
170
171 void _serialelize(const std::vector<char> &in);
172 private:
173 //if text tagname must be zero
174 std::vector<char> _TagName;
175
176 //if text Attributes must be zero
177 std::unique_ptr<Attributes> _firstAttr;
178 Attributes* _lastAttr;
179
180 friend class HtmlString;
181 friend class HtmlTable;
182 friend void print(const Element& element, HtmlString &output,bool formated);
183 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
184 };
189 class TextElement : public Element {
190 public:
191 TextElement();
192 TextElement(const std::string &txt);
193 TextElement(const TextElement &texel);
194 ~TextElement();
195
196 TextElement& operator=(const Element &hel);
197 TextElement& operator=(const Element *hel);
198
199 const std::string getText();
200 void setText(const std::string &txt);
201
202 int getType() const;
203
204 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
205
206 protected:
207 std::vector<char> _Text;
208 friend class HtmlString;
209 friend void print(const Element& element, HtmlString &output,bool formated);
210 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
211 };
216 class CommentElement : public Element{
217 public:
219 CommentElement(const CommentElement &comel);
221
222 CommentElement& operator=(const Element &hel);
223 CommentElement& operator=(const Element *hel);
224
225 const std::string getComment();
226 void setComment(const std::string &txt);
227
228 int getType() const;
229
230 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
231 protected:
232 std::vector<char> _Comment;
233 friend class HtmlString;
234 friend void print(const Element& element, HtmlString &output,bool formated);
235 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
236 };
242 public:
244 ScriptElement(const ScriptElement &scriptsrc);
246
247 ScriptElement& operator=(const Element &hel);
248 ScriptElement& operator=(const Element *hel);
249
250 const std::string getScript();
251 void setScript(const std::string &txt);
252
253 int getType() const;
254
255 void insertChild(const Element* el)=delete;
256 void insertChild(const Element& el)=delete;
257 void appendChild(const Element* el)=delete;
258 void appendChild(const Element& el)=delete;
259
260 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
261 protected:
262
263 std::unique_ptr<Element> _childElement=nullptr;
264
265 std::vector<char> _Script;
266 friend class HtmlString;
267 friend void print(const Element& element, HtmlString &output,bool formated);
268 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
269 };
274 class SvgElement : public HtmlElement{
275 public:
276 SvgElement();
277 SvgElement(const SvgElement &svgsrc);
278 ~SvgElement();
279
280 SvgElement& operator=(const Element &hel);
281 SvgElement& operator=(const Element *hel);
282
283 const std::vector<char> getSvg();
284 void setSvg(const std::string &svg);
285
286 int getType() const;
287
288 void insertChild(const Element* el)=delete;
289 void insertChild(const Element& el)=delete;
290 void appendChild(const Element* el)=delete;
291 void appendChild(const Element& el)=delete;
292
293 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
294 protected:
295
296 std::unique_ptr<Element> _childElement=nullptr;
297
298 std::vector<char> _Svg;
299 friend class HtmlString;
300 friend void print(const Element& element, HtmlString &output,bool formated);
301 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
302 };
303
308 class TextArea : public HtmlElement{
309 public:
310 TextArea();
311 TextArea(const TextArea &textsrc);
312 ~TextArea();
313
314 TextArea& operator=(const Element &hel);
315 TextArea& operator=(const Element *hel);
316
317 const std::vector<char> getText();
318 void setText(const std::string &text);
319
320 int getType() const;
321
322 void insertChild(const Element* el)=delete;
323 void insertChild(const Element& el)=delete;
324 void appendChild(const Element* el)=delete;
325 void appendChild(const Element& el)=delete;
326
327 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
328 protected:
329
330 std::unique_ptr<Element> _childElement=nullptr;
331
332 std::vector<char> _Text;
333 friend class HtmlString;
334 friend void print(const Element& element, HtmlString &output,bool formated);
335 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
336 };
337
346 void print(const Element& element, HtmlString &output,bool formated=false);
347
349 public:
350
351 using value_type = char;
352
353 HtmlString()=default;
354 HtmlString(const HtmlString &str);
355 HtmlString(char str);
356 HtmlString(const std::string &str);
357 ~HtmlString();
358
359 void append(const std::string &src);
360 void append(HtmlString& hstring);
361
362 void push_back(const char src);
363
364 void insert(size_t pos, char src);
365
366 HtmlString& operator+=(const std::string &src);
368 HtmlString& operator=(const std::string &src);
369 HtmlString& operator=(const HtmlString& src);
370 char operator[](size_t pos) const;
371
372 HtmlString& operator<<(const char* src);
373 HtmlString& operator<<(const std::string &src);
375 HtmlString& operator<<(int src);
376 HtmlString& operator<<(char src);
377 HtmlString& operator<<(size_t src);
378
379 const char* operator*();
380
381 size_t size() const;
382 size_t length() const;
383 void clear();
384 bool empty();
385
386 const std::vector<char>& data() const;
387 const std::string str() const;
388 const char *c_str() const;
396
397 private:
398 std::unique_ptr<Element> _rootEl;
399 void _buildTree();
400 void _buildtreenode(DocElements *firstel,DocElements *lastel,std::unique_ptr<Element>&html);
401 std::vector<char> _Data;
402 friend void HtmlEncode(const std::string &input,HtmlString *output);
403 friend class HtmlPage;
404 };
410 void HtmlDecode(const std::string &input,HtmlString &output);
411
417 void HtmlDecode(const std::string &input,std::string &output);
418
425 void HtmlEncode(const std::string &input,std::string &output);
430 class HtmlPage {
431 public:
432 HtmlPage();
433 ~HtmlPage();
434 void loadFile(libhtmlpp::HtmlElement &html,const std::string &path);
435 void saveFile(libhtmlpp::HtmlElement &html,const std::string &path);
436 void loadString(libhtmlpp::HtmlElement &html,const std::string &src);
437 void loadString(libhtmlpp::HtmlElement &html,const HtmlString &node);
439 bool isHtml5();
440 private:
441 void _CheckHeader(const HtmlString& page);
442 bool _Html5 = true;
443 };
444
445 class HtmlTable {
446 public:
447 HtmlTable();
448 ~HtmlTable();
449
450 class Column {
451 public:
452 std::string Data;
453 ~Column();
454 Column(const Column &col);
455 Column();
456 Column(const std::string &data);
457 Column(const HtmlString &data);
458 Column(Column&& col) noexcept;
459 private:
460 std::unique_ptr<Column> _nextColumn;
461 friend class HtmlTable;
462 };
463
464 class Row {
465 public:
466 Row();
467 Row(const Row &row);
468 ~Row();
469
470 Row &operator<<(Column &&col);
471 Row& operator<<(const Column &col);
472 Row& operator<<(const HtmlString &value);
473 Row& operator<<(const std::string &value);
474 Row& operator<<(const char* value);
475 Row& operator<<(int value);
476
477 Column& operator[](size_t pos);
478
479 void delColumn(size_t pos);
480 void clear();
481 private:
482 std::unique_ptr<Column> _firstColumn;
483 Column *_lastColumn;
484 std::unique_ptr<Row> _nextRow;
485 size_t _count;
486 friend class HtmlTable;
487 };
488
489 Row& operator<<(const Row &row);
490 Row& operator[](size_t pos);
491
492 void insert(HtmlElement *element);
493 void parse(HtmlElement *element);
494
495 void setHeader(int count,...);
496 void deleteRow(size_t pos);
497 private:
498 std::unique_ptr<Row> _firstRow;
499 Row *_lastRow;
500 Row _header;
501 size_t _count;
502 };
503};
504
505std::ostream& operator<<(std::ostream& os, const libhtmlpp::HtmlString& p);
Leaf node representing an HTML comment ().
Definition html.h:216
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1487
CommentElement & operator=(const Element &hel)
Definition html.cpp:1464
std::vector< char > _Comment
Definition html.h:232
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
void setComment(const std::string &txt)
Definition html.cpp:1474
const std::string getComment()
Definition html.cpp:1479
Abstract base class for all nodes in the HTML tree.
Definition html.h:76
virtual void remove(Element *el)
Definition html.cpp:1345
Element * _prevElement
Definition html.h:99
void insertAfter(Element *el)
Definition html.cpp:1268
virtual ~Element()
Definition html.cpp:1338
void insertBefore(Element *el)
Definition html.cpp:1236
Element & operator=(const Element &hel)
Definition html.cpp:1309
std::unique_ptr< Element > _nextElement
Definition html.h:98
virtual int getType() const =0
Definition html.cpp:735
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
Element * prevElement() const
Definition html.cpp:1323
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
Element * nextElement() const
Definition html.cpp:1319
int getType() const
Definition html.cpp:2517
const Attributes * firstAttribute() const
Definition html.cpp:2435
HtmlElement * getElementbyTag(const std::string &tag) const
Definition html.cpp:2392
const std::string getAtributte(const std::string &name) const
Definition html.cpp:2487
void _serialelize(const std::vector< char > &in)
Extracts tag name and attributes from a token vector into an HtmlElement.
Definition html.cpp:903
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1013
void appendChild(const Element *el)
Definition html.cpp:783
int getIntAtributte(const std::string &name) const
Definition html.cpp:2501
void setTagname(const std::string &name)
Definition html.cpp:739
void setAttribute(const std::string &name, const std::string &value)
Definition html.cpp:2439
void remove(Element *el)
Definition html.cpp:858
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
bool operator==(const HtmlElement *hel)
Definition html.cpp:830
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
void insertChild(const Element *el)
Definition html.cpp:750
Element * firstChild() const
Definition html.cpp:2419
const std::string getTagname() const
Definition html.cpp:744
HtmlElement * getElementbyID(const std::string &id) const
Definition html.cpp:2365
HtmlElement & operator=(const HtmlElement &hel)
Definition html.cpp:848
std::unique_ptr< Element > _childElement
Definition html.h:169
void setIntAttribute(const std::string &name, int value)
Definition html.cpp:2481
High level loader/saver for HTML documents (files and strings).
Definition html.h:430
void loadString(libhtmlpp::HtmlElement &html, const std::string &src)
Parses an HTML source string and copies the result into html.
Definition html.cpp:1930
void loadString(libhtmlpp::HtmlElement &html, const HtmlString *node)
void saveFile(libhtmlpp::HtmlElement &html, const std::string &path)
Serializes an HtmlElement subtree and writes it to a file.
Definition html.cpp:1957
void loadFile(libhtmlpp::HtmlElement &html, const std::string &path)
Loads an HTML file from disk into a given HtmlElement root.
Definition html.cpp:1901
const std::vector< char > & data() const
Definition html.cpp:350
friend void HtmlEncode(const std::string &input, HtmlString *output)
void append(const std::string &src)
Definition html.cpp:230
HtmlString & operator<<(const char *src)
Definition html.cpp:293
HtmlString & operator+=(const std::string &src)
Definition html.cpp:267
size_t size() const
Definition html.cpp:336
char operator[](size_t pos) const
Definition html.cpp:289
void push_back(const char src)
Definition html.cpp:218
void insert(size_t pos, char src)
Definition html.cpp:253
const char * c_str() const
Definition html.cpp:346
size_t length() const
Definition html.cpp:332
libhtmlpp::Element & parse()
Parses the current buffer into a DOM-like tree and returns the root element.
Definition html.cpp:355
const char * operator*()
Definition html.cpp:328
HtmlString & operator=(const std::string &src)
Definition html.cpp:277
const std::string str() const
Definition html.cpp:340
Row & operator<<(Column &&col)
Definition html.cpp:2662
Column & operator[](size_t pos)
Definition html.cpp:2716
void delColumn(size_t pos)
Definition html.cpp:2732
Row & operator[](size_t pos)
Definition html.cpp:2546
Row & operator<<(const Row &row)
Definition html.cpp:2531
void deleteRow(size_t pos)
Definition html.cpp:2590
void parse(HtmlElement *element)
Definition html.cpp:2577
void insert(HtmlElement *element)
Definition html.cpp:2562
void setHeader(int count,...)
Definition html.cpp:2580
Element representing a <script> tag and its text content.
Definition html.h:241
void setScript(const std::string &txt)
Definition html.cpp:1551
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1563
ScriptElement & operator=(const Element &hel)
Definition html.cpp:1541
void insertChild(const Element &el)=delete
std::unique_ptr< Element > _childElement
Definition html.h:263
void appendChild(const Element &el)=delete
std::vector< char > _Script
Definition html.h:265
void appendChild(const Element *el)=delete
void insertChild(const Element *el)=delete
const std::string getScript()
Definition html.cpp:1555
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
Element representing an embedded <svg> tag and its attributes/content.
Definition html.h:274
int getType() const
Definition html.cpp:1717
void appendChild(const Element &el)=delete
void insertChild(const Element *el)=delete
SvgElement & operator=(const Element &hel)
Definition html.cpp:1698
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1721
std::unique_ptr< Element > _childElement
Definition html.h:296
const std::vector< char > getSvg()
Definition html.cpp:1713
void insertChild(const Element &el)=delete
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
std::vector< char > _Svg
Definition html.h:298
void appendChild(const Element *el)=delete
void setSvg(const std::string &svg)
Definition html.cpp:1708
Element representing an embedded <textarea> tag and its attributes/content.
Definition html.h:308
std::unique_ptr< Element > _childElement
Definition html.h:330
void appendChild(const Element &el)=delete
void insertChild(const Element &el)=delete
void appendChild(const Element *el)=delete
void insertChild(const Element *el)=delete
std::vector< char > _Text
Definition html.h:332
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
TextArea & operator=(const Element &hel)
Definition html.cpp:1779
const std::vector< char > getText()
Definition html.cpp:1794
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1802
int getType() const
Definition html.cpp:1798
void setText(const std::string &text)
Definition html.cpp:1789
Leaf node representing plain text content of an HTML document.
Definition html.h:189
TextElement & operator=(const Element &hel)
Definition html.cpp:1381
std::vector< char > _Text
Definition html.h:207
void setText(const std::string &txt)
Definition html.cpp:1391
int getType() const
Definition html.cpp:1399
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1403
const std::string getText()
Definition html.cpp:1395
std::ostream & operator<<(std::ostream &os, const libhtmlpp::HtmlString &p)
Streams an HtmlString to an output stream using its underlying string.
Definition html.cpp:667
Core namespace for the libhtmlpp HTML parsing and printing library.
Definition css.h:34
ElementType
Definition html.h:64
@ TextEl
Definition html.h:65
@ ScriptEL
Definition html.h:68
@ HtmlEl
Definition html.h:66
@ TextAreaEL
Definition html.h:70
@ SvgEL
Definition html.h:69
@ CommentEl
Definition html.h:67
void print(const Element &element, HtmlString &output, bool formated=false)
Serializes an element (and its subtree) into an HtmlString.
Definition html.cpp:2067
void HtmlEncode(const std::string &input, std::string &output)
Encodes special HTML characters in a string and writes into std::string.
Definition html.cpp:672
void HtmlDecode(const std::string &input, HtmlString &output)
Decodes special HTML characters in a string and appends to an HtmlString.
Definition html.cpp:706
const std::string getValue() const
Definition html.cpp:2427
friend void print(const Element &element, HtmlString &output, bool formated)
Serializes an element (and its subtree) into an HtmlString.
Attributes * nextAttribute() const
Definition html.cpp:2431
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1056
const std::string getKey() const
Definition html.cpp:2423