Font.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 component validators.
  9. *
  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. public function __construct($config) {
  17. $def = $config->getCSSDefinition();
  18. $this->info['font-style'] = $def->info['font-style'];
  19. $this->info['font-variant'] = $def->info['font-variant'];
  20. $this->info['font-weight'] = $def->info['font-weight'];
  21. $this->info['font-size'] = $def->info['font-size'];
  22. $this->info['line-height'] = $def->info['line-height'];
  23. $this->info['font-family'] = $def->info['font-family'];
  24. }
  25. public function validate($string, $config, $context) {
  26. static $system_fonts = array(
  27. 'caption' => true,
  28. 'icon' => true,
  29. 'menu' => true,
  30. 'message-box' => true,
  31. 'small-caption' => true,
  32. 'status-bar' => true
  33. );
  34. // regular pre-processing
  35. $string = $this->parseCDATA($string);
  36. if ($string === '') return false;
  37. // check if it's one of the keywords
  38. $lowercase_string = strtolower($string);
  39. if (isset($system_fonts[$lowercase_string])) {
  40. return $lowercase_string;
  41. }
  42. $bits = explode(' ', $string); // bits to process
  43. $stage = 0; // this indicates what we're looking for
  44. $caught = array(); // which stage 0 properties have we caught?
  45. $stage_1 = array('font-style', 'font-variant', 'font-weight');
  46. $final = ''; // output
  47. for ($i = 0, $size = count($bits); $i < $size; $i++) {
  48. if ($bits[$i] === '') continue;
  49. switch ($stage) {
  50. // attempting to catch font-style, font-variant or font-weight
  51. case 0:
  52. foreach ($stage_1 as $validator_name) {
  53. if (isset($caught[$validator_name])) continue;
  54. $r = $this->info[$validator_name]->validate(
  55. $bits[$i], $config, $context);
  56. if ($r !== false) {
  57. $final .= $r . ' ';
  58. $caught[$validator_name] = true;
  59. break;
  60. }
  61. }
  62. // all three caught, continue on
  63. if (count($caught) >= 3) $stage = 1;
  64. if ($r !== false) break;
  65. // attempting to catch font-size and perhaps line-height
  66. case 1:
  67. $found_slash = false;
  68. if (strpos($bits[$i], '/') !== false) {
  69. list($font_size, $line_height) =
  70. explode('/', $bits[$i]);
  71. if ($line_height === '') {
  72. // ooh, there's a space after the slash!
  73. $line_height = false;
  74. $found_slash = true;
  75. }
  76. } else {
  77. $font_size = $bits[$i];
  78. $line_height = false;
  79. }
  80. $r = $this->info['font-size']->validate(
  81. $font_size, $config, $context);
  82. if ($r !== false) {
  83. $final .= $r;
  84. // attempt to catch line-height
  85. if ($line_height === false) {
  86. // we need to scroll forward
  87. for ($j = $i + 1; $j < $size; $j++) {
  88. if ($bits[$j] === '') continue;
  89. if ($bits[$j] === '/') {
  90. if ($found_slash) {
  91. return false;
  92. } else {
  93. $found_slash = true;
  94. continue;
  95. }
  96. }
  97. $line_height = $bits[$j];
  98. break;
  99. }
  100. } else {
  101. // slash already found
  102. $found_slash = true;
  103. $j = $i;
  104. }
  105. if ($found_slash) {
  106. $i = $j;
  107. $r = $this->info['line-height']->validate(
  108. $line_height, $config, $context);
  109. if ($r !== false) {
  110. $final .= '/' . $r;
  111. }
  112. }
  113. $final .= ' ';
  114. $stage = 2;
  115. break;
  116. }
  117. return false;
  118. // attempting to catch font-family
  119. case 2:
  120. $font_family =
  121. implode(' ', array_slice($bits, $i, $size - $i));
  122. $r = $this->info['font-family']->validate(
  123. $font_family, $config, $context);
  124. if ($r !== false) {
  125. $final .= $r . ' ';
  126. // processing completed successfully
  127. return rtrim($final);
  128. }
  129. return false;
  130. }
  131. }
  132. return false;
  133. }
  134. }
  135. // vim: et sw=4 sts=4