File: 30subclass.t

package info (click to toggle)
libhtml-stripscripts-perl 1.06-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 492 kB
  • sloc: perl: 1,004; makefile: 2
file content (622 lines) | stat: -rw-r--r-- 15,284 bytes parent folder | download | duplicates (5)
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

use strict;
use Test::More tests => 27;

BEGIN { $^W = 1 }

use HTML::StripScripts;

=head1 name

F<t/30subclass.t> - HTML::StripScripts subclassing test script

=head1 DESCRIPTION

This file is part of the reggression test suite of L<HTML::StripScripts>,
testing that subclassing works as documented.  This file also serves as
a set of examples of subclassing L<HTML::StripScripts>.

=head1 TESTS

=over

=item output_start_document

Overriding output_start_document() to prepend an HTML comment.

=cut

{
    package SubClass_output_start_document;
    use base qw(HTML::StripScripts);

    sub output_start_document {
        my ($self) = @_;

    $self->output_comment('<!--foo-->');
    }
}
my $f = SubClass_output_start_document->new;
$f->input_start_document;
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<!--foo-->foo', 'subclass output_start_document' );

=item output_end_document

Overriding output_end_document() to apend an HTML comment.

=cut

{
    package SubClass_output_end_document;
    use base qw(HTML::StripScripts);

    sub output_end_document {
        my ($self) = @_;

    $self->output_comment('<!--foo-->');
    }
}
$f = SubClass_output_end_document->new;
$f->input_start_document;
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, 'foo<!--foo-->', 'subclass output_end_document' );

=item output_start

Overriding output_start() to convert start tags to upper case

=cut

{
    package SubClass_output_start;
    use base qw(HTML::StripScripts);

    sub output_start {
        my ($self, $text) = @_;

    $self->output(uc $text);
    }
}
$f = SubClass_output_start->new;
$f->input_start_document;
$f->input_start('<font color=Red>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<FONT COLOR="RED">foo</font>', 'subclass output_start' );

=item output_text

Overriding output_text() to convert text to upper case

=cut

{
    package SubClass_output_text;
    use base qw(HTML::StripScripts);

    sub output_text {
        my ($self, $text) = @_;

    $self->output(uc $text);
    }
}
$f = SubClass_output_text->new;
$f->input_start_document;
$f->input_start('<font color=Red>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<font color="Red">FOO</font>', 'subclass output_text' );

=item output_end

Overriding output_end() to convert end tags to upper case

=cut

{
    package SubClass_output_end;
    use base qw(HTML::StripScripts);

    sub output_end {
        my ($self, $text) = @_;

    $self->output(uc $text);
    }
}
$f = SubClass_output_end->new;
$f->input_start_document;
$f->input_start('<font color=Red>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<font color="Red">foo</FONT>', 'subclass output_end' );

=item output

Overriding output() to convert all output to upper case

=cut

{
    package SubClass_output;
    use base qw(HTML::StripScripts);

    sub output {
        my ($self, $text) = @_;

    $self->SUPER::output(uc $text);
    }
}
$f = SubClass_output->new;
$f->input_start_document;
$f->input_start('<font color=Red>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<FONT COLOR="RED">FOO</FONT>', 'subclass output' );

=item reject_start

Overriding reject_start() so that rejected start tags are escaped rather than
replaced with HTML comments.

=cut

{
    package SubClass_reject_start;
    use base qw(HTML::StripScripts);

    sub reject_start {
        my ($self, $text) = @_;

    $self->output_text( $self->escape_html_metachars($text) );
    }
}
$f = SubClass_reject_start->new;
$f->input_start_document;
$f->input_start('<foo type=bar>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '&lt;foo type=bar&gt;foo', 'subclass reject start' );

=item reject_end

Overriding reject_end() so that rejected end tags are escaped rather than
replaced with HTML comments.

=cut

{
    package SubClass_reject_end;
    use base qw(HTML::StripScripts);

    sub reject_end {
        my ($self, $text) = @_;

    $self->output_text( $self->escape_html_metachars($text) );
    }
}
$f = SubClass_reject_end->new;
$f->input_start_document;
$f->input_text('foo');
$f->input_end('</i>');
$f->input_end_document;
is( $f->filtered_document, 'foo&lt;/i&gt;', 'subclass reject end' );

=item reject_text

Overriding reject_text() so that rejected non-tag text is replaced with
a different HTML comment than the default.

=cut

{
    package SubClass_reject_text;
    use base qw(HTML::StripScripts);

    sub reject_text {
        my ($self, $text) = @_;

    $self->output_comment('<!--foo-->');
    }
}
$f = SubClass_reject_text->new({ Context => 'Document' });
$f->input_start_document;
$f->input_start('<html>');
$f->input_start('<head>');
$f->input_text('bah');
$f->input_end('</head>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<html><head><!--foo--></head><body>foo</body></html>',
                           'subclass reject_text' );

=item reject_decalaration

Overriding reject_decalaration() so that rejected declarations are replaced
with custom text.

=cut

{
    package SubClass_reject_declaration;
    use base qw(HTML::StripScripts);

    sub reject_declaration {
        my ($self, $text) = @_;

    $self->output_declaration('<! FOO >');
    }
}
$f = SubClass_reject_declaration->new;
$f->input_start_document;
$f->input_declaration('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<! FOO >foo', 'subclass reject_decalaration' );

=item reject_comment

Overriding reject_comment() so that rejected HTML comments are replaced
with custom text.

=cut

{
    package SubClass_reject_comment;
    use base qw(HTML::StripScripts);

    sub reject_comment {
        my ($self, $text) = @_;

    $self->output_comment('<!--foo-->');
    }
}
$f = SubClass_reject_comment->new;
$f->input_start_document;
$f->input_text('foo');
$f->input_comment('<!--# exec foo -->');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, 'foo<!--foo-->foo', 'subclass reject_comment' );

=item reject_process

Overriding reject_process() so that rejected processing instructions are
replaced with custom text.

=cut

{
    package SubClass_reject_process;
    use base qw(HTML::StripScripts);

    sub reject_process {
        my ($self, $text) = @_;

    $self->output_process('<? FOO ?>');
    }
}
$f = SubClass_reject_process->new;
$f->input_start_document;
$f->input_process('<?xml version="1.0" encoding="iso-8859-1"?>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<? FOO ?>foo', 'subclass reject_process' );

=item init_context_whitelist

Overriding init_context_whitelist() so that the filter will allow C<ins>
tags only at C<Flow> level.


=cut

{
    package SubClass_init_context_whitelist;
    use base qw(HTML::StripScripts);

    use vars qw(%_Context);
    %_Context = %{ __PACKAGE__->SUPER::init_context_whitelist };

    foreach my $ctx (keys %_Context) {
        next if $ctx eq 'Flow';
        next unless exists $_Context{$ctx}{'ins'};

        # Found a context other than 'Flow' that allows ins tags.  Take
        # a deeper copy of this part of the context hash before deleting
        # 'ins' from it, to avoid messing with the readonly structure we
        # were passed.
        $_Context{$ctx} = { %{ $_Context{$ctx} } };
        delete $_Context{$ctx}{'ins'};
    }

    sub init_context_whitelist {
        my ($self) = @_;

    return \%_Context;
    }
}
$f = SubClass_init_context_whitelist->new;
$f->input_start_document;
$f->input_start('<ins>');
$f->input_text('foo');
$f->input_end('</ins>');
$f->input_start('<i>');
$f->input_start('<ins>');
$f->input_text('foo');
$f->input_end('</ins>');
$f->input_end_document;
is( $f->filtered_document, '<ins>foo</ins><i></i><ins>foo</ins>', 'subclass init_context_whitelist' );

$f = HTML::StripScripts->new;
$f->input_start_document;
$f->input_start('<ins>');
$f->input_text('foo');
$f->input_end('</ins>');
$f->input_start('<i>');
$f->input_start('<ins>');
$f->input_text('foo');
$f->input_end('</ins>');
$f->input_end_document;
is( $f->filtered_document, '<ins>foo</ins><i><ins>foo</ins></i>',
                           "subclass init_context_whitelist didn't break superclass" );

=item init_attrib_whitelist

Overriding init_attrib_whitelist() so that the filter will not allow C<br>
tags to have the C<clear> attribute.

=cut

{
    package SubClass_init_attrib_whitelist;
    use base qw(HTML::StripScripts);

    use vars qw(%_Attrib);
    %_Attrib = %{ __PACKAGE__->SUPER::init_attrib_whitelist };
    $_Attrib{'br'} = { %{ $_Attrib{'br'} } };
    delete $_Attrib{'br'}{'clear'};

    sub init_attrib_whitelist {
        my ($self) = @_;

        return \%_Attrib;
    }
}
$f = SubClass_init_attrib_whitelist->new;
$f->input_start_document;
$f->input_start('<br clear="left">');
$f->input_end_document;
is( $f->filtered_document, '<br />', 'subclass init_attrib_whitelist' );

$f = HTML::StripScripts->new;
$f->input_start_document;
$f->input_start('<br clear="left">');
$f->input_end_document;
is( $f->filtered_document, '<br clear="left" />', "subclass init_attrib_whitelist didn't break superclass" );

=item init_attval_whitelist

Overriding init_attval_whitelist() so that the color value C<pink> is replaced
with C<blue>.

=cut

{
    package SubClass_init_attval_whitelist;
    use base qw(HTML::StripScripts);

    use vars qw(%_AttVal);
    %_AttVal = %{ __PACKAGE__->SUPER::init_attval_whitelist };
    my $super = $_AttVal{'color'};
    $_AttVal{'color'} = sub {
        my ($filter, $tag, $attname, $attval) = @_;
    $attval =~ s/pink/blue/i;
    &{ $super }($filter, $tag, $attname, $attval);
    };

    sub init_attval_whitelist {
        my ($self) = @_;

        return \%_AttVal;
    }
}
$f = SubClass_init_attval_whitelist->new;
$f->input_start_document;
$f->input_start('<font color=pink>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<font color="blue">foo</font>', 'subclass init_attval_whitelist' );

$f->input_start_document;
$f->input_start('<font color="&#112;&#x69;&#0110;&#X00006B;">');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<font color="blue">foo</font>', 'subclass init_attval_whitelist deobfuscate pink' );

$f = HTML::StripScripts->new;
$f->input_start_document;
$f->input_start('<font color=pink>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<font color="pink">foo</font>', "subclass init_attval_whitelist didn't break superclass" );

=item init_style_whitelist

Overriding init_style_whitelist() so that the C<background-color> style
attribute is not allowed.

=cut

{
    package SubClass_init_style_whitelist;
    use base qw(HTML::StripScripts);

    use vars qw(%_Style);
    %_Style = %{ __PACKAGE__->SUPER::init_style_whitelist };
    delete $_Style{'background-color'};

    sub init_style_whitelist {
        my ($self) = @_;

        return \%_Style;
    }
}
$f = SubClass_init_style_whitelist->new;
$f->input_start_document;
$f->input_start('<span style="background-color: #ffffff; color: pink">');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<span style="color:pink">foo</span>', 'subclass init_style_whitelist' );

$f = HTML::StripScripts->new;
$f->input_start_document;
$f->input_start('<span style="background-color: #ffffff; color: pink">');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<span style="background-color:#ffffff; color:pink">foo</span>',
                           "subclass init_style_whitelist didn't break superclass" );

=item init_deinter_whitelist

Overriding init_deinter_whitelist() so that the C<font> tag will not be
autodeinterleaved.

=cut

{
    package SubClass_init_deinter_whitelist;
    use base qw(HTML::StripScripts);

    use vars qw(%_DeInter);
    %_DeInter = %{ __PACKAGE__->SUPER::init_deinter_whitelist };
    delete $_DeInter{'font'};

    sub init_deinter_whitelist {
        my ($self) = @_;

        return \%_DeInter;
    }
}
$f = SubClass_init_deinter_whitelist->new;
$f->input_start_document;
$f->input_start('<i>');
$f->input_text('foo');
$f->input_start('<font size=4>');
$f->input_text('bar');
$f->input_end('</i>');
$f->input_text('baz');
$f->input_end_document;
is( $f->filtered_document, '<i>foo<font size="4">bar</font></i>baz', 'subclass init_deinter_whitelist' );

$f = HTML::StripScripts->new;
$f->input_start_document;
$f->input_start('<i>');
$f->input_text('foo');
$f->input_start('<font size=4>');
$f->input_text('bar');
$f->input_end('</i>');
$f->input_text('baz');
$f->input_end_document;
is( $f->filtered_document, '<i>foo<font size="4">bar</font></i><font size="4">baz</font>',
                           "subclass init_style_whitelist didn't break superclass" );

=item validate_href_attribute

Overriding validate_href_attribute() so that relative as well as absolute
links will be accepted.

=cut

{
    package SubClass_validate_href_attribute;
    use base qw(HTML::StripScripts);

    sub validate_href_attribute {
        my ($self, $text) = @_;

        $text =~ m#^([\w\./\-]{2,100})$# ? $1 :
    $self->SUPER::validate_href_attribute($text);
    }
}
$f = SubClass_validate_href_attribute->new({ AllowHref => 1 });
$f->input_start_document;
$f->input_start('<a href="/foo.htm">');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<a href="/foo.htm">foo</a>', 'subclass validate_href_attribute' );

=item filter_text

Overriding filter_text() to convert text to upper case

=cut

{
    package SubClass_filter_text;
    use base qw(HTML::StripScripts);

    sub filter_text {
        my ($self, $text) = @_;

    return uc $text;
    }
}
$f = SubClass_filter_text->new;
$f->input_start_document;
$f->input_start('<font color=Red>');
$f->input_text('foo');
$f->input_end_document;
is( $f->filtered_document, '<font color="Red">FOO</font>', 'subclass filter_text' );

=item escape_html_metachars

Overriding escape_html_metachars() to convert escape more aggressively

=cut

{
    package SubClass_escape_html_metachars;
    use base qw(HTML::StripScripts);

    sub escape_html_metachars {
        my ($self, $text) = @_;

    $text =~ s|([<>"'&\200-\377])| sprintf '&#%d;', ord $1 |ge;
    return $text;
    }
}
$f = SubClass_escape_html_metachars->new;
$f->input_start_document;
$f->input_start(qq{<img alt="<foo \xff>" />});
$f->input_end_document;
is( $f->filtered_document, '<img alt="&#60;foo &#255;&#62;" />', 'subclass escape_html_metachars' );

=item strip_nonprintable

Overriding strip_nonprintable() to strip more aggressively

=back

=cut

{
    package SubClass_strip_nonprintable;
    use base qw(HTML::StripScripts);

    sub strip_nonprintable {
        my ($self, $text) = @_;

    $text =~ tr#\000-\007# #s;
    return $text;
    }
}
$f = SubClass_strip_nonprintable->new;
$f->input_start_document;
$f->input_start(qq{<img alt="<foo \xff\x01\x05>" />});
$f->input_end_document;
is( $f->filtered_document, qq{<img alt="&lt;foo \xff &gt;" />}, 'subclass escape_html_metachars' );