File: replace_with.t

package info (click to toggle)
libweb-query-perl 1.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: perl: 984; xml: 329; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 1,175 bytes parent folder | download
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
use strict;
use warnings;
use Test2::V0;

my @modules = qw/ Web::Query Web::Query::LibXML /;

plan tests => scalar @modules;

subtest $_ => sub { test($_) } for @modules;

sub test {
    my $class = shift;

    eval "require $class; 1" 
        or plan skip_all => "couldn't load $class";

    no warnings 'redefine';
    *wq = \&{$class . "::wq" };
    
    my $html = '<p><b>Hi</b><i>there</i><u>world</u></p>';

    is wq($html)->find('b')->replace_with('<strong>Hello</strong>')->end->as_html
        => '<p><strong>Hello</strong><i>there</i><u>world</u></p>';
    
    my $q = wq( $html );
    
    is $q->find('u')->replace_with($q->find('b'))->end->as_html
        => '<p><i>there</i><b>Hi</b></p>';
    
    is wq($html)->find('*')->replace_with(sub {
        my $i = $_->text;
        return "<$i></$i>";
    } )->end->as_html => '<p><hi></hi><there></there><world></world></p>';
    
    is wq($html)->find('*')->replace_with( '<blink />' )->end->as_html
        => '<p><blink></blink><blink></blink><blink></blink></p>';    

    is wq('<p><span>foo</span></p>')->find('span')
        ->replace_with(sub { $_->contents })
        ->end->as_html => '<p>foo</p>';
}