/lib/ext/anewt.new/xhtml/base.lib.php

https://github.com/jijkoun/ssscrape · PHP · 239 lines · 84 code · 32 blank · 123 comment · 8 complexity · 88a6021354c8ae997a0ae765978dc05e MD5 · raw file

  1. <?php
  2. /*
  3. * Anewt, Almost No Effort Web Toolkit, XHTML module
  4. *
  5. * This code is copyrighted and distributed under the terms of the GNU LGPL.
  6. * See the README file for more information.
  7. */
  8. anewt_include('xml/dom');
  9. /**
  10. * XHTML fragment class use to hold zero or more XHTML elements.
  11. *
  12. * This class is useful to group XHTML elements into one entity. It is basically
  13. * a list that can be inserted into a DOM tree.
  14. *
  15. * \see AnewtXMLDomDocumentFragment
  16. */
  17. final class AnewtXHTMLFragment extends AnewtXMLDomDocumentFragment
  18. {
  19. /**
  20. * Render this fragment into a string.
  21. *
  22. * This method renders all children and concenates those strings into one
  23. * single value. Usually XHTML fragments are not rendered directly, but
  24. * added to a DOM tree instead. When that happens, all child nodes of the
  25. * fragment are added to the DOM tree, and the document fragment instance
  26. * itself is no longer of any use. This means that this method is not
  27. * invoked if document fragments are used in combination with a proper DOM
  28. * document (e.g. as used by AnewtPage).
  29. *
  30. * \return
  31. * Rendered XML output or an empty string if the fragment was empty.
  32. */
  33. function render()
  34. {
  35. $out = array();
  36. foreach ($this->child_nodes as $child_node)
  37. {
  38. $out[] = $child_node->render();
  39. }
  40. return to_string($out);
  41. }
  42. }
  43. /**
  44. * \private
  45. *
  46. * Base XHTML element class.
  47. *
  48. * This class is extended by all of the XHTML element classes.
  49. * AnewtXHTMLElement provides functionality shared by all XHTML element
  50. * classes.
  51. */
  52. abstract class AnewtXHTMLElement extends AnewtXMLDomElement
  53. {
  54. /* Rendering instructions for XML nodes */
  55. protected $must_be_empty = false;
  56. public $always_render_closing_tag = true;
  57. /**
  58. * \protected
  59. *
  60. * Create a new XHTML element.
  61. *
  62. * This constructor accepts a variable number of arguments. Each argument
  63. * is appended as a child node, but the last argument is handled
  64. * differently. If the last argument is an associative array, its values are
  65. * used as attribute names and values. If it's not an associative array, it
  66. * is treated just as the other arguments and appended as a child node.
  67. *
  68. * The AnewtXHTMLElement class is abstract and cannot be instantiated
  69. * directly. Use one of its descendants instead (all XHTML element classes
  70. * extend AnewtXHTMLElement); there is one class for each XHTML element in
  71. * the HTML specification.
  72. *
  73. * \param $children
  74. * One or more child nodes (optional).
  75. *
  76. * \param $attributes
  77. * Associative array with element attributes (optional).
  78. */
  79. function __construct($children=null, $attributes=null)
  80. {
  81. assert('!is_null($this->node_name); // node name must be specified');
  82. parent::__construct($this->node_name);
  83. $num_args = func_num_args();
  84. if ($num_args == 0)
  85. return;
  86. $args = func_get_args();
  87. /* Use last element for attributes, but only if it's an associative
  88. * array. */
  89. if (is_assoc_array($args[$num_args - 1]))
  90. {
  91. $attributes = array_pop($args);
  92. $this->set_attributes($attributes);
  93. }
  94. /* Add additional arguments as child nodes. */
  95. $this->append_children($args);
  96. }
  97. /** \{
  98. * \name Class methods
  99. *
  100. * These methods can be used to manipulate the \c class attribute of an
  101. * element. These methods are here for convenience only, since the
  102. * attributes methods of the AnewtXMLDomElement class van be used as well to
  103. * set the value of the \c class attribute.
  104. */
  105. /**
  106. * Set a class for this element.
  107. *
  108. * \param $class_name
  109. * The class name to set.
  110. */
  111. function set_class($class_name)
  112. {
  113. assert('is_string($class_name);');
  114. $this->set_attribute('class', $class_name);
  115. }
  116. /**
  117. * Add a class to this element.
  118. *
  119. * The current classes are left as-is.
  120. *
  121. * \param $class_name
  122. * The class name to add.
  123. */
  124. function add_class($class_name)
  125. {
  126. assert('is_string($class_name);');
  127. if ($this->has_attribute('class'))
  128. $new_class = sprintf(
  129. '%s %s',
  130. $this->get_attribute('class'),
  131. $class_name
  132. );
  133. else
  134. $new_class = $class_name;
  135. $this->set_class($new_class);
  136. }
  137. /**
  138. * Remove class attribute(s) from this element.
  139. *
  140. * If no parameters are specified, the class attribute is removed.
  141. *
  142. * If a class name is provided, it will be removed from the current classes
  143. * of this element. If the provided class name was not set on this element,
  144. * nothing happens.
  145. *
  146. * \param $class_name
  147. * The class to remove (optional). If not given (or \c null), all classes
  148. * are removed.
  149. */
  150. function remove_class($class_name=null)
  151. {
  152. if (!$this->has_attribute('class'))
  153. return;
  154. if (is_null($class_name))
  155. {
  156. $this->remove_attribute('class');
  157. return;
  158. }
  159. assert('is_string($class_name)');
  160. $current_classes = explode(' ', $this->get_attribute('class'));
  161. $new_classes = array();
  162. foreach ($current_classes as $current_class)
  163. {
  164. if ($current_class == $class_name)
  165. continue;
  166. $new_classes[] = $current_class;
  167. }
  168. $this->set_class(join(' ', $new_classes));
  169. }
  170. /** \} */
  171. }
  172. /**
  173. * \private
  174. *
  175. * Base class for block elements.
  176. *
  177. * Do not instantiate this class directly. Inline elements are rendered with
  178. * surrounding whitespace and indentation.
  179. */
  180. abstract class AnewtXHTMLBlockElement extends AnewtXHTMLElement
  181. {
  182. public $render_as_block = true;
  183. }
  184. /**
  185. * \private
  186. *
  187. * Base class for inline elements.
  188. *
  189. * Do not instantiate this class directly. Inline elements are rendered without
  190. * surrounding whitespace and indentation.
  191. */
  192. abstract class AnewtXHTMLInlineElement extends AnewtXHTMLElement
  193. {
  194. public $render_as_block = false;
  195. }
  196. /**
  197. * Raw HTML node for literal HTML data.
  198. *
  199. * Instances of this class can be used to insert literal HTML (e.g. generated by
  200. * other means) into the DOM tree. See AnewtXMLDomRaw for more information
  201. *
  202. * \see AnewtXMLDomRaw
  203. */
  204. final class AnewtXHTMLRaw extends AnewtXMLDomRaw
  205. {
  206. }
  207. ?>