AMPSpan.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\components\amp;
  3. use Yii;
  4. use magyarandras\AMPConverter\TagConverterInterface;
  5. class AMPSpan implements TagConverterInterface
  6. {
  7. private $necessary_scripts = [];
  8. public function convert(\DOMDocument $doc)
  9. {
  10. /* $query = '//div';
  11. $xpath = new \DOMXPath($doc);
  12. $entries = $xpath->query($query);
  13. $allowed_attributes = ['class', 'id'];
  14. foreach ($entries as $tag) {
  15. $div = $doc->createElement('div');
  16. $br = $doc->createElement('br');
  17. foreach ($allowed_attributes as $attribute) {
  18. if ($tag->hasAttribute($attribute)) {
  19. $div->setAttribute($attribute, $tag->getAttribute($attribute));
  20. }
  21. }
  22. if ($tag->hasChildNodes()) {
  23. foreach ($tag->childNodes as $node) {
  24. $ch = $node->cloneNode(true);
  25. if( $ch->nodeName == 'div' && $ch->getAttribute('id') == 'inject'){
  26. //$div->removeChild($ch);
  27. }else{
  28. $div->appendChild( $ch );
  29. }
  30. }
  31. }
  32. $tag->parentNode->replaceChild($div, $tag);
  33. }*/
  34. $ahrefs = $doc->getElementsByTagName('span');
  35. foreach($ahrefs as $element){
  36. for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
  37. if( $element->attributes->item($k)->nodeName != 'id' && $element->attributes->item($k)->nodeName != 'class' ){
  38. $element->removeAttributeNode( $element->attributes->item($k));
  39. }
  40. }
  41. }
  42. return $doc;
  43. }
  44. public function getNecessaryScripts()
  45. {
  46. return $this->necessary_scripts;
  47. }
  48. }