openwebwork/pg — pull request #168 by goehle (2014-09-22 01:35:49+00:00)
Title: Encode PG Special Characters

This address WeBWorK issue #486. It adds `encode_pg_and_html` which adds html character escaping for ev3 and pgml special characters, in addition to html special characters.

I added `encode_pg_html` to
- alt tags for ImageGenerator
- PGML Escape
- AnswerValue protectHTML
- The text inputs in PGbasicmacros
- Input for PGessaymacros
- I changed quoteHTML to use `encode_pg_and_html` in
- problemRandomize
- problemPanic
- contextArbitraryString
- compundProblem

For testing plug the string
```
< > " & ' $ @ \ ` [ * _
```
into various answer fields and make sure that the results are encoded by viewing the page source. This is the problem I have been using to test things.
```
DOCUMENT();

loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"answerDiscussion.pl",
"problemRandomize.pl",
"problemPanic.pl",
"contextArbitraryString.pl",
"PGML.pl",
"PGessaymacros.pl"
);

TEXT(beginproblem());
ProblemRandomize('when'=>"always");

Context("Numeric");

$ans = Real("pi");
$M = Matrix([[1,1],[2,2]]);

$string = Value::protectHTML('< > " & $ @ \\ [ * _ ');
$string2 = PGML::Format::html->Escape('< > " & $ @ \\ [ * _ ');

Context()->texStrings;
BEGIN_TEXT
Panic Button: \{Panic::Button(label => "Request a Hint", penalty => .25)\}
(you will lose 25% of your points if you do)
$BR
protectHTML: $string
$BR
Escape: $string2
$BR
AribtraryString: \{ans_rule()\}
$BR
ANS BOX: \{ANS_BOX()\}
$BR
ANS RULE: \{ANS_RULE()\}
$BR
MATRIX: \{$M->ans_array()\}
$BR
Essay: \{essay_box\}
END_TEXT

Context("ArbitraryString");

ANS(Compute('< > " & $ @ \\ [ * _ ')->cmp(checker => sub {
my ($correct,$student,$ans) = @_;
$correct = $correct->value; # get perl string from String object
$student = $student->value; # ditto

return 1;
}));

Context()->normalStrings;

ANS($ans->with(tolerance=>.0001)->cmp);
ANS($ans->with(tolerance=>.0001)->cmp);
ANS($M->cmp);
ANS(essay_cmp());

ENDDOCUMENT();
```