private function _assertHTMLEquals($expected, $actual, $ignoreWhitespace = false) { $input = [$expected, $actual]; $output = []; foreach ($input as $val) { $html5 = new \Masterminds\HTML5(); $dom = $html5->loadHTMLFragment('<div>' . $val . '</div>'); $out = $html5->saveHTML($dom); if ($ignoreWhitespace) { $out = preg_replace('/>\\s+/m', '>', $out); $out = preg_replace('/\\s+</m', '<', $out); $out = $this->prettyPrint($out); } $out = preg_replace('_^<div>_', '', $out); $out = preg_replace('_</div>$_', '', $out); $output[] = $out; } $this->assertEquals($output[0], $output[1]); }
/** * Test alternate constructors. * @group basic */ public function testDOMQueryHtmlConstructors() { $qp = htmlqp(\QueryPath::HTML_STUB); $this->assertEquals(1, count($qp->get())); $this->assertTrue($qp->get(0) instanceof \DOMNode); // Bad BR tag. $borken = '<html><head></head><body><br></body></html>'; $qp = htmlqp($borken); $this->assertEquals(1, count($qp->get())); $this->assertTrue($qp->get(0) instanceof \DOMNode); // XHTML Faker $borken = '<?xml version="1.0"?><html><head></head><body><br></body></html>'; $qp = htmlqp($borken); $this->assertEquals(1, count($qp->get())); $this->assertTrue($qp->get(0) instanceof \DOMNode); // HTML in a file that looks like XML. $qp = htmlqp(HTML_IN_XML_FILE); $this->assertEquals(1, count($qp->get())); $this->assertTrue($qp->get(0) instanceof \DOMNode); // HTML5 $html5 = new \Masterminds\HTML5(); $dom = $html5->loadHTML(\QueryPath::HTML_STUB); qp($dom, 'html'); // Stripping #13 (CR) from HTML. $borken = '<html><head></head><body><p>' . chr(13) . '</p><div id="after"/></body></html>'; $this->assertFalse(strpos(htmlqp($borken)->html(), ' '), 'Test that CRs are not encoded.'); // Regression for #58: Make sure we aren't getting encoded. $borken = '<html><head><style> .BlueText { color:red; }</style><body></body></html>'; $this->assertFalse(strpos(htmlqp($borken)->html(), ' '), 'Test that LF is not encoded.'); // Low ASCII in a file $borken = '<html><head></head><body><p>' . chr(27) . '</p><div id="after"/></body></html>'; $this->assertEquals(1, htmlqp($borken, '#after')->size()); }