0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-09 15:31:53 +00:00

MOODLE-556: Normalization of whitespace in MathML token elements

This commit is contained in:
Xavier Ripoll 2018-10-17 17:19:54 +02:00
parent e8f71f91da
commit a721ae0ae6
4 changed files with 45 additions and 0 deletions

View File

@ -192,6 +192,7 @@ require 'HTMLPurifier/HTMLModule/Tidy/XHTML.php';
require 'HTMLPurifier/Injector/AutoParagraph.php';
require 'HTMLPurifier/Injector/DisplayLinkURI.php';
require 'HTMLPurifier/Injector/Linkify.php';
require 'HTMLPurifier/Injector/MathSpaceNormalize.php';
require 'HTMLPurifier/Injector/PurifierLinkify.php';
require 'HTMLPurifier/Injector/RemoveEmpty.php';
require 'HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php';

View File

@ -186,6 +186,7 @@ require_once $__dir . '/HTMLPurifier/HTMLModule/Tidy/XHTML.php';
require_once $__dir . '/HTMLPurifier/Injector/AutoParagraph.php';
require_once $__dir . '/HTMLPurifier/Injector/DisplayLinkURI.php';
require_once $__dir . '/HTMLPurifier/Injector/Linkify.php';
require_once $__dir . '/HTMLPurifier/Injector/MathSpaceNormalize.php';
require_once $__dir . '/HTMLPurifier/Injector/PurifierLinkify.php';
require_once $__dir . '/HTMLPurifier/Injector/RemoveEmpty.php';
require_once $__dir . '/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php';

View File

@ -19,6 +19,9 @@ class HTMLPurifier_HTMLModule_Math extends HTMLPurifier_HTMLModule
public function setup($config)
{
// Normalize whitespace inside text elements as per MathML spec 2.1.7
$this->info_injector[] = new HTMLPurifier_Injector_MathSpaceNormalize();
/*****************************************************************
* Meta variables
* Used in this file to simplify code and help adapt the DTD

View File

@ -0,0 +1,40 @@
<?php
class HTMLPurifier_Injector_MathSpaceNormalize extends HTMLPurifier_Injector
{
/**
* Elements to apply handleText to.
* These are those that accept #PCDATA except <cs> and <cbytes>.
* @type array
*/
protected $tags = array('mi', 'mn', 'mo', 'ms', 'mtext', 'ci', 'cn', 'csymbol', 'annotation');
/**
* @param HTMLPurifier_Token $token
*/
public function handleText(&$token)
{
// No parent tag => return to avoid error on following line
if (count($this->currentNesting) == 0) {
return;
}
// Get the parent tag
$parent_token = $this->currentNesting[count($this->currentNesting) - 1];
// If we're not in a "token element" (specified in $tags above), return
if ($parent_token === null || !in_array($parent_token->name, $this->tags)) {
return;
}
// Replace as per the MathML specification, section 2.1.7
$token->data = preg_replace(
'/[ \t\n\r]+/',
' ',
trim($token->data) // Using trim($token->data, ' \t\n\r') trims t,n,r
);
}
}