[Bps-public-commit] r11992 - in HTML-RewriteAttributes: . lib/HTML/RewriteAttributes

sartak at bestpractical.com sartak at bestpractical.com
Thu May 1 13:00:05 EDT 2008


Author: sartak
Date: Thu May  1 13:00:05 2008
New Revision: 11992

Modified:
   HTML-RewriteAttributes/   (props changed)
   HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm
   HTML-RewriteAttributes/t/021-import.t

Log:
 r54725 at onn:  sartak | 2008-05-01 12:59:19 -0400
 Better handling of relative CSS file @import


Modified: HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm
==============================================================================
--- HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm	(original)
+++ HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm	Thu May  1 13:00:05 2008
@@ -3,6 +3,7 @@
 use strict;
 use warnings;
 use base 'HTML::RewriteAttributes';
+use URI;
 
 our $VERSION = '0.01';
 
@@ -51,7 +52,7 @@
         if ($tag eq 'link' && $attr->{type} eq 'text/css') {
             my $content = $self->_import($attr->{href});
             if (defined $content) {
-                $content = $self->_handle_imports($content);
+                $content = $self->_handle_imports($content, $attr->{href});
                 $self->{rewrite_html} .= "\n<style type=\"text/css\">\n<!--\n$content\n-->\n</style>\n";
                 return;
             }
@@ -67,7 +68,7 @@
 sub _default {
     my ($self, $tag, $attrs, $text) = @_;
     if (delete $self->{rewrite_look_for_style}) {
-        $text = $self->_handle_imports($text);
+        $text = $self->_handle_imports($text, '.');
     }
 
     $self->SUPER::_default($tag, $attrs, $text);
@@ -76,21 +77,40 @@
 sub _handle_imports {
     my $self    = shift;
     my $content = shift;
+    my $base    = shift;
+
     return $content if !$self->{rewrite_inline_imports};
 
-    # repeat until we get no substitutions
-    1 while $content =~ s{\@import\s*"([^"]+)"\s*;}{ $self->_import($1) }eg;
+    $content =~ s{
+        \@import \s* " ([^"]+) " \s* ;
+    }{
+        $self->_import($self->_absolutify($1, $base))
+    }xeg;
 
     return $content;
 }
 
+sub _absolutify {
+    my $self = shift;
+    my $path = shift;
+    my $base = shift;
+
+    my $uri = URI->new($path);
+    unless (defined $uri->scheme) {
+        $uri = $uri->abs($base);
+    }
+
+    return $uri->as_string;
+}
+
 sub _import {
     my $self = shift;
-    my $uri  = shift;
+    my $path = shift;
 
-    return '' if $self->{rewrite_inline_imports_seen}{$uri}++;
+    return '' if $self->{rewrite_inline_imports_seen}{$path}++;
 
-    return $self->{rewrite_inline_css_cb}->($uri);
+    my $content = $self->{rewrite_inline_css_cb}->($path);
+    return $self->_handle_imports($content, $path);
 }
 
 1;

Modified: HTML-RewriteAttributes/t/021-import.t
==============================================================================
--- HTML-RewriteAttributes/t/021-import.t	(original)
+++ HTML-RewriteAttributes/t/021-import.t	Thu May  1 13:00:05 2008
@@ -7,11 +7,11 @@
 my $html = << 'END';
 <html>
     <head>
-        <link type="text/css" href="foo.css" />
+        <link type="text/css" href="/foo.css" />
         <style type="text/css">
 @import "bar.css";
         </style>
-        <link type="text/css" href="baz.css" />
+        <link type="text/css" href="/baz.css" />
     </head>
     <body>
     </body>
@@ -19,10 +19,10 @@
 END
 
 my %css = (
-    "foo.css"  => 'foo; @import "quux.css";',
-    "bar.css"  => 'bar; @import "quux.css";',
-    "baz.css"  => 'baz; @import "foo.css";',
-    "quux.css" => 'quux; @import "bar.css"; @import "quux.css";',
+    "/foo.css"  => 'foo; @import "quux.css";',
+    "/bar.css"  => 'bar; @import "quux.css";',
+    "/baz.css"  => 'baz; @import "foo.css";',
+    "/quux.css" => 'quux; @import "bar.css"; @import "quux.css";',
 );
 
 my @seen;
@@ -40,10 +40,10 @@
 
 is(@seen, 0, "no ordinary resources seen");
 is_deeply(\@seen_css, [
-    "foo.css",
-    "quux.css",
-    "bar.css",
-    "baz.css",
+    "/foo.css",
+    "/quux.css",
+    "/bar.css",
+    "/baz.css",
 ]);
 
 $rewrote =~ s/ +$//mg;



More information about the Bps-public-commit mailing list