OPPG-413: Changed how HTML is handled for text. Any text widget with tags specified will now be treated as HTML

namespaces
Frank Ronny Larsen 2013-07-11 14:36:31 +02:00
parent 27345e22f8
commit 4509626a0d
1 changed files with 10 additions and 14 deletions

View File

@ -1190,31 +1190,25 @@ class H5PContentValidator {
* Validate given text value against text semantics. * Validate given text value against text semantics.
*/ */
public function validateText(&$text, $semantics) { public function validateText(&$text, $semantics) {
if (isset($semantics->widget) && $semantics->widget == 'html') { if (isset($semantics->tags)) {
// Build allowed tag list, based in $semantics->tags and known defaults. // Not testing for empty array allows us to use the 4 defaults without
// These four are always allowed. // specifying them in semantics.
$tags = array('div', 'span', 'p', 'br'); $tags = array_merge(array('div', 'span', 'p', 'br'), $semantics->tags);
if (! isset($semantics->tags)) {
// Add defaults used in javascript.
$tags = array_merge($tags, array('strong', 'em', 'del', 'h2', 'h3', 'a', 'ul', 'ol', 'table', 'hr'));
}
else {
$tags = array_merge($tags, $semantics->tags);
}
// Add related tags for table etc. // Add related tags for table etc.
if (in_array('table', $tags)) { if (in_array('table', $tags)) {
$tags = array_merge($tags, array('tr', 'td', 'th', 'colgroup', 'thead', 'tbody', 'tfoot')); $tags = array_merge($tags, array('tr', 'td', 'th', 'colgroup', 'thead', 'tbody', 'tfoot'));
} }
if (in_array('b', $tags)) { if (in_array('b', $tags) && ! in_array('strong', $tags)) {
$tags[] = 'strong'; $tags[] = 'strong';
} }
if (in_array('i', $tags)) { if (in_array('i', $tags) && ! in_array('em', $tags)) {
$tags[] = 'em'; $tags[] = 'em';
} }
if (in_array('ul', $tags) || in_array('ol', $tags)) { if (in_array('ul', $tags) || in_array('ol', $tags) && ! in_array('li', $tags)) {
$tags[] = 'li'; $tags[] = 'li';
} }
// Convert array of tagNames to string of bracketed tags
$allowedtags = implode('', array_map(array($this, 'bracketTags'), $tags)); $allowedtags = implode('', array_map(array($this, 'bracketTags'), $tags));
// Strip invalid HTML tags. // Strip invalid HTML tags.
@ -1224,10 +1218,12 @@ class H5PContentValidator {
// Filter text to plain text. // Filter text to plain text.
$text = htmlspecialchars($text); $text = htmlspecialchars($text);
} }
// Check if string is within allowed length // Check if string is within allowed length
if (isset($semantics->maxLength)) { if (isset($semantics->maxLength)) {
$text = mb_substr($text, 0, $semantics->maxLength); $text = mb_substr($text, 0, $semantics->maxLength);
} }
// Check if string is according to optional regexp in semantics // Check if string is according to optional regexp in semantics
if (isset($semantics->regexp)) { if (isset($semantics->regexp)) {
$pattern = '|' . $semantics->regexp->pattern . '|'; $pattern = '|' . $semantics->regexp->pattern . '|';