9f8adf7085e709fcc34024ca7bc470bdc610364c
[sheet.git] / browser.plp
1 <(common.inc.plp)><:
2 use 5.010;
3 use utf8;
4 use List::Util qw(sum max first);
5
6 Html({
7         title => 'browser compatibility cheat sheet',
8         version => 'v1.0',
9         description => [
10                 "Compatibility table of new web features (HTML5, CSS3, SVG, Javascript)",
11                 "comparing support and usage share for all popular browser versions.",
12         ],
13         keywords => [qw'
14                 web browser support compatibility usage available feature
15                 html html5 css css3 svg javascript js dom mobile
16                 ie internet explorer firefox chrome safari webkit opera
17         '],
18         stylesheet => [qw'circus dark mono red light'],
19         data => ['browser-support.inc.pl'],
20 });
21
22 say "<h1>Browser compatibility</h1>\n";
23
24 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
25 $_->{verrelease} = {
26         # mark last two (future) versions as unreleased, ensure current isn't
27         map { $_->[-1] => 0, $_->[-2] => 0, $_->[-3] => undef } $_->{versions}
28 } for values %{ $caniuse->{agents} };
29
30 my %CSTATS = (
31         'n'   => 'l1',
32         'y'   => 'l5',
33         'y x' => 'l5 ex',
34         'a'   => 'l3',
35         'a x' => 'l3 ex',
36         'p j' => 'l2',
37         'j'   => 'l2',
38         'p'   => 'l2',
39         'p p' => 'l2',
40         'u'   => 'l0',
41 );
42 my %DSTATS = (
43         u => 'unknown',
44         n => 'unsupported',
45         p => 'plugin required',
46         j => 'javascript required',
47         a => 'partial',
48         y => 'supported',
49         x => sub {
50                 join(' ',
51                         'requires prefix',
52                         (map "-$_-", $caniuse->{agents}->{$_[0]}->{prefix} // ()),
53                 );
54         },
55 );
56 my %CSTATUS = (
57         unoff => 'l1', # unofficial
58         wd    => 'l3', # draft
59         cr    => 'l4', # candidate
60         pr    => 'l4', # proposed
61         rec   => 'l5', # recommendation
62         other => 'l2', # non-w3
63         ietf  => 'l5', # standard
64 );
65 my %versions;
66 if (my ($somerow) = values %{ $caniuse->{data} }) {
67         while (my ($browser, $row) = each %{ $somerow->{stats} }) {
68                 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
69         }
70 }
71 my @browsers = keys %versions;
72
73 print <<'';
74 <p id="intro">Alternate rendition of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
75
76 my ($canihas, $usage);
77 given ($get{usage} // 'wm') {
78         when (!$_) {
79                 # none
80         }
81         when (!/^[a-z][\w-]+$/) {
82                 printf "<p>Invalid browser usage data request: <em>%s</em>",
83                         'identifier must be alphanumeric name or <q>0</q>';
84         }
85         $canihas = do "browser-usage-$_.inc.pl" or do {
86                 printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
87                 break;
88         };
89         $usage = $_;
90         my $ref = $canihas->{-title} || 'unknown';
91         $ref = sprintf '<a href="%s">%s</a>', $_, $ref
92                 for $canihas->{-site} || $canihas->{-source} || ();
93         $ref .= " $_" for $canihas->{-date} || ();
94         print "\nwith $ref browser usage statistics";
95 }
96 if ($usage) { # first() does not work inside given >:(
97         # adapt version usage to actual support data
98         my %engineuse;  # prefix => usage sum
99         while (my ($browser, $row) = each %$canihas) {
100                 my $verlist = $versions{$browser} or next;
101                 my %supported = map { $_ => 1 } @$verlist;
102
103                 # cascade unknown versions
104                 $row->{$_} //= undef for @$verlist;  # ensure stable keys during iteration
105                 while (my ($version, $usage) = each %$row) {
106                         next if defined $supported{$version};
107                         my $next = first { paddedver($_) ge paddedver($version) } @$verlist
108                                 or warn("No fallback found for $browser v$version; $usage% ignored"), next;
109                         $row->{$next} += $usage;
110                         $row->{$version} = 0;  # balance browser total
111                 }
112
113                 # reusable aggregates (grouped by prefix (engine) and browser)
114                 $engineuse{ $caniuse->{agents}->{$browser}->{prefix} } +=
115                 $row->{-total} = sum(values %$row);
116         }
117
118         # order browser columns by usage grouped by engine
119         @browsers = sort {
120                 $engineuse{ $caniuse->{agents}->{$b}->{prefix} } <=>
121                 $engineuse{ $caniuse->{agents}->{$a}->{prefix} }
122                         ||
123                 $canihas->{$b}->{-total} <=> $canihas->{$a}->{-total}
124         } @browsers;
125 }
126 :>.
127 </p>
128
129 <:
130 $canihas ||= {
131         map {
132                 $_ => +{
133                         map {
134                                 my $zero = $#$_ - 2;  # baseline index
135                                 ($_->[$zero - 2] =>  .5), # past
136                                 ($_->[$zero - 1] => 10 ), # previous
137                                 ($_->[$zero + 2] =>  0 ), # future
138                                 ($_->[$zero + 1] =>  .5), # next
139                                 ($_->[$zero    ] => 30 ), # current
140                         } $caniuse->{agents}->{$_}->{versions}
141                 }
142         } @browsers
143 }; # fallback hash based on release semantics
144 my $usagemax = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
145
146 my $usagepct = 1;  # score multiplier for 0..100 result
147 # normalise usage percentage to only include shown browsers
148 $usagepct = 100 / featurescore({  # yes for every possible version
149         map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
150 });
151
152 print '<table class="mapped">';
153 print '<col span="3">';  # should match first thead row
154 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
155 say '</colgroup><col>';
156
157 my $header = join('',
158         '<tr>',
159         '<th colspan="3">feature',
160         (map {
161                 my $name = $caniuse->{agents}->{$_}->{browser};
162                 sprintf('<th colspan="%d" class="%s" title="%s">%s',
163                         scalar @{ $versions{$_} },
164                         join(' ', map {"b-a-$_"} grep {$_}
165                                 $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
166                         ),
167                         join(' ',
168                                 sprintf('%.1f%%', $canihas->{$_}->{-total} * $usagepct),
169                                 $name,
170                         ),
171                         do {
172                                 length $name < 3 + @{ $versions{$_} }*2 ? $name
173                                         : $caniuse->{agents}->{$_}->{abbr};
174                         },
175                 )
176         } @browsers),
177         '<th>%',
178 );
179 print '<thead>', $header;
180 # preceding row without any colspan to work around gecko bug
181 print "\n<tr>";
182 print '<td>' x 3;
183 for my $browser (@browsers) {
184         for my $_ (@{ $versions{$browser} }) {
185                 my $release = $caniuse->{agents}->{$browser}->{verrelease}->{$_};
186                 my $future = defined $release;
187                 printf('<td title="%s"%s>%s',
188                         join(' ',
189                                 sprintf('%.1f%%', $canihas->{$browser}->{$_} * $usagepct),
190                                 $future ? 'development' : (),
191                                 "version $_",
192                         ),
193                         $future && ' class="ex"',
194                         showversions($_),
195                 );
196         }
197 }
198 print '<td>' x 1;
199 say '</thead>';
200 say '<tfoot>', $header, '</tfoot>';
201
202 sub featurescore {
203         # relative amount of support for given feature
204         state $statspts = { y=>1, 'y x'=>1, a=>.5, 'a x'=>.5, j=>.2, 'p j'=>.2, 'p p'=>.2, p=>.1 };
205         my $rank = 0;
206         if (my $row = shift) {
207                 if ($canihas) {
208                         while (my ($browser, $versions) = each %$row) {
209                                 ref $versions eq 'HASH' or next;
210                                 while (my ($version, $_) = each %$versions) {
211                                         $rank += ($canihas->{$browser}->{$version} || .001) * $statspts->{$_};
212                                 }
213                         }
214                         return $rank;
215                 }
216
217                 while (my ($browser, $vercols) = each %versions) {
218                         my $div = 0;  # multiplier exponent (decreased to lower value)
219                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
220                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
221                                 my @future;  # find upcoming releases (after current)
222                                 for (reverse @$vercols) {
223                                         last if $_ eq $current;
224                                         push @future, pop @vers;
225                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
226                                 }
227                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
228                         }
229                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
230                 }
231         }
232         return $rank;
233 }
234
235 sub saytitlecol {
236         my ($id) = @_;
237         my $row = $caniuse->{data}->{$id};
238
239         for ($row->{categories}) {
240                 my $cell = $_ ? lc $_->[0] : '-';
241                 $cell =~ s/ api$//;  # trim unessential fluff in 'js api'
242                 printf '<th title="%s">%s', join(' + ', @$_), $cell;
243         }
244
245         print '<td>', map {
246                 sprintf('<a href="%s" onclick="%s">%s</a>',
247                         "#$id",
248                         sprintf("try { %s; return false } catch(err) { return true }",
249                                 "document.getElementById('$id').classList.toggle('target')",
250                         ),
251                         Entity($_),
252                 );
253         } $row->{title};
254         print '<div class=aside>';
255         s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
256                 Entity($row->{description}),
257                 map { s/\s*\n/\n<br>/g; $_ } $row->{notes};
258         printf 'Resources: %s.', join(', ', map {
259                 sprintf '<a href="%s">%s</a>', EscapeHTML($_->{url}), $_->{title}
260         } @$_) for grep { @$_ } $row->{links} // ();
261         printf '<br>Parent feature: %s.', join(', ', map {
262                 sprintf '<a href="%s">%s</a>', EscapeHTML("#$_"), $caniuse->{data}->{$_}->{title}
263         } $_) for $row->{parent} || ();
264         print '</div>';
265 }
266
267 sub saystatuscol {
268         my ($id) = @_;
269         my $row = $caniuse->{data}->{$id};
270
271         for ($row->{status}) {
272                 my $cell = $_ // '-';
273                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
274                 printf '<td title="%s" class="l %s">%s',
275                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
276         }
277 }
278
279 sub saybrowsercols {
280         my ($id, $browser) = @_;
281         my $data = $caniuse->{data}->{$id}->{stats}->{$browser};
282         if (ref $data eq 'ARRAY') {
283                 # special case for unsupported
284                 my $release = $caniuse->{agents}->{$browser}->{verrelease};
285                 $data = {
286                         map { $_ => defined $release->{$_} ? 'u' : 'n' } keys %$release
287                 };
288         }
289
290         my ($prev, @span);
291         for my $ver (@{ $versions{$browser} }, undef) {
292                 my $compare = (
293                         !defined $ver ? undef :      # last column if nameless
294                         ref $data ne 'HASH' ? '' :   # unclassified if no support hash
295                         $data->{$ver} // $prev       # known or inherit from predecessor
296                         // (grep { defined } @{$data}{ @{ $versions{$browser} } })[0]
297                            ~~ 'n' && 'n'             # first known version is unsupported
298                         || 'u'                       # unsure
299                 );
300                 unless (!defined $prev or $prev ~~ $compare) {
301                         my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
302                         printf '<td class="%s" colspan="%d" title="%s">%s',
303                                 join(' ',
304                                         X => $CSTATS{$prev},
305                                         !$usage ? ('p0') : ('p',
306                                                 sprintf('p%01d', $usage * ($usagepct - .0001) / 10),
307                                                 sprintf('p%02d', $usage * ($usagepct - .0001)),
308                                         ),
309                                         sprintf('pp%02d', $usage / $usagemax),
310                                 ),
311                                 scalar @span,
312                                 sprintf('%.1f%% %s', $usage * $usagepct, join(', ',
313                                         map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
314                                         map { $DSTATS{$_} // () }
315                                         map { split / /, $_ }
316                                         $prev
317                                 )),
318                                 showversions(@span),
319                         undef $prev;
320                         @span = ();
321                 }
322                 push @span, $ver;
323                 $prev = $compare;
324         }
325 }
326
327 sub sayusagecol {
328         my ($id) = @_;
329         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) * $usagepct;
330 }
331
332 say '<tbody>';
333 for my $id (sort {
334             featurescore($caniuse->{data}->{$b}->{stats})
335         <=> featurescore($caniuse->{data}->{$a}->{stats})
336 } keys %{ $caniuse->{data} }) {
337         $caniuse->{data}->{$id}->{stats} or next;  # skip metadata [summary]
338         printf '<tr id="%s">', $id;
339         saytitlecol($id);
340         saystatuscol($id);
341         saybrowsercols($id, $_) for @browsers;
342         sayusagecol($id);
343         say '</tr>';
344 }
345 say '</tbody>';
346 say '</table>';
347
348 sub paddedver {
349         # normalised version number comparable as string (cmp)
350         shift =~ /(?:.*-|^)(\d*)(.*)/;
351         # matched (major)(.minor) of last value in range (a-B)
352         return sprintf('%02d', $1 || 0) . $2;
353 }
354
355 sub showversions {
356         my @span = ($_[0], @_>1 ? $_[-1] : ());
357         s/-.*// for $span[0];
358         for (@span) {
359                 s/^\./0./;
360                 s/x$/.*/;
361                 s/.*-//;
362         }
363         return join('‒', @span);
364 }
365
366 :>
367 <hr>
368
369 <div class="legend">
370         <table class="glyphs"><tr>
371         <td class="X l5">supported
372         <td class="X l3">partial
373         <td class="X l2">external (js/plugin)
374         <td class="X l1">missing
375         <td class="X l0">unknown
376         <td class="X ex">prefixed
377         </table>
378
379         <p><: if ($usage) { :>
380                 Usage percentage:
381                 <span class="  p0">0</span> -
382                 <span class="p p0 p00">.01</span> -
383                 <span class="p p0 p05">1-9</span> -
384                 <span class="p p1">10</span> -
385                 <span class="p p2">20</span> -
386                 <span class="p p5">majority</span>
387 <: } else { :>
388                 <table class="glyphs"><tr>
389                         <td class="p p1">previous version</td>
390                         <td class="p p3">current</td>
391                         <td class="p p0 p00">upcoming (within months)</td>
392                         <td class="  p0">future (within a year)</td>
393                 </table>
394 <: } :> </p>
395
396         <div class="right">
397                 <ul class="legend legend-set">
398                 <li>default <strong>style</strong> is
399                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
400                 <li><strong>usage</strong> source is
401                         <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
402                 </ul>
403         </div>
404 </div>
405
406 <script type="text/javascript" src="/searchlocal.js"></script>
407 <script type="text/javascript"> prependsearch(document.getElementById('intro')) </script>
408