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 Element* parentElement() const;
91
92 virtual void remove(Element* el);
93
94 virtual int getType() const=0;
95 protected:
96
97 Element();
98
99 std::unique_ptr<Element> _nextElement;
102
103 friend class HtmlElement;
104 friend class TextElement;
105 friend class HtmlString;
106 friend void print(const Element& element, HtmlString &output,bool formated);
107 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
108 };
109
110 class HtmlElement : public Element {
111 public:
112 HtmlElement();
113 HtmlElement(const std::string &tag);
114 HtmlElement(const HtmlElement &hel);
115 HtmlElement(const HtmlElement *hel);
116 ~HtmlElement();
117
118 HtmlElement& operator=(const HtmlElement &hel);
119 HtmlElement& operator=(const HtmlElement *hel);
120
121 bool operator==(const HtmlElement *hel);
122 bool operator==(const HtmlElement &hel);
123
124 void setAttribute(const std::string &name, const std::string &value);
125
126 void setIntAttribute(const std::string &name, int value);
127
128 const std::string getAtributte(const std::string &name) const;
129
130 int getIntAtributte(const std::string &name) const;
131
132 void insertChild(const Element* el);
133 void insertChild(const Element& el);
134 void appendChild(const Element* el);
135 void appendChild(const Element& el);
136
137 void setTagname(const std::string &name);
138 const std::string getTagname() const;
139
140 HtmlElement *getElementbyID(const std::string &id) const;
141 HtmlElement *getElementbyTag(const std::string &tag) const;
142
143 Element* firstChild() const;
144
145 struct Attributes {
146 Attributes();
147 ~Attributes();
148
149 const std::string getKey() const;
150 const std::string getValue() const;
151 Attributes* nextAttribute() const;
152
153 private:
154 std::vector<char> _Key;
155 std::vector<char> _Value;
156 std::unique_ptr<Attributes> _nextAttr;
157
158 friend class HtmlElement;
159 friend void print(const Element& element, HtmlString &output,bool formated);
160 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
161 };
162
163 const Attributes* firstAttribute() const;
164
165 int getType() const;
166 void remove(Element* el);
167
168 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
169 protected:
170
171 std::unique_ptr<Element> _childElement=nullptr;
172
173 // preserveAttrCase: skip lowercasing attribute keys (SVG's own attributes
174 // like viewBox/preserveAspectRatio are case-sensitive per the XML/SVG spec,
175 // unlike regular HTML attributes).
176 void _serialelize(const std::vector<char> &in, bool preserveAttrCase = false);
177 private:
178 //if text tagname must be zero
179 std::vector<char> _TagName;
180
181 //if text Attributes must be zero
182 std::unique_ptr<Attributes> _firstAttr;
183 Attributes* _lastAttr;
184
185 // Sets `node`'s parent to `parent`, and does the same for every
186 // sibling reachable via nextElement() (they share the same parent)
187 // -- recursing into each HtmlElement-shaped node's own child chain
188 // (with that node as the new parent). Called after building or
189 // copying a subtree, once its own sibling/child links are already
190 // correct, rather than threaded through the parser/copy internals
191 // themselves.
192 static void _assignParentPointers(Element* node, Element* parent);
193
194 friend class HtmlString;
195 friend class HtmlTable;
196 friend void print(const Element& element, HtmlString &output,bool formated);
197 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
198 };
203 class TextElement : public Element {
204 public:
205 TextElement();
206 TextElement(const std::string &txt);
207 TextElement(const TextElement &texel);
208 ~TextElement();
209
210 TextElement& operator=(const Element &hel);
211 TextElement& operator=(const Element *hel);
212
213 const std::string getText();
214 void setText(const std::string &txt);
215
216 int getType() const;
217
218 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
219
220 protected:
221 std::vector<char> _Text;
222 friend class HtmlString;
223 friend void print(const Element& element, HtmlString &output,bool formated);
224 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
225 };
230 class CommentElement : public Element{
231 public:
233 CommentElement(const CommentElement &comel);
235
236 CommentElement& operator=(const Element &hel);
237 CommentElement& operator=(const Element *hel);
238
239 const std::string getComment();
240 void setComment(const std::string &txt);
241
242 int getType() const;
243
244 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
245 protected:
246 std::vector<char> _Comment;
247 friend class HtmlString;
248 friend void print(const Element& element, HtmlString &output,bool formated);
249 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
250 };
256 public:
258 ScriptElement(const ScriptElement &scriptsrc);
260
261 ScriptElement& operator=(const Element &hel);
262 ScriptElement& operator=(const Element *hel);
263
264 const std::string getScript();
265 void setScript(const std::string &txt);
266
267 int getType() const;
268
269 void insertChild(const Element* el)=delete;
270 void insertChild(const Element& el)=delete;
271 void appendChild(const Element* el)=delete;
272 void appendChild(const Element& el)=delete;
273
274 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
275 protected:
276
277 std::unique_ptr<Element> _childElement=nullptr;
278
279 std::vector<char> _Script;
280 friend class HtmlString;
281 friend void print(const Element& element, HtmlString &output,bool formated);
282 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
283 };
288 class SvgElement : public HtmlElement{
289 public:
290 SvgElement();
291 SvgElement(const SvgElement &svgsrc);
292 ~SvgElement();
293
294 SvgElement& operator=(const Element &hel);
295 SvgElement& operator=(const Element *hel);
296
297 const std::vector<char> getSvg();
298 void setSvg(const std::string &svg);
299
300 int getType() const;
301
302 void insertChild(const Element* el)=delete;
303 void insertChild(const Element& el)=delete;
304 void appendChild(const Element* el)=delete;
305 void appendChild(const Element& el)=delete;
306
307 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
308 protected:
309
310 std::unique_ptr<Element> _childElement=nullptr;
311
312 std::vector<char> _Svg;
313 friend class HtmlString;
314 friend void print(const Element& element, HtmlString &output,bool formated);
315 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
316 };
317
322 class TextArea : public HtmlElement{
323 public:
324 TextArea();
325 TextArea(const TextArea &textsrc);
326 ~TextArea();
327
328 TextArea& operator=(const Element &hel);
329 TextArea& operator=(const Element *hel);
330
331 const std::vector<char> getText();
332 void setText(const std::string &text);
333
334 int getType() const;
335
336 void insertChild(const Element* el)=delete;
337 void insertChild(const Element& el)=delete;
338 void appendChild(const Element* el)=delete;
339 void appendChild(const Element& el)=delete;
340
341 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
342 protected:
343
344 std::unique_ptr<Element> _childElement=nullptr;
345
346 std::vector<char> _Text;
347 friend class HtmlString;
348 friend void print(const Element& element, HtmlString &output,bool formated);
349 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
350 };
351
360 void print(const Element& element, HtmlString &output,bool formated=false);
361
363 public:
364
365 using value_type = char;
366
367 HtmlString()=default;
368 HtmlString(const HtmlString &str);
369 HtmlString(char str);
370 HtmlString(const std::string &str);
371 ~HtmlString();
372
373 void append(const std::string &src);
374 void append(HtmlString& hstring);
375
376 void push_back(const char src);
377
378 void insert(size_t pos, char src);
379
380 HtmlString& operator+=(const std::string &src);
382 HtmlString& operator=(const std::string &src);
383 HtmlString& operator=(const HtmlString& src);
384 char operator[](size_t pos) const;
385
386 HtmlString& operator<<(const char* src);
387 HtmlString& operator<<(const std::string &src);
389 HtmlString& operator<<(int src);
390 HtmlString& operator<<(char src);
391 HtmlString& operator<<(size_t src);
392
393 const char* operator*();
394
395 size_t size() const;
396 size_t length() const;
397 void clear();
398 bool empty();
399
400 const std::vector<char>& data() const;
401 const std::string str() const;
402 const char *c_str() const;
410
411 private:
412 std::unique_ptr<Element> _rootEl;
413 void _buildTree();
414 void _buildtreenode(DocElements *firstel,DocElements *lastel,std::unique_ptr<Element>&html);
415 std::vector<char> _Data;
416 friend void HtmlEncode(const std::string &input,HtmlString *output);
417 friend class HtmlPage;
418 };
424 void HtmlDecode(const std::string &input,HtmlString &output);
425
431 void HtmlDecode(const std::string &input,std::string &output);
432
439 void HtmlEncode(const std::string &input,std::string &output);
444 class HtmlPage {
445 public:
446 HtmlPage();
447 ~HtmlPage();
448 void loadFile(libhtmlpp::HtmlElement &html,const std::string &path);
449 void saveFile(libhtmlpp::HtmlElement &html,const std::string &path);
450 void loadString(libhtmlpp::HtmlElement &html,const std::string &src);
451 void loadString(libhtmlpp::HtmlElement &html,const HtmlString &node);
453 bool isHtml5();
454 private:
455 void _CheckHeader(const HtmlString& page);
456 bool _Html5 = true;
457 };
458
459 class HtmlTable {
460 public:
461 HtmlTable();
462 ~HtmlTable();
463
464 class Column {
465 public:
466 std::string Data;
467 ~Column();
468 Column(const Column &col);
469 Column();
470 Column(const std::string &data);
471 Column(const HtmlString &data);
472 Column(Column&& col) noexcept;
473 private:
474 std::unique_ptr<Column> _nextColumn;
475 friend class HtmlTable;
476 };
477
478 class Row {
479 public:
480 Row();
481 Row(const Row &row);
482 ~Row();
483
484 Row &operator<<(Column &&col);
485 Row& operator<<(const Column &col);
486 Row& operator<<(const HtmlString &value);
487 Row& operator<<(const std::string &value);
488 Row& operator<<(const char* value);
489 Row& operator<<(int value);
490
491 Column& operator[](size_t pos);
492
493 void delColumn(size_t pos);
494 void clear();
495 private:
496 std::unique_ptr<Column> _firstColumn;
497 Column *_lastColumn;
498 std::unique_ptr<Row> _nextRow;
499 size_t _count;
500 friend class HtmlTable;
501 };
502
503 Row& operator<<(const Row &row);
504 Row& operator[](size_t pos);
505
506 void insert(HtmlElement *element);
507 void parse(HtmlElement *element);
508
509 void setHeader(int count,...);
510 void deleteRow(size_t pos);
511 private:
512 std::unique_ptr<Row> _firstRow;
513 Row *_lastRow;
514 Row _header;
515 size_t _count;
516 };
517};
518
519std::ostream& operator<<(std::ostream& os, const libhtmlpp::HtmlString& p);
Leaf node representing an HTML comment ().
Definition html.h:230
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1568
CommentElement & operator=(const Element &hel)
Definition html.cpp:1545
std::vector< char > _Comment
Definition html.h:246
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:1102
void setComment(const std::string &txt)
Definition html.cpp:1555
const std::string getComment()
Definition html.cpp:1560
Abstract base class for all nodes in the HTML tree.
Definition html.h:76
virtual void remove(Element *el)
Definition html.cpp:1415
Element * _prevElement
Definition html.h:100
void insertAfter(Element *el)
Definition html.cpp:1334
virtual ~Element()
Definition html.cpp:1408
Element * _parentElement
Definition html.h:101
void insertBefore(Element *el)
Definition html.cpp:1302
Element & operator=(const Element &hel)
Definition html.cpp:1375
std::unique_ptr< Element > _nextElement
Definition html.h:99
virtual int getType() const =0
Definition html.cpp:751
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:1389
Element * parentElement() const
Definition html.cpp:1393
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1102
Element * nextElement() const
Definition html.cpp:1385
int getType() const
Definition html.cpp:2604
const Attributes * firstAttribute() const
Definition html.cpp:2522
HtmlElement * getElementbyTag(const std::string &tag) const
Definition html.cpp:2479
const std::string getAtributte(const std::string &name) const
Definition html.cpp:2574
void _serialelize(const std::vector< char > &in, bool preserveAttrCase=false)
Extracts tag name and attributes from a token vector into an HtmlElement.
Definition html.cpp:947
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1059
void appendChild(const Element *el)
Definition html.cpp:821
int getIntAtributte(const std::string &name) const
Definition html.cpp:2588
void setTagname(const std::string &name)
Definition html.cpp:755
void setAttribute(const std::string &name, const std::string &value)
Definition html.cpp:2526
void remove(Element *el)
Definition html.cpp:898
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:870
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1102
void insertChild(const Element *el)
Definition html.cpp:784
Element * firstChild() const
Definition html.cpp:2506
const std::string getTagname() const
Definition html.cpp:760
HtmlElement * getElementbyID(const std::string &id) const
Definition html.cpp:2452
HtmlElement & operator=(const HtmlElement &hel)
Definition html.cpp:888
std::unique_ptr< Element > _childElement
Definition html.h:171
void setIntAttribute(const std::string &name, int value)
Definition html.cpp:2568
High level loader/saver for HTML documents (files and strings).
Definition html.h:444
void loadString(libhtmlpp::HtmlElement &html, const std::string &src)
Parses an HTML source string and copies the result into html.
Definition html.cpp:2017
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:2044
void loadFile(libhtmlpp::HtmlElement &html, const std::string &path)
Loads an HTML file from disk into a given HtmlElement root.
Definition html.cpp:1988
const std::vector< char > & data() const
Definition html.cpp:360
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:356
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:365
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:350
Row & operator<<(Column &&col)
Definition html.cpp:2749
Column & operator[](size_t pos)
Definition html.cpp:2803
void delColumn(size_t pos)
Definition html.cpp:2819
Row & operator[](size_t pos)
Definition html.cpp:2633
Row & operator<<(const Row &row)
Definition html.cpp:2618
void deleteRow(size_t pos)
Definition html.cpp:2677
void parse(HtmlElement *element)
Definition html.cpp:2664
void insert(HtmlElement *element)
Definition html.cpp:2649
void setHeader(int count,...)
Definition html.cpp:2667
Element representing a <script> tag and its text content.
Definition html.h:255
void setScript(const std::string &txt)
Definition html.cpp:1632
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1644
ScriptElement & operator=(const Element &hel)
Definition html.cpp:1622
void insertChild(const Element &el)=delete
std::unique_ptr< Element > _childElement
Definition html.h:277
void appendChild(const Element &el)=delete
std::vector< char > _Script
Definition html.h:279
void appendChild(const Element *el)=delete
void insertChild(const Element *el)=delete
const std::string getScript()
Definition html.cpp:1636
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:1102
Element representing an embedded <svg> tag and its attributes/content.
Definition html.h:288
int getType() const
Definition html.cpp:1798
void appendChild(const Element &el)=delete
void insertChild(const Element *el)=delete
SvgElement & operator=(const Element &hel)
Definition html.cpp:1779
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1802
std::unique_ptr< Element > _childElement
Definition html.h:310
const std::vector< char > getSvg()
Definition html.cpp:1794
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:1102
std::vector< char > _Svg
Definition html.h:312
void appendChild(const Element *el)=delete
void setSvg(const std::string &svg)
Definition html.cpp:1789
Element representing an embedded <textarea> tag and its attributes/content.
Definition html.h:322
std::unique_ptr< Element > _childElement
Definition html.h:344
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:346
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:1866
const std::vector< char > getText()
Definition html.cpp:1881
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1102
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1889
int getType() const
Definition html.cpp:1885
void setText(const std::string &text)
Definition html.cpp:1876
Leaf node representing plain text content of an HTML document.
Definition html.h:203
TextElement & operator=(const Element &hel)
Definition html.cpp:1462
std::vector< char > _Text
Definition html.h:221
void setText(const std::string &txt)
Definition html.cpp:1472
int getType() const
Definition html.cpp:1480
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:1102
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1484
const std::string getText()
Definition html.cpp:1476
std::ostream & operator<<(std::ostream &os, const libhtmlpp::HtmlString &p)
Streams an HtmlString to an output stream using its underlying string.
Definition html.cpp:683
Core namespace for the libhtmlpp HTML parsing and printing library.
Definition css.h:36
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:2154
void HtmlEncode(const std::string &input, std::string &output)
Encodes special HTML characters in a string and writes into std::string.
Definition html.cpp:688
void HtmlDecode(const std::string &input, HtmlString &output)
Decodes special HTML characters in a string and appends to an HtmlString.
Definition html.cpp:722
const std::string getValue() const
Definition html.cpp:2514
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:2518
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1102
const std::string getKey() const
Definition html.cpp:2510