4 use List::Util qw(sum max);
7 title => 'browser compatibility cheat sheet',
11 keywords => [qw'html css browser feature'],
12 stylesheet => [qw'circus dark mono red light'],
13 data => ['browser-support.inc.pl'],
17 <h1>Browser compatibility</h1>
19 <p id="intro">Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page<:
20 my ($canihas, $usage);
21 given ($get{usage} // 'wm') {
26 printf "<p>Invalid browser usage data request: <em>%s</em>",
27 'identifier must be alphanumeric name or <q>0</q>';
29 $canihas = do "browser-usage-$_.inc.pl" or do {
30 printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
34 my $ref = $canihas->{-source} || 'unknown';
35 $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $canihas->{-url} || ();
36 $ref .= " $_" for $canihas->{-date} || ();
37 print "\nwith $ref browser usage statistics";
43 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
45 # mark last two (future) versions as unreleased, ensure current isn't
46 map { $_->[-1] => 0, $_->[-2] => 0, $_->[-3] => undef } $_->{versions}
47 } for values %{ $caniuse->{agents} };
64 p => 'plugin required',
65 j => 'javascript required',
71 (map "-$_-", $caniuse->{agents}->{$_[0]}->{prefix} // ()),
76 unoff => 'l1', # unofficial
78 cr => 'l4', # candidate
79 pr => 'l4', # proposed
80 rec => 'l5', # recommendation
81 other => 'l2', # non-w3
82 ietf => 'l5', # standard
85 if (my ($somerow) = values %{ $caniuse->{data} }) {
86 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
87 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
90 my @browsers = grep { $versions{$_} }
91 qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
97 my $zero = $#$_ - 2; # baseline index
98 ($_->[$zero - 2] => .5), # past
99 ($_->[$zero - 1] => 10 ), # previous
100 ($_->[$zero + 2] => 0 ), # future
101 ($_->[$zero + 1] => .5), # next
102 ($_->[$zero ] => 30 ), # current
103 } $caniuse->{agents}->{$_}->{versions}
106 }; # fallback hash based on release semantics
107 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
109 print '<table class="mapped">';
110 print '<col span="3">'; # should match first thead row
111 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
114 my $header = join('',
116 '<th colspan="3">feature',
118 my $name = $caniuse->{agents}->{$_}->{browser};
119 sprintf('<th colspan="%d" class="%s" title="%s">%s',
120 scalar @{ $versions{$_} },
121 join(' ', map {"b-a-$_"} grep {$_}
122 $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
125 sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
129 length $name < 3 + @{ $versions{$_} }*2 ? $name
130 : $caniuse->{agents}->{$_}->{abbr};
136 print '<thead>', $header;
137 # preceding row without any colspan to work around gecko bug
140 for my $browser (@browsers) {
141 printf('<td title="%s"%s>%s',
143 sprintf('%.1f%%', $canihas->{$browser}->{$_}),
147 defined $_ && !$_ && ' class="ex"'
148 } $caniuse->{agents}->{$browser}->{verrelease}->{$_}),
150 ) for @{ $versions{$browser} };
154 say '<tfoot>', $header, '</tfoot>';
157 # relative amount of support for given feature
158 state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, 'p p'=>2, p=>1 };
160 if (my $row = shift) {
162 while (my ($browser, $versions) = each %$row) {
163 ref $versions eq 'HASH' or next;
164 while (my ($version, $_) = each %$versions) {
165 $rank += ($canihas->{$browser}->{$version} || .001) * $statspts->{$_};
171 while (my ($browser, $vercols) = each %versions) {
172 my $div = 0; # multiplier exponent (decreased to lower value)
173 my @vers = map { $row->{$browser}->{$_} } @$vercols;
174 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
175 my @future; # find upcoming releases (after current)
176 for (reverse @$vercols) {
177 last if $_ eq $current;
178 push @future, pop @vers;
179 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
181 splice @vers, -1, 0, @future; # move ahead to decrease precedence
183 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
191 my $row = $caniuse->{data}->{$id};
193 for ($row->{categories}) {
194 my $cell = $_ ? lc $_->[0] : '-';
195 $cell =~ s/ api$//; # trim unessential fluff in 'js api'
196 printf '<th title="%s">%s', join(' + ', @$_), $cell;
200 sprintf('<a href="%s" onclick="%s">%s</a>',
202 sprintf("try { %s; return false } catch(err) { return true }",
203 "document.getElementById('$id').classList.toggle('target')",
208 print '<div class=aside>';
209 s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
210 Entity($row->{description}), formathtml($row->{notes}); # sic
211 printf 'Resources: %s.', join(', ', map {
212 sprintf '<a href="%s">%s</a>', EscapeHTML($_->{url}), $_->{title}
213 } @$_) for grep { @$_ } $row->{links} // ();
219 my $row = $caniuse->{data}->{$id};
221 for ($row->{status}) {
222 my $cell = $_ // '-';
223 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
224 printf '<td title="%s" class="l %s">%s',
225 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
230 my ($id, $browser) = @_;
231 my $data = $caniuse->{data}->{$id}->{stats}->{$browser};
234 for my $ver (@{ $versions{$browser} }, undef) {
235 unless (!defined $prev
236 or ref $data eq 'HASH' && $data->{$prev} ~~ $data->{$ver}) {
237 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
238 printf '<td class="%s" colspan="%d" title="%s">%s',
240 X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
241 !$usage ? ('p0') : ('p',
242 sprintf('p%01d', $usage / 10),
243 sprintf('p%02d', $usage),
245 sprintf('pp%02d', $usage / $scorediv),
248 sprintf('%.1f%% %s', $usage, join(', ',
249 map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
250 map { $DSTATS{$_} // () }
251 map { split / /, $_ }
252 ref $data eq 'HASH' && $data->{$prev} || 'u'
265 state $maxscore = featurescore({ # yes for every possible version
266 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
268 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
273 featurescore($caniuse->{data}->{$b}->{stats})
274 <=> featurescore($caniuse->{data}->{$a}->{stats})
275 } keys %{ $caniuse->{data} }) {
276 $caniuse->{data}->{$id}->{stats} or next; # skip metadata [summary]
277 printf '<tr id="%s">', $id;
280 saybrowsercols($id, $_) for @browsers;
288 my $ref = defined wantarray ? [@_] : \@_;
298 # normalised version number comparable as string (cmp)
299 shift =~ /^(\d*)(.*)/;
300 return sprintf('%02d', $1 || 0) . $2;
304 my @span = ($_[0], @_>1 ? $_[-1] : ());
309 return join('‒', @span);
316 <table class="glyphs"><tr>
317 <td class="X l5">supported
318 <td class="X l3">partial
319 <td class="X l2">external (js/plugin)
320 <td class="X l1">missing
321 <td class="X l0">unknown
322 <td class="X ex">prefixed
325 <p><: if ($usage) { :>
327 <span class=" p0">0</span> -
328 <span class="p p0 p00">.01</span> -
329 <span class="p p0 p05">1-9</span> -
330 <span class="p p1">10</span> -
331 <span class="p p2">20</span> -
332 <span class="p p5">majority</span>
334 <table class="glyphs"><tr>
335 <td class="p p1">previous version</td>
336 <td class="p p3">current</td>
337 <td class="p p0 p00">upcoming (within months)</td>
338 <td class=" p0">future (within a year)</td>
343 <ul class="legend legend-set">
344 <li>default <strong>style</strong> is
345 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
346 <li><strong>usage</strong> source is
347 <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
352 <script type="text/javascript" src="/searchlocal.js"></script>
353 <script type="text/javascript"> prependsearch(document.getElementById('intro')) </script>