AMPP.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\components\amp;
  3. use Yii;
  4. use magyarandras\AMPConverter\TagConverterInterface;
  5. class AMPP implements TagConverterInterface
  6. {
  7. private $necessary_scripts = [];
  8. public function convert(\DOMDocument $doc)
  9. {
  10. $query = '//ul|//li|//strong|//table';
  11. $xpath = new \DOMXPath($doc);
  12. $entries = $xpath->query($query);
  13. $allowed_attributes = ['class', 'id'];
  14. foreach($entries as $element){
  15. for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
  16. if( array_search( $element->attributes->item($k)->nodeName, $allowed_attributes ) === false ){
  17. $element->removeAttributeNode( $element->attributes->item($k));
  18. }
  19. }
  20. }
  21. $ahrefs = $doc->getElementsByTagName('p');
  22. foreach($ahrefs as $element){
  23. for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
  24. $element->removeAttributeNode( $element->attributes->item($k));
  25. }
  26. }
  27. return $doc;
  28. }
  29. public function getNecessaryScripts()
  30. {
  31. return $this->necessary_scripts;
  32. }
  33. }