AMPDiv.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\components\amp;
  3. use Yii;
  4. use magyarandras\AMPConverter\TagConverterInterface;
  5. class AMPDiv 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('div');
  35. $divs = iterator_to_array($ahrefs);
  36. foreach ($divs as $div) {
  37. if($div->getAttribute('id') == 'inject'){
  38. $div->parentNode->removeChild($div);
  39. }
  40. }
  41. foreach($ahrefs as $element){
  42. for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
  43. if( $element->attributes->item($k)->nodeName != 'id' && $element->attributes->item($k)->nodeName != 'class' && $element->attributes->item($k)->nodeName != 'title'){
  44. //var_dump( $element->attributes->item($k)->nodeName);
  45. $element->removeAttributeNode( $element->attributes->item($k));
  46. }
  47. }
  48. }
  49. return $doc;
  50. }
  51. public function getNecessaryScripts()
  52. {
  53. return $this->necessary_scripts;
  54. }
  55. }