/
dom.t
3103 lines (2960 loc) · 142 KB
/
dom.t
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
use Mojo::Base -strict;
use Test::More;
use Mojo::DOM;
use Mojo::DOM::HTML qw(tag_to_html);
subtest 'Empty' => sub {
is(Mojo::DOM->new, '', 'right result');
is(Mojo::DOM->new(''), '', 'right result');
is(Mojo::DOM->new->parse(''), '', 'right result');
is(Mojo::DOM->new->at('p'), undef, 'no result');
is(Mojo::DOM->new->append_content(''), '', 'right result');
is(Mojo::DOM->new->all_text, '', 'right result');
};
subtest 'Simple (basics)' => sub {
my $dom = Mojo::DOM->new('<div><div FOO="0" id="a">A</div><div id="b">B</div></div>');
is $dom->at('#b')->text, 'B', 'right text';
my @div;
push @div, $dom->find('div[id]')->map('text')->each;
is_deeply \@div, [qw(A B)], 'found all div elements with id';
@div = ();
$dom->find('div[id]')->each(sub { push @div, $_->text });
is_deeply \@div, [qw(A B)], 'found all div elements with id';
is $dom->at('#a')->attr('foo'), 0, 'right attribute';
is $dom->at('#a')->attr->{foo}, 0, 'right attribute';
is "$dom", '<div><div foo="0" id="a">A</div><div id="b">B</div></div>', 'right result';
};
subtest 'Tap into method chain' => sub {
my $dom = Mojo::DOM->new->parse('<div id="a">A</div><div id="b">B</div>');
is_deeply [$dom->find('[id]')->map(attr => 'id')->each], [qw(a b)], 'right result';
is $dom->tap(sub { $_->at('#b')->remove }), '<div id="a">A</div>', 'right result';
};
subtest 'Build tree from scratch' => sub {
is(Mojo::DOM->new->append_content('<p>')->at('p')->append_content('0')->text, '0', 'right text');
};
subtest 'Simple nesting with healing (tree structure)' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<foo><bar a="b<c">ju<baz a23>s<bazz />t</bar>works</foo>
EOF
is $dom->tree->[0], 'root', 'right type';
is $dom->tree->[1][0], 'tag', 'right type';
is $dom->tree->[1][1], 'foo', 'right tag';
is_deeply $dom->tree->[1][2], {}, 'empty attributes';
is $dom->tree->[1][3], $dom->tree, 'right parent';
is $dom->tree->[1][4][0], 'tag', 'right type';
is $dom->tree->[1][4][1], 'bar', 'right tag';
is_deeply $dom->tree->[1][4][2], {a => 'b<c'}, 'right attributes';
is $dom->tree->[1][4][3], $dom->tree->[1], 'right parent';
is $dom->tree->[1][4][4][0], 'text', 'right type';
is $dom->tree->[1][4][4][1], 'ju', 'right text';
is $dom->tree->[1][4][4][2], $dom->tree->[1][4], 'right parent';
is $dom->tree->[1][4][5][0], 'tag', 'right type';
is $dom->tree->[1][4][5][1], 'baz', 'right tag';
is_deeply $dom->tree->[1][4][5][2], {a23 => undef}, 'right attributes';
is $dom->tree->[1][4][5][3], $dom->tree->[1][4], 'right parent';
is $dom->tree->[1][4][5][4][0], 'text', 'right type';
is $dom->tree->[1][4][5][4][1], 's', 'right text';
is $dom->tree->[1][4][5][4][2], $dom->tree->[1][4][5], 'right parent';
is $dom->tree->[1][4][5][5][0], 'tag', 'right type';
is $dom->tree->[1][4][5][5][1], 'bazz', 'right tag';
is_deeply $dom->tree->[1][4][5][5][2], {}, 'empty attributes';
is $dom->tree->[1][4][5][5][3], $dom->tree->[1][4][5], 'right parent';
is $dom->tree->[1][4][5][6][0], 'text', 'right type';
is $dom->tree->[1][4][5][6][1], 't', 'right text';
is $dom->tree->[1][4][5][6][2], $dom->tree->[1][4][5], 'right parent';
is $dom->tree->[1][5][0], 'text', 'right type';
is $dom->tree->[1][5][1], 'works', 'right text';
is $dom->tree->[1][5][2], $dom->tree->[1], 'right parent';
is "$dom", <<EOF, 'right result';
<foo><bar a="b<c">ju<baz a23>s<bazz></bazz>t</baz></bar>works</foo>
EOF
};
subtest 'Select based on parent' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<body>
<div>test1</div>
<div><div>test2</div></div>
<body>
EOF
is $dom->find('body > div')->[0]->text, 'test1', 'right text';
is $dom->find('body > div')->[1]->text, '', 'no content';
is $dom->find('body > div')->[2], undef, 'no result';
is $dom->find('body > div')->size, 2, 'right number of elements';
is $dom->find('body > div > div')->[0]->text, 'test2', 'right text';
is $dom->find('body > div > div')->[1], undef, 'no result';
is $dom->find('body > div > div')->size, 1, 'right number of elements';
};
subtest 'A bit of everything (basic navigation)' => sub {
my $dom = Mojo::DOM->new->parse(<<EOF);
<!doctype foo>
<foo bar="ba<z">
test
<simple class="working">easy</simple>
<test foo="bar" id="test" />
<!-- lala -->
works well
<![CDATA[ yada yada]]>
<?boom lalalala ?>
<a little bit broken>
< very broken
<br />
more text
</foo>
EOF
ok !$dom->xml, 'XML mode not detected';
is $dom->tag, undef, 'no tag';
is $dom->attr('foo'), undef, 'no attribute';
is $dom->attr(foo => 'bar')->attr('foo'), undef, 'no attribute';
is $dom->tree->[1][0], 'doctype', 'right type';
is $dom->tree->[1][1], ' foo', 'right doctype';
is "$dom", <<EOF, 'right result';
<!DOCTYPE foo>
<foo bar="ba<z">
test
<simple class="working">easy</simple>
<test foo="bar" id="test"></test>
<!-- lala -->
works well
<![CDATA[ yada yada]]>
<?boom lalalala ?>
<a bit broken little>
< very broken
<br>
more text
</a></foo>
EOF
my $simple = $dom->at('foo simple.working[class^="wor"]');
is $simple->parent->all_text,
"\n test\n easy\n \n \n works well\n yada yada\n" . " \n \n < very broken\n \n more text\n",
'right text';
is $simple->tag, 'simple', 'right tag';
is $simple->attr('class'), 'working', 'right class attribute';
is $simple->text, 'easy', 'right text';
is $simple->parent->tag, 'foo', 'right parent tag';
is $simple->parent->attr->{bar}, 'ba<z', 'right parent attribute';
is $simple->parent->children->[1]->tag, 'test', 'right sibling';
is $simple->to_string, '<simple class="working">easy</simple>', 'stringified right';
$simple->parent->attr(bar => 'baz')->attr({this => 'works', too => 'yea'});
is $simple->parent->attr('bar'), 'baz', 'right parent attribute';
is $simple->parent->attr('this'), 'works', 'right parent attribute';
is $simple->parent->attr('too'), 'yea', 'right parent attribute';
is $dom->at('test#test')->tag, 'test', 'right tag';
is $dom->at('[class$="ing"]')->tag, 'simple', 'right tag';
is $dom->at('[class="working"]')->tag, 'simple', 'right tag';
is $dom->at('[class$=ing]')->tag, 'simple', 'right tag';
is $dom->at('[class=working][class]')->tag, 'simple', 'right tag';
is $dom->at('foo > simple')->next->tag, 'test', 'right tag';
is $dom->at('foo > simple')->next->next->tag, 'a', 'right tag';
is $dom->at('foo > test')->previous->tag, 'simple', 'right tag';
is $dom->next, undef, 'no siblings';
is $dom->previous, undef, 'no siblings';
is $dom->at('foo > a')->next, undef, 'no next sibling';
is $dom->at('foo > simple')->previous, undef, 'no previous sibling';
is_deeply [$dom->at('simple')->ancestors->map('tag')->each], ['foo'], 'right results';
ok !$dom->at('simple')->ancestors->first->xml, 'XML mode not active';
};
subtest 'Nodes' => sub {
my $dom = Mojo::DOM->new('<!DOCTYPE before><p>test<![CDATA[123]]><!-- 456 --></p><?after?>');
is $dom->at('p')->preceding_nodes->first->content, ' before', 'right content';
is $dom->at('p')->preceding_nodes->size, 1, 'right number of nodes';
is $dom->at('p')->child_nodes->last->preceding_nodes->first->content, 'test', 'right content';
is $dom->at('p')->child_nodes->last->preceding_nodes->last->content, '123', 'right content';
is $dom->at('p')->child_nodes->last->preceding_nodes->size, 2, 'right number of nodes';
is $dom->preceding_nodes->size, 0, 'no preceding nodes';
is $dom->at('p')->following_nodes->first->content, 'after', 'right content';
is $dom->at('p')->following_nodes->size, 1, 'right number of nodes';
is $dom->child_nodes->first->following_nodes->first->tag, 'p', 'right tag';
is $dom->child_nodes->first->following_nodes->last->content, 'after', 'right content';
is $dom->child_nodes->first->following_nodes->size, 2, 'right number of nodes';
is $dom->following_nodes->size, 0, 'no following nodes';
is $dom->at('p')->previous_node->content, ' before', 'right content';
is $dom->at('p')->previous_node->previous_node, undef, 'no more siblings';
is $dom->at('p')->next_node->content, 'after', 'right content';
is $dom->at('p')->next_node->next_node, undef, 'no more siblings';
is $dom->at('p')->child_nodes->last->previous_node->previous_node->content, 'test', 'right content';
is $dom->at('p')->child_nodes->first->next_node->next_node->content, ' 456 ', 'right content';
is $dom->descendant_nodes->[0]->type, 'doctype', 'right type';
is $dom->descendant_nodes->[0]->content, ' before', 'right content';
is $dom->descendant_nodes->[0], '<!DOCTYPE before>', 'right content';
is $dom->descendant_nodes->[1]->tag, 'p', 'right tag';
is $dom->descendant_nodes->[2]->type, 'text', 'right type';
is $dom->descendant_nodes->[2]->content, 'test', 'right content';
is $dom->descendant_nodes->[5]->type, 'pi', 'right type';
is $dom->descendant_nodes->[5]->content, 'after', 'right content';
is $dom->at('p')->descendant_nodes->[0]->type, 'text', 'right type';
is $dom->at('p')->descendant_nodes->[0]->content, 'test', 'right type';
is $dom->at('p')->descendant_nodes->last->type, 'comment', 'right type';
is $dom->at('p')->descendant_nodes->last->content, ' 456 ', 'right type';
is $dom->child_nodes->[1]->child_nodes->first->parent->tag, 'p', 'right tag';
is $dom->child_nodes->[1]->child_nodes->first->content, 'test', 'right content';
is $dom->child_nodes->[1]->child_nodes->first, 'test', 'right content';
is $dom->at('p')->child_nodes->first->type, 'text', 'right type';
is $dom->at('p')->child_nodes->first->remove->tag, 'p', 'right tag';
is $dom->at('p')->child_nodes->first->type, 'cdata', 'right type';
is $dom->at('p')->child_nodes->first->content, '123', 'right content';
is $dom->at('p')->child_nodes->[1]->type, 'comment', 'right type';
is $dom->at('p')->child_nodes->[1]->content, ' 456 ', 'right content';
is $dom->[0]->type, 'doctype', 'right type';
is $dom->[0]->content, ' before', 'right content';
is $dom->child_nodes->[2]->type, 'pi', 'right type';
is $dom->child_nodes->[2]->content, 'after', 'right content';
is $dom->child_nodes->first->content(' again')->content, ' again', 'right content';
is $dom->child_nodes->grep(sub { $_->type eq 'pi' })->map('remove')->first->type, 'root', 'right type';
is "$dom", '<!DOCTYPE again><p><![CDATA[123]]><!-- 456 --></p>', 'right result';
};
subtest 'Modify nodes' => sub {
my $dom = Mojo::DOM->new('<script>la<la>la</script>');
is $dom->at('script')->type, 'tag', 'right type';
is $dom->at('script')->[0]->type, 'raw', 'right type';
is $dom->at('script')->[0]->content, 'la<la>la', 'right content';
is "$dom", '<script>la<la>la</script>', 'right result';
is $dom->at('script')->child_nodes->first->replace('a<b>c</b>1<b>d</b>')->tag, 'script', 'right tag';
is "$dom", '<script>a<b>c</b>1<b>d</b></script>', 'right result';
is $dom->at('b')->child_nodes->first->append('e')->content, 'c', 'right content';
is $dom->at('b')->child_nodes->first->prepend('f')->type, 'text', 'right type';
is "$dom", '<script>a<b>fce</b>1<b>d</b></script>', 'right result';
is $dom->at('script')->child_nodes->first->following->first->tag, 'b', 'right tag';
is $dom->at('script')->child_nodes->first->next->content, 'fce', 'right content';
is $dom->at('script')->child_nodes->first->previous, undef, 'no siblings';
is $dom->at('script')->child_nodes->[2]->previous->content, 'fce', 'right content';
is $dom->at('b')->child_nodes->[1]->next, undef, 'no siblings';
is $dom->at('script')->child_nodes->first->wrap('<i>:)</i>')->root, '<script><i>:)a</i><b>fce</b>1<b>d</b></script>',
'right result';
is $dom->at('i')->child_nodes->first->wrap_content('<b></b>')->root,
'<script><i>:)a</i><b>fce</b>1<b>d</b></script>', 'no changes';
is $dom->at('i')->child_nodes->first->wrap('<b></b>')->root, '<script><i><b>:)</b>a</i><b>fce</b>1<b>d</b></script>',
'right result';
is $dom->at('b')->child_nodes->first->ancestors->map('tag')->join(','), 'b,i,script', 'right result';
is $dom->at('b')->child_nodes->first->append_content('g')->content, ':)g', 'right content';
is $dom->at('b')->child_nodes->first->prepend_content('h')->content, 'h:)g', 'right content';
is "$dom", '<script><i><b>h:)g</b>a</i><b>fce</b>1<b>d</b></script>', 'right result';
is $dom->at('script > b:last-of-type')->append('<!--y-->')->following_nodes->first->content, 'y', 'right content';
is $dom->at('i')->prepend('z')->preceding_nodes->first->content, 'z', 'right content';
is $dom->at('i')->following->last->text, 'd', 'right text';
is $dom->at('i')->following->size, 2, 'right number of following elements';
is $dom->at('i')->following('b:last-of-type')->first->text, 'd', 'right text';
is $dom->at('i')->following('b:last-of-type')->size, 1, 'right number of following elements';
is $dom->following->size, 0, 'no following elements';
is $dom->at('script > b:last-of-type')->preceding->first->tag, 'i', 'right tag';
is $dom->at('script > b:last-of-type')->preceding->size, 2, 'right number of preceding elements';
is $dom->at('script > b:last-of-type')->preceding('b')->first->tag, 'b', 'right tag';
is $dom->at('script > b:last-of-type')->preceding('b')->size, 1, 'right number of preceding elements';
is $dom->preceding->size, 0, 'no preceding elements';
is "$dom", '<script>z<i><b>h:)g</b>a</i><b>fce</b>1<b>d</b><!--y--></script>', 'right result';
};
subtest 'XML nodes' => sub {
my $dom = Mojo::DOM->new->xml(1)->parse('<b>test<image /></b>');
ok $dom->at('b')->child_nodes->first->xml, 'XML mode active';
ok $dom->at('b')->child_nodes->first->replace('<br>')->child_nodes->first->xml, 'XML mode active';
is "$dom", '<b><br /><image /></b>', 'right result';
};
subtest 'Treating nodes as elements' => sub {
my $dom = Mojo::DOM->new('foo<b>bar</b>baz');
is $dom->child_nodes->first->child_nodes->size, 0, 'no nodes';
is $dom->child_nodes->first->descendant_nodes->size, 0, 'no nodes';
is $dom->child_nodes->first->children->size, 0, 'no children';
is $dom->child_nodes->first->strip->parent, 'foo<b>bar</b>baz', 'no changes';
is $dom->child_nodes->first->at('b'), undef, 'no result';
is $dom->child_nodes->first->find('*')->size, 0, 'no results';
ok !$dom->child_nodes->first->matches('*'), 'no match';
is_deeply $dom->child_nodes->first->attr, {}, 'no attributes';
is $dom->child_nodes->first->namespace, undef, 'no namespace';
is $dom->child_nodes->first->tag, undef, 'no tag';
is $dom->child_nodes->first->text, '', 'no text';
is $dom->child_nodes->first->all_text, '', 'no text';
};
subtest 'Class and ID' => sub {
my $dom = Mojo::DOM->new('<div id="id" class="class">a</div>');
is $dom->at('div#id.class')->text, 'a', 'right text';
};
subtest 'Deep nesting (parent combinator)' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<html>
<head>
<title>Foo</title>
</head>
<body>
<div id="container">
<div id="header">
<div id="logo">Hello World</div>
<div id="buttons">
<p id="foo">Foo</p>
</div>
</div>
<form>
<div id="buttons">
<p id="bar">Bar</p>
</div>
</form>
<div id="content">More stuff</div>
</div>
</body>
</html>
EOF
my $p = $dom->find('body > #container > div p[id]');
is $p->[0]->attr('id'), 'foo', 'right id attribute';
is $p->[1], undef, 'no second result';
is $p->size, 1, 'right number of elements';
my @p;
my @div;
$dom->find('div')->each(sub { push @div, $_->attr('id') });
$dom->find('p')->each(sub { push @p, $_->attr('id') });
is_deeply \@p, [qw(foo bar)], 'found all p elements';
my $ids = [qw(container header logo buttons buttons content)];
is_deeply \@div, $ids, 'found all div elements';
is_deeply [$dom->at('p')->ancestors->map('tag')->each], [qw(div div div body html)], 'right results';
is_deeply [$dom->at('html')->ancestors->each], [], 'no results';
is_deeply [$dom->ancestors->each], [], 'no results';
};
subtest 'Script tag' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<script charset="utf-8">alert('lalala');</script>
EOF
is $dom->at('script')->text, "alert('lalala');", 'right script content';
};
subtest 'HTML5 (unquoted values)' => sub {
my $dom = Mojo::DOM->new('<div id = test foo ="bar" class=tset bar=/baz/ value baz=//>works</div>');
is $dom->at('#test')->text, 'works', 'right text';
is $dom->at('div')->text, 'works', 'right text';
is $dom->at('[foo=bar][foo="bar"]')->text, 'works', 'right text';
is $dom->at('[foo="ba"]'), undef, 'no result';
is $dom->at('[foo=bar]')->text, 'works', 'right text';
is $dom->at('[foo=ba]'), undef, 'no result';
is $dom->at('.tset')->text, 'works', 'right text';
is $dom->at('[bar=/baz/]')->text, 'works', 'right text';
is $dom->at('[baz=//]')->text, 'works', 'right text';
is $dom->at('[value]')->text, 'works', 'right text';
is $dom->at('[value=baz]'), undef, 'no result';
};
subtest 'HTML1 (single quotes, uppercase tags and whitespace in attributes)' => sub {
my $dom = Mojo::DOM->new(q{<DIV id = 'test' foo ='bar' class= "tset">works</DIV>});
is $dom->at('#test')->text, 'works', 'right text';
is $dom->at('div')->text, 'works', 'right text';
is $dom->at('[foo="bar"]')->text, 'works', 'right text';
is $dom->at('[foo="ba"]'), undef, 'no result';
is $dom->at('[foo=bar]')->text, 'works', 'right text';
is $dom->at('[foo=ba]'), undef, 'no result';
is $dom->at('.tset')->text, 'works', 'right text';
};
subtest 'Already decoded Unicode snowman and quotes in selector' => sub {
my $dom = Mojo::DOM->new('<div id="snow'm"an">☃</div>');
is $dom->at('[id="snow\'m\"an"]')->text, '☃', 'right text';
is $dom->at('[id="snow\'m\22 an"]')->text, '☃', 'right text';
is $dom->at('[id="snow\'m\000022an"]')->text, '☃', 'right text';
is $dom->at('[id="snow\'m\22an"]'), undef, 'no result';
is $dom->at('[id="snow\'m\21 an"]'), undef, 'no result';
is $dom->at('[id="snow\'m\000021an"]'), undef, 'no result';
is $dom->at('[id="snow\'m\000021 an"]'), undef, 'no result';
is $dom->at("[id='snow\\'m\"an']")->text, '☃', 'right text';
is $dom->at("[id='snow\\27m\"an']")->text, '☃', 'right text';
};
subtest 'Unicode and escaped selectors' => sub {
my $html = '<html><div id="☃x">Snowman</div><div class="x ♥">Heart</div></html>';
my $dom = Mojo::DOM->new($html);
is $dom->at("#\\\n\\002603x")->text, 'Snowman', 'right text';
is $dom->at('#\\2603 x')->text, 'Snowman', 'right text';
is $dom->at("#\\\n\\2603 x")->text, 'Snowman', 'right text';
is $dom->at(qq{[id="\\\n\\2603 x"]})->text, 'Snowman', 'right text';
is $dom->at(qq{[id="\\\n\\002603x"]})->text, 'Snowman', 'right text';
is $dom->at(qq{[id="\\\\2603 x"]})->text, 'Snowman', 'right text';
is $dom->at("html #\\\n\\002603x")->text, 'Snowman', 'right text';
is $dom->at('html #\\2603 x')->text, 'Snowman', 'right text';
is $dom->at("html #\\\n\\2603 x")->text, 'Snowman', 'right text';
is $dom->at(qq{html [id="\\\n\\2603 x"]})->text, 'Snowman', 'right text';
is $dom->at(qq{html [id="\\\n\\002603x"]})->text, 'Snowman', 'right text';
is $dom->at(qq{html [id="\\\\2603 x"]})->text, 'Snowman', 'right text';
is $dom->at('#☃x')->text, 'Snowman', 'right text';
is $dom->at('div#☃x')->text, 'Snowman', 'right text';
is $dom->at('html div#☃x')->text, 'Snowman', 'right text';
is $dom->at('[id^="☃"]')->text, 'Snowman', 'right text';
is $dom->at('div[id^="☃"]')->text, 'Snowman', 'right text';
is $dom->at('html div[id^="☃"]')->text, 'Snowman', 'right text';
is $dom->at('html > div[id^="☃"]')->text, 'Snowman', 'right text';
is $dom->at('[id^=☃]')->text, 'Snowman', 'right text';
is $dom->at('div[id^=☃]')->text, 'Snowman', 'right text';
is $dom->at('html div[id^=☃]')->text, 'Snowman', 'right text';
is $dom->at('html > div[id^=☃]')->text, 'Snowman', 'right text';
is $dom->at(".\\\n\\002665")->text, 'Heart', 'right text';
is $dom->at('.\\2665')->text, 'Heart', 'right text';
is $dom->at("html .\\\n\\002665")->text, 'Heart', 'right text';
is $dom->at('html .\\2665')->text, 'Heart', 'right text';
is $dom->at(qq{html [class\$="\\\n\\002665"]})->text, 'Heart', 'right text';
is $dom->at(qq{html [class\$="\\2665"]})->text, 'Heart', 'right text';
is $dom->at(qq{[class\$="\\\n\\002665"]})->text, 'Heart', 'right text';
is $dom->at(qq{[class\$="\\2665"]})->text, 'Heart', 'right text';
is $dom->at('.x')->text, 'Heart', 'right text';
is $dom->at('html .x')->text, 'Heart', 'right text';
is $dom->at('.♥')->text, 'Heart', 'right text';
is $dom->at('html .♥')->text, 'Heart', 'right text';
is $dom->at('div.♥')->text, 'Heart', 'right text';
is $dom->at('html div.♥')->text, 'Heart', 'right text';
is $dom->at('[class$="♥"]')->text, 'Heart', 'right text';
is $dom->at('div[class$="♥"]')->text, 'Heart', 'right text';
is $dom->at('html div[class$="♥"]')->text, 'Heart', 'right text';
is $dom->at('html > div[class$="♥"]')->text, 'Heart', 'right text';
is $dom->at('[class$=♥]')->text, 'Heart', 'right text';
is $dom->at('div[class$=♥]')->text, 'Heart', 'right text';
is $dom->at('html div[class$=♥]')->text, 'Heart', 'right text';
is $dom->at('html > div[class$=♥]')->text, 'Heart', 'right text';
is $dom->at('[class~="♥"]')->text, 'Heart', 'right text';
is $dom->at('div[class~="♥"]')->text, 'Heart', 'right text';
is $dom->at('html div[class~="♥"]')->text, 'Heart', 'right text';
is $dom->at('html > div[class~="♥"]')->text, 'Heart', 'right text';
is $dom->at('[class~=♥]')->text, 'Heart', 'right text';
is $dom->at('div[class~=♥]')->text, 'Heart', 'right text';
is $dom->at('html div[class~=♥]')->text, 'Heart', 'right text';
is $dom->at('html > div[class~=♥]')->text, 'Heart', 'right text';
is $dom->at('[class~="x"]')->text, 'Heart', 'right text';
is $dom->at('div[class~="x"]')->text, 'Heart', 'right text';
is $dom->at('html div[class~="x"]')->text, 'Heart', 'right text';
is $dom->at('html > div[class~="x"]')->text, 'Heart', 'right text';
is $dom->at('[class~=x]')->text, 'Heart', 'right text';
is $dom->at('div[class~=x]')->text, 'Heart', 'right text';
is $dom->at('html div[class~=x]')->text, 'Heart', 'right text';
is $dom->at('html > div[class~=x]')->text, 'Heart', 'right text';
is $dom->at('html'), $html, 'right result';
is $dom->at('#☃x')->parent, $html, 'right result';
is $dom->at('#☃x')->root, $html, 'right result';
is $dom->children('html')->first, $html, 'right result';
is $dom->to_string, $html, 'right result';
is $dom->content, $html, 'right result';
};
subtest 'Looks remotely like HTML' => sub {
my $dom = Mojo::DOM->new('<!DOCTYPE H "-/W/D HT 4/E">☃<title class=test>♥</title>☃');
is $dom->at('title')->text, '♥', 'right text';
is $dom->at('*')->text, '♥', 'right text';
is $dom->at('.test')->text, '♥', 'right text';
};
subtest 'Replace elements' => sub {
my $dom = Mojo::DOM->new('<div>foo<p>lalala</p>bar</div>');
is $dom->at('p')->replace('<foo>bar</foo>'), '<div>foo<foo>bar</foo>bar</div>', 'right result';
is "$dom", '<div>foo<foo>bar</foo>bar</div>', 'right result';
$dom->at('foo')->replace(Mojo::DOM->new('text'));
is "$dom", '<div>footextbar</div>', 'right result';
$dom = Mojo::DOM->new('<div>foo</div><div>bar</div>');
$dom->find('div')->each(sub { shift->replace('<p>test</p>') });
is "$dom", '<p>test</p><p>test</p>', 'right result';
$dom = Mojo::DOM->new('<div>foo<p>lalala</p>bar</div>');
is $dom->replace('♥'), '♥', 'right result';
is "$dom", '♥', 'right result';
$dom->replace('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
is $dom->at('p')->replace(''), '<div>foobar</div>', 'right result';
is "$dom", '<div>foobar</div>', 'right result';
is $dom->replace(''), '', 'no result';
is "$dom", '', 'no result';
$dom->replace('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
$dom->find('p')->map(replace => '');
is "$dom", '<div>foobar</div>', 'right result';
$dom = Mojo::DOM->new('<div>♥</div>');
$dom->at('div')->content('☃');
is "$dom", '<div>☃</div>', 'right result';
$dom = Mojo::DOM->new('<div>♥</div>');
$dom->at('div')->content("\x{2603}");
is $dom->to_string, '<div>☃</div>', 'right result';
is $dom->at('div')->replace('<p>♥</p>')->root, '<p>♥</p>', 'right result';
is $dom->to_string, '<p>♥</p>', 'right result';
is $dom->replace('<b>whatever</b>')->root, '<b>whatever</b>', 'right result';
is $dom->to_string, '<b>whatever</b>', 'right result';
$dom->at('b')->prepend('<p>foo</p>')->append('<p>bar</p>');
is "$dom", '<p>foo</p><b>whatever</b><p>bar</p>', 'right result';
is $dom->find('p')->map('remove')->first->root->at('b')->text, 'whatever', 'right result';
is "$dom", '<b>whatever</b>', 'right result';
is $dom->at('b')->strip, 'whatever', 'right result';
is $dom->strip, 'whatever', 'right result';
is $dom->remove, '', 'right result';
$dom->replace('A<div>B<p>C<b>D<i><u>E</u></i>F</b>G</p><div>H</div></div>I');
is $dom->find(':not(div):not(i):not(u)')->map('strip')->first->root, 'A<div>BCD<i><u>E</u></i>FG<div>H</div></div>I',
'right result';
is $dom->at('i')->to_string, '<i><u>E</u></i>', 'right result';
$dom = Mojo::DOM->new('<div><div>A</div><div>B</div>C</div>');
is $dom->at('div')->at('div')->text, 'A', 'right text';
$dom->at('div')->find('div')->map('strip');
is "$dom", '<div>ABC</div>', 'right result';
};
subtest 'Replace element content' => sub {
my $dom = Mojo::DOM->new('<div>foo<p>lalala</p>bar</div>');
is $dom->at('p')->content('bar'), '<p>bar</p>', 'right result';
is "$dom", '<div>foo<p>bar</p>bar</div>', 'right result';
$dom->at('p')->content(Mojo::DOM->new('text'));
is "$dom", '<div>foo<p>text</p>bar</div>', 'right result';
$dom = Mojo::DOM->new('<div>foo</div><div>bar</div>');
$dom->find('div')->each(sub { shift->content('<p>test</p>') });
is "$dom", '<div><p>test</p></div><div><p>test</p></div>', 'right result';
$dom->find('p')->each(sub { shift->content('') });
is "$dom", '<div><p></p></div><div><p></p></div>', 'right result';
$dom = Mojo::DOM->new('<div><p id="☃" /></div>');
$dom->at('#☃')->content('♥');
is "$dom", '<div><p id="☃">♥</p></div>', 'right result';
$dom = Mojo::DOM->new('<div>foo<p>lalala</p>bar</div>');
$dom->content('♥');
is "$dom", '♥', 'right result';
is $dom->content('<div>foo<p>lalala</p>bar</div>'), '<div>foo<p>lalala</p>bar</div>', 'right result';
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
is $dom->content(''), '', 'no result';
is "$dom", '', 'no result';
$dom->content('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
is $dom->at('p')->content(''), '<p></p>', 'right result';
};
subtest 'Mixed search and tree walk' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<table>
<tr>
<td>text1</td>
<td>text2</td>
</tr>
</table>
EOF
my @data;
for my $tr ($dom->find('table tr')->each) {
for my $td (@{$tr->children}) {
push @data, $td->tag, $td->all_text;
}
}
is $data[0], 'td', 'right tag';
is $data[1], 'text1', 'right text';
is $data[2], 'td', 'right tag';
is $data[3], 'text2', 'right text';
is $data[4], undef, 'no tag';
};
subtest 'RSS' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Test Blog</title>
<link>http://blog.example.com</link>
<description>lalala</description>
<generator>Mojolicious</generator>
<item>
<pubDate>Mon, 12 Jul 2010 20:42:00</pubDate>
<title>Works!</title>
<link>http://blog.example.com/test</link>
<guid>http://blog.example.com/test</guid>
<description>
<![CDATA[<p>trololololo>]]>
</description>
<my:extension foo:id="works">
<![CDATA[
[awesome]]
]]>
</my:extension>
</item>
</channel>
</rss>
EOF
ok $dom->xml, 'XML mode detected';
is $dom->find('rss')->[0]->attr('version'), '2.0', 'right version';
is_deeply [$dom->at('title')->ancestors->map('tag')->each], [qw(channel rss)], 'right results';
is $dom->at('extension')->attr('foo:id'), 'works', 'right id';
like $dom->at('#works')->text, qr/\[awesome\]\]/, 'right text';
like $dom->at('[id="works"]')->text, qr/\[awesome\]\]/, 'right text';
is $dom->find('description')->[1]->text, "\n <p>trololololo>\n ", 'right text';
is $dom->at('pubDate')->text, 'Mon, 12 Jul 2010 20:42:00', 'right text';
like $dom->at('[id*="ork"]')->text, qr/\[awesome\]\]/, 'right text';
like $dom->at('[id*="orks"]')->text, qr/\[awesome\]\]/, 'right text';
like $dom->at('[id*="work"]')->text, qr/\[awesome\]\]/, 'right text';
like $dom->at('[id*="or"]')->text, qr/\[awesome\]\]/, 'right text';
ok $dom->at('rss')->xml, 'XML mode active';
ok $dom->at('extension')->parent->xml, 'XML mode active';
ok $dom->at('extension')->root->xml, 'XML mode active';
ok $dom->children('rss')->first->xml, 'XML mode active';
ok $dom->at('title')->ancestors->first->xml, 'XML mode active';
};
subtest 'Namespace' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<?xml version="1.0"?>
<bk:book xmlns='uri:default-ns'
xmlns:bk='uri:book-ns'
xmlns:isbn='uri:isbn-ns'>
<bk:title>Programming Perl</bk:title>
<comment>rocks!</comment>
<nons xmlns=''>
<section>Nothing</section>
</nons>
<meta xmlns='uri:meta-ns'>
<isbn:number>978-0596000271</isbn:number>
</meta>
</bk:book>
EOF
ok $dom->xml, 'XML mode detected';
is $dom->namespace, undef, 'no namespace';
is $dom->at('book comment')->namespace, 'uri:default-ns', 'right namespace';
is $dom->at('book comment')->text, 'rocks!', 'right text';
is $dom->at('book nons section')->namespace, '', 'no namespace';
is $dom->at('book nons section')->text, 'Nothing', 'right text';
is $dom->at('book meta number')->namespace, 'uri:isbn-ns', 'right namespace';
is $dom->at('book meta number')->text, '978-0596000271', 'right text';
is $dom->children('bk\:book')->first->{xmlns}, 'uri:default-ns', 'right attribute';
is $dom->children('book')->first->{xmlns}, 'uri:default-ns', 'right attribute';
is $dom->children('k\:book')->first, undef, 'no result';
is $dom->children('ook')->first, undef, 'no result';
is $dom->at('k\:book'), undef, 'no result';
is $dom->at('ook'), undef, 'no result';
is $dom->at('[xmlns\:bk]')->{'xmlns:bk'}, 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->{'xmlns:bk'}, 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->attr('xmlns:bk'), 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->attr('s:bk'), undef, 'no attribute';
is $dom->at('[bk]')->attr('bk'), undef, 'no attribute';
is $dom->at('[bk]')->attr('k'), undef, 'no attribute';
is $dom->at('[s\:bk]'), undef, 'no result';
is $dom->at('[k]'), undef, 'no result';
is $dom->at('number')->ancestors('meta')->first->{xmlns}, 'uri:meta-ns', 'right attribute';
ok $dom->at('nons')->matches('book > nons'), 'element did match';
ok !$dom->at('title')->matches('book > nons > section'), 'element did not match';
};
subtest 'Dots' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<?xml version="1.0"?>
<foo xmlns:foo.bar="uri:first">
<bar xmlns:fooxbar="uri:second">
<foo.bar:baz>First</fooxbar:baz>
<fooxbar:ya.da>Second</foo.bar:ya.da>
</bar>
</foo>
EOF
is $dom->at('foo bar baz')->text, "First\n ", 'right text';
is $dom->at('baz')->namespace, 'uri:first', 'right namespace';
is $dom->at('foo bar ya\.da')->text, "Second\n ", 'right text';
is $dom->at('ya\.da')->namespace, 'uri:second', 'right namespace';
is $dom->at('foo')->namespace, undef, 'no namespace';
is $dom->at('[xml\.s]'), undef, 'no result';
is $dom->at('b\.z'), undef, 'no result';
};
subtest 'Yadis' => sub {
my $dom = Mojo::DOM->new(<<'EOF');
<?xml version="1.0" encoding="UTF-8"?>
<XRDS xmlns="xri://$xrds">
<XRD xmlns="xri://$xrd*($v*2.0)">
<Service>
<Type>http://o.r.g/sso/2.0</Type>
</Service>
<Service>
<Type>http://o.r.g/sso/1.0</Type>
</Service>
</XRD>
</XRDS>
EOF
ok $dom->xml, 'XML mode detected';
is $dom->at('XRDS')->namespace, 'xri://$xrds', 'right namespace';
is $dom->at('XRD')->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
my $s = $dom->find('XRDS XRD Service');
is $s->[0]->at('Type')->text, 'http://o.r.g/sso/2.0', 'right text';
is $s->[0]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
is $s->[1]->at('Type')->text, 'http://o.r.g/sso/1.0', 'right text';
is $s->[1]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
is $s->[2], undef, 'no result';
is $s->size, 2, 'right number of elements';
};
subtest 'Yadis (roundtrip with namespace)' => sub {
my $yadis = <<'EOF';
<?xml version="1.0" encoding="UTF-8"?>
<xrds:XRDS xmlns="xri://$xrd*($v*2.0)" xmlns:xrds="xri://$xrds">
<XRD>
<Service>
<Type>http://o.r.g/sso/3.0</Type>
</Service>
<xrds:Service>
<Type>http://o.r.g/sso/4.0</Type>
</xrds:Service>
</XRD>
<XRD>
<Service>
<Type test="23">http://o.r.g/sso/2.0</Type>
</Service>
<Service>
<Type Test="23" test="24">http://o.r.g/sso/1.0</Type>
</Service>
</XRD>
</xrds:XRDS>
EOF
my $dom = Mojo::DOM->new($yadis);
ok $dom->xml, 'XML mode detected';
is $dom->at('XRDS')->namespace, 'xri://$xrds', 'right namespace';
is $dom->at('XRD')->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
my $s = $dom->find('XRDS XRD Service');
is $s->[0]->at('Type')->text, 'http://o.r.g/sso/3.0', 'right text';
is $s->[0]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
is $s->[1]->at('Type')->text, 'http://o.r.g/sso/4.0', 'right text';
is $s->[1]->namespace, 'xri://$xrds', 'right namespace';
is $s->[2]->at('Type')->text, 'http://o.r.g/sso/2.0', 'right text';
is $s->[2]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
is $s->[3]->at('Type')->text, 'http://o.r.g/sso/1.0', 'right text';
is $s->[3]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace';
is $s->[4], undef, 'no result';
is $s->size, 4, 'right number of elements';
is $dom->at('[Test="23"]')->text, 'http://o.r.g/sso/1.0', 'right text';
is $dom->at('[test="23"]')->text, 'http://o.r.g/sso/2.0', 'right text';
is $dom->find('xrds\:Service > Type')->[0]->text, 'http://o.r.g/sso/4.0', 'right text';
is $dom->find('xrds\:Service > Type')->[1], undef, 'no result';
is $dom->find('xrds\3AService > Type')->[0]->text, 'http://o.r.g/sso/4.0', 'right text';
is $dom->find('xrds\3AService > Type')->[1], undef, 'no result';
is $dom->find('xrds\3A Service > Type')->[0]->text, 'http://o.r.g/sso/4.0', 'right text';
is $dom->find('xrds\3A Service > Type')->[1], undef, 'no result';
is $dom->find('xrds\00003AService > Type')->[0]->text, 'http://o.r.g/sso/4.0', 'right text';
is $dom->find('xrds\00003AService > Type')->[1], undef, 'no result';
is $dom->find('xrds\00003A Service > Type')->[0]->text, 'http://o.r.g/sso/4.0', 'right text';
is $dom->find('xrds\00003A Service > Type')->[1], undef, 'no result';
is "$dom", $yadis, 'successful roundtrip';
};
subtest 'Result and iterator order' => sub {
my $dom = Mojo::DOM->new('<a><b>1</b></a><b>2</b><b>3</b>');
my @numbers;
$dom->find('b')->each(sub { push @numbers, pop, shift->text });
is_deeply \@numbers, [1, 1, 2, 2, 3, 3], 'right order';
};
subtest 'Attributes on multiple lines' => sub {
my $dom = Mojo::DOM->new("<div test=23 id='a' \n class='x' foo=bar />");
is $dom->at('div.x')->attr('test'), 23, 'right attribute';
is $dom->at('[foo="bar"]')->attr('class'), 'x', 'right attribute';
is $dom->at('div')->attr(baz => undef)->root->to_string, '<div baz class="x" foo="bar" id="a" test="23"></div>',
'right result';
};
subtest 'Markup characters in attribute values' => sub {
my $dom = Mojo::DOM->new(qq{<div id="<a>" \n test='='>Test<div id='><' /></div>});
is $dom->at('div[id="<a>"]')->attr->{test}, '=', 'right attribute';
is $dom->at('[id="<a>"]')->text, 'Test', 'right text';
is $dom->at('[id="><"]')->attr->{id}, '><', 'right attribute';
};
subtest 'Empty attributes' => sub {
my $dom = Mojo::DOM->new(qq{<div test="" test2='' />});
is $dom->at('div')->attr->{test}, '', 'empty attribute value';
is $dom->at('div')->attr->{test2}, '', 'empty attribute value';
is $dom->at('[test]')->tag, 'div', 'right tag';
is $dom->at('[test2]')->tag, 'div', 'right tag';
is $dom->at('[test3]'), undef, 'no result';
is $dom->at('[test=""]')->tag, 'div', 'right tag';
is $dom->at('[test2=""]')->tag, 'div', 'right tag';
is $dom->at('[test3=""]'), undef, 'no result';
};
subtest 'Multi-line attribute' => sub {
my $dom = Mojo::DOM->new(qq{<div class="line1\nline2" />});
is $dom->at('div')->attr->{class}, "line1\nline2", 'multi-line attribute value';
is $dom->at('.line1')->tag, 'div', 'right tag';
is $dom->at('.line2')->tag, 'div', 'right tag';
is $dom->at('.line3'), undef, 'no result';
};
subtest 'Entities in attributes' => sub {
my $dom = Mojo::DOM->new(qq{<a href="/?foo<=bar"></a>});
is $dom->at('a')->{href}, '/?foo<=bar', 'right attribute value';
$dom = Mojo::DOM->new(qq{<a href="/?f<oo=bar"></a>});
is $dom->at('a')->{href}, '/?f<oo=bar', 'right attribute value';
$dom = Mojo::DOM->new(qq{<a href="/?f<-oo=bar"></a>});
is $dom->at('a')->{href}, '/?f<-oo=bar', 'right attribute value';
$dom = Mojo::DOM->new(qq{<a href="/?foo=<"></a>});
is $dom->at('a')->{href}, '/?foo=<', 'right attribute value';
$dom = Mojo::DOM->new(qq{<a href="/?f<oo=bar"></a>});
is $dom->at('a')->{href}, '/?f<oo=bar', 'right attribute value';
};
subtest 'Whitespaces before closing bracket' => sub {
my $dom = Mojo::DOM->new('<div >content</div>');
ok $dom->at('div'), 'tag found';
is $dom->at('div')->text, 'content', 'right text';
is $dom->at('div')->content, 'content', 'right text';
};
subtest 'Class with hyphen' => sub {
my $dom = Mojo::DOM->new('<div class="a">A</div><div class="a-1">A1</div>');
my @div;
$dom->find('.a')->each(sub { push @div, shift->text });
is_deeply \@div, ['A'], 'found first element only';
@div = ();
$dom->find('.a-1')->each(sub { push @div, shift->text });
is_deeply \@div, ['A1'], 'found last element only';
};
subtest 'Defined but false text' => sub {
my $dom = Mojo::DOM->new('<div><div id="a">A</div><div id="b">B</div></div><div id="0">0</div>');
my @div;
$dom->find('div[id]')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A B 0)], 'found all div elements with id';
};
subtest 'Empty tags' => sub {
my $dom = Mojo::DOM->new('<hr /><br/><br id="br"/><br />');
is "$dom", '<hr><br><br id="br"><br>', 'right result';
is $dom->at('br')->content, '', 'empty result';
};
subtest 'Inner XML' => sub {
my $dom = Mojo::DOM->new('<a>xxx<x>x</x>xxx</a>');
is $dom->at('a')->content, 'xxx<x>x</x>xxx', 'right result';
is $dom->content, '<a>xxx<x>x</x>xxx</a>', 'right result';
};
subtest 'Multiple selectors' => sub {
my $dom = Mojo::DOM->new('<div id="a">A</div><div id="b">B</div><div id="c">C</div><p>D</p>');
my @div;
$dom->find('p, div')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A B C D)], 'found all elements';
@div = ();
$dom->find('#a, #c')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A C)], 'found all div elements with the right ids';
@div = ();
$dom->find('div#a, div#b')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A B)], 'found all div elements with the right ids';
@div = ();
$dom->find('div[id="a"], div[id="c"]')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A C)], 'found all div elements with the right ids';
$dom = Mojo::DOM->new('<div id="☃">A</div><div id="b">B</div><div id="♥x">C</div>');
@div = ();
$dom->find('#☃, #♥x')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A C)], 'found all div elements with the right ids';
@div = ();
$dom->find('div#☃, div#b')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A B)], 'found all div elements with the right ids';
@div = ();
$dom->find('div[id="☃"], div[id="♥x"]')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A C)], 'found all div elements with the right ids';
};
subtest 'Multiple attributes' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<div foo="bar" bar="baz">A</div>
<div foo="bar">B</div>
<div foo="bar" bar="baz">C</div>
<div foo="baz" bar="baz">D</div>
EOF
my @div;
$dom->find('div[foo="bar"][bar="baz"]')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A C)], 'found all div elements with the right atributes';
@div = ();
$dom->find('div[foo^="b"][foo$="r"]')->each(sub { push @div, shift->text });
is_deeply \@div, [qw(A B C)], 'found all div elements with the right atributes';
is $dom->at('[foo="bar"]')->previous, undef, 'no previous sibling';
is $dom->at('[foo="bar"]')->next->text, 'B', 'right text';
is $dom->at('[foo="bar"]')->next->previous->text, 'A', 'right text';
is $dom->at('[foo="bar"]')->next->next->next->next, undef, 'no next sibling';
};
subtest 'Pseudo-classes' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<form action="/foo">
<input type="text" name="user" value="test" />
<input type="checkbox" checked="checked" name="groovy">
<select name="a">
<option value="b">b</option>
<optgroup label="c">
<option value="d">d</option>
<option selected="selected" value="e">E</option>
<option value="f">f</option>
</optgroup>
<option value="g">g</option>
<option selected value="h">H</option>
</select>
<input type="submit" value="Ok!" />
<input type="checkbox" checked name="I">
<p id="content">test 123</p>
<p id="no_content"><? test ?><!-- 123 --></p>
</form>
EOF
is $dom->find(':root')->[0]->tag, 'form', 'right tag';
is $dom->find('*:root')->[0]->tag, 'form', 'right tag';
is $dom->find('form:root')->[0]->tag, 'form', 'right tag';
is $dom->find(':root')->[1], undef, 'no result';
is $dom->find(':checked')->[0]->attr->{name}, 'groovy', 'right name';
is $dom->find('option:checked')->[0]->attr->{value}, 'e', 'right value';
is $dom->find(':checked')->[1]->text, 'E', 'right text';
is $dom->find('*:checked')->[1]->text, 'E', 'right text';
is $dom->find(':checked')->[2]->text, 'H', 'right name';
is $dom->find(':checked')->[3]->attr->{name}, 'I', 'right name';
is $dom->find(':checked')->[4], undef, 'no result';
is $dom->find('option[selected]')->[0]->attr->{value}, 'e', 'right value';
is $dom->find('option[selected]')->[1]->text, 'H', 'right text';
is $dom->find('option[selected]')->[2], undef, 'no result';
is $dom->find(':checked[value="e"]')->[0]->text, 'E', 'right text';
is $dom->find('*:checked[value="e"]')->[0]->text, 'E', 'right text';
is $dom->find('option:checked[value="e"]')->[0]->text, 'E', 'right text';
is $dom->at('optgroup option:checked[value="e"]')->text, 'E', 'right text';
is $dom->at('select option:checked[value="e"]')->text, 'E', 'right text';
is $dom->at('select :checked[value="e"]')->text, 'E', 'right text';
is $dom->at('optgroup > :checked[value="e"]')->text, 'E', 'right text';
is $dom->at('select *:checked[value="e"]')->text, 'E', 'right text';
is $dom->at('optgroup > *:checked[value="e"]')->text, 'E', 'right text';
is $dom->find(':checked[value="e"]')->[1], undef, 'no result';
is $dom->find(':empty')->[0]->attr->{name}, 'user', 'right name';
is $dom->find('input:empty')->[0]->attr->{name}, 'user', 'right name';
is $dom->at(':empty[type^="ch"]')->attr->{name}, 'groovy', 'right name';
is $dom->at('p')->attr->{id}, 'content', 'right attribute';
is $dom->at('p:empty')->attr->{id}, 'no_content', 'right attribute';
};
subtest 'More pseudo-classes' => sub {
my $dom = Mojo::DOM->new(<<EOF);
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
<li>H</li>
</ul>
EOF
my @li;
$dom->find('li:nth-child(odd)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all odd li elements';
@li = ();
$dom->find('li:NTH-CHILD(ODD)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all odd li elements';
@li = ();
$dom->find('li:nth-last-child(odd)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all odd li elements';
is $dom->find(':nth-child(odd)')->[0]->tag, 'ul', 'right tag';
is $dom->find(':nth-child(odd)')->[1]->text, 'A', 'right text';
is $dom->find(':nth-child(1)')->[0]->tag, 'ul', 'right tag';
is $dom->find(':nth-child(1)')->[1]->text, 'A', 'right text';
is $dom->find(':nth-last-child(odd)')->[0]->tag, 'ul', 'right tag';
is $dom->find(':nth-last-child(odd)')->last->text, 'H', 'right text';
is $dom->find(':nth-last-child(1)')->[0]->tag, 'ul', 'right tag';
is $dom->find(':nth-last-child(1)')->[1]->text, 'H', 'right text';
@li = ();
$dom->find('li:nth-child(2n+1)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all odd li elements';
@li = ();
$dom->find('li:nth-child(2n + 1)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all odd li elements';
@li = ();
$dom->find('li:nth-last-child(2n+1)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all odd li elements';
@li = ();
$dom->find('li:nth-child(even)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all even li elements';
@li = ();
$dom->find('li:NTH-CHILD(EVEN)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all even li elements';
@li = ();
$dom->find('li:nth-last-child( even )')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all even li elements';
@li = ();
$dom->find('li:nth-child(2n+2)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all even li elements';
@li = ();
$dom->find('li:nTh-chILd(2N+2)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all even li elements';
@li = ();
$dom->find('li:nth-child( 2n + 2 )')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(B D F H)], 'found all even li elements';
@li = ();
$dom->find('li:nth-last-child(2n+2)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A C E G)], 'found all even li elements';
@li = ();
$dom->find('li:nth-child(4n+1)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A E)], 'found the right li elements';
@li = ();
$dom->find('li:nth-last-child(4n+1)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(D H)], 'found the right li elements';
@li = ();
$dom->find('li:nth-child(4n+4)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(D H)], 'found the right li elements';
@li = ();
$dom->find('li:nth-last-child(4n+4)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A E)], 'found the right li elements';
@li = ();
$dom->find('li:nth-child(4n)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(D H)], 'found the right li elements';
@li = ();
$dom->find('li:nth-child( 4n )')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(D H)], 'found the right li elements';
@li = ();
$dom->find('li:nth-last-child(4n)')->each(sub { push @li, shift->text });
is_deeply \@li, [qw(A E)], 'found the right li elements';
@li = ();
$dom->find('li:nth-child(5n-2)')->each(sub { push @li, shift->text });