1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace app\components\amp;
- use Yii;
- use magyarandras\AMPConverter\TagConverterInterface;
- class AMPDiv implements TagConverterInterface
- {
- private $necessary_scripts = [];
- public function convert(\DOMDocument $doc)
- {
- /* $query = '//div';
- $xpath = new \DOMXPath($doc);
- $entries = $xpath->query($query);
- $allowed_attributes = ['class', 'id'];
- foreach ($entries as $tag) {
- $div = $doc->createElement('div');
- $br = $doc->createElement('br');
- foreach ($allowed_attributes as $attribute) {
- if ($tag->hasAttribute($attribute)) {
- $div->setAttribute($attribute, $tag->getAttribute($attribute));
- }
- }
- if ($tag->hasChildNodes()) {
- foreach ($tag->childNodes as $node) {
- $ch = $node->cloneNode(true);
- if( $ch->nodeName == 'div' && $ch->getAttribute('id') == 'inject'){
- //$div->removeChild($ch);
- }else{
- $div->appendChild( $ch );
- }
- }
- }
-
- $tag->parentNode->replaceChild($div, $tag);
- }*/
- $ahrefs = $doc->getElementsByTagName('div');
- $divs = iterator_to_array($ahrefs);
- foreach ($divs as $div) {
- if($div->getAttribute('id') == 'inject'){
- $div->parentNode->removeChild($div);
- }
- }
- foreach($ahrefs as $element){
- for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
- if( $element->attributes->item($k)->nodeName != 'id' && $element->attributes->item($k)->nodeName != 'class' && $element->attributes->item($k)->nodeName != 'title'){
- //var_dump( $element->attributes->item($k)->nodeName);
- $element->removeAttributeNode( $element->attributes->item($k));
- }
- }
- }
- return $doc;
- }
- public function getNecessaryScripts()
- {
- return $this->necessary_scripts;
- }
- }
|