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