1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
// file : web/xhtml -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#ifndef WEB_XHTML
#define WEB_XHTML
#include <xml/serializer>
namespace web
{
// "Canonical" XHTML5 vocabulary.
//
// * One-letter tag names and local variable clash problem
//
// a at|an|an anc anch
// b bt|bo|bl bld bold
// i it|it|it itl ital
// p pt|pr|pr par para
// q qt|qu|qt quo quot
// s st|st|st stk strk
// u ut|un|un unl undr
//
// Other options:
// - _a, a_, xa
// - A, I
// - x::i
// - user-defined literals: "a"_e, "/a"_e, "id"_a
//
// Things can actually get much worse, consider:
//
// int i;
// s << i << "text" << ~i;
//
// So perhaps this is the situation where the explicit namespace
// qualification (e.g., x::p) is the only robust option?
//
//
// * Element/attribute name clash problem (e.g., STYLE)
//
// - some attribute/element name decorator (STYLEA, STYLE_A, STYLE_)
// - rename attribute/element (e.g., STYLEDEF or CSSSTYLE[adds TYPE]);
// in case of STYLE we should probably rename the element since
// attribute will be much more frequently used.
// - "scope" attributes inside elements (P::STYLE); somewhat
// burdensome: P(P::STYLE); could then use low-case names
// for attributes
// - "scope" elements inside other elements (HEAD::STYLE); also
// burdensome.
//
//
// * Text wrapping/indentation
//
// For some (inline) elements we want additional indentation:
//
// 1. Indent content on newline (e.g., for <style>).
// 2. Automatically wrap and indent lines at (or before) certain
// length, say, 80 characters (e.g., for <p>).
//
// Would be nice to be able to implement this at the XHTML level,
// not XML.
//
namespace xhtml
{
const char* const xmlns = "http://www.w3.org/1999/xhtml";
struct attr_value_base
{
const char* name;
mutable const attr_value_base* next;
virtual void
operator() (xml::serializer& s) const = 0;
protected:
explicit
attr_value_base (const char* n): name (n), next (nullptr) {}
};
template <typename T>
struct attr_value: attr_value_base
{
const T* val;
attr_value (const char* n, const T& v): attr_value_base (n), val (&v) {}
virtual void
operator() (xml::serializer& s) const
{
s.attribute (name, *val);
if (next != nullptr)
s << *next;
}
};
struct element_base;
// Element without any content, e.g., *BR.
//
struct empty_element
{
const element_base* e;
void
operator() (xml::serializer& s) const;
};
class end_element
{
public:
const char* name;
explicit
end_element (const char* n): name (n) {}
virtual void
operator() (xml::serializer& s) const
{
return s.end_element (xmlns, name);
}
};
struct element_base
{
virtual void
operator() (xml::serializer& s) const = 0;
virtual end_element
operator~ () const = 0;
empty_element
operator* () const {return empty_element {this};}
};
inline void empty_element::
operator() (xml::serializer& s) const {s << *e << ~*e;}
// Element with an attribute chain, e.g., P(ID = 123, CLASS = "abc").
//
struct attr_element: element_base
{
const element_base* e;
const attr_value_base* a;
attr_element (const element_base& e, const attr_value_base& a)
: e (&e), a (&a) {}
virtual void
operator() (xml::serializer& s) const {s << *e << *a;}
virtual end_element
operator~ () const {return ~*e;}
};
struct element: element_base
{
const char* name;
explicit
element (const char* n): name (n) {}
virtual void
operator() (xml::serializer& s) const {s.start_element (xmlns, name);}
virtual end_element
operator~ () const {return end_element (name);}
// s << elem(attr1 = 123, attr2 = "abc");
//
template <typename T1>
attr_element
operator () (const attr_value<T1>& a1) const
{
return attr_element (*this, a1);
}
template <typename T1, typename... TN>
attr_element
operator () (const attr_value<T1>& a1, const attr_value<TN>&... an) const
{
a1.next = operator() (an...).a;
return attr_element (*this, a1);
}
};
class end_inline_element: public end_element
{
public:
explicit
end_inline_element (const char* n): end_element (n) {}
virtual void
operator() (xml::serializer& s) const
{
s.end_element (xmlns, name);
s.resume_indentation ();
}
};
struct inline_element: element
{
using element::element;
using element::operator();
virtual void
operator() (xml::serializer& s) const
{
s.suspend_indentation ();
element::operator() (s);
}
//@@ Can't do that: you are slicing end_inline_element to
// end_element.
//
virtual end_element
operator~ () const {return end_inline_element (name);}
};
class end_attribute
{
public:
const char* name;
explicit
end_attribute (const char* n): name (n) {}
virtual void
operator() (xml::serializer& s) const {return s.end_attribute (name);}
};
struct attribute
{
const char* name;
explicit
attribute (const char* n): name (n) {}
// s << (attr1 = 123) << (attr2 = "abc");
//
template <typename T>
attr_value<T>
operator= (const T& v) const {return attr_value<T> (name, v);}
// s << attr1 (123) << attr2 ("abc");
//
template <typename T>
attr_value<T>
operator() (const T& v) const {return attr_value<T> (name, v);}
// s << attr1 << 123 << ~attr1 << attr2 << "abc" << ~attr2;
//
void
operator() (xml::serializer& s) const {s.start_attribute (name);}
virtual end_attribute
operator~ () const {return end_attribute (name);}
};
// Elements.
//
// Note that they are all declared static which means we may end
// up with multiple identical copies if this header get included
// into multiple translation units. The hope here is that the
// compiler will "see-through" and eliminate all of them.
//
struct html_element: element
{
html_element (): element ("html") {}
virtual void
operator() (xml::serializer& s) const
{
s.doctype_decl ("html");
s.start_element (xmlns, name);
s.namespace_decl (xmlns, "");
}
};
static const html_element HTML;
struct head_element: element
{
head_element (): element ("head") {}
virtual void
operator() (xml::serializer& s) const
{
s.start_element (xmlns, name);
s.start_element (xmlns, "meta");
s.attribute ("charset", "UTF-8");
s.end_element ();
}
};
static const head_element HEAD;
struct css_style_element: element
{
css_style_element (): element ("style") {}
virtual void
operator() (xml::serializer& s) const
{
s.start_element (xmlns, name);
s.attribute ("type", "text/css");
}
};
static const css_style_element CSS_STYLE;
static const element BODY ("body");
static const element DIV ("div");
static const element P ("p");
static const element TITLE ("title");
static const inline_element A ("a");
static const inline_element B ("b");
static const inline_element I ("i");
static const inline_element U ("u");
static const inline_element EM ("em");
static const inline_element BR ("br");
// Attributes.
//
static const attribute CLASS ("class");
static const attribute HREF ("href");
static const attribute ID ("id");
static const attribute STYLE ("style");
static const attribute TYPE ("type");
}
}
#endif // WEB_XHTML
|