<(common.inc.plp)><:
use 5.010;
use utf8;
-use List::Util qw(sum max);
+use List::Util qw(sum max first);
Html({
title => 'browser compatibility cheat sheet',
version => 'v1.0',
description =>
"caniuse.",
- keywords => [qw'html css browser feature'],
+ keywords => [qw'
+ web browser support compatibility usage available feature
+ html html5 css css3 svg javascript js dom mobile
+ ie internet explorer firefox chrome safari webkit opera
+ '],
stylesheet => [qw'circus dark mono red light'],
data => ['browser-support.inc.pl'],
});
-:>
-<h1>Browser compatibility</h1>
-
-<p id="intro">Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page<:
-my ($canihas, $usage);
-given ($get{usage} // 'wm') {
- when (!$_) {
- # none
- }
- when (!/^\w+$/) {
- printf "<p>Invalid browser usage data request: <em>%s</em>",
- 'identifier must be alphanumeric name or <q>0</q>';
- }
- $canihas = do "browser-usage-$_.inc.pl" or do {
- printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
- break;
- };
- $usage = $_;
- my $ref = $canihas->{-source} || 'unknown';
- $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
- $ref .= " $_" for $canihas->{-date} || ();
- print "\nwith $ref browser usage statistics";
-}
-:>.
-</p>
+say "<h1>Browser compatibility</h1>\n";
-<:
my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
$_->{verrelease} = {
# mark last two (future) versions as unreleased, ensure current isn't
'p p' => 'l2',
'u' => 'l0',
);
+my %DSTATS = (
+ u => 'unknown',
+ n => 'unsupported',
+ p => 'plugin required',
+ j => 'javascript required',
+ a => 'partial',
+ y => 'supported',
+ x => sub {
+ join(' ',
+ 'requires prefix',
+ (map "-$_-", $caniuse->{agents}->{$_[0]}->{prefix} // ()),
+ );
+ },
+);
my %CSTATUS = (
unoff => 'l1', # unofficial
wd => 'l3', # draft
my @browsers = grep { $versions{$_} }
qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
+print <<'';
+<p id="intro">Alternate rendition of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
+
+my ($canihas, $usage);
+given ($get{usage} // 'wm') {
+ when (!$_) {
+ # none
+ }
+ when (!/^\w+$/) {
+ printf "<p>Invalid browser usage data request: <em>%s</em>",
+ 'identifier must be alphanumeric name or <q>0</q>';
+ }
+ $canihas = do "browser-usage-$_.inc.pl" or do {
+ printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
+ break;
+ };
+ $usage = $_;
+ my $ref = $canihas->{-source} || 'unknown';
+ $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
+ $ref .= " $_" for $canihas->{-date} || ();
+ print "\nwith $ref browser usage statistics";
+}
+if ($usage) {
+ # first() does not work inside given >:(
+ while (my ($browser, $row) = each %$canihas) {
+ my $verlist = $versions{$browser} or next;
+ my %supported = map { $_ => 1 } @$verlist;
+ $row->{$_} //= undef for @$verlist; # ensure stable keys during iteration
+ while (my ($version, $usage) = each %$row) {
+ next if defined $supported{$version};
+ my $next = first { paddedver($_) ge paddedver($version) } @$verlist
+ or warn("No fallback found for $browser v$version; $usage% ignored"), next;
+ $row->{$next} += $usage;
+ $row->{$version} = 0; # balance browser total
+ }
+ }
+}
+:>.
+</p>
+
+<:
$canihas ||= {
map {
$_ => +{
}
} @browsers
}; # fallback hash based on release semantics
-my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
+my $usagemax = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
+
+my $usagepct = 1; # score multiplier for 0..100 result
+# normalise usage percentage to only include shown browsers
+$usagepct = 100 / featurescore({ # yes for every possible version
+ map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
+});
print '<table class="mapped">';
-print '<col>' x 3;
+print '<col span="3">'; # should match first thead row
printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
-print "\n";
+say '</colgroup><col>';
my $header = join('',
'<tr>',
'<th colspan="3">feature',
(map {
my $name = $caniuse->{agents}->{$_}->{browser};
- sprintf('<th colspan="%d" title="%s">%s',
+ sprintf('<th colspan="%d" class="%s" title="%s">%s',
scalar @{ $versions{$_} },
+ join(' ', map {"b-a-$_"} grep {$_}
+ $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
+ ),
join(' ',
- sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
+ sprintf('%.1f%%', sum(values %{ $canihas->{$_} }) * $usagepct),
$name,
),
do {
print "\n<tr>";
print '<td>' x 3;
for my $browser (@browsers) {
- printf('<td title="%.1f%%"%s>%s',
- $canihas->{$browser}->{$_},
- (map {
- defined $_ && !$_ && ' class="ex"'
- } $caniuse->{agents}->{$browser}->{verrelease}->{$_}),
- showversions($_),
- ) for @{ $versions{$browser} };
+ for my $_ (@{ $versions{$browser} }) {
+ my $release = $caniuse->{agents}->{$browser}->{verrelease}->{$_};
+ my $future = defined $release;
+ printf('<td title="%s"%s>%s',
+ join(' ',
+ sprintf('%.1f%%', $canihas->{$browser}->{$_} * $usagepct),
+ $future ? 'development' : (),
+ "version $_",
+ ),
+ $future && ' class="ex"',
+ showversions($_),
+ );
+ }
}
print '<td>' x 1;
say '</thead>';
sub featurescore {
# relative amount of support for given feature
- state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, 'p p'=>2, p=>1 };
+ state $statspts = { y=>1, 'y x'=>1, a=>.5, 'a x'=>.5, j=>.2, 'p j'=>.2, 'p p'=>.2, p=>.1 };
my $rank = 0;
if (my $row = shift) {
if ($canihas) {
for ($row->{categories}) {
my $cell = $_ ? lc $_->[0] : '-';
+ $cell =~ s/ api$//; # trim unessential fluff in 'js api'
printf '<th title="%s">%s', join(' + ', @$_), $cell;
}
unless (!defined $prev
or ref $data eq 'HASH' && $data->{$prev} ~~ $data->{$ver}) {
my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
- printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
+ printf '<td class="%s" colspan="%d" title="%s">%s',
join(' ',
X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
!$usage ? ('p0') : ('p',
- sprintf('p%01d', $usage / 10),
- sprintf('p%02d', $usage),
+ sprintf('p%01d', $usage * ($usagepct - .0001) / 10),
+ sprintf('p%02d', $usage * ($usagepct - .0001)),
),
- sprintf('pp%02d', $usage / $scorediv),
+ sprintf('pp%02d', $usage / $usagemax),
),
scalar @span,
- $usage,
+ sprintf('%.1f%% %s', $usage * $usagepct, join(', ',
+ map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
+ map { $DSTATS{$_} // () }
+ map { split / /, $_ }
+ ref $data eq 'HASH' && $data->{$prev} || 'u'
+ )),
showversions(@span),
undef $prev;
@span = ();
sub sayusagecol {
my ($id) = @_;
- state $maxscore = featurescore({ # yes for every possible version
- map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
- });
- print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
+ print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) * $usagepct;
}
say '<tbody>';
sub paddedver {
# normalised version number comparable as string (cmp)
- shift =~ /^(\d*)(.*)/;
+ shift =~ /(?:.*-|^)(\d*)(.*)/;
+ # matched (major)(.minor) of last value in range (a-B)
return sprintf('%02d', $1 || 0) . $2;
}
sub showversions {
my @span = ($_[0], @_>1 ? $_[-1] : ());
+ s/-.*// for $span[0];
for (@span) {
s/^\./0./;
s/x$/.*/;
+ s/.*-//;
}
return join('‒', @span);
}