Skip to content

Commit a80c823

Browse files
committed
long image descriptions
1 parent 38cd196 commit a80c823

File tree

5 files changed

+78
-19
lines changed

5 files changed

+78
-19
lines changed

lib/Plots/JSXGraph.pm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ sub HTML {
3434

3535
my $imageviewClass = $self->plots->axes->style('jsx_navigation') ? '' : ' image-view-elt';
3636
my $tabindex = $self->plots->axes->style('jsx_navigation') ? '' : ' tabindex="0"';
37+
my $details = $self->plots->{description_details} =~ s/LONG-DESCRIPTION-ID/${name}_details/r;
38+
my $aria_details = $details ? qq! aria-details="${name}_details"! : '';
3739

3840
return <<~ "END_HTML";
39-
<div id="jsxgraph-plot-$name" class="jxgbox plots-jsxgraph$imageviewClass"$tabindex
40-
style="width: ${width}px; height: ${height}px;"></div>
41+
<div class="image-container">
42+
<div id="jsxgraph-plot-$name" class="jxgbox plots-jsxgraph$imageviewClass"$tabindex
43+
style="width: ${width}px; height: ${height}px;"$aria_details></div>
44+
$details
45+
</div>
4146
<script>
4247
(async () => {
4348
const jsxPlotDiv = document.getElementById('jsxgraph-plot-$name');

macros/core/PGML.pl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ sub NOOP {
687687
terminator => qr/!\]/,
688688
terminateMethod => 'terminateGetString',
689689
cancelNL => 1,
690-
options => [ "source", "width", "height", "image_options" ]
690+
options => [ "source", "width", "height", "image_options", "long_description" ]
691691
},
692692
"[<" => {
693693
type => 'tag',
@@ -1484,12 +1484,20 @@ sub Text {
14841484

14851485
sub Image {
14861486
my ($self, $item) = @_;
1487-
my $text = $item->{text};
1488-
my $source = $item->{source};
1489-
my $width = $item->{width} || '';
1490-
my $height = $item->{height} || '';
1491-
my $image_options = $item->{image_options} || {};
1492-
return (main::image($source, alt => $text, width => $width, height => $height, %$image_options));
1487+
my $text = $item->{text};
1488+
my $source = $item->{source};
1489+
my $width = $item->{width} || '';
1490+
my $height = $item->{height} || '';
1491+
my $image_options = $item->{image_options} || {};
1492+
my $long_description = $item->{long_description} || {};
1493+
return (main::image(
1494+
$source,
1495+
alt => $text,
1496+
width => $width,
1497+
height => $height,
1498+
long_description => $long_description,
1499+
%$image_options
1500+
));
14931501
}
14941502

14951503
######################################################################

macros/core/PGbasicmacros.pl

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,8 +2778,9 @@ sub image {
27782778
tex_size => '',
27792779
valign => 'middle',
27802780
# default value for alt is undef, since an empty string is the explicit indicator of a decorative image
2781-
alt => undef,
2782-
extra_html_tags => '',
2781+
alt => undef,
2782+
long_description => undef,
2783+
extra_html_tags => '',
27832784
);
27842785
# handle options
27852786
my %out_options = %known_options;
@@ -2802,9 +2803,11 @@ sub image {
28022803
$tex_size = 1000 if $tex_size > 1000;
28032804

28042805
my $alt = $out_options{alt};
2806+
my $desc = $out_options{long_description};
28052807
my $width_ratio = $tex_size * (.001);
28062808
my @image_list = ();
28072809
my @alt_list = ();
2810+
my @desc_list = ();
28082811
my $valign = 'middle';
28092812
$valign = 'top' if ($out_options{valign} eq 'top');
28102813
$valign = 'bottom' if ($out_options{valign} eq 'bottom');
@@ -2823,10 +2826,39 @@ sub image {
28232826
} else {
28242827
for my $i (@image_list) { push(@alt_list, $alt) }
28252828
}
2829+
if (ref($desc) =~ /ARRAY/) {
2830+
@desc_list = @{$desc};
2831+
} else {
2832+
for my $i (@image_list) { push(@desc_list, $desc) }
2833+
}
28262834

28272835
my @output_list = ();
28282836
while (@image_list) {
2829-
my $image_item = shift @image_list;
2837+
my $image_item = shift @image_list;
2838+
my $description_details = '';
2839+
if ($desc) {
2840+
$description_details = tag(
2841+
'details',
2842+
'aria-live' => 'polite',
2843+
tag(
2844+
'summary',
2845+
class => 'my-1',
2846+
title => 'details',
2847+
tag(
2848+
'span',
2849+
class => 'badge badge-info bg-secondary',
2850+
'aria-hidden' => 'true',
2851+
maketext('image description')
2852+
)
2853+
)
2854+
. tag(
2855+
'div',
2856+
id => 'LONG-DESCRIPTION-ID',
2857+
class => 'bg-white py-2 px-3 my-2 border',
2858+
shift @desc_list
2859+
)
2860+
);
2861+
}
28302862
if (ref $image_item eq 'parser::GraphTool') {
28312863
push(
28322864
@output_list,
@@ -2846,6 +2878,7 @@ sub image {
28462878
$image_item->{ariaDescription} = shift @alt_list if $out_options{alt};
28472879

28482880
if ($image_item->ext eq 'html') {
2881+
$image_item->{description_details} = $description_details;
28492882
push(@output_list, $image_item->draw);
28502883
next;
28512884
}
@@ -2862,6 +2895,7 @@ sub image {
28622895
|| ref $image_item eq 'PGtikz');
28632896
my $imageURL = alias($image_item) // '';
28642897
$imageURL = ($envir{use_site_prefix}) ? $envir{use_site_prefix} . $imageURL : $imageURL;
2898+
my $id = $main::PG->getUniqueName('img');
28652899
my $out = "";
28662900

28672901
if ($displayMode eq 'TeX') {
@@ -2889,15 +2923,25 @@ sub image {
28892923
{
28902924
my $altattrib = '';
28912925
if (defined $alt_list[0]) { $altattrib = 'alt="' . encode_pg_and_html(shift @alt_list) . '"' }
2892-
$out =
2893-
qq!<IMG SRC="$imageURL" class="image-view-elt $valign" tabindex="0" role="button"$width_attrib$height_attrib $out_options{extra_html_tags} $altattrib>!;
2926+
$out .= tag(
2927+
'div',
2928+
class => 'image-container',
2929+
qq!<IMG SRC="$imageURL" class="image-view-elt $valign" tabindex="0" aria-details="${id}_details" role="button"$width_attrib$height_attrib $out_options{extra_html_tags} $altattrib>!
2930+
. ($desc ? ($description_details =~ s/LONG-DESCRIPTION-ID/${id}_details/r) : '')
2931+
);
28942932
} elsif ($displayMode eq 'PTX') {
28952933
my $ptxwidth = ($width ? int($width / 6) : 80);
2934+
$out = qq!<image width="$ptxwidth%" source="$imageURL">!;
28962935
if (defined $alt) {
2897-
$out = qq!<image width="$ptxwidth%" source="$imageURL"><description>$alt</description></image>!;
2898-
} else {
2899-
$out = qq!<image width="$ptxwidth%" source="$imageURL" />!;
2936+
$out .= "\n<shortdescription>$alt</shortdescription>";
2937+
}
2938+
if (defined $desc) {
2939+
$out .= "\n<description>\n" . PTX_cleanup($desc) . "\n</description>";
2940+
}
2941+
if (defined $alt || defined $desc) {
2942+
$out .= "\n";
29002943
}
2944+
$out .= '</image>';
29012945
} else {
29022946
$out = "Error: PGbasicmacros: image: Unknown displayMode: $displayMode.\n";
29032947
}

t/build_PG_envir.pl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
my $macros_dir = "$ENV{PG_ROOT}/macros";
1010

11+
use HTML::Entities;
12+
1113
use WeBWorK::PG::Environment;
1214
use WeBWorK::PG;
1315
use PGcore;

t/tikz_test/tikz_image.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ END_TIKZ
2323
ok my $img = image($drawing), 'img tag is generated';
2424

2525
like $img, qr!
26-
^<IMG\s
26+
^<div\sclass="img-container"><IMG\s
2727
SRC="/pg_files/tmp/images/([a-z0-9_-]*)\.svg"\s
2828
class="image-view-elt\smiddle"\s
2929
tabindex="0"\s
3030
role="button"\s
3131
width="200"\s*
32-
>$!x, 'img tag has correct format';
32+
></div>$!x, 'img tag has correct format';
3333

3434
# Note that the image file is not generated until after the `image($drawing)` call.
3535
my $image_file =

0 commit comments

Comments
 (0)