Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcusramberg/fb6638b4650332597b15d82a08a2c89e to your computer and use it in GitHub Desktop.
Save marcusramberg/fb6638b4650332597b15d82a08a2c89e to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious/Plugin/AssetPack/Pipe/Favicon.pm b/lib/Mojolicious/Plugin/AssetPack/Pipe/Favicon.pm
index 342d325..c6a97db 100644
--- a/lib/Mojolicious/Plugin/AssetPack/Pipe/Favicon.pm
+++ b/lib/Mojolicious/Plugin/AssetPack/Pipe/Favicon.pm
@@ -11,7 +11,7 @@ has api_key => sub { die 'api_key() must be set' };
has design => sub { +{desktop_browser => {}} };
has settings => sub { +{error_on_image_too_small => Mojo::JSON->true} };
-has _icons => sub { +{} };
+has _assets => sub { +{} };
sub process {
my ($self, $assets) = @_;
@@ -20,7 +20,7 @@ sub process {
my $store = $self->assetpack->store;
my $asset = $assets->first;
my $attrs = $asset->TO_JSON;
- my ($db, $files, $markup, @icons);
+ my ($db, $files, $markup, @assets);
$attrs->{key} = join '-', sort keys %{$self->design}
or die '[AssetPack] Invalid pipe("Favicon")->design({})';
@@ -36,20 +36,20 @@ sub process {
}
for my $url (@$files) {
- push @icons, $store->asset($url)
+ push @assets, $store->asset($url)
or die "AssetPack was unable to fetch icon asset $url";
}
- $self->assetpack->{by_checksum}{$_->checksum} = $_ for @icons;
- $self->assetpack->{by_topic}{$self->topic} = Mojo::Collection->new(@icons);
+ $self->assetpack->{by_checksum}{$_->checksum} = $_ for @assets;
+ $self->assetpack->{by_topic}{$self->topic} = Mojo::Collection->new(@assets);
for my $child (Mojo::DOM->new($markup)->children->each) {
my $key = $child->{content} ? 'content' : 'href';
- my $icon = shift @icons;
+ my $asset= shift @asset;
$self->_icons->{$icon->url} = [$key => $child, $icon];
}
- if (@icons) {
+ if (@assets) {
my $child
= Mojo::DOM->new('<link rel="shortcut icon" href="favicon.ico">')->children->first;
my $icon = shift @icons;
@@ -59,9 +59,9 @@ sub process {
sub render {
my ($self, $c) = @_;
- my $icons = $self->_icons;
- $icons = [map { $icons->{$_} } sort keys %$icons];
- $_->[1]{$_->[0]} = $_->[2]->url_for($c) for @$icons;
+ my $assets = $self->_assets;
+ $assets = [map { $assets->{$_} } sort keys %$assets];
+ $_->[1]{$_->[0]} = $_->[2]->url_for($c) for @$assets;
return Mojo::ByteStream->new(join "\n", map { $_->[1] } @$icons);
}
diff --git a/t/favicon.t b/t/favicon.t
index 51b9e73..b781f1e 100644
--- a/t/favicon.t
+++ b/t/favicon.t
@@ -4,15 +4,65 @@ use t::Helper;
plan skip_all => 'TEST_REALFAVICONGENERATOR_API_KEY=is_not_set'
unless $ENV{TEST_REALFAVICONGENERATOR_API_KEY};
-my $t = t::Helper->t(pipes => [qw(Favicon)]);
-$t->app->asset->pipe('Favicon')->api_key($ENV{TEST_REALFAVICONGENERATOR_API_KEY});
+{
+ my $t = t::Helper->t(pipes => [qw(Favicon)]);
+ $t->app->asset->pipe('Favicon')->api_key($ENV{TEST_REALFAVICONGENERATOR_API_KEY});
+ $t->app->asset->process('favicon.ico' => '/image/master_favicon_thumbnail.png');
+ $t->get_ok('/')->status_is(200)->element_exists('[sizes="16x16"]')
+ ->element_exists('[sizes="32x32"]');
+
+ like $t->tx->res->dom->at('[rel="shortcut icon"]')->{href}, qr{/favicon\.ico$},
+ 'plain favicon';
+ $t->get_ok($t->tx->res->dom->at('[sizes="16x16"]')->{href})->status_is(200);
+};
+{
+ my $t = t::Helper->t(pipes => [qw(Favicon)]);
+$t->app->asset->pipe('Favicon')->api_key($ENV{TEST_REALFAVICONGENERATOR_API_KEY})
+ ->design(
+ {
+ desktop_browser => {},
+ android_chrome => {
+ picture_aspect => 'shadow',
+ manifest => {name => 'Convos', display => 'standalone', orientation => 'portrait'},
+ theme_color => '#00451D'
+ },
+ firefox_app => {
+ picture_aspect => 'circle',
+ keep_picture_in_circle => 'true',
+ circle_inner_margin => '5',
+ background_color => '#ffffff',
+ manifest => {
+ app_name => 'Convos',
+ app_description => 'A better way to IRC',
+ developer_name => 'Nordaaker',
+ developer_url => 'http://nordaaker.com',
+ }
+ },
+ ios => {
+ picture_aspect => 'background_and_margin',
+ margin => '4',
+ background_color => '#ffffff'
+ },
+ safari_pinned_tab =>
+ {picture_aspect => 'black_and_white', threshold => 60, theme_color => '#00451D'},
+ windows => {
+ picture_aspect => "white_silhouette",
+ background_color => "#00451D",
+ assets => {
+ windows_80_ie_10_tile => \1,
+ windows_10_ie_11_edge_tiles =>
+ {small => \0, medium => \1, big => \1, rectangle => \0}
+ }
+ },
+ }
+ );
+ $t->get_ok('/')->status_is(200)->element_exists('[sizes="76x76"]')
+ ->element_exists('[sizes="120x120"]');
+ diag "ima pony ama";
+ diag explain note $t;
$t->app->asset->process('favicon.ico' => '/image/master_favicon_thumbnail.png');
-$t->get_ok('/')->status_is(200)->element_exists('[sizes="16x16"]')
- ->element_exists('[sizes="32x32"]');
+}
-like $t->tx->res->dom->at('[rel="shortcut icon"]')->{href}, qr{/favicon\.ico$},
- 'plain favicon';
-$t->get_ok($t->tx->res->dom->at('[sizes="16x16"]')->{href})->status_is(200);
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment