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
21 my $canihas = do 'browser-usage.inc.pl'
22 or printf "<p>Browser usage data not found: <em>%s</em>.</p>\n", $! || $@;
24 print "with $_ browser usage statistics." for map {
25 my $ref = $_->{-source} || 'unknown';
26 $ref = sprintf '<a href="%s">%s</a>', $_, $ref for $_->{-url} || ();
27 $ref .= " $_" for $_->{-date} || ();
35 my $caniuse = do 'browser-support.inc.pl' or die $! || $@;
49 unoff => 'l0', # unofficial
51 cr => 'l5', # candidate
52 pr => 'l5', # proposed
53 rec => 'l6', # recommendation
54 other => 'l6', # non-w3
55 ietf => 'l6', # standard
58 if (my ($somerow) = values %{ $caniuse->{data} }) {
59 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
60 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
63 my @browsers = grep { $versions{$_} }
64 qw(trident gecko webkit_saf ios_saf webkit_chr android presto op_mob op_mini);
70 my $zero = $#$_ - 2; # baseline index
71 ($_->[$zero - 2] => .5), # past
72 ($_->[$zero - 1] => 10 ), # previous
73 ($_->[$zero + 2] => 0 ), # future
74 ($_->[$zero + 1] => .5), # next
75 ($_->[$zero ] => 30 ), # current
76 } $caniuse->{agents}->{$_}->{versions}
79 }; # fallback hash based on release semantics
80 my $scorediv = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
82 print '<table class="mapped">';
84 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
89 '<th colspan="3">feature',
91 sprintf('<th colspan="%d" title="%.1f%%">%s',
92 scalar @{ $versions{$_} },
93 sum(values %{ $canihas->{$_} }),
95 my $name = $caniuse->{agents}->{$_}->{browser};
96 length $name < 16 ? $name : $caniuse->{agents}->{$_}->{abbr};
102 print '<thead>', $header;
103 # preceding row without any colspan to work around gecko bug
106 for my $browser (@browsers) {
107 printf('<td title="%.1f%%">%s',
108 $canihas->{$browser}->{$_}, showversions($_),
109 ) for @{ $versions{$browser} };
115 # relative amount of support for given feature
116 state $statspts = { y=>10, 'y x'=>9, a=>5, 'a x'=>5, j=>2, 'p j'=>2, p=>1 };
118 if (my $row = shift) {
120 while (my ($browser, $versions) = each %$row) {
121 ref $versions eq 'HASH' or next;
122 while (my ($version, $_) = each %$versions) {
123 $rank += $canihas->{$browser}->{$version} * $statspts->{$_};
129 while (my ($browser, $vercols) = each %versions) {
130 my $div = 0; # multiplier exponent (decreased to lower value)
131 my @vers = map { $row->{$browser}->{$_} } @$vercols;
132 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
133 my @future; # find upcoming releases (after current)
134 for (reverse @$vercols) {
135 last if $_ eq $current;
136 push @future, pop @vers;
137 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
139 splice @vers, -1, 0, @future; # move ahead to decrease precedence
141 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
148 featurescore($caniuse->{data}->{$b}->{stats})
149 <=> featurescore($caniuse->{data}->{$a}->{stats})
150 } keys %{ $caniuse->{data} }) {
151 my $row = $caniuse->{data}->{$id};
152 my $data = $row->{stats} or next; # skip metadata [summary]
153 printf '<tr id="%s">', $id;
154 for ($row->{categories}) {
155 my $cell = $_ ? lc $_->[0] : '-';
159 sprintf('<a href="%s" onclick="%s">%s</a>',
161 sprintf("try { %s; return false } catch(err) { return true }",
162 "document.getElementById('$id').classList.toggle('target')",
167 print '<div class=aside>';
168 s/\.?$/./, print "<p>$_</p>" for map { ref $_ ? @$_ : $_ || () }
169 $row->{description}, $row->{notes};
170 printf 'Resources: %s.', join(', ',
171 map { qq(<a href="$_->{url}">$_->{title}</a>) } @$_
172 ) for grep { @$_ } $row->{links} // ();
174 for ($row->{status}) {
175 my $cell = $_ // '-';
176 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
177 printf '<td title="%s" class="%s">%s',
178 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
180 for my $browser (@browsers) {
182 for my $ver (@{ $versions{$browser} }, undef) {
183 unless (!defined $prev
184 or ref $data->{$browser} eq 'HASH'
185 && $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
186 my $usage = sum(map { $canihas->{$browser}->{$_} } @span);
187 printf '<td class="%s" colspan="%d" title="%.1f%%">%s',
190 ref $data->{$browser} ne 'HASH' ? 'u' :
191 $data->{$browser}->{$prev} // 'u'
193 !$usage ? ('p0') : ('p',
194 sprintf('p%01d', $usage / 10),
195 sprintf('p%02d', $usage),
197 sprintf('pp%02d', $usage / $scorediv),
209 state $maxscore = featurescore({ # yes for every possible version
210 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
212 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
214 print '<tfoot>', $header;
218 # normalised version number comparable as string (cmp)
219 shift =~ /^(\d*)(.*)/;
220 return sprintf('%02d', $1 || 0) . $2;
224 my @span = ($_[0], @_>1 ? $_[-1] : ());
229 return join('‒', @span);
237 <table class="glyphs"><tr>
238 <td class="X l6">supported
239 <td class="X l5">prefixed
240 <td class="X l4">partial
241 <td class="X l2">external (js/plugin)
242 <td class="X l0">missing
243 <td class="X l9">unknown
248 <span class=" p0">0</span> -
249 <span class="p p0 p00">.01</span> -
250 <span class="p p0 p05">1-9</span> -
251 <span class="p p1">10</span> -
252 <span class="p p2">20</span> -
253 <span class="p p5">majority</span>
257 <ul class="legend legend-set">
258 <li>default <strong>style</strong> is
259 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>