diff --git a/src/DiDom/Document.php b/src/DiDom/Document.php index d96a94d..bd6bcf8 100644 --- a/src/DiDom/Document.php +++ b/src/DiDom/Document.php @@ -2,9 +2,12 @@ namespace DiDom; +use DOMCdataSection; +use DOMComment; use DOMDocument; use DOMElement; use DOMNode; +use DOMText; use DOMXPath; use InvalidArgumentException; use RuntimeException; @@ -128,6 +131,36 @@ public function createElementBySelector($selector, $value = null, array $attribu return $this->createElement($name, $value, $attributes); } + /** + * @param string $content + * + * @return Element + */ + public function createTextNode($content) + { + return new Element(new DOMText($content)); + } + + /** + * @param string $data + * + * @return Element + */ + public function createComment($data) + { + return new Element(new DOMComment($data)); + } + + /** + * @param string $data + * + * @return Element + */ + public function createCdataSection($data) + { + return new Element(new DOMCdataSection($data)); + } + /** * Adds a new child at the end of the children. * @@ -445,7 +478,7 @@ public function first($expression, $type = Query::TYPE_CSS, $wrapNode = true, $c * * @return \DiDom\Element|string * - * @throws InvalidArgumentException if node is not DOMElement, DOMText, DOMComment or DOMAttr + * @throws InvalidArgumentException if node is not DOMElement, DOMText, DOMComment, DOMCdataSection or DOMAttr */ protected function wrapNode($node) { @@ -459,6 +492,9 @@ protected function wrapNode($node) case 'DOMComment': return new Element($node); + case 'DOMCdataSection': + return new Element($node); + case 'DOMAttr': return $node->value; } diff --git a/src/DiDom/Element.php b/src/DiDom/Element.php index c0b0018..79354d2 100644 --- a/src/DiDom/Element.php +++ b/src/DiDom/Element.php @@ -2,8 +2,9 @@ namespace DiDom; -use DOMDocument; +use DOMCdataSection; use DOMComment; +use DOMDocument; use DOMElement; use DOMNode; use DOMText; @@ -19,7 +20,7 @@ class Element /** * The DOM element instance. * - * @var \DOMElement|\DOMText|\DOMComment + * @var \DOMElement|\DOMText|\DOMComment|\DOMCdataSection */ protected $node; @@ -36,7 +37,7 @@ class Element /** * Constructor. * - * @param \DOMElement|\DOMText|\DOMComment|string $tagName The tag name of the element + * @param \DOMElement|\DOMText|\DOMComment|\DOMCdataSection|string $tagName The tag name of the element * @param string|null $value The value of the element * @param array $attributes The attributes of the element */ @@ -774,7 +775,7 @@ public function setValue($value) } /** - * Returns true if current node is DOMElement. + * Returns true if current node is a DOMElement instance. * * @return bool */ @@ -784,7 +785,7 @@ public function isElementNode() } /** - * Returns true if current node is DOMText. + * Returns true if current node is a a DOMText instance. * * @return bool */ @@ -794,7 +795,7 @@ public function isTextNode() } /** - * Returns true if current node is DOMComment. + * Returns true if current node is a DOMComment instance. * * @return bool */ @@ -803,6 +804,16 @@ public function isCommentNode() return $this->node instanceof DOMComment; } + /** + * Returns true if current node is a DOMCdataSection instance. + * + * @return bool + */ + public function isCdataSectionNode() + { + return $this->node instanceof DOMCdataSection; + } + /** * Indicates if two nodes are the same node. * @@ -896,7 +907,7 @@ public function previousSibling($selector = null, $nodeType = null) throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($nodeType))); } - $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment']; + $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection']; if (!in_array($nodeType, $allowedTypes, true)) { throw new RuntimeException(sprintf('Unknown node type "%s". Allowed types: %s', $nodeType, implode(', ', $allowedTypes))); @@ -956,7 +967,7 @@ public function previousSiblings($selector = null, $nodeType = null) throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($nodeType))); } - $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment']; + $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection']; if (!in_array($nodeType, $allowedTypes, true)) { throw new RuntimeException(sprintf('Unknown node type "%s". Allowed types: %s', $nodeType, implode(', ', $allowedTypes))); @@ -1034,7 +1045,7 @@ public function nextSibling($selector = null, $nodeType = null) throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($nodeType))); } - $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment']; + $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection']; if (!in_array($nodeType, $allowedTypes, true)) { throw new RuntimeException(sprintf('Unknown node type "%s". Allowed types: %s', $nodeType, implode(', ', $allowedTypes))); @@ -1094,7 +1105,7 @@ public function nextSiblings($selector = null, $nodeType = null) throw new InvalidArgumentException(sprintf('%s expects parameter 2 to be string, %s given', __METHOD__, gettype($nodeType))); } - $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment']; + $allowedTypes = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection']; if (!in_array($nodeType, $allowedTypes, true)) { throw new RuntimeException(sprintf('Unknown node type "%s". Allowed types: %s', $nodeType, implode(', ', $allowedTypes))); @@ -1330,16 +1341,16 @@ public function cloneNode($deep = true) /** * Sets current node instance. * - * @param \DOMElement|\DOMText|\DOMComment $node + * @param \DOMElement|\DOMText|\DOMComment|\DOMCdataSection $node * * @return \DiDom\Element */ protected function setNode($node) { - $allowedClasses = ['DOMElement', 'DOMText', 'DOMComment']; + $allowedClasses = ['DOMElement', 'DOMText', 'DOMComment', 'DOMCdataSection']; if (!is_object($node) || !in_array(get_class($node), $allowedClasses, true)) { - throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of DOMElement, DOMText or DOMComment, %s given', __METHOD__, (is_object($node) ? get_class($node) : gettype($node)))); + throw new InvalidArgumentException(sprintf('Argument 1 passed to %s must be an instance of DOMElement, DOMText, DOMComment or DOMCdataSection, %s given', __METHOD__, (is_object($node) ? get_class($node) : gettype($node)))); } $this->node = $node; @@ -1350,7 +1361,7 @@ protected function setNode($node) /** * Returns current node instance. * - * @return \DOMElement|\DOMText|\DOMComment + * @return \DOMElement|\DOMText|\DOMComment|\DOMCdataSection */ public function getNode() { diff --git a/tests/DiDom/DocumentTest.php b/tests/DiDom/DocumentTest.php index 504289f..2f6e5fb 100644 --- a/tests/DiDom/DocumentTest.php +++ b/tests/DiDom/DocumentTest.php @@ -120,6 +120,39 @@ public function testCreateElementBySelector() $this->assertEquals(['name' => 'name', 'placeholder' => 'Enter your name'], $element->attributes()); } + public function testCreateTextNode() + { + $document = new Document(); + + $textNode = $document->createTextNode('foo bar baz'); + + $this->assertInstanceOf('DiDom\Element', $textNode); + $this->assertInstanceOf('DOMText', $textNode->getNode()); + $this->assertEquals('foo bar baz', $textNode->text()); + } + + public function testCreateComment() + { + $document = new Document(); + + $comment = $document->createComment('foo bar baz'); + + $this->assertInstanceOf('DiDom\Element', $comment); + $this->assertInstanceOf('DOMComment', $comment->getNode()); + $this->assertEquals('foo bar baz', $comment->text()); + } + + public function testCreateCDATASection() + { + $document = new Document(); + + $cdataSection = $document->createCdataSection('foo bar baz'); + + $this->assertInstanceOf('DiDom\Element', $cdataSection); + $this->assertInstanceOf('DOMCdataSection', $cdataSection->getNode()); + $this->assertEquals('foo bar baz', $cdataSection->text()); + } + /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Argument 1 passed to DiDom\Document::appendChild must be an instance of DiDom\Element or DOMNode, string given diff --git a/tests/fixtures/books.xml b/tests/fixtures/books.xml index e3d1fe8..fa0e549 100644 --- a/tests/fixtures/books.xml +++ b/tests/fixtures/books.xml @@ -1,4 +1,4 @@ - + Gambardella, Matthew