-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,830 additions
and
1,076 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Sulao\HtmlQuery; | ||
|
||
use DOMDocument, DOMNode; | ||
|
||
/** | ||
* Class HtmlDocument | ||
* | ||
* @package Sulao\HtmlQuery | ||
*/ | ||
class HtmlDocument | ||
{ | ||
/** | ||
* @var DOMDocument | ||
*/ | ||
protected $doc; | ||
|
||
/** | ||
* HtmlDocument constructor. | ||
* | ||
* @param DOMDocument $doc | ||
*/ | ||
public function __construct(DOMDocument $doc) | ||
{ | ||
$this->doc = $doc; | ||
} | ||
|
||
/** | ||
* Get DOMDocument | ||
* | ||
* @return DOMDocument | ||
*/ | ||
public function getDoc(): DOMDocument | ||
{ | ||
return $this->doc; | ||
} | ||
|
||
/** | ||
* Get the outer HTML content. | ||
* | ||
* @return string|null | ||
*/ | ||
public function outerHtml() | ||
{ | ||
return $this->doc->saveHTML(); | ||
} | ||
|
||
/** | ||
* Make the static object can be called as a function. | ||
* | ||
* @param string $selector | ||
* | ||
* @return HtmlQuery | ||
*/ | ||
public function __invoke(string $selector) | ||
{ | ||
return $this->query($selector); | ||
} | ||
|
||
/** | ||
* If the parameter is raw html, then create document fragment for it, | ||
* If the parameter is a css selector, get the descendants | ||
* filtered by a css selector. | ||
* | ||
* @param string $selector css selector or raw html | ||
* | ||
* @return HtmlQuery | ||
*/ | ||
public function query(string $selector) | ||
{ | ||
if (Helper::isRawHtml($selector)) { | ||
$frag = $this->doc->createDocumentFragment(); | ||
$frag->appendXML($selector); | ||
|
||
return $this->resolve($frag); | ||
} | ||
|
||
return $this->find($selector); | ||
} | ||
|
||
/** | ||
* Get the descendants of document, filtered by a selector. | ||
* | ||
* @param string $selector | ||
* | ||
* @return HtmlQuery | ||
*/ | ||
public function find(string $selector) | ||
{ | ||
$nodes = Helper::xpathQuery( | ||
Helper::toXpath($selector), | ||
$this->doc, | ||
$this->doc | ||
); | ||
|
||
if (Helper::isIdSelector($selector)) { | ||
$nodes = $nodes ? $nodes[0] : []; | ||
} | ||
|
||
return $this->resolve($nodes); | ||
} | ||
|
||
/** | ||
* Resolve nodes to HtmlQuery instance. | ||
* | ||
* @param DOMNode|DOMNode[] $nodes | ||
* | ||
* @return HtmlQuery | ||
*/ | ||
protected function resolve($nodes) | ||
{ | ||
return new HtmlQuery($this->doc, $nodes); | ||
} | ||
} |
Oops, something went wrong.