You are here

public function cf_dom::__construct in Common Functionality 7.2

Constructor for the cf_dom class.

Parameters

string|null|bool $doctype: (optional) When a document type is passed, a new dom object gets created. When set to NULL, will use the drupal variable called 'cf_dom_doctype' is used. When TRUE, specify that the headers will also be auto added along with the doctype.

bool $preserve_whitespace: (optional) The default whitespace preservation dom setting.

bool $format_output: (optional) The default output formatting dom setting.

string|null $content: (optional) Specify a custom content on initialization. If specified, then the doctype parameter will be ignored.

File

modules/cf_dom/classes/cf_dom.php, line 46
Provides the cf_dom handling class.

Class

cf_dom
The cf_dom class assists in setting up and managing the custom dom object.

Code

public function __construct($doctype = NULL, $preserve_whitespace = TRUE, $format_output = TRUE, $content = NULL) {
  if (is_bool($preserve_whitespace)) {
    $this->preserve_whitespace = $preserve_whitespace;
  }
  else {
    if (class_exists('cf_error')) {
      cf_error::invalid_bool('preserve_whitespace');
    }
  }
  if (is_bool($format_output)) {
    $this->format_output = $format_output;
  }
  else {
    if (class_exists('cf_error')) {
      cf_error::invalid_bool('format_output');
    }
  }
  if (is_null($content)) {
    if (is_null($doctype)) {
      $this->doctype = variable_get('cf_dom_doctype', cf_dom::DOCTYPE);
    }
    elseif (cf_is_empty_or_non_string('doctype', $doctype)) {
      return;
    }
    else {
      $this->doctype = $doctype;
    }
    $this->content = cf_dom::CONTENT_PREFIX . cf_dom::CONTENT_POSTFIX;
  }
  else {
    if (is_string($content)) {
      if (is_bool($doctype) && $doctype) {
        $this->content = cf_dom::CONTENT_PREFIX . $content . cf_dom::CONTENT_POSTFIX;
      }
      else {
        $this->content = $content;
      }
    }
    else {
      if (class_exists('cf_error')) {
        cf_error::invalid_string('content');
      }
      return;
    }
    if (!is_null($doctype) && !is_bool($doctype)) {
      if (cf_is_empty_or_non_string('doctype', $doctype)) {
        return;
      }
      $this->doctype = $doctype;
    }
  }
  $this->dom = new DOMDocument();
  $this->dom->preserveWhiteSpace = $this->preserve_whitespace;
  $this->dom->formatOutput = $this->format_output;
  @$this->dom
    ->loadHTML($this->doctype . $this->content);
  $this->head = NULL;
  $elements = $this->dom
    ->getElementsByTagName('head');
  if ($elements->length > 0) {
    $this->head = $elements
      ->item(0);
  }
  $this->body = NULL;
  $elements = $this->dom
    ->getElementsByTagName('body');
  if ($elements->length > 0) {
    $this->body = $elements
      ->item(0);
  }
}