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 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="glyphs dimap">';
70 printf '<colgroup span="%d">', scalar @{ $versions{$_} } for @browsers;
74 print "<th>$_" for qw(feature status);
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 2, map { scalar @{ $versions{$_} } } @browsers;
84 # relative amount of support for given feature
85 state $statspts = { y=>10, 'y x'=>9, a=>5, j=>2, p=>1 };
88 if (my $row = $caniuse->{data}->{$id}->{stats}) {
89 while (my ($browser, $vercols) = each %versions) {
90 my $div = 0; # multiplier exponent (decreased to lower value)
91 my @vers = map { $row->{$browser}->{$_} } @$vercols;
92 if (my $current = $caniuse->{agents}->{$browser}->{versions}->[-3]) {
93 my @future; # find upcoming releases (after current)
94 for (reverse @$vercols) {
95 last if $_ eq $current;
96 push @future, pop @vers;
97 $_ eq 'u' and $_ = $vers[-1] for $future[-1]; # inherit latest value if unknown
99 splice @vers, -1, 0, @future; # move ahead to decrease precedence
101 $rank += $statspts->{$_} * 2**($div--) for reverse @vers;
108 featurerank($b) <=> featurerank($a)
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 printf '<th title="%s">%s', $row->{description}, $row->{title};
114 printf '<td title="%s" class="%s">%s', $caniuse->{statuses}->{$_}, $CSTATUS{$_} // '', $_ for $row->{status};
115 for my $browser (@browsers) {
117 for my $ver (@{ $versions{$browser} }, undef) {
118 unless (!defined $prev
119 or $data->{$browser}->{$prev} ~~ $data->{$browser}->{$ver}) {
120 printf '<td class="%s" colspan="%d">%s',
121 $CSTATS{ $data->{$browser}->{$prev} },
123 join('‒', $span[0], @span > 1 ? $span[-1] : ());
131 print '<td>', int featurerank($id);
136 # normalised version number comparable as string (cmp)
137 shift =~ /^(\d*)(.*)/;
138 return sprintf('%02d', $1) . $2;
145 <table class="glyphs"><tr>
146 <td class="X di-aa">supported
147 <td class="X di-a">prefixed
148 <td class="X di-d">partial
149 <td class="X di-prop">external (js/plugin)
150 <td class="X di-b">missing
154 <ul class="legend legend-set">
155 <li>default <strong>style</strong> is
156 <:= defined $get{style} && 'set to ' :><em><:= $style :></em>