[Bps-public-commit] HTML-RewriteAttributes branch, master, updated. 1b68700f650458a776bd373bbd027d53435aea1f

Shawn Moore sartak at bestpractical.com
Thu Nov 18 16:54:32 EST 2010


The branch, master has been updated
       via  1b68700f650458a776bd373bbd027d53435aea1f (commit)
       via  bc893521ff64f1b440a274127fb07d1c6f3178ee (commit)
      from  091f7a66c4e35a43c3c6b86ad17a7048efb4c790 (commit)

Summary of changes:
 lib/HTML/RewriteAttributes/Resources.pm  |   12 +++++-
 t/{021-import.t => 023-import-comment.t} |   58 +++++++++++++++--------------
 2 files changed, 40 insertions(+), 30 deletions(-)
 copy t/{021-import.t => 023-import-comment.t} (58%)

- Log -----------------------------------------------------------------
commit bc893521ff64f1b440a274127fb07d1c6f3178ee
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Thu Nov 18 16:54:01 2010 -0500

    Add test file for /* @import "file.css"; */

diff --git a/t/023-import-comment.t b/t/023-import-comment.t
new file mode 100644
index 0000000..3ecdde2
--- /dev/null
+++ b/t/023-import-comment.t
@@ -0,0 +1,86 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use HTML::RewriteAttributes::Resources;
+use Test::More tests => 4;
+
+my $html = << 'END';
+<html>
+    <head>
+        <link type="text/css" href="/comment.css" />
+    </head>
+    <body>
+    </body>
+</html>
+END
+
+my %css = (
+    "/foo.css" => 'SHOULD NOT BE IMPORTED!',
+    "/bar.css" => 'bar;',
+    "/comment.css" => << 'COMMENT',
+begin;
+// @import "foo.css";
+/*
+    from: @import "foo.css";
+*/
+ at import "bar.css";
+/*
+    from: @import "foo.css";
+*/
+end;
+COMMENT
+);
+
+my @seen;
+my @seen_css;
+
+my $rewrote = HTML::RewriteAttributes::Resources->rewrite($html, sub {
+    my $uri = shift;
+    push @seen, $uri;
+    return $uri;
+}, inline_css => sub {
+    my $uri = shift;
+    push @seen_css, $uri;
+    return $css{$uri};
+}, inline_imports => 1);
+
+is(@seen, 0, "no ordinary resources seen");
+is_deeply(\@seen_css, [
+    "/comment.css",
+    "/bar.css",
+]);
+
+$rewrote =~ s/ +$//mg;
+$rewrote =~ s/^ +//mg;
+
+unlike($rewrote, qr/SHOULD NOT BE IMPORTED!/, 'we did not import foo.css since it is only imported in comments');
+
+is($rewrote, << 'END', "rewrote the html correctly");
+<html>
+<head>
+
+<style type="text/css">
+<!--
+
+/* /comment.css */
+begin;
+// @import "foo.css";
+/*
+from: @import "foo.css";
+*/
+
+/* /bar.css */
+bar;
+/*
+from: @import "foo.css";
+*/
+end;
+
+-->
+</style>
+
+</head>
+<body>
+</body>
+</html>
+END

commit 1b68700f650458a776bd373bbd027d53435aea1f
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Thu Nov 18 16:54:12 2010 -0500

    Fix @import statements found only in comments

diff --git a/lib/HTML/RewriteAttributes/Resources.pm b/lib/HTML/RewriteAttributes/Resources.pm
index 92efe27..7104999 100644
--- a/lib/HTML/RewriteAttributes/Resources.pm
+++ b/lib/HTML/RewriteAttributes/Resources.pm
@@ -81,11 +81,19 @@ sub _handle_imports {
 
     return $content if !$self->{rewrite_inline_imports};
 
+    # here we both try to preserve comments *and* ignore any @import
+    # statements that are in comments
     $content =~ s{
+        ( /\* .*? \*/ )
+        |
+        (//[^\n]*)
+        |
         \@import \s* " ([^"]+) " \s* ;
     }{
-        $self->_import($self->_absolutify($1, $base))
-    }xeg;
+          defined($1) ? $1
+        : defined($2) ? $2
+        : $self->_import($self->_absolutify($3, $base))
+    }xsmeg;
 
     return $content;
 }

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list