選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

177 行
6.5KB

  1. <?php
  2. /**
  3. * Validates shorthand CSS property font.
  4. */
  5. class HTMLPurifier_AttrDef_CSS_Font extends HTMLPurifier_AttrDef
  6. {
  7. /**
  8. * Local copy of validators
  9. * @type HTMLPurifier_AttrDef[]
  10. * @note If we moved specific CSS property definitions to their own
  11. * classes instead of having them be assembled at run time by
  12. * CSSDefinition, this wouldn't be necessary. We'd instantiate
  13. * our own copies.
  14. */
  15. protected $info = array();
  16. /**
  17. * @param HTMLPurifier_Config $config
  18. */
  19. public function __construct($config)
  20. {
  21. $def = $config->getCSSDefinition();
  22. $this->info['font-style'] = $def->info['font-style'];
  23. $this->info['font-variant'] = $def->info['font-variant'];
  24. $this->info['font-weight'] = $def->info['font-weight'];
  25. $this->info['font-size'] = $def->info['font-size'];
  26. $this->info['line-height'] = $def->info['line-height'];
  27. $this->info['font-family'] = $def->info['font-family'];
  28. }
  29. /**
  30. * @param string $string
  31. * @param HTMLPurifier_Config $config
  32. * @param HTMLPurifier_Context $context
  33. * @return bool|string
  34. */
  35. public function validate($string, $config, $context)
  36. {
  37. static $system_fonts = array(
  38. 'caption' => true,
  39. 'icon' => true,
  40. 'menu' => true,
  41. 'message-box' => true,
  42. 'small-caption' => true,
  43. 'status-bar' => true
  44. );
  45. // regular pre-processing
  46. $string = $this->parseCDATA($string);
  47. if ($string === '') {
  48. return false;
  49. }
  50. // check if it's one of the keywords
  51. $lowercase_string = strtolower($string);
  52. if (isset($system_fonts[$lowercase_string])) {
  53. return $lowercase_string;
  54. }
  55. $bits = explode(' ', $string); // bits to process
  56. $stage = 0; // this indicates what we're looking for
  57. $caught = array(); // which stage 0 properties have we caught?
  58. $stage_1 = array('font-style', 'font-variant', 'font-weight');
  59. $final = ''; // output
  60. for ($i = 0, $size = count($bits); $i < $size; $i++) {
  61. if ($bits[$i] === '') {
  62. continue;
  63. }
  64. switch ($stage) {
  65. case 0: // attempting to catch font-style, font-variant or font-weight
  66. foreach ($stage_1 as $validator_name) {
  67. if (isset($caught[$validator_name])) {
  68. continue;
  69. }
  70. $r = $this->info[$validator_name]->validate(
  71. $bits[$i],
  72. $config,
  73. $context
  74. );
  75. if ($r !== false) {
  76. $final .= $r . ' ';
  77. $caught[$validator_name] = true;
  78. break;
  79. }
  80. }
  81. // all three caught, continue on
  82. if (count($caught) >= 3) {
  83. $stage = 1;
  84. }
  85. if ($r !== false) {
  86. break;
  87. }
  88. case 1: // attempting to catch font-size and perhaps line-height
  89. $found_slash = false;
  90. if (strpos($bits[$i], '/') !== false) {
  91. list($font_size, $line_height) =
  92. explode('/', $bits[$i]);
  93. if ($line_height === '') {
  94. // ooh, there's a space after the slash!
  95. $line_height = false;
  96. $found_slash = true;
  97. }
  98. } else {
  99. $font_size = $bits[$i];
  100. $line_height = false;
  101. }
  102. $r = $this->info['font-size']->validate(
  103. $font_size,
  104. $config,
  105. $context
  106. );
  107. if ($r !== false) {
  108. $final .= $r;
  109. // attempt to catch line-height
  110. if ($line_height === false) {
  111. // we need to scroll forward
  112. for ($j = $i + 1; $j < $size; $j++) {
  113. if ($bits[$j] === '') {
  114. continue;
  115. }
  116. if ($bits[$j] === '/') {
  117. if ($found_slash) {
  118. return false;
  119. } else {
  120. $found_slash = true;
  121. continue;
  122. }
  123. }
  124. $line_height = $bits[$j];
  125. break;
  126. }
  127. } else {
  128. // slash already found
  129. $found_slash = true;
  130. $j = $i;
  131. }
  132. if ($found_slash) {
  133. $i = $j;
  134. $r = $this->info['line-height']->validate(
  135. $line_height,
  136. $config,
  137. $context
  138. );
  139. if ($r !== false) {
  140. $final .= '/' . $r;
  141. }
  142. }
  143. $final .= ' ';
  144. $stage = 2;
  145. break;
  146. }
  147. return false;
  148. case 2: // attempting to catch font-family
  149. $font_family =
  150. implode(' ', array_slice($bits, $i, $size - $i));
  151. $r = $this->info['font-family']->validate(
  152. $font_family,
  153. $config,
  154. $context
  155. );
  156. if ($r !== false) {
  157. $final .= $r . ' ';
  158. // processing completed successfully
  159. return rtrim($final);
  160. }
  161. return false;
  162. }
  163. }
  164. return false;
  165. }
  166. }
  167. // vim: et sw=4 sts=4