Loading...
Searching...
No Matches
TreeHTML.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31/*
32 * TreeXML.h
33 *
34 * Created on: Jun 24, 2012
35 * Author: mpeura
36 */
37
38
39
40#ifndef DRAIN_TREE_HTML
41#define DRAIN_TREE_HTML
42
43#include <ostream>
44
45#include "FileInfo.h"
46#include "ReferenceMap.h"
47#include "TreeXML.h"
48#include "UtilsXML.h"
49
50namespace drain {
51
52
53class NodeHTML;
54
56/*
57 typedef NodeHTML::xml_tree_t TreeHTML;
58
59 Note: the typedef defined in XML is not the same, it is
60 drain::UnorderedMultiTree<NodeXML<html::tag_t>,false, NodeXML<>::path_t> CoreTreeHTML;
61 That is, the first template is NodeXML<html::tag_t> and not the complete NodeHTML
62 */
63typedef drain::UnorderedMultiTree<NodeHTML,false, NodeXML<>::path_t> TreeHTML;
64
65
66// typedef drain::UnorderedMultiTree<NodeHTML,false, NodeXML<>::path_t> TreeHTML;
67
68struct Html {
69
70 enum tag_t {
71 UNDEFINED=NodeXML<>::UNDEFINED,
72 COMMENT=NodeXML<>::COMMENT,
73 CTEXT=NodeXML<>::CTEXT,
74 SCRIPT=NodeXML<>::SCRIPT,
75 STYLE=NodeXML<>::STYLE,
76 STYLE_SELECT=NodeXML<>::STYLE_SELECT,
77 JAVASCRIPT_SCOPE=XML::JAVASCRIPT_SCOPE,
78 HTML=10,
79 HEAD, BASE, LINK, META, TITLE,
80 BODY, A, BR, CAPTION, DIV, H1, H2, H3, HR, IMG, LI, OL, P, PRE, SPAN, TABLE, TR, TH, TD, UL};
81
82
83};
84
85
86
87// template <>
88// const drain::Enum<Html::tag_t>::dict_t drain::Enum<Html::tag_t>::dict;
89
90DRAIN_ENUM_DICT(Html::tag_t);
91DRAIN_ENUM_OSTREAM(Html::tag_t);
92
93// Note: this specialization actually applies to all XML paths of default type, that is NodeXML<int> .
94template <>
95template <>
96inline
97void drain::NodeXML<>::path_t::appendElem(const Html::tag_t & type){
98 // void TreeHTML::path_t::appendElem(const Html::tag_t & type){
99 appendElem(Enum<Html::tag_t>::dict.getKey(type, false));
100}
101
105//class NodeHTML : public HTML, public NodeXML<html::tag_t> {
106class NodeHTML : public Html, public NodeXML<Html::tag_t> {
107// class NodeHTML : public NodeXML<html::tag_t> {
108
109
110public:
111
112 // typedef int tag_t;
113
115 NodeHTML(const Html::tag_t & t = Html::tag_t(0));
116
118 NodeHTML(const NodeHTML & node);
119
120 inline
121 ~NodeHTML(){};
122
124 virtual
125 bool isSingular() const override final;
126
128 virtual
129 bool isExplicit() const override final;
130
131 inline
132 NodeHTML & operator=(const NodeHTML & node){
133 return XML::xmlAssignNode(*this, node);
134 }
135
136 template <class T>
137 inline
138 NodeHTML & operator=(const T & s){
139 set(s); // XML
140 return *this;
141 }
142
143 /*
144 inline
145 NodeHTML & operator=(const std::initializer_list<std::pair<const char *,const drain::Variable> > &l){
146 set(l);
147 return *this;
148 }
149 */
150
151 /*
152 static // virtual
153 inline
154 std::ostream & docTypeToStream(std::ostream &ostr){
155 ostr << "<!DOCTYPE html>\n";
156 return ostr;
157 }
158 */
159
160 static
161 const FileInfo fileInfo;
162
163 // virtual void handleType(const Html::tag_t &t) override final;
164
165protected:
166
167 virtual
168 void handleType() override final;
169
170};
171
172
173
174
175inline
176std::ostream & operator<<(std::ostream &ostr, const NodeHTML & node){
177 return node.nodeToStream(ostr);
178}
179
180
181// This is the way!
182template <> // for T (Tree class)
183inline
184std::ostream & NodeXML<Html::tag_t>::docTypeToStream(std::ostream &ostr){
185 ostr << "<!DOCTYPE html>\n";
186 return ostr;
187}
188
189
190
191
192
193// Important TAG type initialisations for elements.
194template <>
195const NodeXML<Html::tag_t>::xml_default_elem_map_t NodeXML<Html::tag_t>::xml_default_elems;
196
198
201template <>
202inline
203void TreeHTML::initChild(TreeHTML & child) const {
204 UtilsXML::initChildWithDefaultType(*this, child);
205 /*
206 const typename Html::tag_t type = UtilsXML::retrieveDefaultType(this->data);
207 if (type){
208 child->setType(type);
209 }
210 */
211}
212
213// Preferred template specification
214/*
215template <>
216inline
217TreeHTML & TreeHTML::addChild(){ // const TreeHTML::key_t & key){
218 return UtilsXML::addChild(*this);
219}
220*/
221
222
223
224// Preferred template specification
225template <>
226template <>
227inline
228TreeHTML & TreeHTML::operator()(const Html::tag_t & type){
229 return UtilsXML::setType(*this, type);
230}
231
232
233// NEW 2026/02/27 replacing many!?
234template <> // for T (Tree class)
235template <> // for K (path elem arg)
236inline
237const TreeHTML::key_t & TreeHTML::getKey(const Html::tag_t & type){
238 return Enum<Html::tag_t>::dict.getKey(type, false);
239}
240
241template <> // for T (Tree class)
242template <> // for K (path elem arg)
243inline
244const TreeHTML::key_t & TreeHTML::getKey(const ClassXML & cls){
245 return cls.strPrefixed();
246}
247
248/*
249// old template specification
250template <>
251template <>
252inline
253bool TreeHTML::hasChild(const Html::tag_t & type) const { // const TreeHTML::key_t & key){
254 return TreeHTML::hasChild(Enum<Html::tag_t>::dict.getKey(type, false));
255}
256
257template <> // for T (Tree class)
258template <> // for K (path elem arg)
259TreeHTML & TreeHTML::operator[](const Html::tag_t & type);
260
261template <> // for T (Tree class)
262template <> // for K (path elem arg)
263const TreeHTML & TreeHTML::operator[](const Html::tag_t & type) const;
264
265// Same for ClassXML (designed for children of STYLE element)
266
267template <> // for T (Tree class)
268template <> // for K (path elem arg)
269TreeHTML & TreeHTML::operator[](const ClassXML & cls);
270
272template <> // for T (Tree class)
273template <> // for K (path elem arg)
274const TreeHTML & TreeHTML::operator[](const ClassXML & cls) const ;
275
276template <> // for T (Tree class)
277template <> // for K (path elem arg)
278bool TreeHTML::hasChild(const ClassXML & cls) const;
279*/
280
281template <>
282inline
283//TreeHTML & TreeHTML::operator=(std::initializer_list<std::pair<const char *,const char *> > l){
284TreeHTML & TreeHTML::operator=(std::initializer_list<std::pair<const char *,const Variable> > l){
285 return UtilsXML::assign(*this, l);
286}
287
289template <>
290template <>
291inline
292TreeHTML & TreeHTML::operator=(const std::string & arg){
293 UtilsXML::assignString(*this, arg);
294 return *this;
295}
296
297
298template <>
299template <class T>
300inline
301TreeHTML & TreeHTML::operator=(const T & arg){
302 return UtilsXML::assign(*this, arg);
303}
304
305/*
306inline
307std::ostream & operator<<(std::ostream &ostr, const NodeHTML & node){
308 return drain::Sprinter::toStream(ostr, node.getAttributes());
309}
310*/
311
312
313inline
314std::ostream & operator<<(std::ostream &ostr, const TreeHTML & tree){
315 return NodeHTML::docToStream(ostr, tree);
316}
317
318
319// TODO: try
320// DRAIN_TYPENAME_STR
321DRAIN_TYPENAME(NodeHTML);
322DRAIN_TYPENAME(Html);
323DRAIN_TYPENAME(Html::tag_t);
324
325
326} // drain::
327
328#endif // DRAIN_TREE_HTML
329
Definition FileInfo.h:48
Definition TreeHTML.h:106
virtual bool isSingular() const override final
Tell if this element should always be a single tag, not separate opening and closing tags.
Definition TreeHTML.cpp:192
virtual bool isExplicit() const override final
Tell if this element should always have separate opening and closing tags even when empty,...
Definition TreeHTML.cpp:213
Definition TreeXML.h:341
static std::ostream & docTypeToStream(std::ostream &ostr)
Write the XML definition beginning any XML document.
Definition TreeXML.h:751
virtual std::ostream & nodeToStream(std::ostream &ostr, tag_display_mode mode=EMPTY_TAG) const override
Dumps info. Future option: outputs leading and ending tag.
Definition TreeXML.h:416
static std::ostream & docToStream(std::ostream &ostr, const V &tree)
Definition TreeXML.h:733
void appendElem(const elem_t &elem)
Main method for adding elements.
Definition Path.h:281
static T & assign(T &dst, const T &src)
Assign another tree structure to another.
Definition UtilsXML.h:60
static TX & setType(TX &tree, const typename TX::node_data_t::xml_tag_t &type)
Definition UtilsXML.h:172
static TX & assignString(TX &tree, const std::string &s)
When assigning a string, create new element unless the element itself is of type CTEXT.
Definition UtilsXML.h:134
static N & xmlAssignNode(N &dst, const N &src)
Assign tree node (data) to another.
Definition XML.h:664
Definition DataSelector.cpp:1277
drain::UnorderedMultiTree< NodeHTML, false, NodeXML<>::path_t > TreeHTML
The HTML data structure.
Definition TreeHTML.h:63
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.
A container for a static dictionary of enumeration values.
Definition Enum.h:51
Definition TreeHTML.h:68