PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle/lib/htmlpurifier/HTMLPurifier/AttrDef.php

https://bitbucket.org/geek745/moodle-db2
PHP | 91 lines | 20 code | 10 blank | 61 comment | 0 complexity | a5e98ac35fde8f4d2755e37de98618a2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. /**
  3. * Base class for all validating attribute definitions.
  4. *
  5. * This family of classes forms the core for not only HTML attribute validation,
  6. * but also any sort of string that needs to be validated or cleaned (which
  7. * means CSS properties and composite definitions are defined here too).
  8. * Besides defining (through code) what precisely makes the string valid,
  9. * subclasses are also responsible for cleaning the code if possible.
  10. */
  11. class HTMLPurifier_AttrDef
  12. {
  13. /**
  14. * Tells us whether or not an HTML attribute is minimized. Has no
  15. * meaning in other contexts.
  16. */
  17. var $minimized = false;
  18. /**
  19. * Tells us whether or not an HTML attribute is required. Has no
  20. * meaning in other contexts
  21. */
  22. var $required = false;
  23. /**
  24. * Validates and cleans passed string according to a definition.
  25. *
  26. * @public
  27. * @param $string String to be validated and cleaned.
  28. * @param $config Mandatory HTMLPurifier_Config object.
  29. * @param $context Mandatory HTMLPurifier_AttrContext object.
  30. */
  31. function validate($string, $config, &$context) {
  32. trigger_error('Cannot call abstract function', E_USER_ERROR);
  33. }
  34. /**
  35. * Convenience method that parses a string as if it were CDATA.
  36. *
  37. * This method process a string in the manner specified at
  38. * <http://www.w3.org/TR/html4/types.html#h-6.2> by removing
  39. * leading and trailing whitespace, ignoring line feeds, and replacing
  40. * carriage returns and tabs with spaces. While most useful for HTML
  41. * attributes specified as CDATA, it can also be applied to most CSS
  42. * values.
  43. *
  44. * @note This method is not entirely standards compliant, as trim() removes
  45. * more types of whitespace than specified in the spec. In practice,
  46. * this is rarely a problem, as those extra characters usually have
  47. * already been removed by HTMLPurifier_Encoder.
  48. *
  49. * @warning This processing is inconsistent with XML's whitespace handling
  50. * as specified by section 3.3.3 and referenced XHTML 1.0 section
  51. * 4.7. However, note that we are NOT necessarily
  52. * parsing XML, thus, this behavior may still be correct. We
  53. * assume that newlines have been normalized.
  54. *
  55. * @public
  56. */
  57. function parseCDATA($string) {
  58. $string = trim($string);
  59. $string = str_replace(array("\n", "\t", "\r"), ' ', $string);
  60. return $string;
  61. }
  62. /**
  63. * Factory method for creating this class from a string.
  64. * @param $string String construction info
  65. * @return Created AttrDef object corresponding to $string
  66. * @public
  67. */
  68. function make($string) {
  69. // default implementation, return flyweight of this object
  70. // if overloaded, it is *necessary* for you to clone the
  71. // object (usually by instantiating a new copy) and return that
  72. return $this;
  73. }
  74. /**
  75. * Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work
  76. * properly. THIS IS A HACK!
  77. */
  78. function mungeRgb($string) {
  79. return preg_replace('/rgb\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\)/', 'rgb(\1,\2,\3)', $string);
  80. }
  81. }