note 94078 modified in class.domnamednodemap by danbrown

From: Date: Thu, 15 Oct 2009 14:59:20 +0000
Subject: note 94078 modified in class.domnamednodemap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-161766@lists.php.net to get a copy of this message
I stumbled upon a problem with DOMNamedNodeMap. If you iterate over a DOMNamedNodeMap, representing the attributes of a DOMElement with foreach and you use DOMElement::removeAttributeNode only the first attribute will be handled. Example (not complete): <?php /* * Imagine you got a node like this: * <a onclick="alert('evil')" href="http://example.com">evil</a> * and onclick should be removed, href would not be tested. */ foreach ( $node->attributes as $attribute ) { echo 'checking attribute ', $attribute->name, '<br />'; if ( ! in_array ( $attribute->name, $allowed_attributes ) ) { $node->removeAttributeNode ( $attribute ); } } ?> The output would be: checking attribute onclick --was-- I stumbled upon a problem with DOMNamedNodeMap. If you iterate over a DOMNamedNodeMap, representing the attributes of a DOMElement with foreach and you use DOMElement::removeAttributeNode only the first attribute will be handled. Example (not complete): <?php /* * Imagine you got a node like this: * <a onclick="alert('evil')" href="http://someurl.example">evil</a> * and onclick should be removed, href would not be tested. */ foreach ( $node->attributes as $attribute ) { echo 'checking attribute ', $attribute->name, '<br />'; if ( ! in_array ( $attribute->name, $allowed_attributes ) ) { $node->removeAttributeNode ( $attribute ); } } ?> The output would be: checking attribute onclick http://php.net/manual/en/class.domnamednodemap.php

« previous php.notes (#161766) next »