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>Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
20 with <a href="http://stats.wikimedia.org/archive/squid_reports/">Wikimedia</a>
21 browser usage statistics.</p>
25 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
39 unoff => 'l0', # unofficial
41 cr => 'l5', # candidate
42 pr => 'l5', # proposed
43 rec => 'l6', # recommendation
44 ietf => 'l6', # standard
47 if (my ($somerow) = values %{ $caniuse->{data} }) {
48 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
49 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
52 my @browsers = grep { $versions{$_} }
53 qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
55 my $canihas = do 'browser-usage.inc.pl' || do {
56 printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $_
62 ($_->[4] => 0 ), # future
63 ($_->[3] => .5), # next
64 ($_->[0] => 5 ), # past
65 ($_->[1] => 10 ), # previous
66 ($_->[2] => 30 ), # current
67 } $caniuse->{agents}->{$_}->{versions}
70 }; # fallback hash based on release semantics
72 my $scorediv = (max(map { sum(values %$_) } values %$canihas) // 1) / 100;
74 print '<table class="mapped">';
76 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
81 '<th colspan="3">feature',
83 sprintf('<th colspan="%d" title="%.1f%%">%s',
84 scalar @{ $versions{$_} },
85 sum(values %{ $canihas->{$_} }),
87 my $name = $caniuse->{agents}->{$_}->{browser};
88 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
94 print '<thead>', $header;
95 # preceding row without any colspan to work around gecko bug
98 for my $browser (@browsers) {
99 printf('<td title="%.1f%%">%s',
100 $canihas->{$browser}->{$_}, showversions($_),
101 ) for @{ $versions{$browser} };
107 # relative amount of support for given feature
108 state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
110 if (my $row = shift) {
112 while (my ($browser, $versions) = each %$row) {
113 ref $versions eq 'HASH' or next;
114 while (my ($version, $_) = each %$versions) {
115 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
121 while (my ($browser, $vercols) = each %versions) {
122 my $div = 0; # multiplier exponent (decreased to lower value)
123 my @vers = map { $row->{$browser}->{$_} } @$vercols;
124 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
125 my @future; # find upcoming releases (after current)
126 for (reverse @$vercols) {
127 last if $_ eq $current;
128 push @future, pop @vers;
129 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
131 splice @vers, -1, 0, @future; # move ahead to decrease precedence
133 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
140 featurescore($caniuse->{data}->{$b}->{stats})
141 <=> featurescore($caniuse->{data}->{$a}->{stats})
142 } keys %{ $caniuse->{data} }) {
143 my $row = $caniuse->{data}->{$id};
144 my $data = $row->{stats} or next; # skip metadata [summary]
145 printf '<tr id="%s">', $id;
146 for ($row->{categories}) {
147 my $cell = $_ ? lc $_->[0] : '-';
151 sprintf('<a href="%s" onclick="%s">%s</a>',
153 sprintf("try { %s; return false } catch(err) { return true }",
154 "document.getElementById('$id').classList.toggle('target')",
159 print '<div class=aside>';
160 s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
161 $row->{description}, $row->{notes};
162 printf 'Resources: %s.', join(', ',
163 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
164 ) for grep { @$_ } $row->{links} // ();
166 for ($row->{status}) {
167 my $cell = $_ // '-';
168 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
169 printf '<td title="%s" class="%s">%s',
170 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
172 for my $browser (@browsers) {
174 for my $ver (@{ $versions{$browser} }, undef) {
175 unless (!defined $prev
176 or ref $data->{$browser} eq 'HASH'
177 && $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
178 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
179 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
182 ref $data->{$browser} ne 'HASH' ? 'u' :
183 $data->{$browser}->{$prev} // 'u'
185 !$usage ? ('p0') : ('p',
186 sprintf('p%01d', $usage / 10),
187 sprintf('p%02d', $usage),
189 sprintf('pp%02d', $usage / $scorediv),
201 state $maxscore = featurescore({ # yes for every possible version
202 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
204 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
206 print '<tfoot>', $header;
210 # normalised version number comparable as string (cmp)
211 shift =~ /^(\d*)(.*)/;
212 return sprintf('%02d', $1 || 0) . $2;
216 my @span = ($_[0], @_>1 ? $_[-1] : ());
221 return join('‒', @span);
229 <table class="glyphs"><tr>
230 <td class="X l6">supported
231 <td class="X l5">prefixed
232 <td class="X l4">partial
233 <td class="X l2">external (js/plugin)
234 <td class="X l0">missing
235 <td class="X l9">unknown
240 <span class=" p0">0</span> -
241 <span class="p p0 p00">.01</span> -
242 <span class="p p0 p05">1-9</span> -
243 <span class="p p1">10</span> -
244 <span class="p p2">20</span> -
245 <span class="p p5">majority</span>
249 <ul class="legend legend-set">
250 <li>default <strong>style</strong> is
251 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>