Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mathml #200

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions configdoc/usage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@
<line>279</line>
</file>
</directive>
<directive id="HTML.MathML">
<file name="HTMLPurifier/HTMLModuleManager.php">
<line>282</line>
</file>
</directive>
<directive id="Attr.IDBlacklist">
<file name="HTMLPurifier/IDAccumulator.php">
<line>27</line>
Expand Down
2 changes: 2 additions & 0 deletions library/HTMLPurifier.includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
require 'HTMLPurifier/HTMLModule/Image.php';
require 'HTMLPurifier/HTMLModule/Legacy.php';
require 'HTMLPurifier/HTMLModule/List.php';
require 'HTMLPurifier/HTMLModule/MathML.php';
require 'HTMLPurifier/HTMLModule/Name.php';
require 'HTMLPurifier/HTMLModule/Nofollow.php';
require 'HTMLPurifier/HTMLModule/NonXMLCommonAttributes.php';
Expand Down Expand Up @@ -191,6 +192,7 @@
require 'HTMLPurifier/Injector/AutoParagraph.php';
require 'HTMLPurifier/Injector/DisplayLinkURI.php';
require 'HTMLPurifier/Injector/Linkify.php';
require 'HTMLPurifier/Injector/MathMLSpaceNormalize.php';
require 'HTMLPurifier/Injector/PurifierLinkify.php';
require 'HTMLPurifier/Injector/RemoveEmpty.php';
require 'HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php';
Expand Down
2 changes: 2 additions & 0 deletions library/HTMLPurifier.safe-includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
require_once $__dir . '/HTMLPurifier/HTMLModule/Image.php';
require_once $__dir . '/HTMLPurifier/HTMLModule/Legacy.php';
require_once $__dir . '/HTMLPurifier/HTMLModule/List.php';
require_once $__dir . '/HTMLPurifier/HTMLModule/MathML.php';
require_once $__dir . '/HTMLPurifier/HTMLModule/Name.php';
require_once $__dir . '/HTMLPurifier/HTMLModule/Nofollow.php';
require_once $__dir . '/HTMLPurifier/HTMLModule/NonXMLCommonAttributes.php';
Expand Down Expand Up @@ -185,6 +186,7 @@
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/MathMLSpaceNormalize.php';
require_once $__dir . '/HTMLPurifier/Injector/PurifierLinkify.php';
require_once $__dir . '/HTMLPurifier/Injector/RemoveEmpty.php';
require_once $__dir . '/HTMLPurifier/Injector/RemoveSpansWithoutAttributes.php';
Expand Down
25 changes: 25 additions & 0 deletions library/HTMLPurifier/AttrDef/MathML/Character.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Validates the MathML attribute character.
*/

class HTMLPurifier_AttrDef_MathML_Character extends HTMLPurifier_AttrDef
{

/**
* @param string $char
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string
*/
public function validate($char, $config, $context)
{
if (mb_strlen($char) == 1 && strpos(' \t\n\r', $char) === false) {
return $char;
} elseif (preg_match('/&((#[0-9]+)|(#x[0-9A-Fa-f]+)|([0-9A-Za-z]+));/', $char)) {
return $char;
}
return false;
}
}
44 changes: 44 additions & 0 deletions library/HTMLPurifier/AttrDef/MathML/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Validates Color as defined by MathML.
*/
class HTMLPurifier_AttrDef_MathML_Color extends HTMLPurifier_AttrDef
{

// MathML 3 only accepts HTML4 color names + transparent
static protected $colornames = array(
'aqua',
'black',
'blue',
'fuchsia',
'gray',
'green',
'lime',
'maroon',
'navy',
'olive',
'purple',
'red',
'silver',
'teal',
'white',
'yellow',
'transparent'
);

public function validate($color, $config, $context)
{

$color = trim($color);

if (preg_match('/(#[0-9A-Fa-f]{3})|(#[0-9A-Fa-f]{6})/', $color) || in_array(strtolower($color), static::$colornames)) {
return $color;
}

return false;
}

}

// vim: et sw=4 sts=4
77 changes: 77 additions & 0 deletions library/HTMLPurifier/AttrDef/MathML/ID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* Validates the MathML attribute ID.
* @note This just checks that the ID is valid. It explicitly avoids checking
* or adding to the ID Accumulator because the MathML 3 DTD makes it a
* point to allow repeated IDs.
*/

class HTMLPurifier_AttrDef_MathML_ID extends HTMLPurifier_AttrDef
{

/**
* @param string $id
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string
*/
public function validate($id, $config, $context)
{

$id = trim($id); // trim it first

if ($id === '') {
return false;
}

$prefix = $config->get('Attr.IDPrefix');
if ($prefix !== '') {
$prefix .= $config->get('Attr.IDPrefixLocal');
// prevent re-appending the prefix
if (strpos($id, $prefix) !== 0) {
$id = $prefix . $id;
}
} elseif ($config->get('Attr.IDPrefixLocal') !== '') {
trigger_error(
'%Attr.IDPrefixLocal cannot be used unless ' .
'%Attr.IDPrefix is set',
E_USER_WARNING
);
}

// we purposely avoid using regex, hopefully this is faster

if ($config->get('Attr.ID.HTML5') === true) {
if (preg_match('/[\t\n\x0b\x0c ]/', $id)) {
return false;
}
} else {
if (ctype_alpha($id)) {
// OK
} else {
if (!ctype_alpha(@$id[0])) {
return false;
}
// primitive style of regexps, I suppose
$trim = trim(
$id,
'A..Za..z0..9:-._'
);
if ($trim !== '') {
return false;
}
}
}

$regexp = $config->get('Attr.IDBlacklistRegexp');
if ($regexp && preg_match($regexp, $id)) {
return false;
}

// if no change was made to the ID, return the result
// else, return the new id if stripping whitespace made it
// valid, or return false.
return $id;
}
}
36 changes: 36 additions & 0 deletions library/HTMLPurifier/AttrDef/MathML/Length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Validates the MathML type length (not to be confused with either HTML nor
* CSS's length).
*/

class HTMLPurifier_AttrDef_MathML_Length extends HTMLPurifier_AttrDef
{

/**
* @param string $string
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string
*/
public function validate($string, $config, $context)
{
$string = $this->parseCDATA($string);

// Optimizations
if ($string === '') {
return false;
}
if ($string === '0') {
return '0';
}

$length = HTMLPurifier_MathMLLength::make($string);
if (!$length->isValid()) {
return false;
}

return $length->toString();
}
}
13 changes: 13 additions & 0 deletions library/HTMLPurifier/AttrTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,23 @@ public function __construct()

// "proprietary" types
$this->info['Class'] = new HTMLPurifier_AttrDef_HTML_Class();
$this->info['CSS'] = new HTMLPurifier_AttrDef_CSS();

// number is really a positive integer (one or more digits)
// FIXME: ^^ not always, see start and value of list items
$this->info['Number'] = new HTMLPurifier_AttrDef_Integer(false, false, true);

// MathML types
$this->info['MathML_ID'] = new HTMLPurifier_AttrDef_MathML_ID();
$this->info['MathML_Length'] = new HTMLPurifier_AttrDef_MathML_Length();
$this->info['MathML_UnsignedInteger'] = new HTMLPurifier_AttrDef_Integer(false, true, true);
$this->info['MathML_PositiveInteger'] = new HTMLPurifier_AttrDef_Integer(false, false, true);
$this->info['MathML_Integer'] = new HTMLPurifier_AttrDef_Integer(true, true, true);
$this->info['MathML_UnsignedNumber'] = new HTMLPurifier_AttrDef_CSS_Number(true);
$this->info['MathML_Number'] = new HTMLPurifier_AttrDef_CSS_Number(false);
$this->info['MathML_Character'] = new HTMLPurifier_AttrDef_MathML_Character();
$this->info['MathML_Color'] = new HTMLPurifier_AttrDef_MathML_Color();

}

private static function makeEnum($in)
Expand Down
2 changes: 1 addition & 1 deletion library/HTMLPurifier/ConfigSchema/schema.ser

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions library/HTMLPurifier/ConfigSchema/schema/HTML.MathML.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTML.MathML
TYPE: bool
DEFAULT: false
--DESCRIPTION--
<p>
Enable MathML.</p>
--# vim: et sw=4 sts=4
Loading