Changeset 13739 for lang

Show
Ignore:
Timestamp:
06/12/08 23:27:40 (5 years ago)
Author:
tokuhirom
Message:

added namespace hack. thanks to chiba++

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/mobirc/trunk/lib/App/Mobirc/Plugin/HTMLFilter/DoCoMoCSS.pm

    r13049 r13739  
    44use CSS::Tiny; 
    55use XML::LibXML; 
    6 use HTML::Selector::XPath; 
     6use HTML::Selector::XPath qw(selector_to_xpath); 
    77use App::Mobirc::Util; 
    88use Encode; 
    99use Path::Class; 
     10use XML::LibXML::XPathContext; 
    1011 
    1112# some code copied from HTML::DoCoMoCSS 
     
    1415 
    1516    DEBUG "FILTER DOCOMO CSS"; 
    16     return ($c, $content) unless $c->req->mobile_agent->is_docomo; 
     17    # return ($c, $content) unless $c->req->mobile_agent->is_docomo; 
    1718 
    1819    # escape Numeric character reference. 
     
    2324    my $css = CSS::Tiny->read_string($self->css_text($global_context)); 
    2425    my $doc = eval { XML::LibXML->new->parse_string($content); }; 
    25     $@ and return ($c, $pict_unescape->()); 
     26    if (my $e = $@) { 
     27        return ($c, $pict_unescape->()); 
     28    } 
     29    my $xc               = XML::LibXML::XPathContext->new($doc); 
     30    my $root             = $doc->documentElement(); 
     31    my $namespace        = $root->getAttribute('xmlns'); 
     32    my $namespace_prefix = ''; 
     33    if ($namespace) { 
     34        # xhtml 
     35        $xc->registerNs( 'x', $namespace ); 
     36        $namespace_prefix = 'x:'; 
     37    } 
    2638 
    2739    # apply inline css 
    2840    while (my($selector, $style) = each %{ $css }) { 
    2941        my $style_stringify = join ';', map { "$_:$style->{$_}" } keys %{ $style }; 
    30         for my $element ( $doc->findnodes( HTML::Selector::XPath::selector_to_xpath($selector) ) ) { 
     42        my $xpath = selector_to_xpath($selector); 
     43        $xpath =~ s{^//}{//$namespace_prefix}; 
     44        for my $element ( $xc->findnodes( $xpath ) ) { 
    3145            my $style_attr = $element->getAttribute('style'); 
    3246            $style_attr = (!$style_attr) ? $style_stringify : (join ";", ($style_attr, $style_stringify));