Skip to content

Commit

Permalink
Objects as string
Browse files Browse the repository at this point in the history
  • Loading branch information
rshadow committed Dec 1, 2016
1 parent 665e925 commit 667a1ea
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Mojolicious-Plugin-UniqueTagHelpers

1.2 01-12-2016
Objects as string. Fix Mojo::URL.

1.1 03-11-2016
CPU and debug optimization: do not use md5 for small content

Expand Down
2 changes: 2 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ README.md
t/00-load.t
t/10-stylesheet.t
t/11-stylesheet-block.t
t/12-stylesheet-mojo-url.t
t/20-javascript.t
t/21-javascript-block.t
t/22-javascript-mojo-url.t
t/30-unique.t
t/31-unique-block.t
t/32-unique-long-key.t
Expand Down
14 changes: 9 additions & 5 deletions lib/Mojolicious/Plugin/UniqueTagHelpers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package Mojolicious::Plugin::UniqueTagHelpers;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Util 'md5_sum';

our $VERSION = '1.1';
our $VERSION = '1.2';

sub _block { ref $_[0] eq 'CODE' ? $_[0]() : $_[0] }
sub _block {
ref $_[0] eq 'CODE' ? $_[0]() :
defined $_[0] ? "$_[0]" :
''
}

sub register {
my ($self, $app, $conf) = @_;
Expand All @@ -20,7 +24,7 @@ sub register {
if( defined $content ) {
$hash->{$name} ||= {};
my $key = _block($content);
$key = md5_sum( $key // '' )
$key = md5_sum( $key )
if $conf->{max_key_length} < length $key;

return $c->content( $name ) if exists $hash->{$name}{$key};
Expand All @@ -39,7 +43,7 @@ sub register {
if( defined $content ) {
$hash->{$name} ||= {};
my $key = _block($content);
$key = md5_sum( $key // '' )
$key = md5_sum( $key )
if $conf->{max_key_length} < length $key;

return $c->content( $name ) if exists $hash->{$name}{$key};
Expand All @@ -58,7 +62,7 @@ sub register {
if( defined $content ) {
$hash->{$name} ||= {};
my $key = _block($content);
$key = md5_sum( $key // '' )
$key = md5_sum( $key )
if $conf->{max_key_length} < length $key;

return $c->content( $name ) if exists $hash->{$name}{$key};
Expand Down
52 changes: 52 additions & 0 deletions t/12-stylesheet-mojo-url.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use Mojo::Base -strict;
use lib qw(lib);

use Test::More tests => 6;
use Mojolicious::Lite;
use Test::Mojo;

plugin 'UniqueTagHelpers';

my $t = Test::Mojo->new;

note 'Unique Mojo::URL';

get '/test' => {template => 'test'};
$t ->get_ok('/test')
->status_is(200)
->element_exists('html > head > link:nth-of-type(1)[href="css/head.css"]')
->element_exists_not('html > head > link:nth-of-type(2)[href="css/head.css"]')
->element_exists('html > body > footer > link:nth-of-type(1)[href="css/foot.css"]')
->element_exists_not('html > body > footer > link:nth-of-type(2)[href="css/foot.css"]')
;

#diag $t->tx->res->body;

__DATA__
@@ test.html.ep
% layout 'default';
% stylesheet_for 'header' => url_for 'css/head.css';
% stylesheet_for 'header' => url_for 'css/head.css';
% stylesheet_for 'header' => url_for 'css/head.css';
% stylesheet_for 'footer' => url_for 'css/foot.css';
% stylesheet_for 'footer' => url_for 'css/foot.css';
% stylesheet_for 'footer' => url_for 'css/foot.css';
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head>
<title>LinkedUrl</title>
%= stylesheet_for 'header';
</head>
<body>
<%= content %>
<footer>
%= stylesheet_for 'footer';
</footer>
</body>
</html
53 changes: 53 additions & 0 deletions t/22-javascript-mojo-url.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use Mojo::Base -strict;
use lib qw(lib);

use Test::More tests => 6;
use Mojolicious::Lite;
use Test::Mojo;

plugin 'UniqueTagHelpers';

my $t = Test::Mojo->new;

note 'Unique Mojo::URL';

get '/test' => {template => 'test'};
$t ->get_ok('/test')
->status_is(200)
->element_exists('html > head > script:nth-of-type(1)[src="js/head.js"]')
->element_exists_not('html > head > script:nth-of-type(2)[src="js/head.js"]')
->element_exists('html > body > footer > script:nth-of-type(1)[src="js/foot.js"]')
->element_exists_not('html > body > footer > script:nth-of-type(2)[src="js/foot.js"]')
;

#diag $t->tx->res->body;

__DATA__
@@ test.html.ep
% layout 'default';
% javascript_for 'header' => url_for 'js/head.js';
% javascript_for 'header' => url_for 'js/head.js';
% javascript_for 'header' => url_for 'js/head.js';
% javascript_for 'footer' => url_for 'js/foot.js';
% javascript_for 'footer' => url_for 'js/foot.js';
% javascript_for 'footer' => url_for 'js/foot.js';
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head>
<title>LinkedUrl</title>
%= javascript_for 'header';
</head>
<body>
<%= content %>
<footer>
%= javascript_for 'footer';
</footer>
</body>
</html

0 comments on commit 667a1ea

Please sign in to comment.