2009-10-11 02:07:27 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This class acts as a wrapper around the built in DOMDocument class in order to use it to manage a HTML snippet,
|
|
|
|
* rather than a whole document, while still exposing the DOMDocument API.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-10-11 02:07:27 +02:00
|
|
|
* @subpackage integration
|
|
|
|
*/
|
|
|
|
class SS_HTMLValue extends ViewableData {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var DOMDocument
|
|
|
|
*/
|
|
|
|
protected $document;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $content
|
|
|
|
*/
|
|
|
|
public function __construct($content = null) {
|
2012-10-16 00:59:30 +02:00
|
|
|
$this->setDocument(new DOMDocument('1.0', 'UTF-8'));
|
|
|
|
$this->setScrictErrorChecking(false);
|
|
|
|
$this->setOutputFormatting(false);
|
2009-10-11 02:07:27 +02:00
|
|
|
$this->setContent($content);
|
2012-10-16 00:59:30 +02:00
|
|
|
|
2009-10-11 02:07:27 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Should strict error checking be used?
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
|
|
|
public function setScrictErrorChecking($bool) {
|
|
|
|
$this->getDocument()->scrictErrorChecking = $bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should the output be formatted?
|
|
|
|
* @param boolean $bool
|
|
|
|
*/
|
|
|
|
public function setOutputFormatting($bool) {
|
|
|
|
$this->getDocument()->formatOutput = $bool;
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:27 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent() {
|
2012-09-26 23:34:00 +02:00
|
|
|
// strip any surrounding tags before the <body> and after the </body> which are automatically added by
|
|
|
|
// DOMDocument. Note that we can't use the argument to saveHTML() as it's only supported in PHP 5.3.6+,
|
|
|
|
// we support 5.3.2 as a minimum in addition to the above, trim any surrounding newlines from the output
|
2012-09-14 07:31:12 +02:00
|
|
|
return trim(
|
|
|
|
preg_replace(
|
|
|
|
array(
|
2012-10-16 00:59:30 +02:00
|
|
|
'/(.*)<body>/is',
|
|
|
|
'/<\/body>(.*)/is',
|
2012-09-14 07:31:12 +02:00
|
|
|
),
|
|
|
|
'',
|
2012-09-23 12:28:42 +02:00
|
|
|
urldecode($this->getDocument()->saveHTML())
|
2012-09-14 07:31:12 +02:00
|
|
|
)
|
2009-10-11 02:07:27 +02:00
|
|
|
);
|
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
2009-10-11 02:07:27 +02:00
|
|
|
/**
|
|
|
|
* @param string $content
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setContent($content) {
|
2010-10-19 07:07:27 +02:00
|
|
|
// Ensure that \r (carriage return) characters don't get replaced with " " entity by DOMDocument
|
2010-10-19 07:07:45 +02:00
|
|
|
// This behaviour is apparently XML spec, but we don't want this because it messes up the HTML
|
2010-10-19 07:07:27 +02:00
|
|
|
$content = str_replace(chr(13), '', $content);
|
|
|
|
|
2009-10-13 03:44:41 +02:00
|
|
|
return @$this->getDocument()->loadHTML(
|
|
|
|
'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .
|
|
|
|
"<body>$content</body></html>"
|
|
|
|
);
|
2009-10-11 02:07:27 +02:00
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
2009-10-11 02:07:27 +02:00
|
|
|
/**
|
|
|
|
* @return DOMDocument
|
|
|
|
*/
|
|
|
|
public function getDocument() {
|
|
|
|
return $this->document;
|
|
|
|
}
|
2012-10-16 00:59:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DOMDocument $document
|
|
|
|
*/
|
|
|
|
public function setDocument($document) {
|
|
|
|
$this->document = $document;
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:27 +02:00
|
|
|
/**
|
|
|
|
* A simple convenience wrapper around DOMDocument::getElementsByTagName().
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return DOMNodeList
|
|
|
|
*/
|
|
|
|
public function getElementsByTagName($name) {
|
|
|
|
return $this->getDocument()->getElementsByTagName($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see HTMLValue::getContent()
|
|
|
|
*/
|
|
|
|
public function forTemplate() {
|
|
|
|
return $this->getContent();
|
|
|
|
}
|
|
|
|
|
2009-10-13 03:44:41 +02:00
|
|
|
}
|