Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- Closed #57
- Closed #56
- Closed #52
  • Loading branch information
PerryRylance committed Feb 26, 2022
1 parent 053ac92 commit 5a55e5d
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/DOMDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function create($html)

// NB: Clone nodes here otherwise when they go out of scope, they're garbage collected

foreach($set->first()->childNodes as $node)
foreach($set->first()[0]->childNodes as $node)
$arr []= $node->cloneNode(true);

return new DOMQueryResults($arr);
Expand Down
102 changes: 73 additions & 29 deletions src/DOMDocument/DOMQueryResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ public function __call($name, $arguments)
return $this;
}

public function __get($name)
{
switch($name)
{
case "length":
return count($this->container);
break;

case "html":
$result = "";

foreach($this->container as $el)
$result .= $el->html;

return $result;
break;
}
}

public function offsetExists($offset)
{
return isset($this->container[$offset]);
Expand Down Expand Up @@ -208,7 +227,7 @@ public function first()
if(count($this->container) == 0)
return null;

return $this->container[0];
return new DOMQueryResults( $this->container[0] );
}

/**
Expand All @@ -221,7 +240,7 @@ public function last()
if(count($this->container) == 0)
return null;

return $this->container[count($this->container) - 1];
return new DOMQueryResults( $this->container[count($this->container) - 1] );
}

/**
Expand Down Expand Up @@ -334,11 +353,11 @@ public function css($arg=null, $val=DOMDocument::UNDEFINED)
{
if(is_string($arg))
{
if(!$this->first())
if(!$this->first()->length)
return null;

if($val == DOMDocument::UNDEFINED)
return $this->first()->getInlineStyle($arg);
return $this->first()[0]->getInlineStyle($arg);

if(!is_string($val) && !is_null($val))
throw new \Exception("When a string is supplied as the first argument, the second argument must be a string or null");
Expand Down Expand Up @@ -376,7 +395,7 @@ public function css($arg=null, $val=DOMDocument::UNDEFINED)
*/
public function text($text=null)
{
if($text == null)
if($text === null)
{
$result = "";

Expand All @@ -387,7 +406,12 @@ public function text($text=null)
}

if(!is_scalar($text))
{
var_dump($text);
exit;

throw new \Exception("Input must be scalar");
}

$this->clear();

Expand All @@ -412,13 +436,22 @@ public function html($html=null)
{
// NB: Getter text() returns text for all nodes, html() only returns the HTML string for the first node in jQuery. This library mirrors that behaviour.

if($html == null)
if(is_string($html) && $html == "")
{
$this->clear();
return $this;
}

if($html === null)
{
if(!$this->first())
if(!$this->length)
return null; // NB: Undefined in jQuery

$result = $this->first()->ownerDocument->saveHTML($this->first());

$result = "";

foreach($this->first()->children() as $child)
$result .= $child->html;

return $result;
}

Expand All @@ -433,7 +466,7 @@ public function html($html=null)
"disable_html_ns" => true
]);

$body = $temp->find('#domdocument-import-payload___')->first();
$body = $temp->find('#domdocument-import-payload___')->first()[0];

foreach($this->container as $el)
{
Expand All @@ -457,8 +490,10 @@ public function html($html=null)
public function clear()
{
foreach($this->container as $el)
{
while($el->childNodes->length)
$el->removeChild($el->firstChild);
}

return $this;
}
Expand All @@ -474,7 +509,7 @@ public function wrap($template)
throw new \Exception("Argument must be an instance of DOMElement or DOMQueryResults");

if($template instanceof DOMQueryResults)
$template = $template->first(); // NB: Wrap in the first node of the set, mirror jQuery behaviour
$template = $template->first()[0]; // NB: Wrap in the first node of the set, mirror jQuery behaviour

foreach($this->container as $el)
{
Expand Down Expand Up @@ -502,7 +537,7 @@ public function wrapInner($template)
throw new \Exception("Argument must be an instance of DOMElement or DOMQueryResults");

if($template instanceof DOMQueryResults)
$template = $template->first(); // NB: Wrap in the first node of the set, mirror jQuery behaviour
$template = $template->first()[0]; // NB: Wrap in the first node of the set, mirror jQuery behaviour

foreach($this->container as $el)
{
Expand Down Expand Up @@ -611,7 +646,7 @@ public function prev($selector=null)

$results = [];

for($node = $this->last()->previousSibling; $node != null; $node = $node->previousSibling)
for($node = $this->last()[0]->previousSibling; $node != null; $node = $node->previousSibling)
{
if($node->nodeType != XML_ELEMENT_NODE)
continue;
Expand All @@ -630,12 +665,12 @@ public function prev($selector=null)
*/
public function following($selector=null)
{
if(!$this->first())
if(!$this->length)
return new DOMQueryResults([]);

$results = [];

for($node = $this->first()->nextSibling; $node != null; $node = $node->nextSibling)
for($node = $this->first()[0]->nextSibling; $node != null; $node = $node->nextSibling)
{
if($node->nodeType != XML_ELEMENT_NODE)
continue;
Expand Down Expand Up @@ -688,10 +723,10 @@ public function val($value=null)
// NB: Getter always returns value for first node
if($value == null)
{
if(!$this->first())
if(!$this->length)
return null;

$el = $this->first();
$el = $this->first()[0];

switch(strtolower($el->nodeName))
{
Expand Down Expand Up @@ -857,14 +892,16 @@ public function attr($arg, $val=null)

if($val === null && is_string($arg))
{
if(!$this->first())
if(!$this->first()->length)
return null;

$first = $this->first()[0];

// NB: If the attribute doesn't exist, return null, as the implementation of the PHP \DOMElement class would return an empty string. This is not how jQuery behaves. With thanks to https://github.com/warhuhn/
if(!$this->first()->hasAttribute($arg))
if(!$first->hasAttribute($arg))
return null;

return $this->first()->getAttribute($arg);
return $first->getAttribute($arg);
}

if(is_string($arg))
Expand Down Expand Up @@ -901,10 +938,17 @@ public function prop($name, $value=null)
if(empty($this->container))
return null; // NB: jQuery would return undefined here

$el = $this->first();
$el = $this->first()[0];

if($value === null)
return preg_match('/' . preg_quote($name) . '/i', $el->getAttribute($name));
{
$result = preg_match('/' . preg_quote($name) . '/i', $el->getAttribute($name));

if($result === false)
throw new \Exception("Error in pattern matching in prop");

return $result == 1;
}

foreach($this->container as $el)
{
Expand Down Expand Up @@ -943,7 +987,7 @@ public function data($arg=null, $val=null)
if($val != null)
throw new \Exception("Argument is null but value is provided, invalid arguments");

if(!$this->first())
if(!$this->length)
return null;

// Both arguments are null, return all data
Expand All @@ -955,7 +999,7 @@ public function data($arg=null, $val=null)

return $results;
}

if(is_string($arg))
{
if(is_scalar($val))
Expand All @@ -967,10 +1011,10 @@ public function data($arg=null, $val=null)
}
else if($val == null)
{
if(!$this->first())
if(!$this->length)
return null;

return $this->first()->getAttribute("data-$arg");
return $this->first()[0]->getAttribute("data-$arg");
}
else
throw new \Exception("Invalid arguments");
Expand Down Expand Up @@ -1000,15 +1044,15 @@ public function data($arg=null, $val=null)
*/
public function after($arg)
{
if(!$this->first())
if(!$this->length)
return $this; // NB: Need ownerDocument below, so if we have no nodes, just bail here.

if($arg instanceof DOMElement)
$nodes = [$arg];
else if(is_array($arg) || $arg instanceof DOMQueryResults)
$nodes = $arg;
else if(is_string($arg))
$nodes = [$this->first()->ownerDocument->createTextNode($arg)];
$nodes = [$this->first()[0]->ownerDocument->createTextNode($arg)];
else
throw new \Exception("Invalid argument");

Expand Down Expand Up @@ -1042,7 +1086,7 @@ public function after($arg)
*/
public function before($arg)
{
if(!$this->first())
if(!$this->length)
return $this; // NB: Need ownerDocument below, so if we have no nodes, just bail here.

if($arg instanceof DOMElement)
Expand Down
64 changes: 64 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@


<!-- Test for isBefore / getBreadth / getDepth -->
<div class="before"></div>
<div>
<div class="after"></div>
</div>

<!-- Test for getDepth -->
<div id="depth"></div>

The content of the document......

<video controls></video>

<div class="node-to-be-removed">This should be removed</div>

<ul>
<li class="template"></li>
</ul>

<form>
<fieldset class="checkboxes">
<label>
<input type="checkbox" value="1"> Steak and potatoes
</label>
<label>
<input type="checkbox" value="2"> Rice, corn, chitterling and tomatoes
</label>
<label>
<input type="checkbox" value="3" checked="checked"> Ham and greens
</label>
<label>
<input type="checkbox" value="4" checked="checked"> Chicken pot pie and lima beans
</label>
</fieldset>
<fieldset class="radios">
<label>
<input type="radio" value="a"> Corn dogs and mustard
</label>
<label>
<input type="radio" value="b"> Cracker jacks, and tutti fruity custard
</label>
<label>
<input type="radio" value="c" checked="checked"> Buffalo wings
</label>
</fieldset>
</form>

<div id="html-method-test">
<span>Should only match span</span>
</div>

<div id="shorthand-test">
<ul>
<li>This test failed</li>
<li>This test failed</li>
<li>This test failed</li>
</ul>
</div>

<a id="blog" href="https://perryrylance.com">Visit my blog</a>


Loading

0 comments on commit 5a55e5d

Please sign in to comment.