unicode: dedicated include with character details
[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 else {
127         # order browser columns by name grouped by engine
128         @browsers = sort {
129                 $caniuse->{agents}->{$b}->{prefix} cmp
130                 $caniuse->{agents}->{$a}->{prefix}
131                         ||
132                 $a cmp $b
133         } @browsers;
134 }
135 :>.
136 </p>
137
138 <:
139 $canihas ||= {
140         map {
141                 $_ => +{
142                         map {
143                                 my $zero = $#$_ - 2;  # baseline index
144                                 ($_->[$zero - 2] =>  .5), # past
145                                 ($_->[$zero - 1] => 10 ), # previous
146                                 ($_->[$zero + 2] =>  0 ), # future
147                                 ($_->[$zero + 1] =>  .5), # next
148                                 ($_->[$zero    ] => 30 ), # current
149                         } $caniuse->{agents}->{$_}->{versions}
150                 }
151         } @browsers
152 }; # fallback hash based on release semantics
153 my $usagemax = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
154
155 my $usagepct = 1;  # score multiplier for 0..100 result
156 # normalise usage percentage to only include shown browsers
157 $usagepct = 100.01 / featurescore({  # yes for every possible version
158         map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
159 });
160
161 print '<table class="mapped">';
162 print '<col span="3">';  # should match first thead row
163 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
164 say '</colgroup><col>';
165
166 my $header = join('',
167         '<tr>',
168         '<th colspan="3">feature',
169         (map {
170                 my $name = $caniuse->{agents}->{$_}->{browser};
171                 sprintf('<th colspan="%d" class="%s" title="%s">%s',
172                         scalar @{ $versions{$_} },
173                         join(' ', map {"b-a-$_"} grep {$_}
174                                 $_, @{ $caniuse->{agents}->{$_} }{'prefix', 'type'},
175                         ),
176                         join(' ',
177                                 sprintf('%.1f%%', $canihas->{$_}->{-total} * $usagepct),
178                                 $name,
179                         ),
180                         do {
181                                 length $name < 3 + @{ $versions{$_} }*2 ? $name
182                                         : $caniuse->{agents}->{$_}->{abbr};
183                         },
184                 )
185         } @browsers),
186         '<th>%',
187 );
188 print '<thead>', $header;
189 # preceding row without any colspan to work around gecko bug
190 print "\n<tr>";
191 print '<td>' x 3;
192 for my $browser (@browsers) {
193         for my $_ (@{ $versions{$browser} }) {
194                 my $release = $caniuse->{agents}->{$browser}->{verrelease}->{$_};
195                 my $future = defined $release;
196                 printf('<td title="%s"%s>%s',
197                         join(' ',
198                                 sprintf('%.1f%%', $canihas->{$browser}->{$_} * $usagepct),
199                                 $future ? 'development' : (),
200                                 "version $_",
201                         ),
202                         $future && ' class="ex"',
203                         showversions($_),
204                 );
205         }
206 }
207 print '<td>' x 1;
208 say '</thead>';
209 say '<tfoot>', $header, '</tfoot>';
210
211 sub featurescore {
212         # relative amount of support for given feature
213         state $statspts = { y=>1, 'y x'=>1, a=>.5, 'a x'=>.5, j=>.2, 'p j'=>.2, 'p p'=>.2, p=>.1 };
214         my $rank = 0;
215         if (my $row = shift) {
216                 if ($canihas) {
217                         while (my ($browser, $versions) = each %$row) {
218                                 ref $versions eq 'HASH' or next;
219                                 while (my ($version, $_) = each %$versions) {
220                                         $rank += ($canihas->{$browser}->{$version} || .001) * $statspts->{$_};
221                                 }
222                         }
223                         return $rank;
224                 }
225
226                 while (my ($browser, $vercols) = each %versions) {
227                         my $div = 0;  # multiplier exponent (decreased to lower value)
228                         my @vers = map { $row->{$browser}->{$_} } @$vercols;
229                         if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
230                                 my @future;  # find upcoming releases (after current)
231                                 for (reverse @$vercols) {
232                                         last if $_ eq $current;
233                                         push @future, pop @vers;
234                                         $_ eq 'u' and $_ = $vers[-1] for $future[-1];  # inherit latest value if unknown
235                                 }
236                                 splice @vers, -1, 0, @future;  # move ahead to decrease precedence
237                         }
238                         $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
239                 }
240         }
241         return $rank;
242 }
243
244 sub saytitlecol {
245         my ($id) = @_;
246         my $row = $caniuse->{data}->{$id};
247
248         for ($row->{categories}) {
249                 my $cell = $_ ? lc $_->[0] : '-';
250                 $cell =~ s/ api$//;  # trim unessential fluff in 'js api'
251                 printf '<th title="%s">%s', join(' + ', @$_), $cell;
252         }
253
254         print '<td>', map {
255                 sprintf('<a href="%s" onclick="%s">%s</a>',
256                         "#$id",
257                         sprintf("try { %s; return false } catch(err) { return true }",
258                                 "document.getElementById('$id').classList.toggle('target')",
259                         ),
260                         Entity($_),
261                 );
262         } $row->{title};
263         print '<div class=aside>';
264         s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
265                 Entity($row->{description}),
266                 map { s/\s*\n/\n<br>/g; $_ } $row->{notes};
267         printf 'Resources: %s.', join(', ', map {
268                 sprintf '<a href="%s">%s</a>', EscapeHTML($_->{url}), $_->{title}
269         } @$_) for grep { @$_ } $row->{links} // ();
270         printf '<br>Parent feature: %s.', join(', ', map {
271                 sprintf '<a href="%s">%s</a>', EscapeHTML("#$_"), $caniuse->{data}->{$_}->{title}
272         } $_) for $row->{parent} || ();
273         print '</div>';
274 }
275
276 sub saystatuscol {
277         my ($id) = @_;
278         my $row = $caniuse->{data}->{$id};
279
280         for ($row->{status}) {
281                 my $cell = $_ // '-';
282                 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
283                 printf '<td title="%s" class="l %s">%s',
284                         $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
285         }
286 }
287
288 sub saybrowsercols {
289         my ($id, $browser) = @_;
290         my $data = $caniuse->{data}->{$id}->{stats}->{$browser};
291         if (ref $data eq 'ARRAY') {
292                 # special case for unsupported
293                 my $release = $caniuse->{agents}->{$browser}->{verrelease};
294                 $data = {
295                         map { $_ => defined $release->{$_} ? 'u' : 'n' } keys %$release
296                 };
297         }
298
299         my ($prev, @span);
300         for my $ver (@{ $versions{$browser} }, undef) {
301                 my $compare = (
302                         !defined $ver ? undef :      # last column if nameless
303                         ref $data ne 'HASH' ? '' :   # unclassified if no support hash
304                         $data->{$ver} // $prev       # known or inherit from predecessor
305                         // (grep { defined } @{$data}{ @{ $versions{$browser} } })[0]
306                            ~~ 'n' && 'n'             # first known version is unsupported
307                         || 'u'                       # unsure
308                 );
309                 unless (!defined $prev or $prev ~~ $compare) {
310                         my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
311                         printf '<td class="%s" colspan="%d" title="%s">%s',
312                                 join(' ',
313                                         X => $CSTATS{$prev},
314                                         !$usage ? ('p0') : ('p',
315                                                 sprintf('p%01d', $usage * ($usagepct - .0001) / 10),
316                                                 sprintf('p%02d', $usage * ($usagepct - .0001)),
317                                         ),
318                                         sprintf('pp%02d', $usage / $usagemax),
319                                 ),
320                                 scalar @span,
321                                 sprintf('%.1f%% %s', $usage * $usagepct, join(', ',
322                                         map { ref $_ eq 'CODE' ? $_->($browser) : $_ }
323                                         map { $DSTATS{$_} // () }
324                                         map { split / /, $_ }
325                                         $prev
326                                 )),
327                                 showversions(@span),
328                         undef $prev;
329                         @span = ();
330                 }
331                 push @span, $ver;
332                 $prev = $compare;
333         }
334 }
335
336 sub sayusagecol {
337         my ($id) = @_;
338         print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) * $usagepct;
339 }
340
341 say '<tbody>';
342 for my $id (sort {
343             featurescore($caniuse->{data}->{$b}->{stats})
344         <=> featurescore($caniuse->{data}->{$a}->{stats})
345 } keys %{ $caniuse->{data} }) {
346         $caniuse->{data}->{$id}->{stats} or next;  # skip metadata [summary]
347         printf '<tr id="%s">', $id;
348         saytitlecol($id);
349         saystatuscol($id);
350         saybrowsercols($id, $_) for @browsers;
351         sayusagecol($id);
352         say '</tr>';
353 }
354 say '</tbody>';
355 say '</table>';
356
357 sub paddedver {
358         # normalised version number comparable as string (cmp)
359         shift =~ /(?:.*-|^)(\d*)(.*)/;
360         # matched (major)(.minor) of last value in range (a-B)
361         return sprintf('%02d', $1 || 0) . $2;
362 }
363
364 sub showversions {
365         my @span = ($_[0], @_>1 ? $_[-1] : ());
366         s/-.*// for $span[0];
367         for (@span) {
368                 s/^\./0./;
369                 s/x$/.*/;
370                 s/.*-//;
371         }
372         return join('‒', @span);
373 }
374
375 :>
376 <hr>
377
378 <div class="legend">
379         <table class="glyphs"><tr>
380         <td class="X l5">supported
381         <td class="X l3">partial
382         <td class="X l2">external (js/plugin)
383         <td class="X l1">missing
384         <td class="X l0">unknown
385         <td class="X ex">prefixed
386         </table>
387
388         <p><: if ($usage) { :>
389                 Usage percentage:
390                 <span class="  p0">0</span> -
391                 <span class="p p0 p00">.01</span> -
392                 <span class="p p0 p05">1-9</span> -
393                 <span class="p p1">10</span> -
394                 <span class="p p2">20</span> -
395                 <span class="p p5">majority</span>
396 <: } else { :>
397                 <table class="glyphs"><tr>
398                         <td class="p p1">previous version</td>
399                         <td class="p p3">current</td>
400                         <td class="p p0 p00">upcoming (within months)</td>
401                         <td class="  p0">future (within a year)</td>
402                 </table>
403 <: } :> </p>
404
405         <div class="right">
406                 <ul class="legend legend-set">
407                 <li>default <strong>style</strong> is
408                         <:= defined $get{style} && 'set to ' :><em><:= $style :></em>
409                 <li><strong>usage</strong> source is
410                         <:= !defined $get{usage} && 'default ' :><:= defined $usage ? "<em>$usage</em>" : 'not included (<em>0</em>)' :>
411                 </ul>
412         </div>
413 </div>
414
415 <script type="text/javascript" src="/searchlocal.js"></script>
416 <script type="text/javascript"> prependsearch(document.getElementById('intro')) </script>
417