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 // preserveAttrCase: skip lowercasing attribute keys (SVG's own attributes
172 // like viewBox/preserveAspectRatio are case-sensitive per the XML/SVG spec,
173 // unlike regular HTML attributes).
174 void _serialelize(const std::vector<char> &in, bool preserveAttrCase = false);
175 private:
176 //if text tagname must be zero
177 std::vector<char> _TagName;
178
179 //if text Attributes must be zero
180 std::unique_ptr<Attributes> _firstAttr;
181 Attributes* _lastAttr;
182
183 friend class HtmlString;
184 friend class HtmlTable;
185 friend void print(const Element& element, HtmlString &output,bool formated);
186 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
187 };
192 class TextElement : public Element {
193 public:
194 TextElement();
195 TextElement(const std::string &txt);
196 TextElement(const TextElement &texel);
197 ~TextElement();
198
199 TextElement& operator=(const Element &hel);
200 TextElement& operator=(const Element *hel);
201
202 const std::string getText();
203 void setText(const std::string &txt);
204
205 int getType() const;
206
207 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
208
209 protected:
210 std::vector<char> _Text;
211 friend class HtmlString;
212 friend void print(const Element& element, HtmlString &output,bool formated);
213 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
214 };
219 class CommentElement : public Element{
220 public:
222 CommentElement(const CommentElement &comel);
224
225 CommentElement& operator=(const Element &hel);
226 CommentElement& operator=(const Element *hel);
227
228 const std::string getComment();
229 void setComment(const std::string &txt);
230
231 int getType() const;
232
233 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
234 protected:
235 std::vector<char> _Comment;
236 friend class HtmlString;
237 friend void print(const Element& element, HtmlString &output,bool formated);
238 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
239 };
245 public:
247 ScriptElement(const ScriptElement &scriptsrc);
249
250 ScriptElement& operator=(const Element &hel);
251 ScriptElement& operator=(const Element *hel);
252
253 const std::string getScript();
254 void setScript(const std::string &txt);
255
256 int getType() const;
257
258 void insertChild(const Element* el)=delete;
259 void insertChild(const Element& el)=delete;
260 void appendChild(const Element* el)=delete;
261 void appendChild(const Element& el)=delete;
262
263 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
264 protected:
265
266 std::unique_ptr<Element> _childElement=nullptr;
267
268 std::vector<char> _Script;
269 friend class HtmlString;
270 friend void print(const Element& element, HtmlString &output,bool formated);
271 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
272 };
277 class SvgElement : public HtmlElement{
278 public:
279 SvgElement();
280 SvgElement(const SvgElement &svgsrc);
281 ~SvgElement();
282
283 SvgElement& operator=(const Element &hel);
284 SvgElement& operator=(const Element *hel);
285
286 const std::vector<char> getSvg();
287 void setSvg(const std::string &svg);
288
289 int getType() const;
290
291 void insertChild(const Element* el)=delete;
292 void insertChild(const Element& el)=delete;
293 void appendChild(const Element* el)=delete;
294 void appendChild(const Element& el)=delete;
295
296 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
297 protected:
298
299 std::unique_ptr<Element> _childElement=nullptr;
300
301 std::vector<char> _Svg;
302 friend class HtmlString;
303 friend void print(const Element& element, HtmlString &output,bool formated);
304 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
305 };
306
311 class TextArea : public HtmlElement{
312 public:
313 TextArea();
314 TextArea(const TextArea &textsrc);
315 ~TextArea();
316
317 TextArea& operator=(const Element &hel);
318 TextArea& operator=(const Element *hel);
319
320 const std::vector<char> getText();
321 void setText(const std::string &text);
322
323 int getType() const;
324
325 void insertChild(const Element* el)=delete;
326 void insertChild(const Element& el)=delete;
327 void appendChild(const Element* el)=delete;
328 void appendChild(const Element& el)=delete;
329
330 static size_t parseElement(const std::vector<char> &in,std::unique_ptr<libhtmlpp::Element> &el,size_t start,bool &termination);
331 protected:
332
333 std::unique_ptr<Element> _childElement=nullptr;
334
335 std::vector<char> _Text;
336 friend class HtmlString;
337 friend void print(const Element& element, HtmlString &output,bool formated);
338 friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
339 };
340
349 void print(const Element& element, HtmlString &output,bool formated=false);
350
352 public:
353
354 using value_type = char;
355
356 HtmlString()=default;
357 HtmlString(const HtmlString &str);
358 HtmlString(char str);
359 HtmlString(const std::string &str);
360 ~HtmlString();
361
362 void append(const std::string &src);
363 void append(HtmlString& hstring);
364
365 void push_back(const char src);
366
367 void insert(size_t pos, char src);
368
369 HtmlString& operator+=(const std::string &src);
371 HtmlString& operator=(const std::string &src);
372 HtmlString& operator=(const HtmlString& src);
373 char operator[](size_t pos) const;
374
375 HtmlString& operator<<(const char* src);
376 HtmlString& operator<<(const std::string &src);
378 HtmlString& operator<<(int src);
379 HtmlString& operator<<(char src);
380 HtmlString& operator<<(size_t src);
381
382 const char* operator*();
383
384 size_t size() const;
385 size_t length() const;
386 void clear();
387 bool empty();
388
389 const std::vector<char>& data() const;
390 const std::string str() const;
391 const char *c_str() const;
399
400 private:
401 std::unique_ptr<Element> _rootEl;
402 void _buildTree();
403 void _buildtreenode(DocElements *firstel,DocElements *lastel,std::unique_ptr<Element>&html);
404 std::vector<char> _Data;
405 friend void HtmlEncode(const std::string &input,HtmlString *output);
406 friend class HtmlPage;
407 };
413 void HtmlDecode(const std::string &input,HtmlString &output);
414
420 void HtmlDecode(const std::string &input,std::string &output);
421
428 void HtmlEncode(const std::string &input,std::string &output);
433 class HtmlPage {
434 public:
435 HtmlPage();
436 ~HtmlPage();
437 void loadFile(libhtmlpp::HtmlElement &html,const std::string &path);
438 void saveFile(libhtmlpp::HtmlElement &html,const std::string &path);
439 void loadString(libhtmlpp::HtmlElement &html,const std::string &src);
440 void loadString(libhtmlpp::HtmlElement &html,const HtmlString &node);
442 bool isHtml5();
443 private:
444 void _CheckHeader(const HtmlString& page);
445 bool _Html5 = true;
446 };
447
448 class HtmlTable {
449 public:
450 HtmlTable();
451 ~HtmlTable();
452
453 class Column {
454 public:
455 std::string Data;
456 ~Column();
457 Column(const Column &col);
458 Column();
459 Column(const std::string &data);
460 Column(const HtmlString &data);
461 Column(Column&& col) noexcept;
462 private:
463 std::unique_ptr<Column> _nextColumn;
464 friend class HtmlTable;
465 };
466
467 class Row {
468 public:
469 Row();
470 Row(const Row &row);
471 ~Row();
472
473 Row &operator<<(Column &&col);
474 Row& operator<<(const Column &col);
475 Row& operator<<(const HtmlString &value);
476 Row& operator<<(const std::string &value);
477 Row& operator<<(const char* value);
478 Row& operator<<(int value);
479
480 Column& operator[](size_t pos);
481
482 void delColumn(size_t pos);
483 void clear();
484 private:
485 std::unique_ptr<Column> _firstColumn;
486 Column *_lastColumn;
487 std::unique_ptr<Row> _nextRow;
488 size_t _count;
489 friend class HtmlTable;
490 };
491
492 Row& operator<<(const Row &row);
493 Row& operator[](size_t pos);
494
495 void insert(HtmlElement *element);
496 void parse(HtmlElement *element);
497
498 void setHeader(int count,...);
499 void deleteRow(size_t pos);
500 private:
501 std::unique_ptr<Row> _firstRow;
502 Row *_lastRow;
503 Row _header;
504 size_t _count;
505 };
506};
507
508std::ostream& operator<<(std::ostream& os, const libhtmlpp::HtmlString& p);
Leaf node representing an HTML comment ().
Definition html.h:219
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1499
CommentElement & operator=(const Element &hel)
Definition html.cpp:1476
std::vector< char > _Comment
Definition html.h:235
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:1068
void setComment(const std::string &txt)
Definition html.cpp:1486
const std::string getComment()
Definition html.cpp:1491
Abstract base class for all nodes in the HTML tree.
Definition html.h:76
virtual void remove(Element *el)
Definition html.cpp:1357
Element * _prevElement
Definition html.h:99
void insertAfter(Element *el)
Definition html.cpp:1280
virtual ~Element()
Definition html.cpp:1350
void insertBefore(Element *el)
Definition html.cpp:1248
Element & operator=(const Element &hel)
Definition html.cpp:1321
std::unique_ptr< Element > _nextElement
Definition html.h:98
virtual int getType() const =0
Definition html.cpp:745
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:1335
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1068
Element * nextElement() const
Definition html.cpp:1331
int getType() const
Definition html.cpp:2535
const Attributes * firstAttribute() const
Definition html.cpp:2453
HtmlElement * getElementbyTag(const std::string &tag) const
Definition html.cpp:2410
const std::string getAtributte(const std::string &name) const
Definition html.cpp:2505
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:913
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1025
void appendChild(const Element *el)
Definition html.cpp:793
int getIntAtributte(const std::string &name) const
Definition html.cpp:2519
void setTagname(const std::string &name)
Definition html.cpp:749
void setAttribute(const std::string &name, const std::string &value)
Definition html.cpp:2457
void remove(Element *el)
Definition html.cpp:868
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:840
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1068
void insertChild(const Element *el)
Definition html.cpp:760
Element * firstChild() const
Definition html.cpp:2437
const std::string getTagname() const
Definition html.cpp:754
HtmlElement * getElementbyID(const std::string &id) const
Definition html.cpp:2383
HtmlElement & operator=(const HtmlElement &hel)
Definition html.cpp:858
std::unique_ptr< Element > _childElement
Definition html.h:169
void setIntAttribute(const std::string &name, int value)
Definition html.cpp:2499
High level loader/saver for HTML documents (files and strings).
Definition html.h:433
void loadString(libhtmlpp::HtmlElement &html, const std::string &src)
Parses an HTML source string and copies the result into html.
Definition html.cpp:1948
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:1975
void loadFile(libhtmlpp::HtmlElement &html, const std::string &path)
Loads an HTML file from disk into a given HtmlElement root.
Definition html.cpp:1919
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:2680
Column & operator[](size_t pos)
Definition html.cpp:2734
void delColumn(size_t pos)
Definition html.cpp:2750
Row & operator[](size_t pos)
Definition html.cpp:2564
Row & operator<<(const Row &row)
Definition html.cpp:2549
void deleteRow(size_t pos)
Definition html.cpp:2608
void parse(HtmlElement *element)
Definition html.cpp:2595
void insert(HtmlElement *element)
Definition html.cpp:2580
void setHeader(int count,...)
Definition html.cpp:2598
Element representing a <script> tag and its text content.
Definition html.h:244
void setScript(const std::string &txt)
Definition html.cpp:1563
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1575
ScriptElement & operator=(const Element &hel)
Definition html.cpp:1553
void insertChild(const Element &el)=delete
std::unique_ptr< Element > _childElement
Definition html.h:266
void appendChild(const Element &el)=delete
std::vector< char > _Script
Definition html.h:268
void appendChild(const Element *el)=delete
void insertChild(const Element *el)=delete
const std::string getScript()
Definition html.cpp:1567
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:1068
Element representing an embedded <svg> tag and its attributes/content.
Definition html.h:277
int getType() const
Definition html.cpp:1729
void appendChild(const Element &el)=delete
void insertChild(const Element *el)=delete
SvgElement & operator=(const Element &hel)
Definition html.cpp:1710
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1733
std::unique_ptr< Element > _childElement
Definition html.h:299
const std::vector< char > getSvg()
Definition html.cpp:1725
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:1068
std::vector< char > _Svg
Definition html.h:301
void appendChild(const Element *el)=delete
void setSvg(const std::string &svg)
Definition html.cpp:1720
Element representing an embedded <textarea> tag and its attributes/content.
Definition html.h:311
std::unique_ptr< Element > _childElement
Definition html.h:333
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:335
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:1797
const std::vector< char > getText()
Definition html.cpp:1812
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1068
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1820
int getType() const
Definition html.cpp:1816
void setText(const std::string &text)
Definition html.cpp:1807
Leaf node representing plain text content of an HTML document.
Definition html.h:192
TextElement & operator=(const Element &hel)
Definition html.cpp:1393
std::vector< char > _Text
Definition html.h:210
void setText(const std::string &txt)
Definition html.cpp:1403
int getType() const
Definition html.cpp:1411
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:1068
static size_t parseElement(const std::vector< char > &in, std::unique_ptr< libhtmlpp::Element > &el, size_t start, bool &termination)
Definition html.cpp:1415
const std::string getText()
Definition html.cpp:1407
std::ostream & operator<<(std::ostream &os, const libhtmlpp::HtmlString &p)
Streams an HtmlString to an output stream using its underlying string.
Definition html.cpp:677
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:2085
void HtmlEncode(const std::string &input, std::string &output)
Encodes special HTML characters in a string and writes into std::string.
Definition html.cpp:682
void HtmlDecode(const std::string &input, HtmlString &output)
Decodes special HTML characters in a string and appends to an HtmlString.
Definition html.cpp:716
const std::string getValue() const
Definition html.cpp:2445
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:2449
friend void _copy(libhtmlpp::Element *dest, const libhtmlpp::Element *src)
Definition html.cpp:1068
const std::string getKey() const
Definition html.cpp:2441