From 705c93e8f797a7aa4e4f37fdfd0e49660d43b161 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 16 Nov 2012 18:56:42 +0000 Subject: [PATCH 1/4] Fix crash when loading a DOMDocument node ($loaded not initialised). --- phpQuery/phpQuery/DOMDocumentWrapper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpQuery/phpQuery/DOMDocumentWrapper.php b/phpQuery/phpQuery/DOMDocumentWrapper.php index 47ce35c..a58b1f3 100644 --- a/phpQuery/phpQuery/DOMDocumentWrapper.php +++ b/phpQuery/phpQuery/DOMDocumentWrapper.php @@ -59,6 +59,7 @@ public function load($markup, $contentType = null, $newDocumentID = null) { $this->root = $this->document; $this->charset = $this->document->encoding; // TODO isDocumentFragment + $loaded = true; } else { $loaded = $this->loadMarkup($markup); } @@ -677,4 +678,4 @@ public static function expandEmptyTag($tag, $xml){ } return $xml; } -} \ No newline at end of file +} From ca72bd3cddd011ecd9f3a4fb8b23d141ab925825 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 1 Feb 2013 15:45:24 +0000 Subject: [PATCH 2/4] Add an attrs() method. Returns attributes for all matched elements, not just the first one. E.g. the href attribute of all matching links. --- phpQuery/phpQuery/phpQueryObject.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/phpQuery/phpQuery/phpQueryObject.php b/phpQuery/phpQuery/phpQueryObject.php index 9693cb9..99f1ac0 100644 --- a/phpQuery/phpQuery/phpQueryObject.php +++ b/phpQuery/phpQuery/phpQueryObject.php @@ -2631,6 +2631,21 @@ public function attr($attr = null, $value = null) { return is_null($value) ? '' : $this; } + + /** + * @return The same attribute of each matching element, like + * attr() but returns an array with one entry per matched element. + * Read only. + */ + public function attrs($attr = null) { + $results = array(); + foreach($this->stack(1) as $node) { + $results[] = $node->hasAttribute($attr) + ? $node->getAttribute($attr) + : null; + } + return $results; + } /** * @access private */ From 4e2e30fd593eec688205c3901db2aadb0990dbac Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 19 Mar 2013 14:44:18 +0000 Subject: [PATCH 3/4] Add a .texts() method like .attrs(). --- phpQuery/phpQuery/phpQueryObject.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/phpQuery/phpQuery/phpQueryObject.php b/phpQuery/phpQuery/phpQueryObject.php index 99f1ac0..f0dfa1d 100644 --- a/phpQuery/phpQuery/phpQueryObject.php +++ b/phpQuery/phpQuery/phpQueryObject.php @@ -2264,6 +2264,20 @@ public function text($text = null, $callback1 = null, $callback2 = null, $callba } return $return; } + + /** + * @return The text content of each matching element, like + * text() but returns an array with one entry per matched element. + * Read only. + */ + public function texts($attr = null) { + $results = array(); + foreach($this->elements as $node) { + $results[] = $node->textContent; + } + return $results; + } + /** * Enter description here... * @@ -2646,6 +2660,7 @@ public function attrs($attr = null) { } return $results; } + /** * @access private */ From 7a96e2aead25c50ed5e9c564d32c7f6d53f35253 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 25 Mar 2013 15:13:14 +0000 Subject: [PATCH 4/4] Fix matchClasses(), hasClass() and is() to work with non-space separators. Classes can be separated by any white space, not just space characters. --- phpQuery/phpQuery/phpQueryObject.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpQuery/phpQuery/phpQueryObject.php b/phpQuery/phpQuery/phpQueryObject.php index f0dfa1d..db18fda 100644 --- a/phpQuery/phpQuery/phpQueryObject.php +++ b/phpQuery/phpQuery/phpQueryObject.php @@ -583,11 +583,12 @@ public function newInstance($newStack = null) { * @access private */ protected function matchClasses($class, $node) { + $nodeClasses = preg_split('/\s+/', $node->getAttribute('class') ); + // multi-class if ( mb_strpos($class, '.', 1)) { $classes = explode('.', substr($class, 1)); $classesCount = count( $classes ); - $nodeClasses = explode(' ', $node->getAttribute('class') ); $nodeClassesCount = count( $nodeClasses ); if ( $classesCount > $nodeClassesCount ) return false; @@ -604,8 +605,7 @@ protected function matchClasses($class, $node) { return in_array( // strip leading dot from class name substr($class, 1), - // get classes for element as array - explode(' ', $node->getAttribute('class') ) + $nodeClasses ); } }