Changeset View
Changeset View
Standalone View
Standalone View
src/parser/html/PhutilDOMNode.php
| <?php | <?php | ||||
| final class PhutilDOMNode extends Phobject { | final class PhutilDOMNode extends Phobject { | ||||
| private $content; | private $content; | ||||
| private $tagName; | private $tagName; | ||||
| private $children = array(); | private $children = array(); | ||||
| private $attributes = array(); | private $attributes = array(); | ||||
| private $parentNode; | private $parentNode; | ||||
| private $rawString; | private $rawHead; | ||||
| private $rawTail; | |||||
| public function setContent($content) { | public function setContent($content) { | ||||
| $this->content = $content; | $this->content = $content; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getContent() { | public function getContent() { | ||||
| return $this->content; | return $this->content; | ||||
| Show All 30 Lines | public function setAttributes(array $attributes) { | ||||
| $this->attributes = $attributes; | $this->attributes = $attributes; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getAttributes() { | public function getAttributes() { | ||||
| return $this->attributes; | return $this->attributes; | ||||
| } | } | ||||
| public function setRawString($raw_string) { | public function setRawHead($raw_string) { | ||||
| $this->rawString = $raw_string; | $this->rawHead = $raw_string; | ||||
| return $this; | |||||
| } | |||||
| public function setRawTail($raw_tail) { | |||||
| $this->rawTail = $raw_tail; | |||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getRawString() { | public function getRawString() { | ||||
| return $this->rawString; | $raw = array(); | ||||
| $raw[] = $this->rawHead; | |||||
| foreach ($this->getChildren() as $child) { | |||||
| $raw[] = $child->getRawString(); | |||||
| } | |||||
| $raw[] = $this->rawTail; | |||||
| return implode('', $raw); | |||||
| } | } | ||||
| public function toDictionary() { | public function toDictionary() { | ||||
| if ($this->isContentNode()) { | if ($this->isContentNode()) { | ||||
| return array( | return array( | ||||
| 'content' => $this->content, | 'content' => $this->content, | ||||
| ); | ); | ||||
| } else { | } else { | ||||
| Show All 32 Lines | foreach ($this->getChildren() as $child) { | ||||
| if (isset($tag_map[$tag_name])) { | if (isset($tag_map[$tag_name])) { | ||||
| $nodes[] = $child; | $nodes[] = $child; | ||||
| continue; | continue; | ||||
| } | } | ||||
| // Otherwise, this is some other tag. Convert it into a content | // Otherwise, this is some other tag. Convert it into a content | ||||
| // node. | // node. | ||||
| $raw_content = $child->getRawString(); | $raw_string = $child->getRawString(); | ||||
| $nodes[] = id(new self()) | $nodes[] = id(new self()) | ||||
| ->setContent($raw_content) | ->setContent($raw_string) | ||||
| ->setRawContent($raw_content); | ->setRawHead($raw_string); | ||||
| } | } | ||||
| return $this->mergeContentNodes($nodes); | return $this->mergeContentNodes($nodes); | ||||
| } | } | ||||
| public function getRawContentString() { | public function getRawContentString() { | ||||
| $content_node = $this->selectChildrenWithTags(array()); | $content_node = $this->selectChildrenWithTags(array()); | ||||
| Show All 17 Lines | final class PhutilDOMNode extends Phobject { | ||||
| /** | /** | ||||
| * Given a list of nodes, combine sequences of multiple adjacent content | * Given a list of nodes, combine sequences of multiple adjacent content | ||||
| * nodes into single nodes. | * nodes into single nodes. | ||||
| */ | */ | ||||
| private function mergeContentNodes(array $nodes) { | private function mergeContentNodes(array $nodes) { | ||||
| $list = array(); | $list = array(); | ||||
| $content_block = array(); | $content_block = array(); | ||||
| foreach ($this->getChildren() as $child) { | foreach ($nodes as $node) { | ||||
| if ($child->isContentNode()) { | if ($node->isContentNode()) { | ||||
| $content_block[] = $child; | $content_block[] = $node; | ||||
| continue; | continue; | ||||
| } | } | ||||
| $list[] = $content_block; | $list[] = $content_block; | ||||
| $content_block = array(); | $content_block = array(); | ||||
| $list[] = $child; | $list[] = $node; | ||||
| } | } | ||||
| $list[] = $content_block; | $list[] = $content_block; | ||||
| $results = array(); | $results = array(); | ||||
| foreach ($list as $item) { | foreach ($list as $item) { | ||||
| if (!is_array($item)) { | if (!is_array($item)) { | ||||
| $results[] = $item; | $results[] = $item; | ||||
| Show All 11 Lines | foreach ($list as $item) { | ||||
| $parts = implode('', $parts); | $parts = implode('', $parts); | ||||
| if (!strlen($parts)) { | if (!strlen($parts)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $results[] = id(new self()) | $results[] = id(new self()) | ||||
| ->setContent($parts) | ->setContent($parts) | ||||
| ->setRawString($parts); | ->setRawHead($parts); | ||||
| } | } | ||||
| return $results; | return $results; | ||||
| } | } | ||||
| } | } | ||||