NameSync.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. public function __construct()
  10. {
  11. $this->idDef = new HTMLPurifier_AttrDef_HTML_ID();
  12. }
  13. /**
  14. * @param array $attr
  15. * @param HTMLPurifier_Config $config
  16. * @param HTMLPurifier_Context $context
  17. * @return array
  18. */
  19. public function transform($attr, $config, $context)
  20. {
  21. if (!isset($attr['name'])) {
  22. return $attr;
  23. }
  24. $name = $attr['name'];
  25. if (isset($attr['id']) && $attr['id'] === $name) {
  26. return $attr;
  27. }
  28. $result = $this->idDef->validate($name, $config, $context);
  29. if ($result === false) {
  30. unset($attr['name']);
  31. } else {
  32. $attr['name'] = $result;
  33. }
  34. return $attr;
  35. }
  36. }
  37. // vim: et sw=4 sts=4