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";
44 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
46 # mark last two (future) versions as unreleased, ensure current isn't
47 map { $_->[-1] => 0, $_->[-2] => 0, $_->[-3] => undef } $_->{versions}
48 } for values %{ $caniuse->{agents} };
63 unoff => 'l1', # unofficial
65 cr => 'l4', # candidate
66 pr => 'l4', # proposed
67 rec => 'l5', # recommendation
68 other => 'l5', # non-w3
69 ietf => 'l5', # standard
72 if (my ($somerow) = values %{ $caniuse->{data} }) {
73 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
74 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
77 my @browsers = grep { $versions{$_} }
78 qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
84 my $zero = $#$_ - 2; # baseline index
85 ($_->[$zero - 2] => .5), # past
86 ($_->[$zero - 1] => 10 ), # previous
87 ($_->[$zero + 2] => 0 ), # future
88 ($_->[$zero + 1] => .5), # next
89 ($_->[$zero ] => 30 ), # current
90 } $caniuse->{agents}->{$_}->{versions}
93 }; # fallback hash based on release semantics
94 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
96 print '<table class="mapped">';
98 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
101 my $header = join('',
103 '<th colspan="3">feature',
105 my $name = $caniuse->{agents}->{$_}->{browser};
106 sprintf('<th colspan="%d" title="%s">%s',
107 scalar @{ $versions{$_} },
109 sprintf('%.1f%%', sum(values %{ $canihas->{$_} })),
113 length $name < 3 + @{ $versions{$_} }*2 ? $name
114 : $caniuse->{agents}->{$_}->{abbr};
120 print '<thead>', $header;
121 # preceding row without any colspan to work around gecko bug
124 for my $browser (@browsers) {
125 printf('<td title="%.1f%%"%s>%s',
126 $canihas->{$browser}->{$_},
128 defined $_ && !$_ && ' class="ex"'
129 } $caniuse->{agents}->{$browser}->{verrelease}->{$_}),
131 ) for @{ $versions{$browser} };
137 # relative amount of support for given feature
138 state $statspts = { y=>10, 'y x'=>10, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
140 if (my $row = shift) {
142 while (my ($browser, $versions) = each %$row) {
143 ref $versions eq 'HASH' or next;
144 while (my ($version, $_) = each %$versions) {
145 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
151 while (my ($browser, $vercols) = each %versions) {
152 my $div = 0; # multiplier exponent (decreased to lower value)
153 my @vers = map { $row->{$browser}->{$_} } @$vercols;
154 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
155 my @future; # find upcoming releases (after current)
156 for (reverse @$vercols) {
157 last if $_ eq $current;
158 push @future, pop @vers;
159 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
161 splice @vers, -1, 0, @future; # move ahead to decrease precedence
163 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
171 my $row = $caniuse->{data}->{$id};
173 for ($row->{categories}) {
174 my $cell = $_ ? lc $_->[0] : '-';
179 sprintf('<a href="%s" onclick="%s">%s</a>',
181 sprintf("try { %s; return false } catch(err) { return true }",
182 "document.getElementById('$id').classList.toggle('target')",
187 print '<div class=aside>';
188 s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
189 $row->{description}, $row->{notes};
190 printf 'Resources: %s.', join(', ',
191 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
192 ) for grep { @$_ } $row->{links} // ();
198 my $row = $caniuse->{data}->{$id};
200 for ($row->{status}) {
201 my $cell = $_ // '-';
202 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
203 printf '<td title="%s" class="l %s">%s',
204 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
209 my ($id, $browser) = @_;
210 my $data = $caniuse->{data}->{$id}->{stats}->{$browser};
213 for my $ver (@{ $versions{$browser} }, undef) {
214 unless (!defined $prev
215 or ref $data eq 'HASH' && $data->{$prev} ~~ $data->{$ver}) {
216 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
217 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
219 X => $CSTATS{ ref $data eq 'HASH' && $data->{$prev} || 'u' },
220 !$usage ? ('p0') : ('p',
221 sprintf('p%01d', $usage / 10),
222 sprintf('p%02d', $usage),
224 sprintf('pp%02d', $usage / $scorediv),
239 state $maxscore = featurescore({ # yes for every possible version
240 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
242 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
246 featurescore($caniuse->{data}->{$b}->{stats})
247 <=> featurescore($caniuse->{data}->{$a}->{stats})
248 } keys %{ $caniuse->{data} }) {
249 $caniuse->{data}->{$id}->{stats} or next; # skip metadata [summary]
250 printf '<tr id="%s">', $id;
253 saybrowsercols($id, $_) for @browsers;
257 print '<tfoot>', $header;
261 # normalised version number comparable as string (cmp)
262 shift =~ /^(\d*)(.*)/;
263 return sprintf('%02d', $1 || 0) . $2;
267 my @span = ($_[0], @_>1 ? $_[-1] : ());
272 return join('‒', @span);
280 <table class="glyphs"><tr>
281 <td class="X l5">supported
282 <td class="X l3">partial
283 <td class="X l2">external (js/plugin)
284 <td class="X l1">missing
285 <td class="X l0">unknown
286 <td class="X l0 ex">prefixed
289 <p><: if ($usage) { :>
291 <span class=" p0">0</span> -
292 <span class="p p0 p00">.01</span> -
293 <span class="p p0 p05">1-9</span> -
294 <span class="p p1">10</span> -
295 <span class="p p2">20</span> -
296 <span class="p p5">majority</span>
298 <table class="glyphs"><tr>
299 <td class="p p1">previous version</td>
300 <td class="p p3">current</td>
301 <td class="p p0 p00">upcoming (within months)</td>
302 <td class=" p0">future (within a year)</td>
307 <ul class="legend legend-set">
308 <li>default <strong>style</strong> is
309 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
310 <li><strong>usage</strong> source is
311 <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
316 <script src="/searchlocal.js"></script>
317 <script> prependsearch(document.getElementById('intro')) </script>