-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebxx.h
2062 lines (1874 loc) · 74.7 KB
/
webxx.h
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef WEBXX_H
#define WEBXX_H
// _ https://github.com/rthrfrd/webxx
// __ _____| |____ ___ __
// \ \ /\ / / _ | '_ \ \/ \ \/ / MIT License
// \ V V | __| |_) > < > < Copyright (c) 2022
// \_/\_/ \___|_.__/_/\_/_/\_\ Alexander Carver
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <cstddef>
#include <cstring>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <typeinfo>
#include <unordered_set>
#include <vector>
#define WEBXX_CSS_PROP(NAME)\
namespace internal { namespace res { constexpr char NAME ## P[] = #NAME; }}\
using NAME = internal::CssProperty<internal::res::NAME ## P>
#define WEBXX_CSS_PROP_ALIAS(NAME,ALIAS)\
namespace internal { namespace res { constexpr char ALIAS ## P[] = #NAME; }}\
using ALIAS = internal::CssProperty<internal::res::ALIAS ## P>
#define WEBXX_CSS_AT_SINGLE(NAME,ALIAS)\
namespace internal { namespace res { constexpr char ALIAS ## CA[] = #NAME; }}\
using ALIAS = internal::CssAtSingle<internal::res::ALIAS ## CA>
#define WEBXX_CSS_AT_NESTED(NAME,ALIAS)\
namespace internal { namespace res { constexpr char ALIAS ## CA[] = #NAME; }}\
using ALIAS = internal::CssAtNested<internal::res::ALIAS ## CA>
#define WEBXX_HTML_EL(TAG)\
namespace internal { namespace res { constexpr char TAG ## T[] = #TAG; }}\
using TAG = internal::HtmlNodeDefined<internal::res::TAG ## T>
#define WEBXX_HTML_EL_ALIAS(TAG,ALIAS)\
namespace internal { namespace res { constexpr char ALIAS ## T[] = #TAG; }}\
using ALIAS = internal::HtmlNodeDefined<internal::res::ALIAS ## T>
#define WEBXX_HTML_EL_SELF_CLOSING(TAG)\
namespace internal { namespace res { constexpr char TAG ## T[] = #TAG; }}\
using TAG = internal::HtmlNodeDefined<internal::res::TAG ## T,internal::none,true>
#define WEBXX_HTML_ATTR(NAME)\
namespace internal { namespace res { constexpr char NAME ## A[] = #NAME; }}\
using _ ## NAME = internal::HtmlAttributeDefined<internal::res::NAME ## A>
#define WEBXX_HTML_ATTR_ALIAS(NAME,ALIAS)\
namespace internal { namespace res { constexpr char ALIAS ## A[] = #NAME; }}\
using _ ## ALIAS = internal::HtmlAttributeDefined<internal::res::ALIAS ## A>
#define WEBXX_MOVE_ONLY_CONSTRUCTORS(CLASS)\
CLASS (CLASS &&) = default;\
CLASS & operator= (CLASS &&) = default;\
CLASS (const CLASS &) = delete;\
CLASS & operator= (const CLASS &) = delete;
#ifdef _MSC_VER
#define WEBXX_FN_SIG __FUNCSIG__
#else
#define WEBXX_FN_SIG __PRETTY_FUNCTION__
#endif
namespace Webxx { namespace internal {
static constexpr const char none[] = "";
}}
////| |////
////| Placeholders |////
////| |////
namespace Webxx { namespace internal {
class Placeholder : public std::string {
public:
using std::string::string;
};
using PlaceholderPopulator = std::function<const std::string_view(
const std::string_view&,
const std::string_view&
)>;
inline const std::string_view noopPopulator (
const std::string_view& value,
const std::string_view&
) {
return value;
}
namespace exports {
using PlaceholderPopulator = PlaceholderPopulator;
using _ = Placeholder;
}
}}
////| |////
////| Text |////
////| |////
namespace Webxx { namespace internal {
struct Text {
enum class Type {
LITERAL = 0,
PLACEHOLDER = 1,
};
Type type;
mutable std::optional<std::string> data;
std::string_view view;
Text (Text&& other) : // move construct
type{other.type},
data{std::move(other.data)},
view{data ? *data : other.view}
{}
Text& operator= (Text&& other) { // move assign
this->type = other.type;
this->data = std::move(other.data);
this->view = this->data ? *(this->data) : other.view;
return *this;
}
Text (const Text& other) : // copy construct
type{other.type},
data{std::move(other.data)},
view{data ? *data : other.view}
{}
Text& operator= (Text& other) { // copy assign
this->type = other.type;
this->data = std::move(other.data);
this->view = this->data ? *(this->data) : other.view;
return *this;
}
constexpr Text () : // empty
type{Type::LITERAL},
data{},
view{none}
{}
Text (std::string&& value) : // own
type{Type::LITERAL},
data{std::move(value)},
view{*data}
{}
constexpr Text (const char* const value) : // view
type{Type::LITERAL},
data{},
view{value}
{}
Text (const std::string& value) : // own
type{Type::LITERAL},
data{value},
view{*data}
{}
Text (const std::string_view value) : // view
type{Type::LITERAL},
data{},
view{value}
{}
Text (Placeholder&& tPlaceholder) : // own
type{Type::PLACEHOLDER},
data{std::move(tPlaceholder)},
view{*data}
{}
};
}}
////| |////
////| CSS |////
////| |////
namespace Webxx { namespace internal {
struct CssRule {
struct Data {
bool canNest;
const char* label;
Text value;
std::vector<Text> selectors;
std::vector<CssRule> children;
WEBXX_MOVE_ONLY_CONSTRUCTORS(Data)
Data (
bool tCanNest = false,
const char* tLabel = none,
Text&& tValue = none,
std::vector<Text>&& tSelectors = {},
std::vector<CssRule>&& tChildren = {}
) :
canNest{tCanNest},
label{tLabel},
value{std::move(tValue)},
selectors{std::move(tSelectors)},
children{std::move(tChildren)}
{}
};
mutable Data data;
CssRule (CssRule&&) = default; // move construct
CssRule& operator= (CssRule&&) = default; // move assign
CssRule (const CssRule& other) : data { // copy construct
std::move(other.data)
} {}
CssRule& operator= (CssRule& other) { // copy assign
this->data = std::move(other.data);
return *this;
}
CssRule (Text&& tSelector) : data {
true,
none,
{},
{std::move(tSelector)},
{},
} {}
CssRule (std::initializer_list<Text>&& tSelectors) : data {
true,
none,
{},
std::move(tSelectors),
{},
} {}
CssRule (Text&& tSelector, std::initializer_list<CssRule>&& tRules) : data {
true,
none,
{},
{std::move(tSelector)},
std::move(tRules),
} {}
CssRule (std::initializer_list<Text>&& tSelectors, std::initializer_list<CssRule>&& tRules) : data {
true,
none,
{},
std::move(tSelectors),
std::move(tRules),
} {}
template <class... T, class = CssRule>
CssRule (Text&& tSelector, T&& ...tRules) : data {
true,
none,
{},
{std::move(tSelector)},
{std::forward<T>(tRules)...},
} {}
template <class... T, class = CssRule>
CssRule (std::initializer_list<Text>&& tSelectors, T&& ...tRules) : data {
true,
none,
{},
std::move(tSelectors),
{std::forward<T>(tRules)...},
} {}
CssRule (
bool tCanNest = false,
const char* tLabel = none,
Text&& tValue = {},
std::vector<Text>&& tSelectors = {},
std::vector<CssRule>&& tChildren = {}
) : data {
tCanNest,
tLabel,
std::move(tValue),
std::move(tSelectors),
std::move(tChildren),
} {}
};
template<const char* const NAME>
struct CssProperty : CssRule {
CssProperty (Text&& tValue) :
CssRule(false, NAME, std::move(tValue), {}, {})
{}
};
template<const char* const PREFIX>
struct CssAtSingle : CssRule {
CssAtSingle (Text&& tSelector) :
CssRule(false, PREFIX, {}, {std::move(tSelector)}, {})
{}
CssAtSingle (std::initializer_list<Text>&& tSelectors) :
CssRule(false, PREFIX, {}, std::move(tSelectors), {})
{}
};
template<const char* const PREFIX>
struct CssAtNested : CssRule {
CssAtNested () :
CssRule(true, PREFIX, {}, {}, {})
{}
CssAtNested (Text&& tSelector) :
CssRule(true, PREFIX, {}, {std::move(tSelector)}, {})
{}
CssAtNested (std::initializer_list<Text>&& tSelectors) :
CssRule(true, PREFIX, {}, std::move(tSelectors), {})
{}
CssAtNested (Text&& tSelector, std::initializer_list<CssRule>&& tRules) :
CssRule(
true,
PREFIX,
{},
{std::move(tSelector)},
std::move(tRules)
)
{}
CssAtNested (std::initializer_list<Text>&& tSelectors, std::initializer_list<CssRule>&& tRules) :
CssRule(
true,
PREFIX,
{},
std::move(tSelectors),
std::move(tRules)
)
{}
template <class... T, class = CssRule>
CssAtNested (Text&& tSelector, T&& ...tRules) :
CssRule(
true,
PREFIX,
{},
{std::move(tSelector)},
{std::forward<T>(tRules)...}
)
{}
template <class... T, class = CssRule>
CssAtNested (std::initializer_list<Text>&& tSelectors, T&& ...tRules) :
CssRule(
true,
PREFIX,
{},
std::move(tSelectors),
{std::forward<T>(tRules)...}
)
{}
};
struct CssVar : CssRule {
CssVar (const char* tName, Text&& tValue) :
CssRule(false, tName, std::move(tValue), {}, {})
{}
};
namespace exports {
template<const char* NAME>
using property = CssProperty<NAME>;
using rule = CssRule;
using styles = std::initializer_list<CssRule>;
using prop = CssVar;
}
}}
////| |////
////| HTML |////
////| |////
namespace Webxx { namespace internal {
constexpr char doctype[] = "<!doctype html>";
constexpr char styleTag[] = "style";
typedef const char* HtmlAttributeName;
struct HtmlAttribute {
struct Data {
HtmlAttributeName name;
std::vector<Text> values;
WEBXX_MOVE_ONLY_CONSTRUCTORS(Data)
Data (
HtmlAttributeName tName = none,
std::vector<Text>&& tValues = {}
) :
name{tName},
values{std::move(tValues)}
{}
};
mutable Data data;
HtmlAttribute (HtmlAttribute&&) = default; // move construct
HtmlAttribute& operator= (HtmlAttribute&&) = default; // move assign
HtmlAttribute (const HtmlAttribute& other) : // copy construct
data {std::move(other.data)}
{}
HtmlAttribute& operator= (HtmlAttribute& other) { // copy assign
this->data = std::move(other.data);
return *this;
}
HtmlAttribute (
HtmlAttributeName tName = none,
std::vector<Text>&& tValues = {}
) : data {
tName,
std::move(tValues)
} {}
};
template<HtmlAttributeName NAME>
struct HtmlAttributeDefined : public HtmlAttribute {
HtmlAttributeDefined () :
HtmlAttribute(NAME, {})
{}
HtmlAttributeDefined (std::vector<Text>&& values) :
HtmlAttribute(NAME, std::move(values))
{}
HtmlAttributeDefined (std::initializer_list<Text>&& values) :
HtmlAttribute(NAME, std::move(values))
{}
template <class... T, class = Text>
HtmlAttributeDefined (T&& ...values) :
HtmlAttribute(NAME, {std::forward<T>(values)...})
{}
};
typedef const char* TagName;
typedef const char* Prefix;
typedef bool SelfClosing;
enum CollectionTarget {
NONE = 0,
CSS = 1,
SCRIPT = 2,
PLACEHOLDER = 3,
VARIABLE = 4,
HEAD = 5,
};
struct HtmlNodeOptions {
TagName tagName;
Prefix prefix;
SelfClosing selfClosing;
CollectionTarget gathersCollection;
CollectionTarget emitsCollection;
HtmlNodeOptions (HtmlNodeOptions&&) = default;
HtmlNodeOptions& operator= (HtmlNodeOptions&& other) = default;
HtmlNodeOptions (const HtmlNodeOptions&) = delete;
HtmlNodeOptions& operator= (const HtmlNodeOptions&) = delete;
HtmlNodeOptions () :
tagName{none},
prefix{none},
selfClosing{false},
gathersCollection{NONE},
emitsCollection{NONE}
{}
HtmlNodeOptions (
TagName tTagName,
Prefix tPrefix,
SelfClosing tSelfClosing,
CollectionTarget tGathersCollection,
CollectionTarget tEmitsCollection
) :
tagName{tTagName},
prefix{tPrefix},
selfClosing{tSelfClosing},
gathersCollection{tGathersCollection},
emitsCollection{tEmitsCollection}
{}
};
struct HtmlNode;
typedef std::string_view ComponentName;
typedef std::size_t ComponentTypeId;
typedef std::function<HtmlNode()> ContentProducer;
struct HtmlNode {
struct Data {
HtmlNodeOptions options;
std::vector<HtmlAttribute> attributes;
std::vector<HtmlNode> children;
Text content;
ContentProducer contentLazy;
std::vector<CssRule> css;
ComponentTypeId componentTypeId;
WEBXX_MOVE_ONLY_CONSTRUCTORS(Data)
Data (
HtmlNodeOptions&& tOptions = {none, none, false, NONE, NONE},
std::vector<HtmlAttribute>&& tAttributes = {},
std::vector<HtmlNode>&& tChildren = {},
Text&& tContent = {},
ContentProducer&& tContentLazy = {},
std::vector<CssRule>&& tCss = {},
ComponentTypeId tComponentTypeId = 0
) :
options{std::move(tOptions)},
attributes{std::move(tAttributes)},
children{std::move(tChildren)},
content{std::move(tContent)},
contentLazy{std::move(tContentLazy)},
css{std::move(tCss)},
componentTypeId{tComponentTypeId}
{}
};
mutable Data data;
HtmlNode (HtmlNode&&) = default; // move construct
HtmlNode& operator= (HtmlNode&&) = default; // move assign
HtmlNode (const HtmlNode& other) : data { // copy construct
std::move(other.data)
} {}
HtmlNode& operator= (HtmlNode& other) { // copy assign
this->data = std::move(other.data);
return *this;
}
HtmlNode (Placeholder&& tPlaceholder) : data {
{none, none, false, PLACEHOLDER, NONE},
{},
{},
std::move(tPlaceholder),
} {}
HtmlNode (ContentProducer&& tNodeProducer) : data {
{none, none, false, NONE, NONE},
{},
{},
{},
std::move(tNodeProducer),
} {}
HtmlNode (std::string&& tContent) : data {
{none, none, false, NONE, NONE},
{},
{},
std::move(tContent),
} {}
HtmlNode (const char* tContent) : data {
{none, none, false, NONE, NONE},
{},
{},
tContent,
} {}
HtmlNode (const std::string& tContent) : data {
{none, none, false, NONE, NONE},
{},
{},
tContent,
} {}
HtmlNode (const std::string_view tContent) : data {
{none, none, false, NONE, NONE},
{},
{},
std::move(tContent),
} {}
HtmlNode (
HtmlNodeOptions&& tOptions = {none, none, false, NONE, NONE},
std::vector<HtmlAttribute>&& tAttributes = {},
std::vector<HtmlNode>&& tChildren = {},
Text&& tContent = {},
ContentProducer&& tContentLazy = {},
std::vector<CssRule>&& tCss = {},
ComponentTypeId tComponentTypeId = 0
) : data {
std::move(tOptions),
std::move(tAttributes),
std::move(tChildren),
std::move(tContent),
std::move(tContentLazy),
std::move(tCss),
tComponentTypeId
} {}
};
template<
TagName TAG = none,
Prefix PREFIX = none,
SelfClosing SELF_CLOSING = false,
CollectionTarget COLLECTS = NONE,
CollectionTarget COLLECTION = NONE
>
struct HtmlNodeDefined : public HtmlNode {
HtmlNodeDefined () :
HtmlNode(
{TAG, PREFIX, SELF_CLOSING, COLLECTS, COLLECTION}
)
{}
HtmlNodeDefined (std::vector<HtmlNode>&& tChildren) :
HtmlNode(
{TAG, PREFIX, SELF_CLOSING, COLLECTS, COLLECTION},
{},
std::move(tChildren)
)
{}
HtmlNodeDefined (std::initializer_list<HtmlNode>&& tChildren) :
HtmlNode(
{TAG, PREFIX, SELF_CLOSING, COLLECTS, COLLECTION},
{},
std::move(tChildren)
)
{}
template <class... T, class = HtmlNode>
HtmlNodeDefined (std::initializer_list<HtmlAttribute>&& tAttributes, T&& ...tChildren) :
HtmlNode(
{TAG, PREFIX, SELF_CLOSING, COLLECTS, COLLECTION},
std::move(tAttributes),
{std::forward<T>(tChildren)...}
)
{}
template <class... T, class = HtmlNode>
HtmlNodeDefined (std::vector<HtmlAttribute>&& tAttributes, T&& ...tChildren) :
HtmlNode(
{TAG, PREFIX, SELF_CLOSING, COLLECTS, COLLECTION},
std::move(tAttributes),
{std::forward<T>(tChildren)...}
)
{}
};
struct HtmlStyleNode : HtmlNode {
HtmlStyleNode () :
HtmlNode({styleTag, none, false, NONE, NONE})
{}
HtmlStyleNode (std::initializer_list<CssRule>&& tCss) :
HtmlNode({styleTag, none, false, NONE, NONE}, {}, {}, {}, {}, std::move(tCss))
{}
template<typename ...T>
HtmlStyleNode (std::initializer_list<HtmlAttribute>&& tAttributes, T&& ...tCss) :
HtmlNode({styleTag, none, false, NONE, NONE, NONE}, std::move(tAttributes), {}, {std::forward<T>(tCss)...})
{}
// HtmlStyleNode (std::initializer_list<HtmlAttributeProxy>&& tAttrs, std::initializer_list<CssRuleProxy>&& tCss) :
};
struct HtmlStyleCollectionNode : HtmlNode {
HtmlStyleCollectionNode () :
HtmlNode({none, none, false, NONE, CSS})
{}
HtmlStyleCollectionNode (std::initializer_list<CssRule> &&tCss) :
HtmlNode({none, none, false, NONE, CSS}, {}, {}, {}, {}, std::move(tCss))
{}
};
typedef HtmlNodeDefined<none, none, false, NONE, HEAD> HtmlHeadCollectionNode;
namespace exports {
// HTML extensibility:
template<TagName TAG>
using el = HtmlNodeDefined<TAG>;
template<HtmlAttributeName NAME>
using attr = HtmlAttributeDefined<NAME>;
using node = HtmlNode;
using nodes = std::vector<HtmlNode>;
using children = std::initializer_list<HtmlNode>;
using attrs = std::initializer_list<HtmlAttribute>;
// HTML special purpose nodes:
using doc = HtmlNodeDefined<none, doctype>;
using text = HtmlNodeDefined<>;
using fragment = HtmlNodeDefined<>;
using lazy = ContentProducer;
using style = HtmlStyleNode;
using styleTarget = HtmlNodeDefined<styleTag, none, false, CSS, NONE>;
using headTarget = HtmlNodeDefined<none, none, false, HEAD, NONE>;
}
}}
////| |////
////| Component |////
////| |////
namespace Webxx { namespace internal {
struct ComponentBase : public HtmlNode {
ComponentBase (
const ComponentTypeId tTypeId,
HtmlStyleCollectionNode&& tCss,
HtmlNode&& tRoot,
HtmlHeadCollectionNode&& tHead
) : HtmlNode(
{none, none, false, NONE, NONE},
{},
{
std::move(tRoot),
std::move(tCss),
std::move(tHead),
},
{},
{},
{},
tTypeId
) {}
};
template <typename T>
constexpr size_t ctHash () {
size_t hash{0};
for (const char& c : WEBXX_FN_SIG) {
(hash ^= static_cast<size_t>(c)) <<= 1;
}
return hash;
}
template <typename T>
constexpr size_t compileTimeTypeId = ctHash<T>();
template <class T, size_t K = compileTimeTypeId<T>>
struct Component : public ComponentBase {
constexpr Component (
HtmlNode
&& tRootNode
) : ComponentBase(
K,
{},
std::move(tRootNode),
{}
) {}
constexpr Component (
HtmlStyleCollectionNode&& tCss,
HtmlNode&& tRootNode
) : ComponentBase(
K,
std::move(tCss),
std::move(tRootNode),
{}
) {}
constexpr Component (
HtmlStyleCollectionNode&& tCss,
HtmlNode&& tRootNode,
HtmlHeadCollectionNode&& tHeadNode
) : ComponentBase(
K,
std::move(tCss),
std::move(tRootNode),
std::move(tHeadNode)
) {}
};
namespace exports {
template <class T>
using component = Component<T>;
}
}}
////| |////
////| Rendering |////
////| |////
namespace Webxx { namespace internal {
constexpr char componentScopePrefix[] = "data-c";
struct CollectedCss {
const ComponentTypeId componentTypeId;
const std::vector<CssRule>& css;
bool operator == (const CollectedCss &other) const noexcept {
return (other.componentTypeId == componentTypeId);
}
};
struct CollectedHtml {
const ComponentTypeId componentTypeId;
const std::vector<HtmlNode>& nodes;
bool operator == (const CollectedHtml &other) const noexcept {
return (other.componentTypeId == componentTypeId);
}
};
}}
template<>
struct std::hash<Webxx::internal::CollectedCss> {
std::size_t operator() (const Webxx::internal::CollectedCss& collectedCss) const noexcept {
return collectedCss.componentTypeId;
}
};
template<>
struct std::hash<Webxx::internal::CollectedHtml> {
std::size_t operator() (const Webxx::internal::CollectedHtml& collectedHtml) const noexcept {
return collectedHtml.componentTypeId;
}
};
namespace Webxx { namespace internal {
typedef std::function<void(const std::string_view&, std::string&)> RenderReceiverFn;
constexpr std::size_t renderBufferDefaultSize{16 * 1024};
inline void renderToInternalBuffer (const std::string_view& data, std::string& buffer) {
buffer.append(data);
}
struct RenderOptions {
mutable PlaceholderPopulator placeholderPopulator{noopPopulator};
mutable RenderReceiverFn renderReceiverFn{renderToInternalBuffer};
mutable std::size_t renderBufferSize{renderBufferDefaultSize};
mutable std::string renderBuffer{};
RenderOptions()
{}
RenderOptions(PlaceholderPopulator tPlaceholderPopulator) :
placeholderPopulator{tPlaceholderPopulator}
{}
RenderOptions(PlaceholderPopulator tPlaceholderPopulator, RenderReceiverFn tReceiverFn) :
placeholderPopulator{tPlaceholderPopulator},
renderReceiverFn{tReceiverFn},
renderBufferSize(0)
{}
RenderOptions(PlaceholderPopulator tPlaceholderPopulator, RenderReceiverFn tReceiverFn, std::size_t tRenderBufferSize) :
placeholderPopulator{tPlaceholderPopulator},
renderReceiverFn(tReceiverFn),
renderBufferSize(tRenderBufferSize)
{}
};
typedef std::unordered_set<CollectedCss> CollectedCsses;
typedef std::unordered_set<CollectedHtml> CollectedHtmls;
struct Collector {
CollectedCsses csses;
CollectedHtmls heads;
RenderOptions options;
Collector(const RenderOptions& tOptions) :
csses{}, heads{}, options{tOptions} {};
void collect (HtmlNode* node, const ComponentTypeId currentComponent) {
ComponentTypeId nextComponent = currentComponent;
if (node->data.componentTypeId) {
nextComponent = node->data.componentTypeId;
}
if (node->data.options.emitsCollection == HEAD && !node->data.children.empty()) {
heads.insert({nextComponent, node->data.children});
}
if (node->data.options.emitsCollection == CSS && !node->data.css.empty()) {
csses.insert({nextComponent, node->data.css});
}
if (node->data.contentLazy) {
node->data.children.push_back(node->data.contentLazy());
}
this->collect(&(node->data.children), nextComponent);
}
template<class T>
void collect (std::vector<T>* tNodes, const ComponentTypeId currentComponent) {
for (auto &node : *tNodes) {
this->collect(&node, currentComponent);
}
}
void collect (const void*, const ComponentTypeId) {}
};
struct Renderer {
const Collector& collector;
const RenderOptions& options;
Renderer(const Collector& tCollector, const RenderOptions& tOptions) :
collector{tCollector}, options{tOptions}
{
options.renderBuffer.reserve(options.renderBufferSize);
if (!options.placeholderPopulator) {
options.placeholderPopulator = noopPopulator;
}
}
private:
inline void sendToRender (const std::string_view& rendered) {
options.renderReceiverFn(rendered, options.renderBuffer);
}
public:
const std::string componentName (ComponentTypeId type) {
return std::to_string(type);
}
void render (const HtmlAttribute& attribute, const ComponentTypeId) {
sendToRender(attribute.data.name);
if (!attribute.data.values.empty()) {
sendToRender("=\"");
bool shouldSeparate = false;
for(auto &value : attribute.data.values) {
if (shouldSeparate) {
sendToRender(" ");
}
switch (value.type) {
case Text::Type::LITERAL:
sendToRender(value.view);
break;
case Text::Type::PLACEHOLDER:
sendToRender(options.placeholderPopulator(value.view, attribute.data.name));
break;
}
shouldSeparate = true;
}
sendToRender("\"");
}
}
void render (const std::initializer_list<HtmlAttribute>& attributes, const ComponentTypeId currentComponent) {
for (auto &attribute : attributes) {
sendToRender(" ");
render(attribute, currentComponent);
}
}
void render (const std::vector<HtmlAttribute>& attributes, const ComponentTypeId currentComponent) {
for (auto &attribute : attributes) {
sendToRender(" ");
render(attribute, currentComponent);
}
}
void render (const HtmlNode& node, const ComponentTypeId currentComponent) {
ComponentTypeId nextComponent = currentComponent;
if (node.data.componentTypeId) {
nextComponent = node.data.componentTypeId;
}
if (node.data.options.emitsCollection != NONE) {
// Nodes belonging to a collection will be rendered where they are collected:
return;
}
if (strlen(node.data.options.prefix)) {
sendToRender(node.data.options.prefix);
}
if (strlen(node.data.options.tagName)) {
sendToRender("<");
sendToRender(node.data.options.tagName);
if (!node.data.attributes.empty()) {
render(node.data.attributes, nextComponent);
}
if (nextComponent) {
sendToRender(" ");
sendToRender(componentScopePrefix);
sendToRender(componentName(nextComponent));
}
if (node.data.options.selfClosing) {
sendToRender("/");
}
sendToRender(">");
}
if (node.data.options.gathersCollection == PLACEHOLDER) {
sendToRender(options.placeholderPopulator(node.data.content.view, node.data.options.tagName));
} else {
sendToRender(node.data.content.view);
}
if (!node.data.children.empty()) {
render(node.data.children, nextComponent);
}
if (!node.data.css.empty()) {
render(node.data.css, 0);
}
if (node.data.options.gathersCollection == CSS) {
render(collector.csses, nextComponent);
}
if (node.data.options.gathersCollection == HEAD) {
render(collector.heads, nextComponent);
}
if ((!node.data.options.selfClosing) && strlen(node.data.options.tagName)) {
sendToRender("</");