allow adding a title attribute to links for convenience
[php-utility-classes.git] / classes / document.php-class
index 41e817a4d96b678740e1fa0ca82591a886af529c..d4d4e4782012baf86dd98b3ef6b3d6edbd82b97d 100755 (executable)
@@ -1,23 +1,7 @@
 <?php
-/* ***** BEGIN LICENSE BLOCK *****
- *
- * The contents of this file are subject to Austrian copyright reegulations
- * ("Urheberrecht"); you may not use this file except in compliance with
- * those laws.
- * This contents and any derived work, if it gets distributed in any way,
- * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
- * either express or implied.
- *
- * The Original Code is KaiRo's extended DOM document classes.
- *
- * The Initial Developer of the Original Code is
- * KaiRo - Robert Kaiser.
- * Portions created by the Initial Developer are Copyright (C) 2010
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s): Robert Kaiser <kairo@kairo.at>
- *
- * ***** END LICENSE BLOCK ***** */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 class ExtendedDocument extends DOMDocument {
   // ExtendedDocument PHP class
@@ -27,8 +11,9 @@ class ExtendedDocument extends DOMDocument {
   //   CONSTRUCTOR
   //   construct a new DOM Document that uses our element definitions
   //
-  // static function initHTML5()
+  // static function initHTML5([$doc])
   //   initialize as an HTML5 document and return references to its basic elements.
+  //     If a $doc is handed over (an ExtendedDocument or a derived class), load the content into that document.
   //     returns an associative array with the following elements: 'html', 'head', 'title', 'body'
   //
   // public function appendElement($name, [$value])
@@ -40,7 +25,7 @@ class ExtendedDocument extends DOMDocument {
   //   with an ExtendedDocument::createXMLFragment() of the given XML data inside
   //     returns the new child
   //
-  // public function appendLink($target, [$value])
+  // public function appendLink($target, [$value], [$title])
   //   appends an ExtendedDocument::createElementLink() as a child of this document (see there for params)
   //     returns the new child
   //
@@ -100,7 +85,7 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
   //     returns the new child
   //
-  // public function appendElementSelect($name, [$id], [$options], [$default])
+  // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
   //     returns the new child
   //
@@ -116,6 +101,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends a DOMDocument::createTextNode() as a child of this document (see there for params)
   //     returns the new child
   //
+  // public function appendLinebreak()
+  //   appends a <br> as a child of this document
+  //     returns the new child
+  //
   // public function appendEntity($name)
   //   appends a DOMDocument::createEntityReference() as a child of this document (see there for params)
   //     returns the new child
@@ -136,19 +125,15 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
   //     NO return value!
   //
-  // public function appendJSFile($jsURL)
+  // public function appendJSFile($jsURL, [$defer], [$async])
   //   appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
   //     returns the new child
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this document (see there for params)
-  //     returns the new child
-  //
-  // public function createElementLink($target, [$value])
-  //   returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
+  // public function createElementLink($target, [$value], [$title])
+  //   returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value as well as (optional) title attribute
   //
   // public function createElementImage($src, [$alt_text])
-  //   returns an ExtendedElement that is an HTML <img> with the given (src) and alt attributes (set to '' by default)
+  //   returns an ExtendedElement that is an HTML <img> with the given src and alt attributes (set to '' by default)
   //
   // public function createElementForm($action, $method, $name)
   //   returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
@@ -194,9 +179,9 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
   //   and optionally id and value
   //
-  // public function createElementSelect($name, [$id], [$options], [$default])
+  // public function createElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
-  //   array of options (key => description) and key of the by-default selected entry
+  //   array of options (key => description) and key of the by-default selected entry (matched including type when strictmatch is true)
   //
   // public function createElementOption($key, $desc, [$selected])
   //   returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
@@ -208,12 +193,9 @@ class ExtendedDocument extends DOMDocument {
   // public function createElementJS($jsdata)
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
   //
-  // public function createElementJSFile($jsURL)
+  // public function createElementJSFile($jsURL, [$defer], [$async])
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type linking to the file given by the URL
-  //
-  // public function createCOMElement($module, $attributes)
-  //   returns an ExtendedElement that is in COM_NS namespace, with the given module as name and the
-  //     given name=>value array as attributes
+  //     $defer and $async are boolean attributes that set if the script should be executed after parsing but before onload, and if it should be loaded asynchronously.
 
   function __construct($version = '1.0', $encoding = 'utf-8') {
     // make sure the default DOMDocument constructor runs
@@ -222,8 +204,8 @@ class ExtendedDocument extends DOMDocument {
     $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
   }
 
-  static function initHTML5() {
-    $doc = new ExtendedDocument();
+  static function initHTML5($doc = null) {
+    if (is_null($doc)) { $doc = new ExtendedDocument(); }
     $doc->loadHTML('<!DOCTYPE html><html></html>'); // this seems to be the only way to get the DOCTYPE set properly.
 
     // Created basic HTML document structure.
@@ -248,8 +230,8 @@ class ExtendedDocument extends DOMDocument {
     //$aelem->appendChild($this->createXMLFragment($xmldata));
     return $aelem;
   }
-  public function appendLink($target, $value = '') {
-    return $this->appendChild($this->createElementLink($target, $value));
+  public function appendLink($target, $value = '', $title = null) {
+    return $this->appendChild($this->createElementLink($target, $value, $title));
   }
   public function appendImage($src, $alt_text = '') {
     return $this->appendChild($this->createElementImage($src, $alt_text));
@@ -294,8 +276,8 @@ class ExtendedDocument extends DOMDocument {
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
   }
-  public function appendElementSelect($name, $id = null, $options = array(), $default = null) {
-    return $this->appendChild($this->createElementSelect($name, $id, $options, $default));
+  public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
+    return $this->appendChild($this->createElementSelect($name, $id, $options, $default, $strictmatch));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->createElementOption($key, $desc, $selected));
@@ -306,6 +288,9 @@ class ExtendedDocument extends DOMDocument {
   public function appendText($text) {
     return $this->appendChild($this->createTextNode($text));
   }
+  public function appendLinebreak() {
+    return $this->appendChild($this->createElement('br'));
+  }
   public function appendEntity($name) {
     return $this->appendChild($this->createEntityReference($name));
   }
@@ -315,17 +300,19 @@ class ExtendedDocument extends DOMDocument {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->createElementJSFile($jsdata));
-  }
-  public function appendCOMElement($module, $attributes) {
-    return $this->appendChild($this->createCOMElement($module, $attributes));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->createElementJSFile($jsdata, $defer, $async));
   }
 
   public function appendHTMLMarkup($htmldata, $parentNode = null) {
     if (is_null($parentNode)) { $parentNode =& $this; }
-    // XXX: just a workaround for now!
-    $parentNode->appendChild($this->createCDATASection($htmldata));
+    // Use loadHTML() to parse and turn the markup into proper HTML.
+    $tmpdoc = new ExtendedDocument;
+    // The XML line is needed to tell the parser that we need UTF-8 parsing.
+    $tmpdoc->loadHTML('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html><body>'.$htmldata.'</body></html>');
+    foreach ($tmpdoc->getElementsByTagName('body')->item(0)->childNodes as $child) {
+      $parentNode->appendChild($this->importNode($child, true));
+    }
   }
 
   public function appendXMLMarkup($xmldata, $parentNode = null) {
@@ -343,13 +330,14 @@ class ExtendedDocument extends DOMDocument {
   public function createElement($name, $value = '') {
     // Adding the $value in DOMDocument's createElement does NOT escape it, so override it and use appendText to support that.
     $aelem = parent::createElement($name);
-    $aelem->appendText($value);
+    if (strlen($value)) { $aelem->appendText($value); }
     return $aelem;
   }
 
-  public function createElementLink($target, $value = '') {
+  public function createElementLink($target, $value = '', $title = null) {
     $link = $this->createElement('a', $value);
     $link->setAttribute('href', $target); // XXX: take care of & etc. in links
+    if (!is_null($title)) { $link->setAttribute('title', $title); }
     return $link;
   }
 
@@ -365,7 +353,7 @@ class ExtendedDocument extends DOMDocument {
     $formelem->setAttribute('action', $action);
     $formelem->setAttribute('method', $method);
     $formelem->setAttribute('name', $name);
-    $formelem->setAttribute('id', $id);
+    if (!is_null($id)) { $formelem->setAttribute('id', $id); }
     return $formelem;
   }
 
@@ -474,12 +462,12 @@ class ExtendedDocument extends DOMDocument {
     return $txtarea;
   }
 
-  public function createElementSelect($name, $id = null, $options = array(), $default = null) {
+  public function createElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
     $select = $this->createElement('select');
     $select->setAttribute('name', $name);
     if (!is_null($id)) { $select->setAttribute('id', $id); }
     foreach ($options as $key => $desc) {
-      $select->appendElementOption($key, $desc, ($key == $default));
+      $select->appendElementOption($key, $desc, $strictmatch ? ($key === $default) : ($key == $default));
     }
     return $select;
   }
@@ -505,23 +493,19 @@ class ExtendedDocument extends DOMDocument {
     return $jselem;
   }
 
-  public function createElementJSFile($jsURL) {
+  public function createElementJSFile($jsURL, $defer = false, $async = false) {
     $jselem = $this->createElement('script');
     // Note: type can/should be left out for HTML5.
     $jselem->setAttribute('type', 'text/javascript');
+    if ($defer) {
+      $jselem->setAttribute('defer', '');
+    }
+    if ($async) {
+      $jselem->setAttribute('async', '');
+    }
     $jselem->setAttribute('src', $jsURL);
     return $jselem;
   }
-
-  public function createCOMElement($module, $attributes) {
-    $com_elem = $this->createElementNS(COM_NS, $module);
-    if (is_array($attributes) && count($attributes)) {
-      foreach ($attributes as $name=>$value) {
-        $com_elem->setAttribute($name, $value);
-      }
-    }
-    return $com_elem;
-  }
 }
 
 class ExtendedElement extends DOMElement {
@@ -537,7 +521,7 @@ class ExtendedElement extends DOMElement {
   //   with an ExtendedDocument::createXMLFragment() of the given XML data inside
   //     returns the new child
   //
-  // public function appendLink($target, [$value])
+  // public function appendLink($target, [$value], [$title])
   //   appends an ExtendedDocument::createElementLink() as a child of this element (see there for params)
   //     returns the new child
   //
@@ -597,7 +581,7 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
   //     returns the new child
   //
-  // public function appendElementSelect($name, [$id], [$options], [$default])
+  // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
   //     returns the new child
   //
@@ -613,6 +597,10 @@ class ExtendedElement extends DOMElement {
   //   appends a DOMDocument::createTextNode() as a child of this element (see there for params)
   //     returns the new child
   //
+  // public function appendLinebreak()
+  //   appends a <br> as a child of this element
+  //     returns the new child
+  //
   // public function appendEntity($name)
   //   appends a DOMDocument::createEntityReference() as a child of this element (see there for params)
   //     returns the new child
@@ -633,13 +621,19 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
   //     NO return value!
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this element (see there for params)
+  // public function appendJSFile($jsURL, [$defer], [$async])
+  //   appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
   //     returns the new child
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this element (see there for params)
-  //     returns the new child
+  // public function setClass($classname)
+  //   sets the 'class' attribute of the element to the given classname value
+  //
+  // public function addClass($classname)
+  //   adds the given classname value to the space-separated list in the 'class' attribute
+  //     returns the value of the 'class' attribute
+  //
+  // public function setID($elem_id)
+  //   sets the 'id' attribute of the element to the given elem_id value
 
   public function appendElement($name, $value = '') {
     return $this->appendChild($this->ownerDocument->createElement($name, $value));
@@ -649,8 +643,8 @@ class ExtendedElement extends DOMElement {
     $aelem->appendXMLMarkup($xmldata);
     return $aelem;
   }
-  public function appendLink($target, $value = '') {
-    return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
+  public function appendLink($target, $value = '', $title = null) {
+    return $this->appendChild($this->ownerDocument->createElementLink($target, $value, $title));
   }
   public function appendImage($src, $alt_text = '') {
     return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
@@ -695,8 +689,8 @@ class ExtendedElement extends DOMElement {
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
   }
-  public function appendElementSelect($name, $id = null, $options = array(), $default = null) {
-    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
+  public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
+    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
@@ -707,6 +701,9 @@ class ExtendedElement extends DOMElement {
   public function appendText($text) {
     return $this->appendChild($this->ownerDocument->createTextNode($text));
   }
+  public function appendLinebreak() {
+    return $this->appendChild($this->ownerDocument->createElement('br'));
+  }
   public function appendEntity($name) {
     return $this->appendChild($this->ownerDocument->createEntityReference($name));
   }
@@ -722,11 +719,21 @@ class ExtendedElement extends DOMElement {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
+  }
+  public function setClass($classname) {
+    $this->setAttribute('class', $classname);
   }
-  public function appendCOMElement($module, $attributes) {
-    return $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  public function addClass($classname) {
+    $classval = $this->getAttribute('class');
+    if (strlen($classval)) { $classval .= ' '; }
+    $classval .= $classname;
+    $this->setClass($classval);
+    return $classval;
+  }
+  public function setID($elem_id) {
+    $this->setAttribute('id', $elem_id);
   }
 }
 
@@ -743,7 +750,7 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   with an ExtendedDocument::createXMLFragment() of the given XML data inside
   //     returns the new child
   //
-  // public function appendLink($target, [$value])
+  // public function appendLink($target, [$value], [$title])
   //   appends an ExtendedDocument::createElementLink() as a child of this fragment (see there for params)
   //     returns the new child
   //
@@ -803,7 +810,7 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
   //     returns the new child
   //
-  // public function appendElementSelect($name, [$id], [$options], [$default])
+  // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
   //     returns the new child
   //
@@ -819,6 +826,10 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // public function appendLinebreak()
+  //   appends a <br> as a child of this fragment
+  //     returns the new child
+  //
   // public function appendEntity($name)
   //   appends a DOMDocument::createEntityReference() as a child of this fragment (see there for params)
   //     returns the new child
@@ -839,12 +850,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
   //     NO return value!
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this fragment (see there for params)
-  //     returns the new child
-  //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this fragment (see there for params)
+  // public function appendJSFile($jsURL, [$defer], [$async])
+  //   appends an ExtendedDocument::createElementJSFile() as a child of this fragment (see there for params)
   //     returns the new child
 
   public function appendElement($name, $value = '') {
@@ -855,8 +862,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
     $aelem->appendXMLMarkup($xmldata);
     return $aelem;
   }
-  public function appendLink($target, $value = '') {
-    return $this->appendChild($this->ownerDocument->createElementLink($target, $value));
+  public function appendLink($target, $value = '', $title = null) {
+    return $this->appendChild($this->ownerDocument->createElementLink($target, $value, $title));
   }
   public function appendImage($src, $alt_text = '') {
     return $this->appendChild($this->ownerDocument->createElementImage($src, $alt_text));
@@ -901,8 +908,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
   }
-  public function appendElementSelect($name, $id = null, $options = array(), $default = null) {
-    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
+  public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
+    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
@@ -913,6 +920,9 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendText($text) {
     return $this->appendChild($this->ownerDocument->createTextNode($text));
   }
+  public function appendLinebreak() {
+    return $this->appendChild($this->ownerDocument->createElement('br'));
+  }
   public function appendEntity($name) {
     return $this->appendChild($this->ownerDocument->createEntityReference($name));
   }
@@ -928,11 +938,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata));
-  }
-  public function appendCOMElement($module, $attributes) {
-    return $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
   }
 }
 ?>