1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 19:30:21 +02:00

Further refactor ConfigDoc, creating HTMLXSLTProcessor. Update NEWS.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1104 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-05-28 02:55:50 +00:00
parent c113f43440
commit aaf4839c34
7 changed files with 90 additions and 49 deletions

View File

@@ -1,5 +1,6 @@
<?php
require_once 'ConfigDoc/HTMLXSLTProcessor.php';
require_once 'ConfigDoc/XMLSerializer/Types.php';
require_once 'ConfigDoc/XMLSerializer/ConfigSchema.php';

View File

@@ -0,0 +1,56 @@
<?php
/**
* Special XSLTProcessor specifically for HTML documents. Loosely
* based off of XSLTProcessor, but not really
*/
class ConfigDoc_HTMLXSLTProcessor
{
protected $xsltProcessor;
public function __construct() {
$this->xsltProcessor = new XSLTProcessor();
}
/**
* Imports stylesheet for processor to use
* @param $xsl XSLT DOM tree, or filename of the XSL transformation
*/
public function importStylesheet($xsl) {
if (is_string($xsl)) {
$xsl_file = $xsl;
$xsl = new DOMDocument();
$xsl->load($xsl_file);
}
return $this->xsltProcessor->importStylesheet($xsl);
}
/**
* Transforms an XML file into HTML based on the stylesheet
* @param $xml XML DOM tree
*/
public function transformToHTML($xml) {
$out = $this->xsltProcessor->transformToXML($xml);
// fudges for HTML backwards compatibility
$out = str_replace('/>', ' />', $out); // <br /> not <br/>
$out = str_replace(' xmlns=""', '', $out); // rm unnecessary xmlns
if (class_exists('Tidy')) {
// cleanup output
$config = array(
'indent' => true,
'output-xhtml' => true,
'wrap' => 80
);
$tidy = new Tidy;
$tidy->parseString($out, $config, 'utf8');
$tidy->cleanRepair();
$out = (string) $tidy;
}
return $out;
}
}
?>

View File

@@ -1,9 +1,26 @@
<?php
/**
* The XMLSerializer hierarchy of classes consist of classes that take
* objects and serialize them into XML, specifically DOM, form; this
* super-class contains convenience functions for those classes.
*/
class ConfigDoc_XMLSerializer
{
protected function appendHTMLDiv($document, $node, $html) {
// oh no, it's a global!
global $purifier;
$html = $purifier->purify($html);
$dom_html = $document->createDocumentFragment();
$dom_html->appendXML($html);
$dom_div = $document->createElement('div');
$dom_div->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$dom_div->appendChild($dom_html);
$node->appendChild($dom_div);
}
}

View File

@@ -10,7 +10,7 @@ class ConfigDoc_XMLSerializer_ConfigSchema extends ConfigDoc_XMLSerializer
* @todo Split into sub-serializers
* @param $schema HTMLPurifier_ConfigSchema to serialize
*/
function serialize($schema) {
public function serialize($schema) {
$dom_document = new DOMDocument('1.0', 'UTF-8');
$dom_root = $dom_document->createElement('configdoc');
$dom_document->appendChild($dom_root);
@@ -35,7 +35,7 @@ class ConfigDoc_XMLSerializer_ConfigSchema extends ConfigDoc_XMLSerializer
);
$dom_namespace_description = $dom_document->createElement('description');
$dom_namespace->appendChild($dom_namespace_description);
appendHTMLDiv($dom_document, $dom_namespace_description,
$this->appendHTMLDiv($dom_document, $dom_namespace_description,
$schema->info_namespace[$namespace_name]->description);
foreach ($namespace_info as $name => $info) {
@@ -100,7 +100,7 @@ class ConfigDoc_XMLSerializer_ConfigSchema extends ConfigDoc_XMLSerializer
$dom_description->setAttribute('file', $file);
$dom_description->setAttribute('line', $line);
}
appendHTMLDiv($dom_document, $dom_description, $description);
$this->appendHTMLDiv($dom_document, $dom_description, $description);
$dom_descriptions->appendChild($dom_description);
}
}

View File

@@ -9,7 +9,7 @@ class ConfigDoc_XMLSerializer_Types extends ConfigDoc_XMLSerializer
* Serializes the types in a schema into DOM form
* @param $schema HTMLPurifier_ConfigSchema owner of types to serialize
*/
function serialize($schema) {
public function serialize($schema) {
$types_document = new DOMDocument('1.0', 'UTF-8');
$types_root = $types_document->createElement('types');
$types_document->appendChild($types_root);