mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
wpHtmlTagProcessor.php
2775 lines (2504 loc) · 103 KB
/
wpHtmlTagProcessor.php
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
<?php
/**
* Unit tests covering WP_HTML_Tag_Processor functionality.
*
* @package WordPress
* @subpackage HTML-API
*/
/**
* @group html-api
*
* @coversDefaultClass WP_HTML_Tag_Processor
*/
class Tests_HtmlApi_WpHtmlTagProcessor extends WP_UnitTestCase {
const HTML_SIMPLE = '<div id="first"><span id="second">Text</span></div>';
const HTML_WITH_CLASSES = '<div class="main with-border" id="first"><span class="not-main bold with-border" id="second">Text</span></div>';
const HTML_MALFORMED = '<div><span class="d-md-none" Notifications</span><span class="d-none d-md-inline">Back to notifications</span></div>';
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_tag
*/
public function test_get_tag_returns_null_before_finding_tags() {
$processor = new WP_HTML_Tag_Processor( '<div>Test</div>' );
$this->assertNull( $processor->get_tag(), 'Calling get_tag() without selecting a tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_tag
*/
public function test_get_tag_returns_null_when_not_in_open_tag() {
$processor = new WP_HTML_Tag_Processor( '<div>Test</div>' );
$this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );
$this->assertNull( $processor->get_tag(), 'Accessing a non-existing tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_tag
*/
public function test_get_tag_returns_open_tag_name() {
$processor = new WP_HTML_Tag_Processor( '<div>Test</div>' );
$this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' );
$this->assertSame( 'DIV', $processor->get_tag(), 'Accessing an existing tag name did not return "div"' );
}
/**
* @ticket 58009
*
* @covers WP_HTML_Tag_Processor::has_self_closing_flag
*
* @dataProvider data_has_self_closing_flag
*
* @param string $html Input HTML whose first tag might contain the self-closing flag `/`.
* @param bool $flag_is_set Whether the input HTML's first tag contains the self-closing flag.
*/
public function test_has_self_closing_flag_matches_input_html( $html, $flag_is_set ) {
$processor = new WP_HTML_Tag_Processor( $html );
$processor->next_tag( array( 'tag_closers' => 'visit' ) );
if ( $flag_is_set ) {
$this->assertTrue( $processor->has_self_closing_flag(), 'Did not find the self-closing tag when it was present.' );
} else {
$this->assertFalse( $processor->has_self_closing_flag(), 'Found the self-closing tag when it was absent.' );
}
}
/**
* Data provider. HTML tags which might have a self-closing flag, and an indicator if they do.
*
* @return array[]
*/
public static function data_has_self_closing_flag() {
return array(
// These should not have a self-closer, and will leave an element un-closed if it's assumed they are self-closing.
'Self-closing flag on non-void HTML element' => array( '<div />', true ),
'No self-closing flag on non-void HTML element' => array( '<div>', false ),
// These should not have a self-closer, but are benign when used because the elements are void.
'Self-closing flag on void HTML element' => array( '<img />', true ),
'No self-closing flag on void HTML element' => array( '<img>', false ),
'Self-closing flag on void HTML element without spacing' => array( '<img/>', true ),
// These should not have a self-closer, but as part of a tag closer they are entirely ignored.
'Self-closing flag on tag closer' => array( '</textarea />', true ),
'No self-closing flag on tag closer' => array( '</textarea>', false ),
// These can and should have self-closers, and will leave an element un-closed if it's assumed they aren't self-closing.
'Self-closing flag on a foreign element' => array( '<circle />', true ),
'No self-closing flag on a foreign element' => array( '<circle>', false ),
// These involve syntax peculiarities.
'Self-closing flag after extra spaces' => array( '<div />', true ),
'Self-closing flag after attribute' => array( '<div id=test/>', true ),
'Self-closing flag after quoted attribute' => array( '<div id="test"/>', true ),
'Self-closing flag after boolean attribute' => array( '<div enabled/>', true ),
'Boolean attribute that looks like a self-closer' => array( '<div / >', false ),
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_null_before_finding_tags() {
$processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );
$this->assertNull( $processor->get_attribute( 'class' ), 'Accessing an attribute without selecting a tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_null_when_not_in_open_tag() {
$processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );
$this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );
$this->assertNull( $processor->get_attribute( 'class' ), 'Accessing an attribute of a non-existing tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_null_when_in_closing_tag() {
$processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );
$this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' );
$this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Querying an existing closing tag did not return true' );
$this->assertNull( $processor->get_attribute( 'class' ), 'Accessing an attribute of a closing tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_null_when_attribute_missing() {
$processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );
$this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' );
$this->assertNull( $processor->get_attribute( 'test-id' ), 'Accessing a non-existing attribute did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_attribute_value() {
$processor = new WP_HTML_Tag_Processor( '<div class="test">Test</div>' );
$this->assertTrue( $processor->next_tag( 'div' ), 'Querying an existing tag did not return true' );
$this->assertSame( 'test', $processor->get_attribute( 'class' ), 'Accessing a class="test" attribute value did not return "test"' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_true_for_boolean_attribute() {
$processor = new WP_HTML_Tag_Processor( '<div enabled class="test">Test</div>' );
$this->assertTrue( $processor->next_tag( array( 'class_name' => 'test' ) ), 'Querying an existing tag did not return true' );
$this->assertTrue( $processor->get_attribute( 'enabled' ), 'Accessing a boolean "enabled" attribute value did not return true' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_string_for_truthy_attributes() {
$processor = new WP_HTML_Tag_Processor( '<div enabled=enabled checked=1 hidden="true" class="test">Test</div>' );
$this->assertTrue( $processor->next_tag(), 'Querying an existing tag did not return true' );
$this->assertSame( 'enabled', $processor->get_attribute( 'enabled' ), 'Accessing a boolean "enabled" attribute value did not return true' );
$this->assertSame( '1', $processor->get_attribute( 'checked' ), 'Accessing a checked=1 attribute value did not return "1"' );
$this->assertSame( 'true', $processor->get_attribute( 'hidden' ), 'Accessing a hidden="true" attribute value did not return "true"' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_decodes_html_character_references() {
$processor = new WP_HTML_Tag_Processor( '<div id="the "grande" is < 32oz†"></div>' );
$processor->next_tag();
$this->assertSame( 'the "grande" is < 32oz†', $processor->get_attribute( 'id' ), 'HTML Attribute value was returned without decoding character references' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_attributes_parser_treats_slash_as_attribute_separator() {
$processor = new WP_HTML_Tag_Processor( '<div a/b/c/d/e="test">Test</div>' );
$this->assertTrue( $processor->next_tag(), 'Querying an existing tag did not return true' );
$this->assertTrue( $processor->get_attribute( 'a' ), 'Accessing an existing attribute did not return true' );
$this->assertTrue( $processor->get_attribute( 'b' ), 'Accessing an existing attribute did not return true' );
$this->assertTrue( $processor->get_attribute( 'c' ), 'Accessing an existing attribute did not return true' );
$this->assertTrue( $processor->get_attribute( 'd' ), 'Accessing an existing attribute did not return true' );
$this->assertSame( 'test', $processor->get_attribute( 'e' ), 'Accessing an existing e="test" did not return "test"' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*
* @dataProvider data_attribute_name_case_variants
*
* @param string $attribute_name Name of data-enabled attribute with case variations.
*/
public function test_get_attribute_is_case_insensitive_for_attributes_with_values( $attribute_name ) {
$processor = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' );
$processor->next_tag();
$this->assertSame(
'true',
$processor->get_attribute( $attribute_name ),
'Accessing an attribute by a differently-cased name did not return its value'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*
* @dataProvider data_attribute_name_case_variants
*
* @param string $attribute_name Name of data-enabled attribute with case variations.
*/
public function test_attributes_parser_is_case_insensitive_for_attributes_without_values( $attribute_name ) {
$processor = new WP_HTML_Tag_Processor( '<div DATA-enabled>Test</div>' );
$processor->next_tag();
$this->assertTrue(
$processor->get_attribute( $attribute_name ),
'Accessing an attribute by a differently-cased name did not return its value'
);
}
/**
* Data provider.
*
* @return array[].
*/
public static function data_attribute_name_case_variants() {
return array(
array( 'DATA-enabled' ),
array( 'data-enabled' ),
array( 'DATA-ENABLED' ),
array( 'DatA-EnABled' ),
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::remove_attribute
*/
public function test_remove_attribute_is_case_insensitive() {
$processor = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' );
$processor->next_tag();
$processor->remove_attribute( 'data-enabled' );
$this->assertSame( '<div >Test</div>', $processor->get_updated_html(), 'A case-insensitive remove_attribute call did not remove the attribute' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_set_attribute_is_case_insensitive() {
$processor = new WP_HTML_Tag_Processor( '<div DATA-enabled="true">Test</div>' );
$processor->next_tag();
$processor->set_attribute( 'data-enabled', 'abc' );
$this->assertSame( '<div data-enabled="abc">Test</div>', $processor->get_updated_html(), 'A case-insensitive set_attribute call did not update the existing attribute' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
*/
public function test_get_attribute_names_with_prefix_returns_null_before_finding_tags() {
$processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );
$this->assertNull(
$processor->get_attribute_names_with_prefix( 'data-' ),
'Accessing attributes by their prefix did not return null when no tag was selected'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
*/
public function test_get_attribute_names_with_prefix_returns_null_when_not_in_open_tag() {
$processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );
$processor->next_tag( 'p' );
$this->assertNull( $processor->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a non-existing tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
*/
public function test_get_attribute_names_with_prefix_returns_null_when_in_closing_tag() {
$processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );
$processor->next_tag( 'div' );
$processor->next_tag( array( 'tag_closers' => 'visit' ) );
$this->assertNull( $processor->get_attribute_names_with_prefix( 'data-' ), 'Accessing attributes of a closing tag did not return null' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
*/
public function test_get_attribute_names_with_prefix_returns_empty_array_when_no_attributes_present() {
$processor = new WP_HTML_Tag_Processor( '<div>Test</div>' );
$processor->next_tag( 'div' );
$this->assertSame( array(), $processor->get_attribute_names_with_prefix( 'data-' ), 'Accessing the attributes on a tag without any did not return an empty array' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
*/
public function test_get_attribute_names_with_prefix_returns_matching_attribute_names_in_lowercase() {
$processor = new WP_HTML_Tag_Processor( '<div DATA-enabled class="test" data-test-ID="14">Test</div>' );
$processor->next_tag();
$this->assertSame(
array( 'data-enabled', 'data-test-id' ),
$processor->get_attribute_names_with_prefix( 'data-' ),
'Accessing attributes by their prefix did not return their lowercase names'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute_names_with_prefix
*/
public function test_get_attribute_names_with_prefix_returns_attribute_added_by_set_attribute() {
$processor = new WP_HTML_Tag_Processor( '<div data-foo="bar">Test</div>' );
$processor->next_tag();
$processor->set_attribute( 'data-test-id', '14' );
$this->assertSame(
'<div data-test-id="14" data-foo="bar">Test</div>',
$processor->get_updated_html(),
"Updated HTML doesn't include attribute added via set_attribute"
);
$this->assertSame(
array( 'data-test-id', 'data-foo' ),
$processor->get_attribute_names_with_prefix( 'data-' ),
"Accessing attribute names doesn't find attribute added via set_attribute"
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::__toString
*/
public function test_to_string_returns_updated_html() {
$processor = new WP_HTML_Tag_Processor( '<hr id="remove" /><div enabled class="test">Test</div><span id="span-id"></span>' );
$processor->next_tag();
$processor->remove_attribute( 'id' );
$processor->next_tag();
$processor->set_attribute( 'id', 'div-id-1' );
$processor->add_class( 'new_class_1' );
$this->assertSame(
$processor->get_updated_html(),
(string) $processor,
'get_updated_html() returned a different value than __toString()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_updated_html
*/
public function test_get_updated_html_applies_the_updates_so_far_and_keeps_the_processor_on_the_current_tag() {
$processor = new WP_HTML_Tag_Processor( '<hr id="remove" /><div enabled class="test">Test</div><span id="span-id"></span>' );
$processor->next_tag();
$processor->remove_attribute( 'id' );
$processor->next_tag();
$processor->set_attribute( 'id', 'div-id-1' );
$processor->add_class( 'new_class_1' );
$this->assertSame(
'<hr /><div id="div-id-1" enabled class="test new_class_1">Test</div><span id="span-id"></span>',
$processor->get_updated_html(),
'Calling get_updated_html after updating the attributes of the second tag returned different HTML than expected'
);
$processor->set_attribute( 'id', 'div-id-2' );
$processor->add_class( 'new_class_2' );
$this->assertSame(
'<hr /><div id="div-id-2" enabled class="test new_class_1 new_class_2">Test</div><span id="span-id"></span>',
$processor->get_updated_html(),
'Calling get_updated_html after updating the attributes of the second tag for the second time returned different HTML than expected'
);
$processor->next_tag();
$processor->remove_attribute( 'id' );
$this->assertSame(
'<hr /><div id="div-id-2" enabled class="test new_class_1 new_class_2">Test</div><span ></span>',
$processor->get_updated_html(),
'Calling get_updated_html after removing the id attribute of the third tag returned different HTML than expected'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_updated_html
*/
public function test_get_updated_html_without_updating_any_attributes_returns_the_original_html() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$this->assertSame(
self::HTML_SIMPLE,
$processor->get_updated_html(),
'Casting WP_HTML_Tag_Processor to a string without performing any updates did not return the initial HTML snippet'
);
}
/**
* Ensures that when seeking to an earlier spot in the document that
* all previously-enqueued updates are applied as they ought to be.
*
* @ticket 58160
*/
public function test_get_updated_html_applies_updates_to_content_after_seeking_to_before_parsed_bytes() {
$processor = new WP_HTML_Tag_Processor( '<div><img hidden></div>' );
$processor->next_tag();
$processor->set_attribute( 'wonky', true );
$processor->next_tag();
$processor->set_bookmark( 'here' );
$processor->next_tag( array( 'tag_closers' => 'visit' ) );
$processor->seek( 'here' );
$this->assertSame( '<div wonky><img hidden></div>', $processor->get_updated_html() );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::next_tag
*/
public function test_next_tag_with_no_arguments_should_find_the_next_existing_tag() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$this->assertTrue( $processor->next_tag(), 'Querying an existing tag did not return true' );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::next_tag
*/
public function test_next_tag_should_return_false_for_a_non_existing_tag() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );
}
/**
* @ticket 59209
*
* @covers WP_HTML_Tag_Processor::next_tag
*/
public function test_next_tag_matches_decoded_class_names() {
$processor = new WP_HTML_Tag_Processor( '<div class="<egg>">' );
$this->assertTrue( $processor->next_tag( array( 'class_name' => '<egg>' ) ), 'Failed to find tag with HTML-encoded class name.' );
}
/**
* @ticket 56299
* @ticket 57852
*
* @covers WP_HTML_Tag_Processor::next_tag
* @covers WP_HTML_Tag_Processor::is_tag_closer
*/
public function test_next_tag_should_stop_on_closers_only_when_requested() {
$processor = new WP_HTML_Tag_Processor( '<div><img /></div>' );
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'div' ) ), 'Did not find desired tag opener' );
$this->assertFalse( $processor->next_tag( array( 'tag_name' => 'div' ) ), 'Visited an unwanted tag, a tag closer' );
$processor = new WP_HTML_Tag_Processor( '<div><img /></div>' );
$processor->next_tag(
array(
'tag_name' => 'div',
'tag_closers' => 'visit',
)
);
$this->assertFalse( $processor->is_tag_closer(), 'Indicated a tag opener is a tag closer' );
$this->assertTrue(
$processor->next_tag(
array(
'tag_name' => 'div',
'tag_closers' => 'visit',
)
),
'Did not stop at desired tag closer'
);
$this->assertTrue( $processor->is_tag_closer(), 'Indicated a tag closer is a tag opener' );
$processor = new WP_HTML_Tag_Processor( '<div>' );
$this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), "Did not find a tag opener when tag_closers was set to 'visit'" );
$this->assertFalse( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), "Found a closer where there wasn't one" );
}
/**
* @ticket 57852
*
* @covers WP_HTML_Tag_Processor::next_tag
* @covers WP_HTML_Tag_Processor::is_tag_closer
*/
public function test_next_tag_should_stop_on_rcdata_and_script_tag_closers_when_requested() {
$processor = new WP_HTML_Tag_Processor( '<script>abc</script>' );
$processor->next_tag();
$this->assertFalse(
$processor->next_tag( array( 'tag_closers' => 'visit' ) ),
'Should not have found closing SCRIPT tag when closing an opener.'
);
$processor = new WP_HTML_Tag_Processor( 'abc</script>' );
$this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer when there was no tag opener' );
$processor = new WP_HTML_Tag_Processor( '<textarea>abc</textarea>' );
$processor->next_tag();
$this->assertFalse(
$processor->next_tag( array( 'tag_closers' => 'visit' ) ),
'Should not have found closing TEXTAREA when closing an opener.'
);
$processor = new WP_HTML_Tag_Processor( 'abc</textarea>' );
$this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer when there was no tag opener' );
$processor = new WP_HTML_Tag_Processor( '<title>abc</title>' );
$processor->next_tag();
$this->assertFalse(
$processor->next_tag( array( 'tag_closers' => 'visit' ) ),
'Should not have found closing TITLE when closing an opener.'
);
$processor = new WP_HTML_Tag_Processor( 'abc</title>' );
$this->assertTrue( $processor->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );
}
/**
* Verifies that updates to a document before calls to `get_updated_html()` don't
* lead to the Tag Processor jumping to the wrong tag after the updates.
*
* @ticket 58179
*
* @covers WP_HTML_Tag_Processor::get_updated_html
*/
public function test_internal_pointer_returns_to_original_spot_after_inserting_content_before_cursor() {
$tags = new WP_HTML_Tag_Processor( '<div>outside</div><section><div><img>inside</div></section>' );
$tags->next_tag();
$tags->add_class( 'foo' );
$tags->next_tag( 'section' );
// Return to this spot after moving ahead.
$tags->set_bookmark( 'here' );
// Move ahead.
$tags->next_tag( 'img' );
$tags->seek( 'here' );
$this->assertSame( '<div class="foo">outside</div><section><div><img>inside</div></section>', $tags->get_updated_html() );
$this->assertSame( 'SECTION', $tags->get_tag() );
$this->assertFalse( $tags->is_tag_closer() );
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_set_attribute_on_a_non_existing_tag_does_not_change_the_markup() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );
$this->assertFalse( $processor->next_tag( 'div' ), 'Querying a non-existing tag did not return false' );
$processor->set_attribute( 'id', 'primary' );
$this->assertSame(
self::HTML_SIMPLE,
$processor->get_updated_html(),
'Calling get_updated_html after updating a non-existing tag returned an HTML that was different from the original HTML'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::set_attribute
* @covers WP_HTML_Tag_Processor::remove_attribute
* @covers WP_HTML_Tag_Processor::add_class
* @covers WP_HTML_Tag_Processor::remove_class
*/
public function test_attribute_ops_on_tag_closer_do_not_change_the_markup() {
$processor = new WP_HTML_Tag_Processor( '<div id=3></div invalid-id=4>' );
$processor->next_tag(
array(
'tag_name' => 'div',
'tag_closers' => 'visit',
)
);
$this->assertFalse( $processor->is_tag_closer(), 'Skipped tag opener' );
$processor->next_tag(
array(
'tag_name' => 'div',
'tag_closers' => 'visit',
)
);
$this->assertTrue( $processor->is_tag_closer(), 'Skipped tag closer' );
$this->assertFalse( $processor->set_attribute( 'id', 'test' ), "Allowed setting an attribute on a tag closer when it shouldn't have" );
$this->assertFalse( $processor->remove_attribute( 'invalid-id' ), "Allowed removing an attribute on a tag closer when it shouldn't have" );
$this->assertFalse( $processor->add_class( 'sneaky' ), "Allowed adding a class on a tag closer when it shouldn't have" );
$this->assertFalse( $processor->remove_class( 'not-appearing-in-this-test' ), "Allowed removing a class on a tag closer when it shouldn't have" );
$this->assertSame(
'<div id=3></div invalid-id=4>',
$processor->get_updated_html(),
'Calling get_updated_html after updating a non-existing tag returned an HTML that was different from the original HTML'
);
}
/**
* Passing a double quote inside of an attribute value could lead to an XSS attack as follows:
*
* ```php
* $processor = new WP_HTML_Tag_Processor( '<div class="header"></div>' );
* $processor->next_tag();
* $processor->set_attribute('class', '" onclick="alert');
* echo $p;
* // <div class="" onclick="alert"></div>
* ```
*
* To prevent it, `set_attribute` calls `esc_attr()` on its given values.
*
* ```php
* <div class="" onclick="alert"></div>
* ```
*
* @ticket 56299
*
* @dataProvider data_set_attribute_prevents_xss
* @covers WP_HTML_Tag_Processor::set_attribute
*
* @param string $attribute_value A value with potential XSS exploit.
*/
public function test_set_attribute_prevents_xss( $attribute_value ) {
$processor = new WP_HTML_Tag_Processor( '<div></div>' );
$processor->next_tag();
$processor->set_attribute( 'test', $attribute_value );
/*
* Testing the escaping is hard using tools that properly parse
* HTML because they might interpret the escaped values. It's hard
* with tools that don't understand HTML because they might get
* confused by improperly-escaped values.
*
* Since the input HTML is known, the test will do what looks like
* the opposite of what is expected to be done with this library.
* But by doing so, the test (a) has full control over the
* content and (b) looks at the raw values.
*/
$match = null;
preg_match( '~^<div test=(.*)></div>$~', $processor->get_updated_html(), $match );
list( , $actual_value ) = $match;
$this->assertSame( '"' . esc_attr( $attribute_value ) . '"', $actual_value, 'Entities were not properly escaped in the attribute value' );
}
/**
* Data provider.
*
* @return string[][].
*/
public static function data_set_attribute_prevents_xss() {
return array(
array( '"' ),
array( '"' ),
array( '&' ),
array( '&' ),
array( '€' ),
array( "'" ),
array( '<>' ),
array( '"";' ),
array( '" onclick="alert(\'1\');"><span onclick=""></span><script>alert("1")</script>' ),
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_set_attribute_with_a_non_existing_attribute_adds_a_new_attribute_to_the_markup() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->set_attribute( 'test-attribute', 'test-value' );
$this->assertSame(
'<div test-attribute="test-value" id="first"><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML does not include attribute added via set_attribute()'
);
$this->assertSame(
'test-value',
$processor->get_attribute( 'test-attribute' ),
'get_attribute() (called after get_updated_html()) did not return attribute added via set_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_updated_values_before_they_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->set_attribute( 'test-attribute', 'test-value' );
$this->assertSame(
'test-value',
$processor->get_attribute( 'test-attribute' ),
'get_attribute() (called before get_updated_html()) did not return attribute added via set_attribute()'
);
$this->assertSame(
'<div test-attribute="test-value" id="first"><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML does not include attribute added via set_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_returns_updated_values_before_they_are_applied_with_different_name_casing() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->set_attribute( 'test-ATTribute', 'test-value' );
$this->assertSame(
'test-value',
$processor->get_attribute( 'test-attribute' ),
'get_attribute() (called before get_updated_html()) did not return attribute added via set_attribute()'
);
$this->assertSame(
'<div test-ATTribute="test-value" id="first"><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML does not include attribute added via set_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_added_class_names_before_they_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->add_class( 'my-class' );
$this->assertSame(
'my-class',
$processor->get_attribute( 'class' ),
'get_attribute() (called before get_updated_html()) did not return class name added via add_class()'
);
$this->assertSame(
'<div class="my-class" id="first"><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML does not include class name added via add_class()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_added_class_names_before_they_are_applied_and_retains_classes_from_previous_add_class_calls() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->add_class( 'my-class' );
$this->assertSame(
'my-class',
$processor->get_attribute( 'class' ),
'get_attribute() (called before get_updated_html()) did not return class name added via add_class()'
);
$processor->add_class( 'my-other-class' );
$this->assertSame(
'my-class my-other-class',
$processor->get_attribute( 'class' ),
'get_attribute() (called before get_updated_html()) did not return class names added via subsequent add_class() calls'
);
$this->assertSame(
'<div class="my-class my-other-class" id="first"><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML does not include class names added via subsequent add_class() calls'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_removed_attribute_before_it_is_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->remove_attribute( 'id' );
$this->assertNull(
$processor->get_attribute( 'id' ),
'get_attribute() (called before get_updated_html()) returned attribute that was removed by remove_attribute()'
);
$this->assertSame(
'<div ><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML includes attribute that was removed by remove_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_adding_and_then_removing_an_attribute_before_those_updates_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->set_attribute( 'test-attribute', 'test-value' );
$processor->remove_attribute( 'test-attribute' );
$this->assertNull(
$processor->get_attribute( 'test-attribute' ),
'get_attribute() (called before get_updated_html()) returned attribute that was added via set_attribute() and then removed by remove_attribute()'
);
$this->assertSame(
self::HTML_SIMPLE,
$processor->get_updated_html(),
'Updated HTML includes attribute that was added via set_attribute() and then removed by remove_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_setting_and_then_removing_an_existing_attribute_before_those_updates_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_SIMPLE );
$processor->next_tag();
$processor->set_attribute( 'id', 'test-value' );
$processor->remove_attribute( 'id' );
$this->assertNull(
$processor->get_attribute( 'id' ),
'get_attribute() (called before get_updated_html()) returned attribute that was overwritten by set_attribute() and then removed by remove_attribute()'
);
$this->assertSame(
'<div ><span id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML includes attribute that was overwritten by set_attribute() and then removed by remove_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_removed_class_names_before_they_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );
$processor->next_tag();
$processor->remove_class( 'with-border' );
$this->assertSame(
'main',
$processor->get_attribute( 'class' ),
'get_attribute() (called before get_updated_html()) returned the wrong attribute after calling remove_attribute()'
);
$this->assertSame(
'<div class="main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML includes wrong attribute after calling remove_attribute()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_setting_and_then_removing_a_class_name_before_those_updates_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );
$processor->next_tag();
$processor->add_class( 'foo-class' );
$processor->remove_class( 'foo-class' );
$this->assertSame(
'main with-border',
$processor->get_attribute( 'class' ),
'get_attribute() (called before get_updated_html()) returned class name that was added via add_class() and then removed by remove_class()'
);
$this->assertSame(
self::HTML_WITH_CLASSES,
$processor->get_updated_html(),
'Updated HTML includes class that was added via add_class() and then removed by remove_class()'
);
}
/**
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::get_attribute
*/
public function test_get_attribute_reflects_duplicating_and_then_removing_an_existing_class_name_before_those_updates_are_applied() {
$processor = new WP_HTML_Tag_Processor( self::HTML_WITH_CLASSES );
$processor->next_tag();
$processor->add_class( 'with-border' );
$processor->remove_class( 'with-border' );
$this->assertSame(
'main',
$processor->get_attribute( 'class' ),
'get_attribute() (called before get_updated_html()) returned class name that was duplicated via add_class() and then removed by remove_class()'
);
$this->assertSame(
'<div class="main" id="first"><span class="not-main bold with-border" id="second">Text</span></div>',
$processor->get_updated_html(),
'Updated HTML includes class that was duplicated via add_class() and then removed by remove_class()'
);
}
/**
* According to HTML spec, only the first instance of an attribute counts.
* The other ones are ignored.
*
* @ticket 56299
*
* @covers WP_HTML_Tag_Processor::set_attribute
*/
public function test_update_first_attribute_when_duplicated_attributes_exist() {