5 title => 'browser compatibility cheat sheet',
9 keywords => [qw'html css browser feature'],
10 stylesheet => [qw'light dark circus mono red'],
11 data => ['caniuse.js'],
15 <h1>Browser compatibility</h1>
17 <p>Alternate view of Fyrd's <a href="http://caniuse.com/">when can I use...</a> site.</p>
21 use File::Slurp 'read_file';
22 my $source = read_file('caniuse.js');
23 for ($source) { # cleanup
24 # convert seperate variables to hash keys
26 s/^caniuse\.(\w+) = /"$1":/gm;
29 # fractions not supported by barekey
30 s/(?<=[,{]) (\d*\.\d) (?=:')/"$1"/gx;
31 # escapes not supported in singlequote
32 s{'((?:[^\\']+|\\.)*)'}{
39 my $caniuse = from_json($source, {
40 # allow_singlequote => 1,
53 unoff => 'di-rare', # unofficial
55 pr => 'di-prop', # proposed
56 cr => 'di-d', # candidate
57 rec => 'di-a', # recommendation
58 ietf => 'di-aa', # standard
60 my @browsers = qw(trident gecko webkit_saf webkit_chr presto);
62 if (my ($somerow) = values %{ $caniuse->{data} }) {
63 while (my ($browser, $row) = each %{ $somerow->{stats} }) {
64 $versions{$browser} = [ sort { paddedver($a) cmp paddedver($b) } keys %$row ];
68 print '<table class="mapped">';
70 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
74 print '<th colspan="3">feature';
75 printf '<th colspan="%d">%s',
76 scalar @{ $versions{$_} }, $caniuse->{agents}->{$_}->{browser}
78 # preceding row without any colspan to work around gecko bug
80 print '<td>' x $_ for 3, (map { scalar @{ $versions{$_} } } @browsers), 1;
84 # relative amount of support for given feature
85 state $statspts = { y=>10, 'y x'=>9, a=>5, j=>2, p=>1 };
87 if (my $row = shift) {
88 while (my ($browser, $vercols) = each %versions) {
89 my $div = 0; # multiplier exponent (decreased to lower value)
90 my @vers = map { $row->{$browser}->{$_} } @$vercols;
91 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
92 my @future; # find upcoming releases (after current)
93 for (reverse @$vercols) {
94 last if $_ eq $current;
95 push @future, pop @vers;
96 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
98 splice @vers, -1, 0, @future; # move ahead to decrease precedence
100 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
107 featurescore($caniuse->{data}->{$b}->{stats})
108 <=> featurescore($caniuse->{data}->{$a}->{stats})
109 } keys %{ $caniuse->{data} }) {
110 my $row = $caniuse->{data}->{$id};
111 my $data = $row->{stats} or next; # skip metadata [summary]
112 printf '<tr id="%s">', $id;
113 for ($row->{categories}) {
114 my $cell = $_ ? lc $_->[0] : '-';
117 printf '<td title="%s">%s', $row->{description}, $row->{title};
118 for ($row->{status}) {
119 my $cell = $_ // '-';
120 $cell = sprintf '<a href="%s">%s</a>', $_, $cell for $row->{spec} // ();
121 printf '<td title="%s" class="%s">%s',
122 $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $cell;
124 for my $browser (@browsers) {
126 for my $ver (@{ $versions{$browser} }, undef) {
127 unless (!defined $prev
128 or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
129 printf '<td class="%s" colspan="%d">%s',
130 $CSTATS{ $data->{$browser}->{$prev} },
132 showversions(\@span),
140 state $maxscore = featurescore({ # yes for every possible version
141 map { $_ => { map {$_ => 'y'} @{$versions{$_}} } } keys %versions
143 print '<td>', int featurescore($caniuse->{data}->{$id}->{stats}) / $maxscore * 100;
148 # normalised version number comparable as string (cmp)
149 shift =~ /^(\d*)(.*)/;
150 return sprintf('%02d', $1) . $2;
155 splice @$span, 1, -1;
160 return join('‒', @$span);
167 <table class="glyphs"><tr>
168 <td class="X di-aa">supported
169 <td class="X di-a">prefixed
170 <td class="X di-d">partial
171 <td class="X di-prop">external (js/plugin)
172 <td class="X di-b">missing
176 <ul class="legend legend-set">
177 <li>default <strong>style</strong> is
178 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>