diff --git a/CHANGELOG.md b/CHANGELOG.md index bd2dffc..9862bea 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed - Curl interface and curl implementation has been removed. - Removed support for the depth first search option. -- findById() method removed from Dom object. +- `findById()` method removed from Dom object. +- Removed `load()` method in Dom object. ## 2.2.0 diff --git a/README.md b/README.md index 54c28d0..cbd6480 100755 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ require "vendor/autoload.php"; use PHPHtmlParser\Dom; $dom = new Dom; -$dom->load('

Hey bro, click here
:)

'); +$dom->loadStr('

Hey bro, click here
:)

'); $a = $dom->find('a')[0]; echo $a->text; // "click here" ``` @@ -86,7 +86,7 @@ $dom->loadFromUrl('http://google.com'); $html = $dom->outerHtml; // or -$dom->load('http://google.com'); +$dom->loadFromUrl('http://google.com'); $html = $dom->outerHtml; // same result as the first example ``` @@ -137,11 +137,11 @@ $dom->setOptions([ 'strict' => true, // Set a global option to enable strict html parsing. ]); -$dom->load('http://google.com', [ +$dom->loadFromUrl('http://google.com', [ 'whitespaceTextNode' => false, // Only applies to this load. ]); -$dom->load('http://gmail.com'); // will not have whitespaceTextNode set to false. +$dom->loadFromUrl('http://gmail.com'); // will not have whitespaceTextNode set to false. ``` At the moment we support 8 options. diff --git a/src/PHPHtmlParser/Dom.php b/src/PHPHtmlParser/Dom.php index ba9ea4a..1cba505 100755 --- a/src/PHPHtmlParser/Dom.php +++ b/src/PHPHtmlParser/Dom.php @@ -15,7 +15,6 @@ use PHPHtmlParser\Exceptions\CurlException; use PHPHtmlParser\Exceptions\LogicalException; use PHPHtmlParser\Exceptions\NotLoadedException; -use PHPHtmlParser\Exceptions\ParentNotFoundException; use PHPHtmlParser\Exceptions\StrictException; use PHPHtmlParser\Exceptions\UnknownChildTypeException; use Psr\Http\Client\ClientInterface; @@ -139,29 +138,6 @@ public function __get($name) return $this->root->$name; } - /** - * Attempts to load the dom from any resource, string, file, or URL. - * - * @throws ChildNotFoundException - * @throws CircularException - * @throws CurlException - * @throws StrictException - * @throws LogicalException - */ - public function load(string $str, array $options = []): Dom - { - // check if it's a file - if (\strpos($str, "\n") === false && \is_file($str)) { - return $this->loadFromFile($str, $options); - } - // check if it's a url - if (\preg_match("/^https?:\/\//i", $str)) { - return $this->loadFromUrl($str, $options); - } - - return $this->loadStr($str, $options); - } - /** * Loads the dom from a document file/url. * diff --git a/src/PHPHtmlParser/StaticDom.php b/src/PHPHtmlParser/StaticDom.php index b4c3ef2..411ca3d 100755 --- a/src/PHPHtmlParser/StaticDom.php +++ b/src/PHPHtmlParser/StaticDom.php @@ -56,23 +56,6 @@ public static function mount(string $className = 'Dom', ?Dom $dom = null): bool return true; } - /** - * Creates a new dom object and calls load() on the - * new object. - * - * @throws ChildNotFoundException - * @throws CircularException - * @throws CurlException - * @throws StrictException - */ - public static function load(string $str): Dom - { - $dom = new Dom(); - self::$dom = $dom; - - return $dom->load($str); - } - /** * Creates a new dom object and calls loadFromFile() on the * new object. @@ -114,6 +97,14 @@ public static function loadFromUrl(string $url, array $options = [], ClientInter return $dom->loadFromUrl($url, $options, $client, $request); } + public static function loadStr(string $str, array $options = []): Dom + { + $dom = new Dom(); + self::$dom = $dom; + + return $dom->loadStr($str, $options); + } + /** * Sets the $dom variable to null. */ diff --git a/tests/DomTest.php b/tests/DomTest.php index d68b0fc..2a904cc 100755 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -20,14 +20,14 @@ public function testParsingCData() $html = ""; $dom = new Dom(); $dom->setOptions(['cleanupInput' => false]); - $dom->load($html); + $dom->loadStr($html); $this->assertSame($html, $dom->root->outerHtml()); } - public function testLoad() + public function testloadStr() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $div = $dom->find('div', 0); $this->assertEquals('

Hey bro, click here
:)

', $div->outerHtml); } @@ -44,7 +44,7 @@ public function testNotLoaded() public function testIncorrectAccess() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $div = $dom->find('div', 0); $this->assertEquals(null, $div->foo); } @@ -52,7 +52,7 @@ public function testIncorrectAccess() public function testLoadSelfclosingAttr() { $dom = new Dom(); - $dom->load("

baz
"); + $dom->loadStr("

baz
"); $br = $dom->find('br', 0); $this->assertEquals('
', $br->outerHtml); } @@ -60,7 +60,7 @@ public function testLoadSelfclosingAttr() public function testLoadSelfclosingAttrToString() { $dom = new Dom(); - $dom->load("

baz
"); + $dom->loadStr("

baz
"); $br = $dom->find('br', 0); $this->assertEquals('
', (string) $br); } @@ -68,7 +68,7 @@ public function testLoadSelfclosingAttrToString() public function testLoadEscapeQuotes() { $dom = new Dom(); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $div = $dom->find('div', 0); $this->assertEquals('

Hey bro, click here

', $div->outerHtml); } @@ -76,14 +76,14 @@ public function testLoadEscapeQuotes() public function testLoadNoOpeningTag() { $dom = new Dom(); - $dom->load('
PR Manager
content
'); + $dom->loadStr('
PR Manager
content
'); $this->assertEquals('content', $dom->find('.content', 0)->text); } public function testLoadNoClosingTag() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $root = $dom->find('div', 0)->getParent(); $this->assertEquals('

Hey bro, click here


', $root->outerHtml); } @@ -91,7 +91,7 @@ public function testLoadNoClosingTag() public function testLoadAttributeOnSelfClosing() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $br = $dom->find('br', 0); $this->assertEquals('both', $br->getAttribute('class')); } @@ -99,7 +99,7 @@ public function testLoadAttributeOnSelfClosing() public function testLoadClosingTagOnSelfClosing() { $dom = new Dom(); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); } @@ -108,7 +108,7 @@ public function testLoadClosingTagOnSelfClosingNoSlash() $dom = new Dom(); $dom->addNoSlashTag('br'); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); } @@ -116,7 +116,7 @@ public function testLoadClosingTagAddSelfClosingTag() { $dom = new Dom(); $dom->addSelfClosingTag('mytag'); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); } @@ -127,7 +127,7 @@ public function testLoadClosingTagAddSelfClosingTagArray() 'mytag', 'othertag', ]); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $this->assertEquals('

Hey bro, click here

', $dom->find('div', 0)->innerHtml); } @@ -135,7 +135,7 @@ public function testLoadClosingTagRemoveSelfClosingTag() { $dom = new Dom(); $dom->removeSelfClosingTag('br'); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $this->assertEquals('

Hey bro, click here


', $dom->find('div', 0)->innerHtml); } @@ -143,35 +143,35 @@ public function testLoadClosingTagClearSelfClosingTag() { $dom = new Dom(); $dom->clearSelfClosingTags(); - $dom->load('

Hey bro, click here

'); + $dom->loadStr('

Hey bro, click here

'); $this->assertEquals('

Hey bro, click here


', $dom->find('div', 0)->innerHtml); } public function testLoadNoValueAttribute() { $dom = new Dom(); - $dom->load('
Main content here
'); + $dom->loadStr('
Main content here
'); $this->assertEquals('
Main content here
', $dom->innerHtml); } public function testLoadBackslashAttributeValue() { $dom = new Dom(); - $dom->load('
Main content here
'); + $dom->loadStr('
Main content here
'); $this->assertEquals('
Main content here
', $dom->innerHtml); } public function testLoadNoValueAttributeBefore() { $dom = new Dom(); - $dom->load('
Main content here
'); + $dom->loadStr('
Main content here
'); $this->assertEquals('
Main content here
', $dom->innerHtml); } public function testLoadUpperCase() { $dom = new Dom(); - $dom->load('

hEY BRO, CLICK HERE

'); + $dom->loadStr('

hEY BRO, CLICK HERE

'); $this->assertEquals('

hEY BRO, CLICK HERE

', $dom->find('div', 0)->innerHtml); } @@ -206,7 +206,7 @@ public function testLoadFromFileNotFound() public function testLoadUtf8() { $dom = new Dom(); - $dom->load('

Dzień

'); + $dom->loadStr('

Dzień

'); $this->assertEquals('Dzień', $dom->find('p', 0)->text); } @@ -268,56 +268,56 @@ public function testLoadFromUrl() public function testToStringMagic() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $this->assertEquals('

Hey bro, click here
:)

', (string) $dom); } public function testGetMagic() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $this->assertEquals('

Hey bro, click here
:)

', $dom->innerHtml); } public function testFirstChild() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $this->assertEquals('

Hey bro, click here

', $dom->firstChild()->outerHtml); } public function testLastChild() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $this->assertEquals('
', $dom->lastChild()->outerHtml); } public function testGetElementById() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $this->assertEquals('click here', $dom->getElementById('78')->outerHtml); } public function testGetElementsByTag() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $this->assertEquals('

Hey bro, click here

', $dom->getElementsByTag('p')[0]->outerHtml); } public function testGetElementsByClass() { $dom = new Dom(); - $dom->load('

Hey bro, click here


'); + $dom->loadStr('

Hey bro, click here


'); $this->assertEquals('

Hey bro, click here

', $dom->getElementsByClass('all')[0]->innerHtml); } public function testScriptCleanerScriptTag() { $dom = new Dom(); - $dom->load(' + $dom->loadStr('

.....

', [ @@ -607,12 +607,12 @@ public function testUniqueIdForAllObjects() { // Create a dom which will be used as a parent/container for a paragraph $dom1 = new \PHPHtmlParser\Dom; - $dom1->load('
A container div
'); // Resets the counter (doesn't matter here as the counter was 0 even without resetting) + $dom1->loadStr('
A container div
'); // Resets the counter (doesn't matter here as the counter was 0 even without resetting) $div = $dom1->firstChild(); // Create a paragraph outside of the first dom $dom2 = new \PHPHtmlParser\Dom; - $dom2->load('

Our new paragraph.

'); // Resets the counter + $dom2->loadStr('

Our new paragraph.

'); // Resets the counter $paragraph = $dom2->firstChild(); $div->addChild($paragraph); @@ -623,7 +623,7 @@ public function testUniqueIdForAllObjects() public function testFindDescendantsOfMatch() { $dom = new Dom(); - $dom->load('

+ $dom->loadStr('

test testing @@ -641,7 +641,7 @@ public function testFindDescendantsOfMatch() public function testCompatibleWithWordPressShortcode() { $dom = new Dom(); - $dom->load('

+ $dom->loadStr('

[wprs_alert type="success" content="this is a short code" /]

'); diff --git a/tests/Node/HtmlTest.php b/tests/Node/HtmlTest.php index 677b280..a4db814 100755 --- a/tests/Node/HtmlTest.php +++ b/tests/Node/HtmlTest.php @@ -500,7 +500,7 @@ public function testAncestorByTagFailure() public function testReplaceNode() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $id = $dom->find('p')[0]->id(); $newChild = new HtmlNode('h1'); $dom->find('p')[0]->getParent()->replaceChild($id, $newChild); @@ -510,7 +510,7 @@ public function testReplaceNode() public function testTextNodeFirstChild() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $p = $dom->find('p'); foreach ($p as $element) { $child = $element->firstChild(); diff --git a/tests/Node/TextTest.php b/tests/Node/TextTest.php index 27dd03e..d36eddc 100755 --- a/tests/Node/TextTest.php +++ b/tests/Node/TextTest.php @@ -57,7 +57,7 @@ public function testSetTextToTextNode() public function testSetText() { $dom = new Dom(); - $dom->load('

Hey bro, click here
:)

'); + $dom->loadStr('

Hey bro, click here
:)

'); $a = $dom->find('a')[0]; $a->firstChild()->setText('biz baz'); $this->assertEquals('

Hey bro, biz baz
:)

', (string) $dom); diff --git a/tests/Options/CleanupTest.php b/tests/Options/CleanupTest.php index 0a8a9ba..b7e5325 100755 --- a/tests/Options/CleanupTest.php +++ b/tests/Options/CleanupTest.php @@ -76,7 +76,7 @@ public function testRemoveScriptsFalse() public function testSmartyScripts() { $dom = new Dom(); - $dom->load(' + $dom->loadStr(' aa={123} '); $this->assertEquals(' aa= ', $dom->innerHtml); @@ -88,7 +88,7 @@ public function testSmartyScriptsDisabled() $dom->setOptions([ 'removeSmartyScripts' => false, ]); - $dom->load(' + $dom->loadStr(' aa={123} '); $this->assertEquals(' aa={123} ', $dom->innerHtml); diff --git a/tests/Options/PreserveLineBreaks.php b/tests/Options/PreserveLineBreaks.php index 3df7223..ad095a3 100755 --- a/tests/Options/PreserveLineBreaks.php +++ b/tests/Options/PreserveLineBreaks.php @@ -13,7 +13,7 @@ public function testPreserveLineBreakTrue() $dom->setOptions([ 'preserveLineBreaks' => true, ]); - $dom->load('
+ $dom->loadStr('
'); $this->assertEquals("
\n
", (string) $dom); @@ -25,7 +25,7 @@ public function testPreserveLineBreakBeforeClosingTag() $dom->setOptions([ 'preserveLineBreaks' => true, ]); - $dom->load('
loadStr('
'); $this->assertEquals('
', (string) $dom); diff --git a/tests/Options/StrictTest.php b/tests/Options/StrictTest.php index cb01598..96d457b 100755 --- a/tests/Options/StrictTest.php +++ b/tests/Options/StrictTest.php @@ -14,7 +14,7 @@ public function testConfigStrict() $dom->setOptions([ 'strict' => true, ]); - $dom->load('

Hey you

Ya you!

'); + $dom->loadStr('

Hey you

Ya you!

'); $this->assertEquals(' ', $dom->getElementById('hey')->nextSibling()->text); } @@ -26,7 +26,7 @@ public function testConfigStrictMissingSelfClosing() ]); try { // should throw an exception - $dom->load('

Hey you


Ya you!

'); + $dom->loadStr('

Hey you


Ya you!

'); // we should not get here $this->assertTrue(false); } catch (StrictException $e) { @@ -42,7 +42,7 @@ public function testConfigStrictMissingAttribute() ]); try { // should throw an exception - $dom->load('

Hey you

Ya you!

'); + $dom->loadStr('

Hey you

Ya you!

'); // we should not get here $this->assertTrue(false); } catch (StrictException $e) { @@ -56,7 +56,7 @@ public function testConfigStrictBRTag() $dom->setOptions([ 'strict' => true, ]); - $dom->load('
'); + $dom->loadStr('
'); $this->assertTrue(true); } } diff --git a/tests/Options/WhitespaceTextNodeTest.php b/tests/Options/WhitespaceTextNodeTest.php index 541fbec..0097f28 100755 --- a/tests/Options/WhitespaceTextNodeTest.php +++ b/tests/Options/WhitespaceTextNodeTest.php @@ -13,7 +13,7 @@ public function testConfigGlobalNoWhitespaceTextNode() $dom->setOptions([ 'whitespaceTextNode' => false, ]); - $dom->load('

Hey you

Ya you!

'); + $dom->loadStr('

Hey you

Ya you!

'); $this->assertEquals('Ya you!', $dom->getElementById('hey')->nextSibling()->text); } @@ -23,7 +23,7 @@ public function testConfigLocalOverride() $dom->setOptions([ 'whitespaceTextNode' => false, ]); - $dom->load('

Hey you

Ya you!

', [ + $dom->loadStr('

Hey you

Ya you!

', [ 'whitespaceTextNode' => true, ]); $this->assertEquals(' ', $dom->getElementById('hey')->nextSibling()->text); diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index 899a062..a78f508 100755 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -144,6 +144,6 @@ public function testUnknownOptionDom() ]); $this->expectException(UnknownOptionException::class); - $dom->load('
'); + $dom->loadStr('
'); } } diff --git a/tests/StaticDomTest.php b/tests/StaticDomTest.php index 2fb225f..fbc1a5b 100755 --- a/tests/StaticDomTest.php +++ b/tests/StaticDomTest.php @@ -25,16 +25,16 @@ public function testMountWithDom() $this->assertTrue($status); } - public function testLoad() + public function testloadStr() { - $dom = Dom::load('

Hey bro, click here
:)

'); + $dom = Dom::loadStr('

Hey bro, click here
:)

'); $div = $dom->find('div', 0); $this->assertEquals('

Hey bro, click here
:)

', $div->outerHtml); } public function testLoadWithFile() { - $dom = Dom::load('tests/data/files/small.html'); + $dom = Dom::loadFromFile('tests/data/files/small.html'); $this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text); } @@ -47,14 +47,14 @@ public function testLoadFromFile() /** * @expectedException \PHPHtmlParser\Exceptions\NotLoadedException */ - public function testFindNoLoad() + public function testFindNoloadStr() { Dom::find('.post-user font', 0); } public function testFindI() { - Dom::load('tests/data/files/big.html'); + Dom::loadFromFile('tests/data/files/big.html'); $this->assertEquals('В кустах блестит металл
И искрится ток
Человечеству конец', Dom::find('i')[1]->innerHtml); }