/protected/components/ezcomponents/Document/src/document/xml/xhtml/filter/element/base.php

https://github.com/kamarulismail/kamarul-playground · PHP · 236 lines · 147 code · 12 blank · 77 comment · 4 complexity · d56928735df0c1d248a5eae985f61e52 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the abstract ezcDocumentXhtmlElementBaseFilter base class.
  4. *
  5. * @package Document
  6. * @version 1.3.1
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * Filter for XHTML elements.
  13. *
  14. * @package Document
  15. * @version 1.3.1
  16. * @access private
  17. */
  18. abstract class ezcDocumentXhtmlElementBaseFilter
  19. {
  20. /**
  21. * Filter a single element
  22. *
  23. * @param DOMElement $element
  24. */
  25. abstract public function filterElement( DOMElement $element );
  26. /**
  27. * Check if filter handles the current element
  28. *
  29. * Returns a boolean value, indicating weather this filter can handle
  30. * the current element.
  31. *
  32. * @param DOMElement $element
  33. * @return void
  34. */
  35. abstract public function handles( DOMElement $element );
  36. /**
  37. * Is block level element
  38. *
  39. * Returns true, if the element is a block level element in XHtml, and
  40. * false otherwise.
  41. *
  42. * @param DOMElement $element
  43. * @return boolean
  44. */
  45. protected function isBlockLevelElement( DOMElement $element )
  46. {
  47. return in_array(
  48. $element->tagName,
  49. array(
  50. 'address',
  51. 'blockquote',
  52. 'body',
  53. 'center',
  54. 'del',
  55. 'dir',
  56. 'div',
  57. 'dl',
  58. 'fieldset',
  59. 'form',
  60. 'h1',
  61. 'h2',
  62. 'h3',
  63. 'h4',
  64. 'h5',
  65. 'h6',
  66. 'hr',
  67. 'ins',
  68. 'li',
  69. 'menu',
  70. 'noframes',
  71. 'noscript',
  72. 'ol',
  73. 'p',
  74. 'pre',
  75. 'table',
  76. 'th',
  77. 'td',
  78. 'ul',
  79. )
  80. );
  81. }
  82. /**
  83. * Check if node is an inline element
  84. *
  85. * Check if the passed node is an inline element, eg. may occur inside a
  86. * text block, like a paragraph.
  87. *
  88. * @param DOMNode $node
  89. * @return bool
  90. */
  91. protected function isInlineElement( DOMNode $node )
  92. {
  93. return (
  94. ( $node->nodeType === XML_TEXT_NODE ) ||
  95. ( ( $node->nodeType === XML_ELEMENT_NODE ) &&
  96. in_array( $node->tagName, array(
  97. 'a',
  98. 'abbr',
  99. 'acronym',
  100. 'applet',
  101. 'b',
  102. 'basefont',
  103. 'bdo',
  104. 'big',
  105. 'button',
  106. 'cite',
  107. 'code',
  108. 'del',
  109. 'dfn',
  110. 'em',
  111. 'font',
  112. 'i',
  113. 'img',
  114. 'ins',
  115. 'input',
  116. 'iframe',
  117. 'kbd',
  118. 'label',
  119. 'map',
  120. 'object',
  121. 'q',
  122. 'samp',
  123. 'script',
  124. 'select',
  125. 'small',
  126. 'span',
  127. 'strong',
  128. 'sub',
  129. 'sup',
  130. 'textarea',
  131. 'tt',
  132. 'var',
  133. ), true )
  134. )
  135. );
  136. }
  137. /**
  138. * Is current element placed inline
  139. *
  140. * Checks if the current element is placed inline, which means, it is
  141. * either a descendant of some other inline element, or part of a
  142. * paragraph.
  143. *
  144. * @param DOMElement $element
  145. * @return void
  146. */
  147. protected function isInline( DOMElement $element )
  148. {
  149. $toCheck = $element;
  150. do {
  151. if ( in_array( $toCheck->getProperty( 'type' ), array(
  152. 'para',
  153. 'subtitle',
  154. 'title',
  155. 'term',
  156. 'abbrev',
  157. 'acronym',
  158. 'anchor',
  159. 'attribution',
  160. 'author',
  161. 'citation',
  162. 'citetitle',
  163. 'email',
  164. 'emphasis',
  165. 'footnoteref',
  166. 'link',
  167. 'literal',
  168. 'literallayout',
  169. 'quote',
  170. 'subscript',
  171. 'superscript',
  172. 'ulink',
  173. ) ) )
  174. {
  175. return true;
  176. }
  177. } while ( ( $toCheck = $toCheck->parentNode ) &&
  178. ( $toCheck instanceof DOMElement ) );
  179. return false;
  180. }
  181. /**
  182. * Check for element class
  183. *
  184. * Check if element has the given class in its class attribute. Returns
  185. * true, if it is contained, or false, if not.
  186. *
  187. * @param DOMElement $element
  188. * @param string $class
  189. * @return bool
  190. */
  191. protected function hasClass( DOMElement $element, $class )
  192. {
  193. return ( $element->hasAttribute( 'class' ) &&
  194. preg_match(
  195. '((?:^|\s)' . preg_quote( $class ) . '(?:\s|$))',
  196. $element->getAttribute( 'class' )
  197. )
  198. );
  199. }
  200. /**
  201. * Shows a string representation of the current node.
  202. *
  203. * Is only there for debugging purposes
  204. *
  205. * @param DOMElement $element
  206. * @param bool $newLine
  207. * @access private
  208. */
  209. protected function showCurrentNode( DOMElement $element, $newLine = true )
  210. {
  211. if ( $element->parentNode &&
  212. ( $element->parentNode instanceof DOMElement ) )
  213. {
  214. $this->showCurrentNode( $element->parentNode, false );
  215. }
  216. echo '> ', $element->tagName;
  217. if ( $element->getProperty( 'type' ) !== false )
  218. {
  219. echo ' (', $element->getProperty( 'type' ), ')';
  220. }
  221. echo $newLine ? "\n" : ' ';
  222. }
  223. }
  224. ?>