Release 4.5.0
[htmlpurifier.git] / extras / ConfigDoc / HTMLXSLTProcessor.php
blobf7095285bb1b68a47bc657c22e5cc4e6742e8c6a
1 <?php
3 /**
4 * Decorator/extender XSLT processor specifically for HTML documents.
5 */
6 class ConfigDoc_HTMLXSLTProcessor
9 /**
10 * Instance of XSLTProcessor
12 protected $xsltProcessor;
14 public function __construct($proc = false) {
15 if ($proc === false) $proc = new XSLTProcessor();
16 $this->xsltProcessor = $proc;
19 /**
20 * @note Allows a string $xsl filename to be passed
22 public function importStylesheet($xsl) {
23 if (is_string($xsl)) {
24 $xsl_file = $xsl;
25 $xsl = new DOMDocument();
26 $xsl->load($xsl_file);
28 return $this->xsltProcessor->importStylesheet($xsl);
31 /**
32 * Transforms an XML file into compatible XHTML based on the stylesheet
33 * @param $xml XML DOM tree, or string filename
34 * @return string HTML output
35 * @todo Rename to transformToXHTML, as transformToHTML is misleading
37 public function transformToHTML($xml) {
38 if (is_string($xml)) {
39 $dom = new DOMDocument();
40 $dom->load($xml);
41 } else {
42 $dom = $xml;
44 $out = $this->xsltProcessor->transformToXML($dom);
46 // fudges for HTML backwards compatibility
47 // assumes that document is XHTML
48 $out = str_replace('/>', ' />', $out); // <br /> not <br/>
49 $out = str_replace(' xmlns=""', '', $out); // rm unnecessary xmlns
51 if (class_exists('Tidy')) {
52 // cleanup output
53 $config = array(
54 'indent' => true,
55 'output-xhtml' => true,
56 'wrap' => 80
58 $tidy = new Tidy;
59 $tidy->parseString($out, $config, 'utf8');
60 $tidy->cleanRepair();
61 $out = (string) $tidy;
64 return $out;
67 /**
68 * Bulk sets parameters for the XSL stylesheet
69 * @param array $options Associative array of options to set
71 public function setParameters($options) {
72 foreach ($options as $name => $value) {
73 $this->xsltProcessor->setParameter('', $name, $value);
77 /**
78 * Forward any other calls to the XSLT processor
80 public function __call($name, $arguments) {
81 call_user_func_array(array($this->xsltProcessor, $name), $arguments);
86 // vim: et sw=4 sts=4