NameSync.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Post-transform that performs validation to the name attribute; if
  4. * it is present with an equivalent id attribute, it is passed through;
  5. * otherwise validation is performed.
  6. */
  7. class HTMLPurifier_AttrTransform_NameSync extends HTMLPurifier_AttrTransform
  8. {
  9. /**
  10. * @type HTMLPurifier_AttrDef_HTML_ID
  11. */
  12. public $idDef;
  13. public function __construct()
  14. {
  15. $this->idDef = new HTMLPurifier_AttrDef_HTML_ID();
  16. }
  17. /**
  18. * @param array $attr
  19. * @param HTMLPurifier_Config $config
  20. * @param HTMLPurifier_Context $context
  21. * @return array
  22. */
  23. public function transform($attr, $config, $context)
  24. {
  25. if (!isset($attr['name'])) {
  26. return $attr;
  27. }
  28. $name = $attr['name'];
  29. if (isset($attr['id']) && $attr['id'] === $name) {
  30. return $attr;
  31. }
  32. $result = $this->idDef->validate($name, $config, $context);
  33. if ($result === false) {
  34. unset($attr['name']);
  35. } else {
  36. $attr['name'] = $result;
  37. }
  38. return $attr;
  39. }
  40. }
  41. // vim: et sw=4 sts=4