Skip to content
This repository was archived by the owner on Apr 26, 2020. It is now read-only.

pq() crashes if you pass a DOMDocument to it #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion phpQuery/phpQuery/DOMDocumentWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -677,4 +678,4 @@ public static function expandEmptyTag($tag, $xml){
}
return $xml;
}
}
}
36 changes: 33 additions & 3 deletions phpQuery/phpQuery/phpQueryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);
}
}
Expand Down Expand Up @@ -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...
*
Expand Down Expand Up @@ -2631,6 +2645,22 @@ 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
*/
Expand Down