PageRenderTime 63ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/saf/lib/HTML/CSS_Parser.php

https://github.com/lux/sitellite
PHP | 199 lines | 113 code | 15 blank | 71 comment | 24 complexity | a4317005f9dc4ecae906a503f0b8d2ff MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. loader_import ('saf.Parser');
  3. /**
  4. * Parses CSS data into 3-dimensional associative arrays. Properly handles
  5. * multiple declaration blocks for the same element, but will only keep
  6. * the final value of a property that is declared again, which should not
  7. * be a problem for most cases. Also properly handles /* style comments.
  8. *
  9. * Example:
  10. * <code>
  11. * <?php
  12. *
  13. * loader_import ('saf.HTML.CSS_Parser');
  14. *
  15. * $css = new CSS_Parser ();
  16. *
  17. * $css->parse ('a { text-decoration: none } div.content { line-height: 15px }');
  18. *
  19. * info ($css->list['a']);
  20. *
  21. * info ($css->getClasses ('div'));
  22. *
  23. * ? >
  24. * </code>
  25. *
  26. * @package HTML
  27. *
  28. */
  29. class CSS_Parser extends Parser {
  30. /**
  31. * Whether the parser is in a comment or not.
  32. *
  33. */
  34. var $comment = false;
  35. /**
  36. * Whether the parser is in a block or not.
  37. *
  38. */
  39. var $block = false;
  40. /**
  41. * The structure created from the previous call to parse().
  42. *
  43. * @access public
  44. */
  45. var $list = array ();
  46. /**
  47. * Constructor method.
  48. *
  49. */
  50. function CSS_Parser () {
  51. $this->addInternal ('_comment', '/*');
  52. $this->addInternal ('_comment_end', '*/');
  53. $this->addInternal ('_block', '{');
  54. $this->addInternal ('_block_end', '}');
  55. $this->comment = false;
  56. $this->block = false;
  57. $this->list = array ();
  58. }
  59. /**
  60. * Returns an array of classes found in the CSS data. Optionally
  61. * limits the classes to those that apply to the specified element.
  62. *
  63. * @access public
  64. * @param string
  65. * @return array
  66. *
  67. */
  68. function getClasses ($tag = false) {
  69. $classes = array ();
  70. foreach (array_keys ($this->list) as $block) {
  71. $names = preg_split ('|[\t >]+|', $block, -1, PREG_SPLIT_NO_EMPTY);
  72. foreach ($names as $name) {
  73. if (strpos ($name, '.') !== false) {
  74. // class found
  75. list ($t, $class) = explode ('.', $name);
  76. if ($tag) {
  77. if (empty ($t) || $tag == $t) {
  78. $classes[] = array_shift (explode (':', $class));
  79. }
  80. } else {
  81. $classes[] = array_shift (explode (':', $class));
  82. }
  83. }
  84. }
  85. }
  86. return array_unique ($classes);
  87. }
  88. /**
  89. * Returns an array of IDs found in the CSS data. Optionally
  90. * limits the IDs to those that apply to the specified element.
  91. *
  92. * @access public
  93. * @param string
  94. * @return array
  95. *
  96. */
  97. function getIDs ($tag = false) {
  98. $ids = array ();
  99. foreach (array_keys ($this->list) as $block) {
  100. $names = preg_split ('|[\t >]+|', $block, -1, PREG_SPLIT_NO_EMPTY);
  101. foreach ($names as $name) {
  102. if (strpos ($name, '#') !== false) {
  103. // ID found
  104. list ($t, $id) = explode ('#', $name);
  105. if ($tag) {
  106. if (empty ($t) || $tag == $t) {
  107. $ids[] = $id;
  108. }
  109. } else {
  110. $ids[] = $id;
  111. }
  112. }
  113. }
  114. }
  115. return array_unique ($ids);
  116. }
  117. /**
  118. * Returns all of the stylesheet properties of the specified element.
  119. *
  120. * @access public
  121. * @param string
  122. * @return array
  123. *
  124. */
  125. function getStyle ($tag) {
  126. return $this->list[$tag];
  127. }
  128. function _comment ($token, $name) {
  129. $this->comment = true;
  130. }
  131. function _comment_end ($token, $name) {
  132. $this->comment = false;
  133. }
  134. function _default ($token, $name) {
  135. if ($this->comment) {
  136. return;
  137. }
  138. if (! $this->block) {
  139. $this->block = trim ($token);
  140. if (strpos ($this->block, ',') !== false) {
  141. $this->block = explode (',', $this->block);
  142. foreach ($this->block as $k => $v) {
  143. $this->block[$k] = trim ($v);
  144. if (! isset ($this->list[$this->block[$k]])) {
  145. $this->list[$this->block[$k]] = array ();
  146. }
  147. }
  148. } else {
  149. if (! isset ($this->list[$this->block])) {
  150. $this->list[$this->block] = array ();
  151. }
  152. }
  153. } else {
  154. $lines = explode (';', trim ($token));
  155. foreach ($lines as $line) {
  156. if (strpos ($line, ':') === false) {
  157. continue;
  158. }
  159. list ($name, $value) = explode (':', $line, 2);
  160. if (is_array ($this->block)) {
  161. $n = trim ($name);
  162. $val = trim ($value);
  163. foreach ($this->block as $k => $v) {
  164. $this->list[$this->block[$k]][$n] = $val;
  165. }
  166. } else {
  167. $this->list[$this->block][trim ($name)] = trim ($value);
  168. }
  169. }
  170. }
  171. }
  172. function _block ($token, $name) {
  173. if ($this->comment) {
  174. return;
  175. }
  176. }
  177. function _block_end ($token, $name) {
  178. if ($this->comment) {
  179. return;
  180. }
  181. $this->block = false;
  182. }
  183. }
  184. ?>