TargetBlank.php 887 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. // must be called POST validation
  3. /**
  4. * Adds target="blank" to all outbound links. This transform is
  5. * only attached if Attr.TargetBlank is TRUE. This works regardless
  6. * of whether or not Attr.AllowedFrameTargets
  7. */
  8. class HTMLPurifier_AttrTransform_TargetBlank extends HTMLPurifier_AttrTransform
  9. {
  10. private $parser;
  11. public function __construct() {
  12. $this->parser = new HTMLPurifier_URIParser();
  13. }
  14. public function transform($attr, $config, $context) {
  15. if (!isset($attr['href'])) {
  16. return $attr;
  17. }
  18. // XXX Kind of inefficient
  19. $url = $this->parser->parse($attr['href']);
  20. $scheme = $url->getSchemeObj($config, $context);
  21. if ($scheme->browsable && !$url->isBenign($config, $context)) {
  22. $attr['target'] = '_blank';
  23. }
  24. return $attr;
  25. }
  26. }
  27. // vim: et sw=4 sts=4