Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality get node indices and delete nodes #19

Open
wants to merge 2 commits into
base: 1.5.0
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
49 changes: 47 additions & 2 deletions Src/Sunra/PhpSimple/simplehtmldom_1_5/simple_html_dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class simple_html_dom_node
public $nodetype = HDOM_TYPE_TEXT;
public $tag = 'text';
public $attr = array();
/** @var simple_html_dom_node[] $children */
public $children = array();
public $nodes = array();
public $parent = null;
Expand Down Expand Up @@ -150,6 +149,26 @@ function clear()
$this->children = null;
}

/**
* Get this node's starting (self) index in the html tree
* @return mixed int
*/
function getSelfIndex(){
return $this->_[HDOM_INFO_BEGIN];
}

/**
* Get the index of the last child belonging to this node
* @return mixed
*/
function getLastChildIndex(){
if (isset($this->_[HDOM_INFO_END])) {
return $this->_[HDOM_INFO_END];
}else {
return $this->getSelfIndex();
}
}

// dump node's tree
function dump($show_attr=true, $deep=0)
{
Expand Down Expand Up @@ -355,8 +374,9 @@ function innertext()
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);

$ret = '';
foreach ($this->nodes as $n)
foreach ($this->nodes as $n) {
$ret .= $n->outertext();
}
return $ret;
}

Expand Down Expand Up @@ -1038,6 +1058,31 @@ function __destruct()
$this->clear();
}

/**
* Delete a node and its children recursively
*
* @param $nodes array Node to be deleted
*/
function deleteNodes($nodes)
{
foreach ($nodes as $n) {
$n->outertext = "";
}
$this->load($this->save(), true, false);
}

/**
* @param $selector string
*/
function deleteNodeBySelector($selector){
foreach ($this->find($selector) as $node)
{
$node->outertext = '';
}

$this->load($this->save());
}

// load html from string
function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "sunra/php-simple-html-dom-parser",
"name": "michaelquery/php-simple-html-dom-parser",
"type": "library",
"description": "Composer adaptation of: A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way! Require PHP 5+. Supports invalid HTML. Find tags on an HTML page with selectors just like jQuery. Extract contents from HTML in a single line.",
"keywords": ["html", "dom", "parser"],
"homepage": "https://github.com/sunra/php-simple-html-dom-parser",
"homepage": "https://github.com/michaelquery/php-simple-html-dom-parser",
"license": "MIT",
"authors": [
{
"homepage": "http://sourceforge.net/projects/simplehtmldom/"
},
{
"name": "Sunra",
"email": "[email protected]",
"homepage": "https://github.com/sunra"
"name": "Michael Query",
"email": "[email protected]",
"homepage": "https://github.com/michaelquery"
}
],
"require": {
Expand All @@ -21,4 +21,4 @@
"autoload": {
"psr-0": { "Sunra\\PhpSimple\\HtmlDomParser": "Src/" }
}
}
}