Skip to content

Commit 3dba462

Browse files
committed
Fix feedback previews when the display mode is "images".
Currently if the display mode is "images", then the student and correct answer previews don't work. The images are never actually rendered, thus the image files are never created, and so the `alt` value is shown which is the original TeX for the answer. The reason for this is that the image generator runs before the post content processor runs. So equation images in the problem are rendered, but not those in the feedback. That was done because of the hack of inserting the string `MaRkEr` followed by the image number hash when the image is initially inserted into the problem text, and then later replacing that with the alignment styel when the image is rendered. That results in invalid HTML which `Mojo::DOM` doesn't like. To fix the issue the `MaRkEr` hack is reworked. Instead of that string, a `data-imagegen-alignment-marker` attribute is used whose value is the image number hash. Since that is valid HTML, `Mojo::DOM` is fine with it and leaves it as it is. So the image generator can now be run after the post content processor runs, and that renders the images in feedback as well. Care is needed for the images in feedback when the `data-imagegen-alignment-marker` is replaced. Since the rendered `img` tag is inside the `data-bs-content` attribute of the feedback button, it is HTML encoded. So instead of double quotes, the HTML double quote escape character (") is used.
1 parent bab8339 commit 3dba462

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

lib/WeBWorK/PG.pm

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,12 @@ sub new_helper ($invocant, %options) {
160160
);
161161
}
162162

163-
# HTML_dpng uses an ImageGenerator. We have to render the queued equations. This must be done before the post
164-
# processing, since the image tags output by the image generator initially include markers which are invalid html.
165-
# Mojo::DOM will change these markers into attributes with values and this will fail.
166-
if ($image_generator) {
167-
$image_generator->render(
168-
refresh => $options{refreshMath2img} // 0,
169-
body_text => $translator->r_text,
170-
);
171-
}
172-
173163
$translator->post_process_content if ref($translator->{rh_pgcore}) eq 'PGcore';
174164
$translator->stringify_answers;
175165

166+
$image_generator->render(body_text => $translator->r_text, refresh => $options{refreshMath2img} // 0)
167+
if $image_generator;
168+
176169
# Add the result summary set in post processing into the result.
177170
$result->{summary} = $translator->{rh_pgcore}{result_summary}
178171
if ref($translator->{rh_pgcore}) eq 'PGcore'

lib/WeBWorK/PG/ImageGenerator.pm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ sub add ($self, $string, $mode = 'inline') {
250250
# Determine what the image's "number" is.
251251
if ($useCache) {
252252
$imageNum = $self->{equationCache}->lookup($realString);
253-
$aligntag = 'MaRkEr' . $imageNum if $self->{useMarkers};
254-
$depths->{$imageNum} = 'none' if $self->{store_depths};
253+
$aligntag = qq{data-imagegen-alignment-marker="$imageNum"} if $self->{useMarkers};
254+
$depths->{$imageNum} = 'none' if $self->{store_depths};
255255
# Insert a slash after 2 characters. This effectively divides the images into 16^2 = 256 subdirectories.
256256
substr($imageNum, 2, 0) = '/';
257257
} else {
@@ -461,11 +461,16 @@ sub fix_markers ($self) {
461461

462462
my %depths = %{ $self->{depths} };
463463
for my $depthkey (keys %depths) {
464+
# The data-imagegen-alignment-marker value may be quoted with double quotes or with " if the image is
465+
# inside another HTML element attribute (such as for images in the feedback button). So both quote types need
466+
# to be checked, and the replaced style attribute needs to use the same quoting that it comes in with.
464467
if ($depths{$depthkey} eq 'none') {
465-
${ $self->{body_text} } =~ s/MaRkEr$depthkey/style="vertical-align:$self->{dvipng_align}"/g;
468+
${ $self->{body_text} } =~
469+
s/data-imagegen-alignment-marker=("|")$depthkey\1/style=$1vertical-align:$self->{dvipng_align}$1/g;
466470
} else {
467471
my $ndepth = 0 - $depths{$depthkey};
468-
${ $self->{body_text} } =~ s/MaRkEr$depthkey/style="vertical-align:${ndepth}px"/g;
472+
${ $self->{body_text} } =~
473+
s/data-imagegen-alignment-marker=("|")$depthkey\1/style=$1vertical-align:${ndepth}px$1/g;
469474
}
470475
}
471476
return;

0 commit comments

Comments
 (0)