AMPAhref.php 952 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace app\components\amp;
  3. use Yii;
  4. use magyarandras\AMPConverter\TagConverterInterface;
  5. class AMPAhref implements TagConverterInterface
  6. {
  7. private $necessary_scripts = [];
  8. public function convert(\DOMDocument $doc)
  9. {
  10. $allowed_attributes = ['class', 'id', 'target', 'href'];
  11. $allowed_target = ['_blank', '_self', '_top'];
  12. $ahrefs = $doc->getElementsByTagName('a');
  13. foreach($ahrefs as $element){
  14. for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
  15. if( array_search( $element->attributes->item($k)->nodeName, $allowed_attributes ) === false || ( $element->attributes->item($k)->nodeName == 'target' && array_search( $element->attributes->item($k)->nodeValue, $allowed_target) === false) ){
  16. $element->removeAttributeNode( $element->attributes->item($k));
  17. }
  18. }
  19. }
  20. return $doc;
  21. }
  22. public function getNecessaryScripts()
  23. {
  24. return $this->necessary_scripts;
  25. }
  26. }