mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-31 19:30:21 +02:00
[3.0.0] <style> tags can now be extracted from input HTML using %HTML.ExtractStyleBlocks. These contents can be retrieved from $context->get('StyleBlocks');
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1468 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -79,6 +79,14 @@ It is not necessary and will have no effect for PHP 4.
|
||||
This directive has been available since 2.1.0.
|
||||
');
|
||||
|
||||
HTMLPurifier_ConfigSchema::define(
|
||||
'HTML', 'ExtractStyleBlocks', false, 'bool', '
|
||||
This directive enables extraction of <code>style</code> tags contents so
|
||||
that they can be incorporated in the <code>head</code> of the document,
|
||||
after sufficient validation.
|
||||
This directive has been available since 3.0.0.
|
||||
');
|
||||
|
||||
/**
|
||||
* Forgivingly lexes HTML (SGML-style) markup into tokens.
|
||||
*
|
||||
@@ -338,6 +346,34 @@ class HTMLPurifier_Lexer
|
||||
// represent non-SGML characters (horror, horror!)
|
||||
$html = HTMLPurifier_Encoder::cleanUTF8($html);
|
||||
|
||||
if ($config->get('HTML', 'ExtractStyleBlocks')) {
|
||||
// extract <style> CSS blocks
|
||||
$html = $this->extractStyleBlocks($html, $config, $context);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
private $_styleMatches = array();
|
||||
|
||||
/**
|
||||
* Save the contents of CSS blocks to style matches
|
||||
*/
|
||||
protected function styleCallback($matches) {
|
||||
$this->_styleMatches[] = $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes inline <style> tags from HTML, saves them for later use
|
||||
* @todo Extend to indicate non-text/css style blocks
|
||||
*/
|
||||
public function extractStyleBlocks($html, $config, $context) {
|
||||
$html = preg_replace_callback('#<style(?:\s.*)?>(.+)</style>#isU', array($this, 'styleCallback'), $html);
|
||||
$style_blocks = $this->_styleMatches;
|
||||
$this->_styleMatches = array(); // reset
|
||||
// this is a persistent context, so we have to overwrite it with every call
|
||||
if ($context->exists('StyleBlocks')) $context->destroy('StyleBlocks');
|
||||
$context->register('StyleBlocks', $style_blocks);
|
||||
return $html;
|
||||
}
|
||||
|
||||
|
@@ -186,6 +186,7 @@ class HTMLPurifier_Lexer_DOMLex extends HTMLPurifier_Lexer
|
||||
|
||||
$ret .= '<html><head>';
|
||||
$ret .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
|
||||
// No protection if $html contains a stray </div>!
|
||||
$ret .= '</head><body><div>'.$html.'</div></body></html>';
|
||||
return $ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user