Skip to content

Commit

Permalink
Merge branch 'wip-MDL-26423' of git://github.com/sammarshallou/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Mar 7, 2011
2 parents 5f9f071 + b031caf commit 2c10c5f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
26 changes: 26 additions & 0 deletions lib/simpletest/testweblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ public function test_clean_text() {
$this->assertEqual('lala xx', clean_text($text, FORMAT_MOODLE));
$this->assertEqual('lala xx', clean_text($text, FORMAT_HTML));
}

/**
* Tests the 'allowid' option for format_text.
*/
public function test_format_text_allowid() {
global $CFG;

// This test relates only to html purifier, so switch to it if not in use
$oldcfg = $CFG->enablehtmlpurifier;
$CFG->enablehtmlpurifier = true;

// Start off by not allowing ids (default)
$options = array(
'nocache' => true
);
$result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
$this->assertEqual('<div>Frog</div>', $result);

// Now allow ids
$options['allowid'] = true;
$result = format_text('<div id="example">Frog</div>', FORMAT_HTML, $options);
$this->assertEqual('<div id="example">Frog</div>', $result);

// Switch config back
$CFG->enablehtmlpurifier = $oldcfg;
}
}


30 changes: 22 additions & 8 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,8 @@ function format_text_menu() {
* context : The context that will be used for filtering.
* overflowdiv : If set to true the formatted text will be encased in a div
* with the class no-overflow before being returned. Default false.
* allowid : If true then id attributes will not be removed, even when
* using htmlpurifier. Default false.
* </pre>
*
* @todo Finish documenting this function
Expand Down Expand Up @@ -1069,7 +1071,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
switch ($format) {
case FORMAT_HTML:
if (!$options['noclean']) {
$text = clean_text($text, FORMAT_HTML);
$text = clean_text($text, FORMAT_HTML, $options);
}
$text = $filtermanager->filter_text($text, $context, array('originalformat' => FORMAT_HTML));
break;
Expand All @@ -1092,15 +1094,15 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
case FORMAT_MARKDOWN:
$text = markdown_to_html($text);
if (!$options['noclean']) {
$text = clean_text($text, FORMAT_HTML);
$text = clean_text($text, FORMAT_HTML, $options);
}
$text = $filtermanager->filter_text($text, $context, array('originalformat' => FORMAT_MARKDOWN));
break;

default: // FORMAT_MOODLE or anything else
$text = text_to_html($text, null, $options['para'], $options['newlines']);
if (!$options['noclean']) {
$text = clean_text($text, FORMAT_HTML);
$text = clean_text($text, FORMAT_HTML, $options);
}
$text = $filtermanager->filter_text($text, $context, array('originalformat' => $format));
break;
Expand Down Expand Up @@ -1445,9 +1447,11 @@ function trusttext_active() {
*
* @param string $text The text to be cleaned
* @param int $format deprecated parameter, should always contain FORMAT_HTML or FORMAT_MOODLE
* @param array $options Array of options; currently only option supported is 'allowid' (if true,
* does not remove id attributes when cleaning)
* @return string The cleaned up text
*/
function clean_text($text, $format = FORMAT_HTML) {
function clean_text($text, $format = FORMAT_HTML, $options = array()) {
global $ALLOWED_TAGS, $CFG;

if (empty($text) or is_numeric($text)) {
Expand All @@ -1464,7 +1468,7 @@ function clean_text($text, $format = FORMAT_HTML) {
}

if (!empty($CFG->enablehtmlpurifier)) {
$text = purify_html($text);
$text = purify_html($text, $options);
} else {
/// Fix non standard entity notations
$text = fix_non_standard_entities($text);
Expand Down Expand Up @@ -1492,16 +1496,19 @@ function clean_text($text, $format = FORMAT_HTML) {
*
* @global object
* @param string $text The (X)HTML string to purify
* @param array $options Array of options; currently only option supported is 'allowid' (if set,
* does not remove id attributes when cleaning)
*/
function purify_html($text) {
function purify_html($text, $options = array()) {
global $CFG;

// this can not be done only once because we sometimes need to reset the cache
$cachedir = $CFG->dataroot.'/cache/htmlpurifier';
check_dir_exists($cachedir);

static $purifier = false;
if ($purifier === false) {
$type = !empty($options['allowid']) ? 'allowid' : 'normal';
static $purifiers = array();
if (empty($purifiers[$type])) {
require_once $CFG->libdir.'/htmlpurifier/HTMLPurifier.safe-includes.php';
$config = HTMLPurifier_Config::createDefault();

Expand All @@ -1522,6 +1529,10 @@ function purify_html($text) {
$config->set('HTML.SafeEmbed', true);
}

if ($type === 'allowid') {
$config->set('Attr.EnableID', true);
}

$def = $config->getHTMLDefinition(true);
$def->addElement('nolink', 'Block', 'Flow', array()); // skip our filters inside
$def->addElement('tex', 'Inline', 'Inline', array()); // tex syntax, equivalent to $$xx$$
Expand All @@ -1530,6 +1541,9 @@ function purify_html($text) {
$def->addAttribute('span', 'xxxlang', 'CDATA'); // current problematic multilang

$purifier = new HTMLPurifier($config);
$purifiers[$type] = $purifier;
} else {
$purifier = $purifiers[$type];
}

$multilang = (strpos($text, 'class="multilang"') !== false);
Expand Down

0 comments on commit 2c10c5f

Please sign in to comment.