Skip to content

Commit

Permalink
Add support of DOMCdataSection nodes and add methods createTextNode()…
Browse files Browse the repository at this point in the history
…, createComment(), createCdataSection() to the Document class
  • Loading branch information
Imangazaliev committed Jan 17, 2019
1 parent 0096dfa commit a8389cc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 16 deletions.
38 changes: 37 additions & 1 deletion src/DiDom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace DiDom;

use DOMCdataSection;
use DOMComment;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMText;
use DOMXPath;
use InvalidArgumentException;
use RuntimeException;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
39 changes: 25 additions & 14 deletions src/DiDom/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace DiDom;

use DOMDocument;
use DOMCdataSection;
use DOMComment;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMText;
Expand All @@ -19,7 +20,7 @@ class Element
/**
* The DOM element instance.
*
* @var \DOMElement|\DOMText|\DOMComment
* @var \DOMElement|\DOMText|\DOMComment|\DOMCdataSection
*/
protected $node;

Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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;
Expand All @@ -1350,7 +1361,7 @@ protected function setNode($node)
/**
* Returns current node instance.
*
* @return \DOMElement|\DOMText|\DOMComment
* @return \DOMElement|\DOMText|\DOMComment|\DOMCdataSection
*/
public function getNode()
{
Expand Down
33 changes: 33 additions & 0 deletions tests/DiDom/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/books.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
Expand Down

0 comments on commit a8389cc

Please sign in to comment.