[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Renderers; 9 10 use s9e\TextFormatter\Renderer; 11 use XSLTProcessor; 12 13 class XSLT extends Renderer 14 { 15 /** 16 * @var XSLTProcessor The lazy-loaded XSLTProcessor instance used by this renderer 17 */ 18 protected $proc; 19 20 /** 21 * @var bool Whether parameters need to be reloaded 22 */ 23 protected $reloadParams = false; 24 25 /** 26 * @var string The stylesheet used by this renderer 27 */ 28 protected $stylesheet; 29 30 /** 31 * Constructor 32 * 33 * @param string $stylesheet The stylesheet used to render intermediate representations 34 */ 35 public function __construct($stylesheet) 36 { 37 $this->stylesheet = $stylesheet; 38 39 // Capture the parameters' values from the stylesheet 40 preg_match_all('#<xsl:param name="([^"]+)"(?>/>|>([^<]+))#', $stylesheet, $matches); 41 foreach ($matches[1] as $k => $paramName) 42 { 43 $this->params[$paramName] = htmlspecialchars_decode($matches[2][$k]); 44 } 45 } 46 47 /** 48 * Serializer 49 * 50 * @return string[] List of properties to serialize 51 */ 52 public function __sleep() 53 { 54 $props = get_object_vars($this); 55 unset($props['proc']); 56 57 if (empty($props['reloadParams'])) 58 { 59 unset($props['reloadParams']); 60 } 61 62 return array_keys($props); 63 } 64 65 /** 66 * Unserialize helper 67 * 68 * Will reload parameters if they were changed between generation and serialization 69 * 70 * @return void 71 */ 72 public function __wakeup() 73 { 74 if (!empty($this->reloadParams)) 75 { 76 $this->setParameters($this->params); 77 $this->reloadParams = false; 78 } 79 } 80 81 /** 82 * {@inheritdoc} 83 */ 84 public function setParameter($paramName, $paramValue) 85 { 86 /** 87 * @link https://bugs.php.net/64137 88 */ 89 if (strpos($paramValue, '"') !== false && strpos($paramValue, "'") !== false) 90 { 91 $paramValue = str_replace('"', "\xEF\xBC\x82", $paramValue); 92 } 93 else 94 { 95 $paramValue = (string) $paramValue; 96 } 97 98 if (!isset($this->params[$paramName]) || $this->params[$paramName] !== $paramValue) 99 { 100 $this->load(); 101 $this->proc->setParameter('', $paramName, $paramValue); 102 $this->params[$paramName] = $paramValue; 103 $this->reloadParams = true; 104 } 105 } 106 107 /** 108 * {@inheritdoc} 109 */ 110 protected function renderRichText($xml) 111 { 112 // Load the intermediate representation 113 $dom = $this->loadXML($xml); 114 115 // Load the stylesheet 116 $this->load(); 117 118 // Perform the transformation and cast it as a string because it may return NULL if the 119 // transformation didn't output anything 120 $this->setLocale(); 121 $output = (string) $this->proc->transformToXml($dom); 122 $this->restoreLocale(); 123 124 // XSLTProcessor does not correctly identify <embed> as a void element. We fix it by 125 // removing </embed> end tags 126 $output = str_replace('</embed>', '', $output); 127 128 // Remove the \n that XSL adds at the end of the output, if applicable 129 if (substr($output, -1) === "\n") 130 { 131 $output = substr($output, 0, -1); 132 } 133 134 // Force HTML attributes to use double quotes to be consistent with the PHP renderer 135 if (strpos($output, "='") !== false) 136 { 137 $output = $this->normalizeAttributes($output); 138 } 139 140 return $output; 141 } 142 143 /** 144 * Create an XSLTProcessor and load the stylesheet 145 * 146 * @return void 147 */ 148 protected function load() 149 { 150 if (!isset($this->proc)) 151 { 152 $xsl = $this->loadXML($this->stylesheet); 153 154 $this->proc = new XSLTProcessor; 155 $this->proc->importStylesheet($xsl); 156 } 157 } 158 159 /** 160 * Normalize given attribute's value to use double quotes 161 * 162 * @param string[] $m 163 * @return string 164 */ 165 protected function normalizeAttribute(array $m) 166 { 167 if ($m[0][0] === '"') 168 { 169 return $m[0]; 170 } 171 172 return '"' . str_replace('"', '"', substr($m[0], 1, -1)) . '"'; 173 } 174 175 /** 176 * Normalize all attributes in given HTML to use double quotes 177 * 178 * @param string $html 179 * @return string 180 */ 181 protected function normalizeAttributes($html) 182 { 183 return preg_replace_callback('(<\\S++ [^>]++>)', [$this, 'normalizeElement'], $html); 184 } 185 186 /** 187 * Normalize attributes in given element to use double quotes 188 * 189 * @param string[] $m 190 * @return string 191 */ 192 protected function normalizeElement(array $m) 193 { 194 if (strpos($m[0], "='") === false) 195 { 196 return $m[0]; 197 } 198 199 return preg_replace_callback('((?:"[^"]*"|\'[^\']*\'))S', [$this, 'normalizeAttribute'], $m[0]); 200 } 201 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |