Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14002140
D20630.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D20630.diff
View Options
diff --git a/src/parser/html/PhutilDOMNode.php b/src/parser/html/PhutilDOMNode.php
--- a/src/parser/html/PhutilDOMNode.php
+++ b/src/parser/html/PhutilDOMNode.php
@@ -7,7 +7,8 @@
private $children = array();
private $attributes = array();
private $parentNode;
- private $rawString;
+ private $rawHead;
+ private $rawTail;
public function setContent($content) {
$this->content = $content;
@@ -54,13 +55,27 @@
return $this->attributes;
}
- public function setRawString($raw_string) {
- $this->rawString = $raw_string;
+ public function setRawHead($raw_string) {
+ $this->rawHead = $raw_string;
+ return $this;
+ }
+
+ public function setRawTail($raw_tail) {
+ $this->rawTail = $raw_tail;
return $this;
}
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() {
@@ -109,11 +124,11 @@
// Otherwise, this is some other tag. Convert it into a content
// node.
- $raw_content = $child->getRawString();
+ $raw_string = $child->getRawString();
$nodes[] = id(new self())
- ->setContent($raw_content)
- ->setRawContent($raw_content);
+ ->setContent($raw_string)
+ ->setRawHead($raw_string);
}
return $this->mergeContentNodes($nodes);
@@ -147,16 +162,16 @@
private function mergeContentNodes(array $nodes) {
$list = array();
$content_block = array();
- foreach ($this->getChildren() as $child) {
- if ($child->isContentNode()) {
- $content_block[] = $child;
+ foreach ($nodes as $node) {
+ if ($node->isContentNode()) {
+ $content_block[] = $node;
continue;
}
$list[] = $content_block;
$content_block = array();
- $list[] = $child;
+ $list[] = $node;
}
$list[] = $content_block;
@@ -184,7 +199,7 @@
$results[] = id(new self())
->setContent($parts)
- ->setRawString($parts);
+ ->setRawHead($parts);
}
return $results;
diff --git a/src/parser/html/PhutilHTMLParser.php b/src/parser/html/PhutilHTMLParser.php
--- a/src/parser/html/PhutilHTMLParser.php
+++ b/src/parser/html/PhutilHTMLParser.php
@@ -104,7 +104,7 @@
$node = id(new PhutilDOMNode())
->setContent($content)
- ->setRawString($content);
+ ->setRawHead($content);
$this->getCursor()->appendChild($node);
}
@@ -182,8 +182,12 @@
while ($cursor) {
if ($cursor->getTagName() === $tag_name) {
+ // Add this raw content to the raw content of the tag we're closing.
+ $cursor->setRawTail('<'.$raw_content.'>');
+
$parent = $cursor->getParentNode();
$this->setCursor($parent);
+
return true;
}
$cursor = $cursor->getParentNode();
@@ -205,7 +209,7 @@
$node = id(new PhutilDOMNode())
->setTagName($tag_name)
->setAttributes($attribute_map)
- ->setRawString('<'.$raw_content.'>');
+ ->setRawHead('<'.$raw_content.'>');
$cursor = $this->getCursor();
$cursor->appendChild($node);
diff --git a/src/parser/html/__tests__/PhutilHTMLParserTestCase.php b/src/parser/html/__tests__/PhutilHTMLParserTestCase.php
--- a/src/parser/html/__tests__/PhutilHTMLParserTestCase.php
+++ b/src/parser/html/__tests__/PhutilHTMLParserTestCase.php
@@ -42,4 +42,63 @@
}
}
+ public function testSelectChildrenWithTags() {
+ $input = '<a></a><b></b><a /><c /><a /><x></x><y /><a /><d>x</d><a /><e>';
+ $document = id(new PhutilHTMLParser())
+ ->parseDocument($input);
+
+ $children = $document->selectChildrenWithTags(array('a'));
+
+ $list = array();
+ foreach ($children as $child) {
+ $list[] = $child->toDictionary();
+ }
+
+ $this->assertEqual(
+ array(
+ array(
+ 'tag' => 'a',
+ 'attributes' => array(),
+ 'children' => array(),
+ ),
+ array(
+ 'content' => '<b></b>',
+ ),
+ array(
+ 'tag' => 'a',
+ 'attributes' => array(),
+ 'children' => array(),
+ ),
+ array(
+ 'content' => '<c />',
+ ),
+ array(
+ 'tag' => 'a',
+ 'attributes' => array(),
+ 'children' => array(),
+ ),
+ array(
+ 'content' => '<x></x><y />',
+ ),
+ array(
+ 'tag' => 'a',
+ 'attributes' => array(),
+ 'children' => array(),
+ ),
+ array(
+ 'content' => '<d>x</d>',
+ ),
+ array(
+ 'tag' => 'a',
+ 'attributes' => array(),
+ 'children' => array(),
+ ),
+ array(
+ 'content' => '<e>',
+ ),
+ ),
+ $list,
+ pht('Child selection of: %s.', $input));
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Oct 26, 3:36 PM (2 w, 4 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6713872
Default Alt Text
D20630.diff (4 KB)
Attached To
Mode
D20630: Fix some remarkup issues with selective construction of child nodes after HTML DOM parsing
Attached
Detach File
Event Timeline
Log In to Comment