-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomTree.h
More file actions
962 lines (884 loc) · 22.5 KB
/
DomTree.h
File metadata and controls
962 lines (884 loc) · 22.5 KB
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
// Copyright (C) 2025, Flaviu Marc.
//
// Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#pragma once
#include <array>
#include <stack>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <string_view>
namespace domtree
{
constexpr std::string_view whitespace{ " \n\r\t" };
constexpr std::array<std::string_view, 16> self_closing_tags
{
"area",
"base",
"br",
"col",
"command",
"embed",
"hr",
"img",
"input",
"keygen",
"link",
"meta",
"param",
"source",
"track",
"wbr"
};
constexpr std::array<std::string_view, 1> non_valid_tags
{
"align"
};
struct Attribute
{
std::string m_key{};
std::string m_value{};
char m_quote{ '\"' };
};
struct Tag
{
public:
Tag() = default;
Tag(const std::string& name)
: m_name(name)
{
}
Tag(const std::string& name, const std::string& value)
: m_name(name)
, m_value(value)
{
}
Tag(const std::string& name, const std::string& value, const std::vector<Attribute>& attributes)
: m_name(name)
, m_value(value)
, m_attributes(attributes)
{
}
Tag(const std::string& name, const std::vector<Attribute>& attributes)
: m_name(name)
, m_attributes(attributes)
{
}
Tag(std::string&& name)
: m_name(std::move(name))
{
}
Tag(std::string&& name, std::string&& value)
: m_name(std::move(name))
, m_value(std::move(value))
{
}
Tag(std::string&& name, std::string&& value, std::vector<Attribute>&& attributes)
: m_name(std::move(name))
, m_value(std::move(value))
, m_attributes(std::move(attributes))
{
}
Tag(std::string&& name, std::vector<Attribute>&& attributes)
: m_name(std::move(name))
, m_attributes(std::move(attributes))
{
}
Tag(const Tag& rhs)
: m_parent(rhs.m_parent)
, m_name(rhs.m_name)
, m_value(rhs.m_value)
, m_childs(rhs.m_childs)
, m_attributes(rhs.m_attributes)
{
}
Tag& operator=(const Tag& rhs)
{
if (this != &rhs)
{
m_parent = rhs.m_parent;
m_name = rhs.m_name;
m_value = rhs.m_value;
m_childs = rhs.m_childs;
m_attributes = rhs.m_attributes;
}
return *this;
}
Tag(Tag&& rhs) noexcept
: m_parent(std::move(rhs.m_parent))
, m_name(std::move(rhs.m_name))
, m_value(std::move(rhs.m_value))
, m_childs(std::move(rhs.m_childs))
, m_attributes(std::move(rhs.m_attributes))
{
rhs.m_parent = nullptr;
}
Tag& operator=(Tag&& rhs) noexcept
{
if (this != &rhs)
{
m_parent = std::move(rhs.m_parent);
m_name = std::move(rhs.m_name);
m_value = std::move(rhs.m_value);
m_childs = std::move(rhs.m_childs);
m_attributes = std::move(rhs.m_attributes);
rhs.m_parent = nullptr;
}
return *this;
}
~Tag() = default;
public:
std::string m_name{};
std::string m_value{};
std::vector<Attribute> m_attributes{};
std::vector<std::shared_ptr<Tag>> m_childs{};
Tag* m_parent{};
public:
void AddAttributes(const std::vector<Attribute>& attributes)
{
std::copy(std::begin(attributes), std::end(attributes), std::back_inserter(m_attributes));
}
void AddAttributes(std::vector<Attribute>&& attributes)
{
if (m_attributes.empty())
{
m_attributes = std::move(attributes);
}
else
{
m_attributes.reserve(m_attributes.size() + attributes.size());
std::move(std::begin(attributes), std::end(attributes), std::back_inserter(m_attributes));
attributes.clear();
}
}
void SetValue(const std::string& text)
{
m_value = text;
}
void SetValue(std::string&& text)
{
m_value = std::move(text);
}
void AddText(const std::string& text)
{
Tag tag{};
tag.m_value = text;
tag.m_parent = this;
m_childs.push_back(std::make_shared<Tag>(std::move(tag)));
}
void AddText(std::string&& text)
{
Tag tag{};
tag.m_value = std::move(text);
tag.m_parent = this;
m_childs.push_back(std::make_shared<Tag>(std::move(tag)));
}
void AddChild(Tag& tag)
{
tag.m_parent = this;
m_childs.push_back(std::make_shared<Tag>(tag));
}
void AddChild(Tag&& tag)
{
tag.m_parent = this;
m_childs.push_back(std::make_shared<Tag>(std::move(tag)));
}
};
class CDomTree
{
public:
CDomTree() = default;
CDomTree(const CDomTree& rhs)
: m_currentTag(rhs.m_currentTag)
, m_data(rhs.m_data)
, m_tags(rhs.m_tags)
, m_tables(rhs.m_tables)
, m_bufferIndex(rhs.m_bufferIndex)
, m_svg(rhs.m_svg)
, m_style(rhs.m_style)
, m_script(rhs.m_script)
, m_td(rhs.m_td)
, m_tr(rhs.m_tr)
, m_table(rhs.m_table)
, m_p(rhs.m_p)
, m_a(rhs.m_a)
, m_dl(rhs.m_dl)
, m_dt(rhs.m_dt)
, m_label(rhs.m_label)
{
}
CDomTree& operator=(const CDomTree& rhs)
{
if (this != &rhs)
{
m_currentTag = rhs.m_currentTag;
m_data = rhs.m_data;
m_tags = rhs.m_tags;
m_tables = rhs.m_tables;
m_bufferIndex = rhs.m_bufferIndex;
m_svg = rhs.m_svg;
m_style = rhs.m_style;
m_script = rhs.m_script;
m_td = rhs.m_td;
m_tr = rhs.m_tr;
m_table = rhs.m_table;
m_p = rhs.m_p;
m_a = rhs.m_a;
m_dl = rhs.m_dl;
m_dt = rhs.m_dt;
m_label = rhs.m_label;
}
return *this;
}
CDomTree(CDomTree&& rhs) noexcept
: m_currentTag(std::move(rhs.m_currentTag))
, m_data(std::move(rhs.m_data))
, m_tags(std::move(rhs.m_tags))
, m_tables(std::move(rhs.m_tables))
, m_bufferIndex(std::move(rhs.m_bufferIndex))
, m_svg(std::move(rhs.m_svg))
, m_style(std::move(rhs.m_style))
, m_script(std::move(rhs.m_script))
, m_td(std::move(rhs.m_td))
, m_tr(std::move(rhs.m_tr))
, m_table(std::move(rhs.m_table))
, m_p(std::move(rhs.m_p))
, m_a(std::move(rhs.m_a))
, m_dl(std::move(rhs.m_dl))
, m_dt(std::move(rhs.m_dt))
, m_label(std::move(rhs.m_label))
{
rhs.m_currentTag = nullptr;
rhs.m_bufferIndex = 0;
rhs.m_svg = false;
rhs.m_style = false;
rhs.m_script = false;
rhs.m_td = TagState::closed;
rhs.m_tr = TagState::closed;
rhs.m_table = TagState::closed;
rhs.m_p = TagState::closed;
rhs.m_a = TagState::closed;
rhs.m_dl = TagState::closed;
rhs.m_dt = TagState::closed;
rhs.m_label = TagState::closed;
}
CDomTree& operator=(CDomTree&& rhs) noexcept
{
if (this != &rhs)
{
m_currentTag = std::move(rhs.m_currentTag);
m_data = std::move(rhs.m_data);
m_tags = std::move(rhs.m_tags);
m_tables = std::move(rhs.m_tables);
m_bufferIndex = std::move(rhs.m_bufferIndex);
m_svg = std::move(rhs.m_svg);
m_style = std::move(rhs.m_style);
m_script = std::move(rhs.m_script);
m_td = std::move(rhs.m_td);
m_tr = std::move(rhs.m_tr);
m_table = std::move(rhs.m_table);
m_p = std::move(rhs.m_p);
m_a = std::move(rhs.m_a);
m_dl = std::move(rhs.m_dl);
m_dt = std::move(rhs.m_dt);
m_label = std::move(rhs.m_label);
rhs.m_currentTag = nullptr;
rhs.m_bufferIndex = 0;
rhs.m_svg = false;
rhs.m_style = false;
rhs.m_script = false;
rhs.m_td = TagState::closed;
rhs.m_tr = TagState::closed;
rhs.m_table = TagState::closed;
rhs.m_p = TagState::closed;
rhs.m_a = TagState::closed;
rhs.m_dl = TagState::closed;
rhs.m_dt = TagState::closed;
rhs.m_label = TagState::closed;
}
return *this;
}
~CDomTree() = default;
public:
std::vector<std::shared_ptr<Tag>>& GetTags() { return m_tags; }
const std::vector<std::shared_ptr<Tag>>& GetTags() const { return m_tags; }
void Parse(const std::string& data)
{
m_data = data;
Parse();
}
void Parse(std::string&& data)
{
m_data = std::move(data);
Parse();
}
std::string GetData() const
{
std::string out{};
PrintData(m_tags, out);
return out + "\n";
}
private:
void Parse()
{
m_bufferIndex = 0;
while (m_bufferIndex < m_data.length())
{
if (!ParseNextToken())
break;
}
}
bool ParseNextToken()
{
if (m_bufferIndex >= m_data.length())
return false;
if (!m_currentTag || !m_script || !m_style || !m_svg)
SkipWhiteSpaces();
if (m_svg)
return ParseValue();
if ('<' == m_data[m_bufferIndex])
return ParseTag();
else
return ParseValue();
}
bool ParseTag()
{
if (m_bufferIndex >= m_data.length())
return false;
m_bufferIndex++;
if (m_bufferIndex >= m_data.length())
return false;
switch (m_data[m_bufferIndex])
{
case '/':
m_bufferIndex++;
ParseClosingTag();
break;
case '!':
case '?':
if ('!' == m_data[m_bufferIndex]
&& m_bufferIndex < m_data.length() - 3
&& '-' == m_data[m_bufferIndex + 1]
&& '-' == m_data[m_bufferIndex + 2])
ParseCommentTag();
else
ParseSpecialTag();
break;
default:
if (!ParseOpeningTag())
return false;
}
return true;
}
bool ParseValue()
{
if (!m_currentTag)
return false;
std::string value{};
do
{
if (!m_script && !m_style && !m_svg)
{
while (m_bufferIndex < m_data.length() - 1 && '<' != m_data[m_bufferIndex])
value += m_data[m_bufferIndex++];
break;
}
if (m_script)
{
while (m_bufferIndex < m_data.length() - 5
&& !('<' == m_data[m_bufferIndex]
&& '/' == m_data[m_bufferIndex + 1]
&& 's' == std::tolower(m_data[m_bufferIndex + 2])
&& 'c' == std::tolower(m_data[m_bufferIndex + 3])
&& 'r' == std::tolower(m_data[m_bufferIndex + 4])))
value += m_data[m_bufferIndex++];
m_script = false;
break;
}
if (m_style)
{
while (m_bufferIndex < m_data.length() - 5
&& !('<' == m_data[m_bufferIndex]
&& '/' == m_data[m_bufferIndex + 1]
&& 's' == std::tolower(m_data[m_bufferIndex + 2])
&& 't' == std::tolower(m_data[m_bufferIndex + 3])
&& 'y' == std::tolower(m_data[m_bufferIndex + 4])))
value += m_data[m_bufferIndex++];
m_style = false;
break;
}
if (m_svg)
{
while (m_bufferIndex < m_data.length() - 5
&& !('<' == m_data[m_bufferIndex]
&& '/' == m_data[m_bufferIndex + 1]
&& 's' == std::tolower(m_data[m_bufferIndex + 2])
&& 'v' == std::tolower(m_data[m_bufferIndex + 3])
&& 'g' == std::tolower(m_data[m_bufferIndex + 4])))
value += m_data[m_bufferIndex++];
m_svg = false;
}
} while (false);
Tag tag{};
tag.m_parent = m_currentTag;
tag.m_value = value;
m_currentTag->m_childs.push_back(std::make_shared<Tag>(std::move(tag)));
return true;
}
bool ParseSpecialTag()
{
Tag tag{};
while (m_bufferIndex < m_data.length() && '>' != m_data[m_bufferIndex])
tag.m_name += m_data[m_bufferIndex++];
if (m_tags.empty() || !m_currentTag)
{
m_tags.push_back(std::make_shared<Tag>(std::move(tag)));
}
else
{
tag.m_parent = m_currentTag;
tag.m_parent->m_childs.push_back(std::make_shared<Tag>(std::move(tag)));
}
if ('>' == m_data[m_bufferIndex] || m_bufferIndex >= m_data.length())
m_bufferIndex++;
return true;
}
bool ParseCommentTag()
{
Tag tag{};
while (m_bufferIndex < m_data.length() - 3 &&
!('>' == m_data[m_bufferIndex]
&& '-' == m_data[m_bufferIndex - 1]
&& '-' == m_data[m_bufferIndex - 2]))
tag.m_name += m_data[m_bufferIndex++];
if (m_tags.empty() || !m_currentTag)
{
m_tags.push_back(std::make_shared<Tag>(std::move(tag)));
}
else
{
tag.m_parent = m_currentTag;
tag.m_parent->m_childs.push_back(std::make_shared<Tag>(std::move(tag)));
}
if ('>' == m_data[m_bufferIndex] || m_bufferIndex >= m_data.length())
m_bufferIndex++;
return true;
}
bool ParseOpeningTag()
{
SkipWhiteSpaces();
std::shared_ptr<Tag> tag = std::make_shared<Tag>();
// parse tag name
while (m_bufferIndex < m_data.length()
&& !IsWhiteSpace(m_data[m_bufferIndex])
&& '>' != m_data[m_bufferIndex]
&& '/' != m_data[m_bufferIndex])
tag->m_name += std::tolower(m_data[m_bufferIndex++]);
if (std::binary_search(non_valid_tags.cbegin(), non_valid_tags.cend(), tag->m_name))
{
SkipCurrentTag();
return true;
}
const bool isSelfClosingTag = (std::binary_search(self_closing_tags.cbegin(), self_closing_tags.cend(), tag->m_name));
if (m_tags.empty() || !m_currentTag)
{
m_tags.push_back(tag);
m_currentTag = m_tags.back().get();
}
else
{
if (!isSelfClosingTag && IsWatched(tag->m_name))
PerformCorrectnessOnOpen(tag->m_name);
if (m_currentTag)
{
tag->m_parent = m_currentTag; // put m_currentTag to local tag as parent
tag->m_parent->m_childs.push_back(tag); // add to local tag parent childs the actual local tag
m_currentTag = tag->m_parent->m_childs.back().get(); // setup m_currentTag as local tag
SetupMultiLineTags();
}
}
if (m_currentTag)
{
ParseAttributes();
if (isSelfClosingTag)
{
m_currentTag = m_currentTag->m_parent;
}
else if (IsWatched(m_currentTag->m_name))
{
UpdateWatched(m_currentTag->m_name, TagState::opened);
}
}
if ('>' == m_data[m_bufferIndex] || m_bufferIndex >= m_data.length())
m_bufferIndex++;
return true;
}
void ParseClosingTag()
{
SkipWhiteSpaces();
std::string tagName{};
while (m_bufferIndex < m_data.length() && !IsWhiteSpace(m_data[m_bufferIndex]) && '>' != m_data[m_bufferIndex])
tagName += std::tolower(m_data[m_bufferIndex++]);
if ('>' == m_data[m_bufferIndex] || m_data[m_bufferIndex] >= m_data.length())
m_bufferIndex++;
if (binary_search(non_valid_tags.cbegin(), non_valid_tags.cend(), tagName))
return;
if (m_currentTag)
{
bool valid_close{ true };
if (IsWatched(tagName))
{
UpdateWatched(tagName, TagState::closed);
PerformCorrectnessOnClose(tagName);
valid_close = CloseParagraphes(tagName);
}
if (m_currentTag && valid_close)
m_currentTag = m_currentTag->m_parent;
if ("table" == tagName && !m_tables.empty())
RestoreCurrentTable();
}
}
// corrected tags: tr, td, dt
void PerformCorrectnessOnOpen(const std::string& tagName)
{
if ("td" == tagName)
{
if (TagState::opened == m_td && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_td = TagState::closed;
}
return;
}
if ("tr" == tagName)
{
if (TagState::opened == m_td && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_td = TagState::closed;
}
if (TagState::opened == m_tr && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_tr = TagState::closed;
}
return;
}
if ("table" == tagName && TagState::opened == m_td && m_currentTag)
{
m_tables.push({ m_table, m_tr, m_td });
m_tr = m_td = TagState::closed;
m_table = TagState::opened;
return;
}
if ("dt" == tagName && TagState::opened == m_dt && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_dt = TagState::closed;
return;
}
}
// corrected tags: table, dt
void PerformCorrectnessOnClose(const std::string& tagName)
{
if ("table" == tagName)
{
if (TagState::opened == m_td && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_td = TagState::closed;
}
if (TagState::opened == m_tr && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_tr = TagState::closed;
}
return;
}
if ("dl" == tagName)
{
if (TagState::opened == m_dt && m_currentTag)
{
m_currentTag = m_currentTag->m_parent;
m_dt = TagState::closed;
}
}
}
void ParseAttributes()
{
char quote{ '\"' };
while (m_bufferIndex < m_data.length() && '>' != m_data[m_bufferIndex])
{
SkipWhiteSpaces();
if (m_bufferIndex < m_data.length() && '>' != m_data[m_bufferIndex] && '/' != m_data[m_bufferIndex])
{
std::string key{}, value{};
while (m_bufferIndex < m_data.length()
&& '=' != m_data[m_bufferIndex]
&& '>' != m_data[m_bufferIndex]
&& !IsWhiteSpace(m_data[m_bufferIndex]))
key += m_data[m_bufferIndex++];
SkipWhiteSpaces();
if (m_bufferIndex < m_data.length() && '=' == m_data[m_bufferIndex])
{
m_bufferIndex++;
SkipWhiteSpaces();
if (m_bufferIndex < m_data.length() && ('\"' == m_data[m_bufferIndex] || '\'' == m_data[m_bufferIndex]))
{
quote = m_data[m_bufferIndex++];
while (m_bufferIndex < m_data.length() && quote != m_data[m_bufferIndex])
value += m_data[m_bufferIndex++];
}
}
else
{
m_bufferIndex--;
}
m_currentTag->m_attributes.push_back(Attribute{ key, value, quote });
}
if ('>' != m_data[m_bufferIndex])
m_bufferIndex++;
}
}
void SkipWhiteSpaces()
{
while (m_bufferIndex < m_data.length() && IsWhiteSpace(m_data[m_bufferIndex]))
m_bufferIndex++;
}
constexpr bool IsWhiteSpace(const char c) const
{
return (' ' == c || '\r' == c || '\n' == c || '\t' == c);
}
std::string GetIndent(size_t tabs) const
{
std::ostringstream buffer{};
while (tabs--)
buffer << "\t";
return buffer.str();
}
void PrintData(const std::vector<std::shared_ptr<Tag>>& tags, std::string& data, const size_t level = 0) const
{
for (const auto& it : tags)
{
if (it->m_name.empty())
continue;
if ('!' != it->m_name.front() && '?' != it->m_name.front())
{
PrintName(*it, data, level);
PrintValue(*it, data, level);
PrintClose(*it, data, level);
}
else
{
if (!data.empty())
data += "\n";
data += GetIndent(level) + "<" + it->m_name + ">";
}
}
}
void PrintName(const Tag& tag, std::string& data, const size_t level) const
{
if (!data.empty())
data += "\n";
data += GetIndent(level) + "<" + tag.m_name;
for (const auto& attr : tag.m_attributes)
data += " " + attr.m_key + "=" + attr.m_quote + attr.m_value + attr.m_quote;
if (std::binary_search(self_closing_tags.cbegin(), self_closing_tags.cend(), tag.m_name))
data += "/";
data += ">";
}
void PrintValue(const Tag& tag, std::string& data, const size_t level) const
{
if (0 == tag.m_childs.size())
{
data += tag.m_value.substr(0, tag.m_value.find_last_not_of(whitespace) + 1);
return;
}
for (auto&& it : tag.m_childs)
{
if (it->m_name.empty()) // is value
{
if (1 == tag.m_childs.size() &&
tag.m_childs.front()->m_name.empty() &&
"script" != tag.m_name &&
"svg" != tag.m_name &&
"style" != tag.m_name)
data += it->m_value.substr(0, it->m_value.find_last_not_of(whitespace) + 1);
else
data += "\n" + GetIndent(level + 1) + it->m_value.substr(0, it->m_value.find_last_not_of(whitespace) + 1);
}
else
{
PrintData({ it }, data, level + 1);
}
}
}
void PrintClose(const Tag& tag, std::string& data, const size_t level) const
{
if (!std::binary_search(self_closing_tags.cbegin(), self_closing_tags.cend(), tag.m_name))
{
if (tag.m_childs.size() > 1 || (0 != tag.m_childs.size() && !tag.m_childs.front()->m_name.empty()) ||
"script" == tag.m_name || "svg" == tag.m_name || "style" == tag.m_name)
data += "\n" + GetIndent(level);
data += "</" + tag.m_name + ">";
}
}
private:
enum class TagState
{
closed = 0,
opened
};
private:
void SetupMultiLineTags()
{
m_script = m_style = m_svg = false;
if ("script" == m_currentTag->m_name)
{
m_script = true;
return;
}
if ("style" == m_currentTag->m_name)
{
m_style = true;
return;
}
if ("svg" == m_currentTag->m_name)
{
m_svg = true;
}
}
// watched tags: p, a, table, tr, td
bool IsWatched(const std::string& tag) const
{
if ("p" == tag || "a" == tag || "td" == tag || "tr" == tag || "table" == tag || "label" == tag ||
"dl" == tag || "dt" == tag)
return true;
return false;
}
// update tags: table, tr, td, dl, dt
// tags updated on open only: p, a
void UpdateWatched(const std::string& tagName, TagState state)
{
if ("p" == tagName && TagState::opened == state) // update the p tag at openning only
{
m_p = state;
return;
}
if ("a" == tagName && TagState::opened == state) // update the a tag at openning only
{
m_a = state;
return;
}
if ("td" == tagName)
{
m_td = state;
return;
}
if ("tr" == tagName)
{
m_tr = state;
return;
}
if ("table" == tagName)
{
m_table = state;
return;
}
if ("label" == tagName && TagState::opened == state)
{
m_label = state;
return;
}
if ("dl" == tagName)
{
m_dl = state;
return;
}
if ("dt" == tagName)
{
m_dt = state;
return;
}
}
// return false if the current closing tag should remain at the same level
bool CloseParagraphes(const std::string& tagName)
{
if ("p" == tagName)
{
if (TagState::opened == m_p)
m_p = TagState::closed;
else
return false;
}
if ("a" == tagName)
{
if (TagState::opened == m_a)
m_a = TagState::closed;
else
return false;
}
if ("label" == tagName)
{
if (TagState::opened == m_label)
m_label = TagState::closed;
else
return false;
}
return true;
}
// restore the current table state
void RestoreCurrentTable()
{
m_table = m_tables.top().m_table;
m_tr = m_tables.top().m_tr;
m_td = m_tables.top().m_td;
m_tables.pop();
}
// skip the current tag
void SkipCurrentTag() { while (m_bufferIndex < m_data.length() && '>' != m_data[m_bufferIndex])
m_bufferIndex++;
if ('>' == m_data[m_bufferIndex] || m_bufferIndex >= m_data.length())
m_bufferIndex++;
}
private:
struct TableState
{
TagState m_table{ TagState::closed };
TagState m_tr{ TagState::closed };
TagState m_td{ TagState::closed };
};
private:
std::string m_data{};
std::stack<TableState> m_tables;
std::vector<std::shared_ptr<Tag>> m_tags{};
size_t m_bufferIndex{};
Tag* m_currentTag{};
// tags for correctness
private:
TagState m_td{ TagState::closed };
TagState m_tr{ TagState::closed };
TagState m_table{ TagState::closed };
TagState m_p{ TagState::closed }; // paragraph tag
TagState m_a{ TagState::closed };
TagState m_label{ TagState::closed };
TagState m_dl{ TagState::closed };
TagState m_dt{ TagState::closed };
// multilines tags
private:
bool m_svg{ false };
bool m_style{ false };
bool m_script{ false };
};
}