This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dhtmlparser.d
997 lines (845 loc) · 23.6 KB
/
dhtmlparser.d
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
/**
* D Module for parsing HTML in similar way like BeautifulSoup.
*
* Version: 1.5.2
* Date: 30.07.2013
*
* Authors:
* Bystroushaak (bystrousak@kitakitsune.org)
* Website:
* Github; https://github.com/Bystroushaak/DHTMLParser
* Copyright:
* This work is licensed under a CC BY (http://creativecommons.org/licenses/by/3.0/)
*/
module dhtmlparser;
import std.string;
import std.array;
import std.ascii;
import std.algorithm : remove;
import quote_escaper;
/**
* Container for parsed html elements.
*/
class HTMLElement{
private string element, tagname;
private bool istag, isendtag, iscomment, isnonpairtag;
/// Nested tags. Encapsulation would complicate whole class much more then public property.
public HTMLElement[] childs;
/// Tag parameters.
public string[string] params;
public HTMLElement endtag, openertag;
/// Useful as container for document (root of the DOM).
this(){
this("");
}
/**
* Standard constructor used when parsing document from string.
*/
this(string str){
// this code is ugly - crime of premature optimization :(
this.element = str;
this.parseIsTag();
this.parseIsComment();
if (!this.istag || this.iscomment)
this.tagname = element;
else
this.parseTagName();
if (this.isComment() || !this.isTag())
return;
this.parseIsEndTag();
this.parseIsNonPairTag();
if (this.istag && !this.isendtag && this.element.indexOf("=") > 0)
this.parseParams();
this.tagname = this.tagname.toLower();
}
/**
* Special constructor used when creating DOM.
*/
this(string tagname, string[string] params){
tagname = tagname.strip().replace(" ", "");
string nonpair = "";
if (tagname.startsWith("<"))
tagname = tagname[1 .. $];
if (tagname.endsWith("/>")){
tagname = tagname[0 .. $ - 2];
nonpair = " /";
}else if (tagname.endsWith(">"))
tagname = tagname[0 .. $ - 1];
// Convert into single string
string output = "<" ~ tagname;
foreach(key, val; params)
output ~= " " ~ key ~ "=\"" ~ quote_escaper.escape(val, '"') ~ "\"";
this(output ~ nonpair ~ ">");
this.tagname = this.tagname.toLower();
}
/**
* This constructor is used for creating DOM from elements.
*
* Example:
* -----
* HTMLElement e = new HTMLElement([
* new HTMLElement("<val>",[
* new HTMLElement("xe")
* ])
* ]);
*
* writeln(e);
* -----
* Writes;
* -----
* <val>
* xe
* </val>
* -----
*/
this(string tagname, string[string] params, HTMLElement childs[]){
this(tagname, params);
this.childs ~= HTMLElement.closeElements(childs);
}
/// Same as previous, but with less options.
this(string tagname, HTMLElement childs[]){
if (tagname.strip().length != 0){
// containers with childs are automatically considered as tags
if (!tagname.startsWith("<"))
tagname = "<" ~ tagname;
if (!tagname.endsWith(">"))
tagname ~= ">";
}
this(tagname);
this.childs ~= HTMLElement.closeElements(childs);
}
/// Same as previous, but with less options.
this(HTMLElement childs[]){
this();
this.childs ~= HTMLElement.closeElements(childs);
}
/* *************************************************************************
* Finders *****************************************************************
************************************************************************ */
/**
* Same as findAll, but returns tags without endtags. You can always get them
* from .endtag property..
*
* See_also:
* findAll
*/
public HTMLElement[] find(string tag_name, string[string] params = null, bool function(HTMLElement) fn = null){
HTMLElement[] output;
HTMLElement[] dom = this.findAll(tag_name, params, fn);
// remove endtags
foreach(e; dom)
if (!e.isEndTag())
output ~= e;
return output;
}
/**
* Same as findAllB, but returns tags without endtags. You can always get them
* from .endtag property..
*
* See_also:
* findAll
*/
public HTMLElement[] findB(string tag_name, string[string] params = null, bool function(HTMLElement) fn = null){
HTMLElement[] output;
HTMLElement[] dom = this.findAllB(tag_name, params, fn);
// remove endtags
foreach(e; dom)
if (!e.isEndTag())
output ~= e;
return output;
}
/**
* Simple search engine (depth-first algorithm - http://en.wikipedia.org/wiki/Depth-first_search).
*
* Finds elements and subelements which match patterns given by parameters.
* Allows searching defined by users lambda function.
*
* Params:
* tag_name = Name of searched element.
* params = Associative array containing searched parameters
* fn = User defined function. Function takes elements and returns true if wanted.
*
* Examples:
*
* ---
* import std.stdio;
*
* HTMLElement dom = parseString("<div id='xe' a='b'>obsah xe divu</div><div id='xu' a='b'>obsah xu divu</div>");
*
* writeln(dom);
*
* // writes:
* <div a="b" id="xe">
* obsah xe divu
* </div>
* <div a="b" id="xu">
* obsah xu divu
* </div>
* ---
* Search by parameters;
* ---
* writeln(dom.find("div", ["id":"xe"]))
*
* // writes:
* [<div a="b" id="xe">
* obsah xe divu
* </div>
* ]
* ---
* Search by lambda function;
* ---
* writeln(dom.find(null, null, function(HTMLElement e){return ("id" in e.params && e.params["id"] == "xu");}));
*
* // writes:
* [<div a="b" id="xu">
* obsah xu divu
* </div>
* ]
* ---
*
* Returns: Array of matching elements.
*/
public HTMLElement[] findAll(string tag_name, string[string] params = null, bool function(HTMLElement) fn = null){
HTMLElement[] output;
if (this.isAlmostEqual(tag_name, params, fn))
output ~= this;
HTMLElement tmp[];
foreach(el; this.childs){
tmp = el.findAll(tag_name, params, fn);
if (tmp.length > 0)
output ~= tmp;
}
return output;
}
/**
* Simple search engine using Breadth-first algorithm - http://en.wikipedia.org/wiki/Breadth-first_search
*
* Finds elements and subelements which match patterns given by parameters.
* Allows searching defined by users lambda function.
*
* Params:
* tag_name = Name of searched element.
* params = Associative array containing searched parameters
* fn = User defined function. Function takes elements and returns true if wanted.
*
* Returns: Array of matching elements.
*
* See_also:
* findAll
*/
public HTMLElement[] findAllB(string tag_name, string[string] params = null, bool function(HTMLElement) fn = null, bool _first = true){
HTMLElement[] output;
if (this.isAlmostEqual(tag_name, params, fn))
output ~= this;
HTMLElement[] breadth_search = this.childs;
for (size_t index; index < breadth_search.length; index += 1){
auto el = breadth_search[index];
if (el.isAlmostEqual(tag_name, params, fn))
output ~= el;
if (el.childs.length > 0)
breadth_search ~= el.childs;
}
return output;
}
/* *************************************************************************
* PARSERS *****************************************************************
************************************************************************ */
private void parseIsTag(){
if (this.element.startsWith("<") && this.element.endsWith(">"))
this.istag = true;
else
this.istag = false;
}
private void parseIsEndTag(){
char last;
this.isendtag = false;
if (this.element.startsWith("<") && this.element.endsWith(">")){
foreach(char c; this.element){
if (c == '/' && last == '<')
this.isendtag = true;
if (c > 32)
last = c;
}
}
}
private void parseIsNonPairTag(){
char last;
this.isnonpairtag = false;
// Tags endings with /> are nonpair
if (this.element.startsWith("<") && this.element.endsWith(">")){
foreach(char c; this.element){
if (c == '>' && last == '/'){
this.isnonpairtag = true;
return;
}
if (c > 32)
last = c;
}
}
// Nonpair tags
string[] npt = [
"br",
"hr",
"img",
"input",
"link",
"meta",
"spacer",
"frame",
"base"
];
// Check listed nonpair tags
foreach(string tagname; npt){
if (tagname.toLower() == this.tagname.toLower()){
this.isnonpairtag = true;
break;
}
}
}
private void parseIsComment(){
if (this.element.startsWith("<!--") && this.element.endsWith("-->"))
this.iscomment = true;
else
this.iscomment = false;
}
private void parseTagName(){
foreach(string el; this.element.split(" ")){
el = el.replace("/", "").replace("<", "").replace(">", "");
if (el.length > 0){
this.tagname = el;
return;
}
}
}
private void parseParams(){
if (this.element.indexOf(" ") <= 0 || this.element.indexOf("=") <= 0)
return;
// Remove '<' & '>'
string params = this.element[1 .. $-1].strip();
// Remove tagname
params = params[params.indexOf(this.getTagName()) + this.getTagName().length .. $].strip();
// Parser machine
ubyte next_state = 0;
string key, value;
char end_quote = 0;
char buff[2] = [' ', ' '];
foreach(char c; params){
switch(next_state){
case 0: // key
if (!isWhite(c)) // skip whitespaces
if (c == '=')
next_state = 1;
else
key ~= c;
break;
case 1: // value decisioner
if (!isWhite(c))
if (c == '\'' || c == '"'){
next_state = 3;
end_quote = c;
}else{
next_state = 2;
value ~= c;
}
break;
case 2: // one word parameter without quotes
if (isWhite(c)){
next_state = 0;
this.params[key.toLower()] = value;
key = "";
value = "";
}else
value ~= c;
break;
case 3: // quoted string
if (c == end_quote && (buff[0] != '\\' || (buff[0]) == '\\' && buff[1] == '\\')){
next_state = 0;
this.params[key.toLower()] = quote_escaper.unescape(value, end_quote);
key = "";
value = "";
end_quote = 0;
}else
value ~= c;
break;
default: // every switch have to have default :S
break;
}
rotate_buff(buff);
buff[0] = c;
}
if (key != ""){
if (end_quote != 0 && value.strip() != "")
this.params[key.toLower()] = quote_escaper.unescape(value, end_quote);
else
this.params[key.toLower()] = value;
}
}
//* /Parsers ***************************************************************
/* *************************************************************************
* Getters *****************************************************************
************************************************************************ */
/**
* True if element is tag (not content).
*/
public bool isTag(){
return this.istag;
}
/**
* True if HTMLElement is end tag (/tag).
*/
public bool isEndTag(){
return this.isendtag;
}
/**
* True if HTMLElement is nonpair tag (br for example).
*/
public bool isNonPairTag(){
return this.isnonpairtag;
}
/**
* True if HTMLElement is html comment.
*/
public bool isComment(){
return this.iscomment;
}
/**
* True if is opening tag.
*/
public bool isOpeningTag(){
if (this.isTag() && !this.isComment() && !this.isEndTag() && !this.isNonPairTag())
return true;
else
return false;
}
/**
* Returns true, if this element is endtag to opener.
*/
public bool isEndTagTo(HTMLElement opener){
if (this.isendtag && opener.isOpeningTag())
if (this.tagname.toLower() == opener.getTagName().toLower())
return true;
else
return false;
else
return false;
}
/**
* Returns tag (with parameters), without content or endtag.
*/
public string tagToString(){
if (! this.isOpeningTag())
return this.element;
else{
string output = "<" ~ this.tagname;
foreach(key, val; this.params)
output ~= " " ~ key ~ "=\"" ~ quote_escaper.escape(val, '"') ~ "\"";
return output ~ ">";
}
}
/**
* Returns tag name.
*/
public string getTagName(){
return this.tagname;
}
/**
* Returns content of tag (everything between opener and endtag).
*/
public string getContent(){
string output;
foreach(c; this.childs)
if (! c.isEndTag())
output ~= c.prettify();
// remove \n from end, prettify is nice, but sometimes you need just value
if (output.endsWith("\n"))
output = output[0 .. $ - 1];
return output;
}
/**
* Returns prettified tag with content.
*
* See_also: toString()
*/
public string prettify(uint depth = 0, string separator = " ", bool last = true, bool pre = false, bool inline = false){
string output;
if (this.getTagName() != "" && this.tagToString().strip() == "")
return "";
// if not inside <pre> and not inline, shift tag to the right
if (!pre && !inline)
for (int i = 0; i < depth; i++)
output ~= separator;
// for <pre> set 'pre' flag
if (this.getTagName().toLower() == "pre" && this.isOpeningTag()){
pre = true;
separator = "";
}
output ~= this.tagToString();
// detect if inline
bool is_inline = inline; // is_inline shows if inline was set by detection, or as parameter
foreach(c; this.childs)
if (!(c.isTag() || c.isComment()))
if (c.tagToString().strip().length != 0)
inline = true;
// don't shift if inside container (containers have blank tagname)
uint original_depth = depth;
if (this.getTagName() != "")
if (!pre && !inline){ // inside <pre> doesn't shift tags
depth++;
if (this.tagToString().strip() != "")
output ~= "\n";
}
// prettify childs
foreach(e; this.childs){
if (!e.isEndTag())
output ~= e.prettify(depth, separator, false, pre, inline);
}
// endtag
if (this.endtag !is null){
if (!pre && !inline)
for (int i = 0; i < original_depth; i++)
output ~= separator;
output ~= this.endtag.tagToString().strip();
if (!is_inline)
output ~= "\n";
}
return output;
}
//* /Getters ***************************************************************
/* *************************************************************************
* Operators ***************************************************************
**************************************************************************/
/**
* Returns original string, which was parsed to DOM.
*
* If you want prettified string, try .prettify()
*
* See_also: prettify()
*/
public override string toString(){
string output;
if (! this.childs.empty){
output ~= this.element;
foreach(c; this.childs)
output ~= c.toString();
if (this.endtag !is null)
output ~= this.endtag.tagToString();
}else if (!this.isEndTag())
output ~= this.tagToString();
return output;
}
/**
* Compare element with given tagname, params and/or by lambda function.
*
* Lambda function is same as in .find().
*/
public bool isAlmostEqual(string tag_name, string[string] params = null, bool function(HTMLElement) fn = null){
// search by lambda function
if (fn != null)
if (fn(this))
return true;
// compare tagname
if (this.tagname == tag_name && this.tagname != "" && this.tagname != null){
// compare parameters
if (params == null || params.length == 0)
return true;
else if (this.params.length > 0){
foreach(key, val; params){
if (key !in this.params)
return false;
else if (params[key] != this.params[key])
return false;
}
return true;
}
}
return false;
}
//* /Operators *************************************************************
/* *************************************************************************
* Setters *****************************************************************
************************************************************************ */
public void isNonPairTag(bool isnonpairtag){
this.isnonpairtag = isnonpairtag;
if (!isnonpairtag){
this.endtag = null;
this.childs = null;
}
}
/**
* Replace element.
*
* Useful when you don't want change manually all references to object.
*/
public void replaceWith(HTMLElement el){
this.childs = el.childs;
this.params = el.params;
this.endtag = el.endtag;
this.openertag = el.openertag;
this.tagname = el.getTagName();
this.element = el.tagToString();
this.istag = el.isTag();
this.isendtag = el.isEndTag();
this.iscomment = el.isComment();
this.isnonpairtag = el.isNonPairTag();
}
/**
* Remove subelement (child) specified by reference.
*
* This can't be used for removing subelements by value! If you want do such
* thing, do:
*
* ----------------
* foreach(e; dom.find("value"))
* dom.removeChild(e);
* ----------------
*
* Params:
* child = child which will be removed from dom (compared by reference)
* end_tag_too = remove end tag too - default true
*/
void removeChild(HTMLElement child, bool end_tag_too = true){
if (this.childs.length <= 0)
return;
HTMLElement end_tag;
if (end_tag_too)
end_tag = child.endtag;
foreach(int i, HTMLElement e; this.childs){
if (e is child)
this.childs = this.childs.remove(i);
else if (end_tag_too && e is end_tag && end_tag !is null)
this.childs = this.childs.remove(i);
else
e.removeChild(child, end_tag_too);
}
}
//* /Setters ***************************************************************
/* Static methods *********************************************************/
// Close tags - used in some constructors
private static HTMLElement[] closeElements(HTMLElement childs[]){
HTMLElement o[];
// Close all unclosed pair tags
foreach(e; childs){
if (e.isTag()){
if (!e.isNonPairTag() && !e.isEndTag() && !e.isComment() && e.endtag is null){
e.childs = closeElements(e.childs);
o ~= e;
o ~= new HTMLElement("</" ~ e.getTagName() ~ ">");
// Join opener and endtag
e.endtag = o[$ - 1];
o[$ - 1].openertag = e;
}else
o ~= e;
}else
o ~= e;
}
return o;
}
//* /Static methods ********************************************************
}
private void rotate_buff(T)(T[] buff){
for(int i = cast(int) buff.length - 1; i > 0; i--)
buff[i] = buff[i - 1];
}
/**
* Parse HTML from text into array filled with tags end text.
*
* Source code is little bit unintutive, because it is simple parser machine.
* For better understanding, look at; http://kitakitsune.org/images/field_parser.png
*/
private string[] rawSplit(string itxt){
char echr;
char[4] buff;
string content;
string[] array;
ubyte next_state = 0;
bool inside_tag = false;
foreach(char c; itxt){
switch(next_state){
case 0: // content
if (c == '<'){
if (content.length > 0)
array ~= content;
content = "" ~ c;
next_state = 1;
inside_tag = false;
}else
content ~= c;
break;
case 1: // html tag
if (c == '>'){
array ~= content ~ c;
content = "";
next_state = 0;
}else if (c == '\'' || c == '"'){
echr = c;
content ~= c;
next_state = 2;
}else if (c == '-' && buff[0] == '-' && buff[1] == '!' && buff[2] == '<'){
if (content[0 .. ($ - 3)].length > 0)
array ~= content[0 .. ($ - 3)];
content = content[($ - 3) .. $] ~ c;
next_state = 3;
}else{
if (c == '<') // jump back into tag instead of content
inside_tag = true;
content ~= c;
}
break;
case 2: // "" / ''
if (c == echr && (buff[0] != '\\' || (buff[0] == '\\' && buff[1] == '\\')))
next_state = 1;
content ~= c;
break;
case 3: // html comments
if (c == '>' && buff[0] == '-' && buff[1] == '-'){
if (inside_tag)
next_state = 1;
else
next_state = 0;
inside_tag = false;
array ~= content ~ c;
content = "";
}else
content ~= c;
break;
default: // switch without default is deprecated :S
break;
}
// rotate buffer
rotate_buff(buff);
buff[0] = c;
}
if (content.length > 0)
array ~= content;
return array;
}
/**
* Repair tags with comments (<HT<!-- asad -->ML> is parsed to ["<HT", "<!-- asad -->", "ML>"]
* and I need ["<HTML>", "<!-- asad -->"])
*/
private HTMLElement[] repairTags(HTMLElement[] raw_input){
HTMLElement[] ostack;
uint index;
foreach(HTMLElement el; raw_input){
if (el.isComment()){
if (index > 0 && index < raw_input.length){
if (raw_input[index - 1].tagToString().startsWith("<") && raw_input[index + 1].tagToString().endsWith(">")){
ostack[$ - 1] = new HTMLElement(ostack[$ - 1].tagToString() ~ raw_input[index + 1].tagToString());
ostack ~= el;
index++;
continue;
}
}
}
ostack ~= el;
}
return ostack;
}
/**
* Go through istack and search endtag. Element at first index is considered as opening tag.
*
* Returns: index of end tag or 0 if not found.
*/
private uint indexOfEndTag(HTMLElement[] istack){
if (istack.length <= 0)
return 0;
if (!istack[0].isOpeningTag())
return 0;
HTMLElement opener = istack[0];
uint cnt = 0;
foreach(uint index, HTMLElement el; istack[1 .. $]){
if (el.isOpeningTag() && (el.getTagName().toLower() == opener.getTagName().toLower()))
cnt++;
else if (el.isEndTagTo(opener))
if (cnt == 0)
return index + 1;
else
cnt--;
}
return 0;
}
/**
* Recursively go through element array and create DOM.
*/
private HTMLElement[] parseDOM(HTMLElement[] istack){
uint end_tag_offset;
HTMLElement[] ostack;
for (uint index; index < istack.length;) {
HTMLElement el = istack[index];
end_tag_offset = indexOfEndTag(istack[index .. $]); // Check if this is pair tag
if (!el.isNonPairTag && end_tag_offset == 0 && !el.isEndTag())
el.isNonPairTag = true;
if (end_tag_offset != 0){
el.childs = parseDOM(istack[index + 1 .. end_tag_offset + index]);
el.endtag = istack[end_tag_offset + index]; // Reference to endtag
el.endtag.openertag = el; // This is endtags openertag
ostack ~= el;
ostack ~= el.endtag;
index = end_tag_offset + index;
} else {
if (!el.isEndTag()) {
ostack ~= el;
}
index += 1;
}
}
return ostack;
}
/**
* Parse given string and return DOM from HTMLElements.
*
* See_also: HTMLElement
*/
public static HTMLElement parseString(string txt){
HTMLElement[] istack;
// remove UTF BOM (prettify fails if not)
if (txt.length > 3 && txt.startsWith("\xef\xbb\xbf")) // utf8
txt = txt[3 .. $];
// Convert array of strings to HTMLElements
foreach(string el; rawSplit(txt))
istack ~= new HTMLElement(el);
HTMLElement container = new HTMLElement("");
container.childs ~= parseDOM(repairTags(istack));
return container;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
unittest{
HTMLElement dom = parseString(
"<div Id='xe' a='b'>obsah xe divu</div> <!-- Id, not id :) -->
<div id='xu' a='b'>obsah xu divu</div>"
);
HTMLElement divXe, divXu;
// find test
divXe = dom.find("div", ["id":"xe"])[0];
divXu = dom.find("div", ["id":"xu"])[0];
assert(divXe.tagToString() == `<div a="b" id="xe">`);
assert(divXu.tagToString() == `<div a="b" id="xu">`);
// unit test for toString (must returns original string)
assert(divXe.toString() == `<div Id='xe' a='b'>obsah xe divu</div>`);
assert(divXu.toString() == `<div id='xu' a='b'>obsah xu divu</div>`);
// getTagName() test
assert(divXe.getTagName() == "div");
assert(divXe.getTagName() == divXu.getTagName());
// isComment() test
assert(divXe.isComment() == false);
assert(divXe.isComment() == divXu.isComment());
assert(divXe.isNonPairTag() != divXe.isOpeningTag());
assert(divXe.isTag() == true);
assert(divXe.isTag() == divXu.isTag());
assert(divXe.getContent() == "obsah xe divu");
// find()/findB() test
dom = parseString(`
<div id=first>
First div.
<div id=first.subdiv>
Subdiv in first div.
</div>
</div>
<div id=second>
Second.
</div>
`);
// find/findB unittest
assert(dom.find("div")[1].getContent().strip() == "Subdiv in first div.");
assert(dom.findB("div")[1].getContent().strip() == "Second.");
}