Skip to content
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
4 changes: 4 additions & 0 deletions History.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== 1.1.7 / 2021-02-23
* Added `remove` method to delete selector from the object
* Added `html` method to return the object as raw html

=== 1.1.5 / 2014-10-20
* Added nth-child-last support
* Fixed attribute selectors
Expand Down
24 changes: 19 additions & 5 deletions selector.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// --- Selector.inc - (c) Copyright TJ Holowaychuk <tj@vision-media.ca> MIT Licensed

define('SELECTOR_VERSION', '1.1.6');
define('SELECTOR_VERSION', '1.1.7');

/**
* SelectorDOM.
Expand All @@ -16,20 +16,34 @@ define('SELECTOR_VERSION', '1.1.6');
*/

class SelectorDOM {
private $dom = null;
private $xpath = null;

public function __construct($data) {
if ($data instanceof DOMDocument) {
$this->xpath = new DOMXpath($data);
$this->dom = $data;
} else {
$dom = new DOMDocument();
@$dom->loadHTML($data);
$this->xpath = new DOMXpath($dom);
$this->dom = new DOMDocument();
@$this->dom->loadHTML($data);
}
$this->xpath = new DOMXpath($this->dom);
}

public function select($selector, $as_array = true) {
$elements = $this->xpath->evaluate(selector_to_xpath($selector));
return $as_array ? elements_to_array($elements) : $elements;
}

public function remove($selector) {
$nodes = $this->xpath->query(selector_to_xpath($selector));
foreach ($nodes as $node) {
$node->parentNode->removeChild($node);
}
}

public function html() {
return $this->dom->saveHTML();
}
}

/**
Expand Down