Skip to content

Commit 1f435fb

Browse files
committed
Fix create Document or modify Element from string
1 parent 168ecd9 commit 1f435fb

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,5 @@ matrix:
1414
install:
1515
- composer install --no-interaction
1616

17-
before_install:
18-
- sudo apt-get -qq update
19-
- sudo apt-get install -y libxml2 libxml2-dev
20-
21-
script:
22-
- php -i
17+
script:
2318
- vendor/bin/phpunit --coverage-text

lib/Document.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ public function loadHtml($html)
6868
libxml_use_internal_errors(true);
6969
libxml_disable_entity_loader(true);
7070

71-
$this->document->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED);
71+
$sxe = simplexml_load_string($html);
72+
if (libxml_get_errors()) {
73+
$this->document->loadHTML('<?xml encoding="UTF-8">' . $html);
74+
} else {
75+
$this->document = dom_import_simplexml($sxe)->ownerDocument;
76+
}
7277

7378
libxml_clear_errors();
7479
libxml_disable_entity_loader(false);

lib/Element.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use DOMElement;
77
use DOMNode;
8+
use RuntimeException;
89

910
/**
1011
* Class Element
@@ -47,6 +48,11 @@ protected function replaceNode($string)
4748
}
4849

4950
$newDocument = new Document($string);
51+
52+
if ($newDocument->outertext != $string) {
53+
throw new RuntimeException("Not valid HTML fragment");
54+
}
55+
5056
$newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true);
5157

5258
$this->node->parentNode->replaceChild($newNode, $this->node);
@@ -63,14 +69,20 @@ protected function replaceNode($string)
6369
*/
6470
protected function replaceChild($string)
6571
{
72+
if (!empty($string)) {
73+
$newDocument = new Document($string);
74+
75+
if ($newDocument->outertext != $string) {
76+
throw new RuntimeException("Not valid HTML fragment");
77+
}
78+
}
79+
6680
foreach ($this->node->childNodes as $node) {
6781
$this->node->removeChild($node);
6882
}
6983

7084
if (!empty($string)) {
71-
$newDocument = new Document($string);
7285
$newNode = $this->node->ownerDocument->importNode($newDocument->getDocument()->documentElement, true);
73-
7486
$this->node->appendChild($newNode);
7587
}
7688

tests/FastSimpleHTMLDom/DocumentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function findTests()
132132
array($html, 'input[id=in]', 1),
133133
array($html, '#in', 1),
134134
array($html, '*[id]', 52),
135-
array($html, 'text', 640),
135+
array($html, 'text', 462),
136136
array($html, 'comment', 3),
137137
);
138138
}

tests/FastSimpleHTMLDom/ElementTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ class ElementTest extends TestCase
1111
{
1212
public function testConstructor()
1313
{
14-
$html = '<input name="username" value="John">';
14+
$html = '<input name="username" value="John">User name</input>';
1515

1616
$document = new Document($html);
1717
$node = $document->getDocument()->documentElement;
18+
1819
$element = new Element($node);
1920

2021
$this->assertEquals('input', $element->tag);
21-
$this->assertEquals('', $element->plaintext);
22+
$this->assertEquals('User name', $element->plaintext);
2223
$this->assertEquals('username', $element->name);
2324
$this->assertEquals('John', $element->value);
2425
}
@@ -113,7 +114,7 @@ public function findTests()
113114
array($html, 'input[id=in]', 1),
114115
array($html, '#in', 1),
115116
array($html, '*[id]', 52),
116-
array($html, 'text', 640),
117+
array($html, 'text', 462),
117118
array($html, 'comment', 3),
118119
);
119120
}

tests/FastSimpleHTMLDom/NodeListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function findTests()
4141
array($html, 'input[id=in]', 1),
4242
array($html, '#in', 1),
4343
array($html, '*[id]', 51),
44-
array($html, 'text', 539),
44+
array($html, 'text', 390),
4545
);
4646
}
4747

0 commit comments

Comments
 (0)