summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Farina <[email protected]>2013-06-26 09:19:16 -0400
committerMatt Farina <[email protected]>2013-06-26 09:19:16 -0400
commitb73ec8ad9becdadf091ddc1fbcc8cdcfde509abe (patch)
tree040543a0efdb5acf4dd33399698bee88691d6a9c
parente1978e35fc16007620866de2713f05d0dbefb470 (diff)
Added a helper method of loadHTMLFragment to get a DOMDocumentFragment.
-rw-r--r--src/HTML5.php17
-rw-r--r--test/HTML5/Html5Test.php7
2 files changed, 23 insertions, 1 deletions
diff --git a/src/HTML5.php b/src/HTML5.php
index d43539f..7009136 100644
--- a/src/HTML5.php
+++ b/src/HTML5.php
@@ -58,7 +58,7 @@ class HTML5 {
}
/**
- * Parse an HTML string.
+ * Parse a HTML Document from a string.
*
* Take a string of HTML 5 (or earlier) and parse it into a
* DOMDocument.
@@ -85,6 +85,21 @@ class HTML5 {
}
/**
+ * Parse a HTML fragment from a string.
+ *
+ * @param string $string
+ * The html5 fragment as a string.
+ *
+ * @return \DOMDocumentFragment
+ * A DOM fragment. The DOM is part of libxml, which is included with
+ * almost all distributions of PHP.
+ */
+ public static function loadHTMLFragment($string) {
+ $input = new StringInputStream($string);
+ return self::parseFragment($input);
+ }
+
+ /**
* Save a DOM into a given file as HTML5.
*
* @param mixed $dom
diff --git a/test/HTML5/Html5Test.php b/test/HTML5/Html5Test.php
index 442fe63..e122509 100644
--- a/test/HTML5/Html5Test.php
+++ b/test/HTML5/Html5Test.php
@@ -18,6 +18,13 @@ class Html5Test extends TestCase {
$this->assertEmpty($dom->errors);
}
+ public function testLoadHTMLFragment() {
+ $fragment = '<section id="Foo"><div class="Bar">Baz</div></section>';
+ $dom = \HTML5::loadHTMLFragment($fragment);
+ $this->assertInstanceOf('\DOMDocumentFragment', $dom);
+ $this->assertEmpty($dom->errors);
+ }
+
public function testSaveHTML() {
$dom = \HTML5::load(__DIR__ . '/Html5Test.html');
$this->assertInstanceOf('\DOMDocument', $dom);