diff options
author | emkael <emkael@tlen.pl> | 2016-10-31 21:58:33 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-10-31 21:59:22 +0100 |
commit | d216b3147bc3f37cf2337acab5767c6a4f74aa2e (patch) | |
tree | 6090989e5071db101a1112131e2b075a02dccbc4 /lib/phptal/PHPTAL/Dom/CDATASection.php | |
parent | b23bfbb17d1d5f6852a1690f246a84c2d38ae969 (diff) |
* PHPTAL library
Diffstat (limited to 'lib/phptal/PHPTAL/Dom/CDATASection.php')
-rw-r--r-- | lib/phptal/PHPTAL/Dom/CDATASection.php | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/phptal/PHPTAL/Dom/CDATASection.php b/lib/phptal/PHPTAL/Dom/CDATASection.php new file mode 100644 index 0000000..838429b --- /dev/null +++ b/lib/phptal/PHPTAL/Dom/CDATASection.php @@ -0,0 +1,49 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel LesiĆski <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * Outputs <![CDATA[ ]]> blocks, sometimes converts them to text + * @todo this might be moved to CDATA processing in Element + * + * @package PHPTAL + * @subpackage Dom + */ +class PHPTAL_Dom_CDATASection extends PHPTAL_Dom_Node +{ + public function generateCode(PHPTAL_Php_CodeWriter $codewriter) + { + $mode = $codewriter->getOutputMode(); + $value = $this->getValueEscaped(); + $inCDATAelement = PHPTAL_Dom_Defs::getInstance()->isCDATAElementInHTML($this->parentNode->getNamespaceURI(), $this->parentNode->getLocalName()); + + // in HTML5 must limit it to <script> and <style> + if ($mode === PHPTAL::HTML5 && $inCDATAelement) { + $codewriter->pushHTML($codewriter->interpolateCDATA(str_replace('</', '<\/', $value))); + } elseif (($mode === PHPTAL::XHTML && $inCDATAelement) // safe for text/html + || ($mode === PHPTAL::XML && preg_match('/[<>&]/', $value)) // non-useless in XML + || ($mode !== PHPTAL::HTML5 && preg_match('/<\?|\${structure/', $value))) // hacks with structure (in X[HT]ML) may need it + { + // in text/html "</" is dangerous and the only sensible way to escape is ECMAScript string escapes. + if ($mode === PHPTAL::XHTML) $value = str_replace('</', '<\/', $value); + + $codewriter->pushHTML($codewriter->interpolateCDATA('<![CDATA['.$value.']]>')); + } else { + $codewriter->pushHTML($codewriter->interpolateHTML( + htmlspecialchars($value, ENT_QUOTES, $codewriter->getEncoding()) + )); + } + } +} |