diff options
author | Andres Rey <[email protected]> | 2016-11-02 09:55:00 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2016-11-02 09:55:00 +0000 |
commit | 9238420ea7bfb6975610739b3685005b9ac8e5e6 (patch) | |
tree | 5bd66e3e94b61e141b7089b6cf6d5560b139c30d /src/Readability.php | |
parent | f46b43d0b5351af668c1c7229a0cddaef3ba5582 (diff) |
Added removeAndGetNext but needs some work. Also articleByLine
Diffstat (limited to 'src/Readability.php')
-rw-r--r-- | src/Readability.php | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Readability.php b/src/Readability.php index d3b7270..3810ba8 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -281,4 +281,41 @@ class Readability extends Element implements ReadabilityInterface { return $this->node; } + + public function removeAndGetNext($node) + { + $nextNode = $this->getNextNode($node, true); + $node->node->parentNode->removeChild($node); + return $nextNode; + } + + private function getNextNode($originalNode, $ignoreSelfAndKids) + { + /** + * Traverse the DOM from node to node, starting at the node passed in. + * Pass true for the second parameter to indicate this node itself + * (and its kids) are going away, and we want the next node over. + * + * Calling this in a loop will traverse the DOM depth-first. + */ + + // First check for kids if those aren't being ignored + if (!$ignoreSelfAndKids && $originalNode->node->firstChild) { + return $originalNode->node->firstChild; + } + + // Then for siblings... + if ($originalNode->node->nextSibling) { + return $originalNode->node->nextSibling; + } + + // And finally, move up the parent chain *and* find a sibling + // (because this is depth-first traversal, we will have already + // seen the parent nodes themselves). + do { + $originalNode = $this->getParent(); + } while ($originalNode && !$originalNode->node->nextSibling); + + return $originalNode && $originalNode->node->nextSibling; + } } |