Version: current

      1 <?php
      2 /**
      3  * This class acts as a wrapper around the built in DOMDocument class in order to use it to manage a HTML snippet,
      4  * rather than a whole document, while still exposing the DOMDocument API.
      5  *
      6  * @package sapphire
      7  * @subpackage integration
      8  */
      9 class SS_HTMLValue extends ViewableData {
     10
     11     /**
     12      * @var DOMDocument
     13      */
     14     protected $document;
     15     private $content;
     16     /**
     17      * @param string $content
     18      */
     19     public function __construct($content = null) {
     20         $this->document = new DOMDocument('1.0', 'UTF-8');
     21         $this->document->scrictErrorChecking = false;
     22
     23         $this->setContent($content);
     24
     25         parent::__construct();
     26     }
     27
     28     /**
     29      * @return string
     30      */
     31     public function getContent() {
     32         // strip the body tags from the output (which are automatically added by DOMDocument)
     33         //return $this->content;
     34                 $content = preg_replace (
     35             array (
     36                 '/^\s*<body[^>]*>/i',
     37                 '/<\/body[^>]*>\s*$/i'
     38             ),
     39             null,
     40             $this->getDocument()->saveHTML($this->getDocument()->documentElement->lastChild)
     41 //          $this->getDocument()->saveXML($this->getDocument()->documentElement->lastChild)
     42         );
     43                 /* Fix closed tags */
     44                 $xhtml[] = '/<script([^<]*)\/>/';
     45                 $html[]  = '<script\\1></script>';
     46
     47                 $xhtml[] = '/<iframe([^<]*)\/>/';
     48                 $html[]  = '<iframe\\1></iframe>';
     49
     50                 $xhtml[] = '/<div([^<]*)\/>/';
     51                 $html[]  = '<div\\1></div>';
     52
     53                 $xhtml[] = '/<a([^<]*)\/>/';
     54                 $html[]  = '<a\\1></a>';
     55
     56                 //$xhtml[] = '/\/>/';
     57                 //$html[]  = '>';
     58
     59                 $xhtml[] = '/%5Bsitetree_link%20id=(\d+)%5D/';
     60                 $html[]  = '[sitetree_link id=$1]';
     61
     62                 $content = preg_replace($xhtml, $html, $content);
     63                 /* end fix */
     64         return $content;
     65     }
     66
     67     /**
     68      * @param string $content
     69      * @return bool
     70      */
     71     public function setContent($content) {
     72         $this->content = $content;
     73         return @$this->getDocument()->loadHTML(
     74                         //'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'.
     75             '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .
     76             "<body>$content</body></html>"
     77         );
     78     }
     79
     80     /**
     81      * @return DOMDocument
     82      */
     83     public function getDocument() {
     84         return $this->document;
     85     }
     86
     87     /**
     88      * A simple convenience wrapper around DOMDocument::getElementsByTagName().
     89      *
     90      * @param string $name
     91      * @return DOMNodeList
     92      */
     93     public function getElementsByTagName($name) {
     94         return $this->getDocument()->getElementsByTagName($name);
     95     }
     96
     97     /**
     98      * @see HTMLValue::getContent()
     99      */
    100     public function forTemplate() {
    101         return $this->getContent();
    102     }
    103
    104 }
    105

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -