From 8d6bfa40374989d432b74e89a0d92724b4bd38cf Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 21 Jan 2007 15:09:07 +0000 Subject: [PATCH] [1.4.0] YouTube preservation code added to the core by adding HTMLPurifier_Filter hierarchy. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@673 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 3 ++ TODO | 1 - docs/enduser-youtube.html | 7 +++-- library/HTMLPurifier.php | 17 +++++++++++ library/HTMLPurifier/Filter.php | 39 +++++++++++++++++++++++++ library/HTMLPurifier/Filter/YouTube.php | 34 +++++++++++++++++++++ smoketests/preserveYouTube.php | 29 +++--------------- 7 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 library/HTMLPurifier/Filter.php create mode 100644 library/HTMLPurifier/Filter/YouTube.php diff --git a/NEWS b/NEWS index 31ec8718..2bd97555 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,9 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier characters while %Core.Encoding is set to a non-UTF-8 encoding. ! Support for configuration directive aliases added ! Config object can now be instantiated from ini files +! YouTube preservation code added to the core, with two lines of code + you can add it as a filter to your code. See smoketests/preserveYouTube.php + for sample code. - Replaced version check with functionality check for DOM (thanks Stephen Khoo) . Added smoketest 'all.php', which loads all other smoketests via frames diff --git a/TODO b/TODO index 8942453c..6d5cf64f 100644 --- a/TODO +++ b/TODO @@ -8,7 +8,6 @@ TODO List ========================== 1.4 release - # Add hooks for custom behavior (for instance, YouTube preservation) - Aggressive caching ? Configuration profiles: sets of directives that get set with one func call diff --git a/docs/enduser-youtube.html b/docs/enduser-youtube.html index c70d7b44..0cfd3587 100644 --- a/docs/enduser-youtube.html +++ b/docs/enduser-youtube.html @@ -172,9 +172,10 @@ introduced after it has finished.

Future plans

-

It would probably be a good idea if this code was added to the core -library. Look out for the inclusion of this into the core as a decorator -or the like.

+

This functionality is part of the core library, using the +HTMLPurifier_Filter class to acheive the desired effect. Our implementation +is slightly different, and this page will be updated to reflect that +once 1.4.0 is released.

\ No newline at end of file diff --git a/library/HTMLPurifier.php b/library/HTMLPurifier.php index 3bd32887..0e1b54ea 100644 --- a/library/HTMLPurifier.php +++ b/library/HTMLPurifier.php @@ -67,6 +67,7 @@ class HTMLPurifier var $version = '1.3.2'; var $config; + var $filters; var $lexer, $strategy, $generator; @@ -94,6 +95,14 @@ class HTMLPurifier } + /** + * Adds a filter to process the output. First come first serve + * @param $filter HTMLPurifier_Filter object + */ + function addFilter($filter) { + $this->filters[] = $filter; + } + /** * Filters an HTML snippet/document to be XSS-free and standards-compliant. * @@ -111,6 +120,10 @@ class HTMLPurifier $context = new HTMLPurifier_Context(); $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); + for ($i = 0, $size = count($this->filters); $i < $size; $i++) { + $html = $this->filters[$i]->preFilter($html, $config, $context); + } + // purified HTML $html = $this->generator->generateFromTokens( @@ -126,6 +139,10 @@ class HTMLPurifier $config, $context ); + for ($i = $size - 1; $i >= 0; $i--) { + $html = $this->filters[$i]->postFilter($html, $config, $context); + } + $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context); $this->context =& $context; return $html; diff --git a/library/HTMLPurifier/Filter.php b/library/HTMLPurifier/Filter.php new file mode 100644 index 00000000..94c5ae7b --- /dev/null +++ b/library/HTMLPurifier/Filter.php @@ -0,0 +1,39 @@ +preFilter, + * 2->preFilter, 3->preFilter, purify, 3->postFilter, 2->postFilter, + * 1->postFilter. + */ + +class HTMLPurifier_Filter +{ + + /** + * Name of the filter for identification purposes + */ + var $name; + + /** + * Pre-processor function, handles HTML before HTML Purifier + */ + function preFilter($html, $config, &$context) {} + + /** + * Post-processor function, handles HTML after HTML Purifier + */ + function postFilter($html, $config, &$context) {} + +} + +?> \ No newline at end of file diff --git a/library/HTMLPurifier/Filter/YouTube.php b/library/HTMLPurifier/Filter/YouTube.php new file mode 100644 index 00000000..8abbb693 --- /dev/null +++ b/library/HTMLPurifier/Filter/YouTube.php @@ -0,0 +1,34 @@ +]+>.+?'. + 'http://www.youtube.com/v/([A-Za-z0-9]+).+?#'; + $pre_replace = '\1'; + return preg_replace($pre_regex, $pre_replace, $html); + } + + function postFilter($html, $config, &$context) { + $post_regex = '#([A-Za-z0-9]+)#'; + $post_replace = ''. + ''. + ''. + ''. + ''; + return preg_replace($post_regex, $post_replace, $html); + } + +} + +?> \ No newline at end of file diff --git a/smoketests/preserveYouTube.php b/smoketests/preserveYouTube.php index ef347b47..24820f8d 100644 --- a/smoketests/preserveYouTube.php +++ b/smoketests/preserveYouTube.php @@ -15,34 +15,13 @@ echo '';

HTML Purifier Preserve YouTube Smoketest

]+>.+?'. - 'http://www.youtube.com/v/([A-Za-z0-9]+).+?#'; - $pre_replace = '\1'; - $html = preg_replace($pre_regex, $pre_replace, $html); - $html = parent::purify($html, $config); - $post_regex = '#([A-Za-z0-9]+)#'; - $post_replace = ''. - ''. - ''. - ''. - ''; - $html = preg_replace($post_regex, $post_replace, $html); - return $html; - } -} - $string = ''; $regular_purifier = new HTMLPurifier(); -$youtube_purifier = new HTMLPurifierX_PreserveYouTube(); + +$youtube_purifier = new HTMLPurifier(); +require_once 'HTMLPurifier/Filter/YouTube.php'; +$youtube_purifier->addFilter(new HTMLPurifier_Filter_YouTube()); ?>

Unpurified