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

sartak at bestpractical.com sartak at bestpractical.com
Tue Apr 29 17:48:44 EDT 2008


Author: sartak
Date: Tue Apr 29 17:48:43 2008
New Revision: 11961

Added:
   HTML-RewriteAttributes/t/020-inline-css.t
Modified:
   HTML-RewriteAttributes/   (props changed)
   HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm

Log:
 r54665 at onn:  sartak | 2008-04-29 17:48:39 -0400
 Start working out inlining CSS


Modified: HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm
==============================================================================
--- HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm	(original)
+++ HTML-RewriteAttributes/lib/HTML/RewriteAttributes/Resources.pm	Tue Apr 29 17:48:43 2008
@@ -15,6 +15,19 @@
     tr      => { background => 1 },
 );
 
+sub _rewrite {
+    my $self = shift;
+    my $html = shift;
+    my $cb   = shift;
+    my %args = @_;
+
+    $self->{rewrite_inline_css_cb} = $args{inline_css};
+    $self->{rewrite_inline_imports} = $args{inline_imports};
+    $self->{rewrite_inline_imports_seen} = {};
+
+    $self->SUPER::_rewrite($html, $cb);
+}
+
 sub _should_rewrite {
     my ($self, $tag, $attr) = @_;
 
@@ -28,5 +41,40 @@
     return $self->{rewrite_callback}->($value, tag => $tag, attr => $attr, rewriter => $self);
 }
 
+sub _start_tag {
+    my $self = shift;
+    my ($tag, $attr, $attrseq, $text) = @_;
+
+    if ($self->{rewrite_inline_css_cb}) {
+        if ($tag eq 'link' && $attr->{type} eq 'text/css') {
+            my $content = $self->{rewrite_inline_css_cb}->($attr->{href});
+            if (defined $content) {
+                $content = $self->_handle_imports($content);
+                $self->{rewrite_html} .= "\n<style type=\"text/css\">\n<!--\n$content\n-->\n</style>\n";
+                return;
+            }
+        }
+    }
+
+    $self->SUPER::_start_tag(@_);
+}
+
+sub _default {
+    my ($self, $tag, $attrs, $text) = @_;
+
+    if ($tag && $tag eq 'script' && $attrs->{type} eq 'text/css') {
+        $text = $self->_handle_imports($text);
+    }
+
+    $self->SUPER::_default($tag, $attrs, $text);
+}
+
+sub _handle_imports {
+    my $self    = shift;
+    my $content = shift;
+    return $content if !$self->{rewrite_inline_imports};
+    return $content;
+}
+
 1;
 

Added: HTML-RewriteAttributes/t/020-inline-css.t
==============================================================================
--- (empty file)
+++ HTML-RewriteAttributes/t/020-inline-css.t	Tue Apr 29 17:48:43 2008
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use HTML::RewriteAttributes::Resources;
+use Test::More tests => 3;
+
+my $html = << 'END';
+<html>
+    <head>
+        <link type="text/css" href="foo.css" />
+    </head>
+    <body>
+        <img src="moose.jpg" />
+        <img src="http://example.com/nethack.png">
+        <a href="Example.html">Example</a>
+        <p align="justified" style="color: red">
+            hooray
+        </p>
+    </body>
+</html>
+END
+
+my @seen;
+my @seen_inline;
+
+my $rewrote = HTML::RewriteAttributes::Resources->rewrite($html, sub {
+    my $uri  = shift;
+    my %args = @_;
+
+    push @seen, [$uri, $args{tag}, $args{attr}];
+
+    return uc $uri;
+}, inline_css => sub {
+    my $uri = shift;
+
+    push @seen_inline, $uri;
+
+    "INLINED CSS";
+});
+
+is_deeply(\@seen, [
+    ["moose.jpg" => img => "src"],
+    ["http://example.com/nethack.png" => img => "src"],
+]);
+
+is_deeply(\@seen_inline, [
+    "foo.css",
+]);
+
+is($rewrote, << "END", "rewrote the html correctly");
+<html>
+    <head>
+        
+<style type="text/css">
+<!--
+INLINED CSS
+-->
+</style>
+
+    </head>
+    <body>
+        <img src="MOOSE.JPG" />
+        <img src="HTTP://EXAMPLE.COM/NETHACK.PNG">
+        <a href="Example.html">Example</a>
+        <p align="justified" style="color: red">
+            hooray
+        </p>
+    </body>
+</html>
+END
+



More information about the Bps-public-commit mailing list