@@ -2,34 +2,34 @@ |
||
| 2 | 2 | |
| 3 | 3 | class SS_HTML5Value extends SS_HTMLValue { |
| 4 | 4 | |
| 5 | - public function setContent($content) { |
|
| 6 | - require_once(HTML5LIB_PATH.'/HTML5/Parser.php'); |
|
| 5 | + public function setContent($content) { |
|
| 6 | + require_once(HTML5LIB_PATH.'/HTML5/Parser.php'); |
|
| 7 | 7 | |
| 8 | - // Convert any errors to exceptions |
|
| 9 | - set_error_handler( |
|
| 10 | - function($no, $str){ |
|
| 11 | - throw new Exception("HTML Parse Error: ".$str); |
|
| 12 | - }, |
|
| 13 | - error_reporting() |
|
| 14 | - ); |
|
| 8 | + // Convert any errors to exceptions |
|
| 9 | + set_error_handler( |
|
| 10 | + function($no, $str){ |
|
| 11 | + throw new Exception("HTML Parse Error: ".$str); |
|
| 12 | + }, |
|
| 13 | + error_reporting() |
|
| 14 | + ); |
|
| 15 | 15 | |
| 16 | - // Use HTML5lib to parse the HTML fragment |
|
| 17 | - try { |
|
| 18 | - $document = HTML5_Parser::parse( |
|
| 19 | - '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>'. |
|
| 20 | - "<body>$content</body></html>" |
|
| 21 | - ); |
|
| 22 | - } |
|
| 23 | - catch (Exception $e) { |
|
| 24 | - $document = false; |
|
| 25 | - } |
|
| 16 | + // Use HTML5lib to parse the HTML fragment |
|
| 17 | + try { |
|
| 18 | + $document = HTML5_Parser::parse( |
|
| 19 | + '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>'. |
|
| 20 | + "<body>$content</body></html>" |
|
| 21 | + ); |
|
| 22 | + } |
|
| 23 | + catch (Exception $e) { |
|
| 24 | + $document = false; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - // Disable our error handler (restoring to previous value) |
|
| 28 | - restore_error_handler(); |
|
| 27 | + // Disable our error handler (restoring to previous value) |
|
| 28 | + restore_error_handler(); |
|
| 29 | 29 | |
| 30 | - // If we couldn't parse the HTML, set the error state |
|
| 31 | - if ($document) $this->setDocument($document); |
|
| 32 | - else $this->setInvalid(); |
|
| 33 | - } |
|
| 30 | + // If we couldn't parse the HTML, set the error state |
|
| 31 | + if ($document) $this->setDocument($document); |
|
| 32 | + else $this->setInvalid(); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -19,8 +19,7 @@ discard block |
||
| 19 | 19 | '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>'. |
| 20 | 20 | "<body>$content</body></html>" |
| 21 | 21 | ); |
| 22 | - } |
|
| 23 | - catch (Exception $e) { |
|
| 22 | + } catch (Exception $e) { |
|
| 24 | 23 | $document = false; |
| 25 | 24 | } |
| 26 | 25 | |
@@ -28,8 +27,11 @@ discard block |
||
| 28 | 27 | restore_error_handler(); |
| 29 | 28 | |
| 30 | 29 | // If we couldn't parse the HTML, set the error state |
| 31 | - if ($document) $this->setDocument($document); |
|
| 32 | - else $this->setInvalid(); |
|
| 30 | + if ($document) { |
|
| 31 | + $this->setDocument($document); |
|
| 32 | + } else { |
|
| 33 | + $this->setInvalid(); |
|
| 34 | + } |
|
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | } |
| 36 | 38 | \ No newline at end of file |
@@ -4,62 +4,62 @@ |
||
| 4 | 4 | * @subpackage tests |
| 5 | 5 | */ |
| 6 | 6 | class SS_HTML5ValueTest extends SapphireTest { |
| 7 | - public function testInvalidHTMLParsing() { |
|
| 8 | - $value = new SS_HTML5Value(); |
|
| 7 | + public function testInvalidHTMLParsing() { |
|
| 8 | + $value = new SS_HTML5Value(); |
|
| 9 | 9 | |
| 10 | - $invalid = array ( |
|
| 11 | - '<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p><p></p>', |
|
| 12 | - '<meta content="text/html"></meta>' => '<meta content="text/html">', |
|
| 13 | - '<p><div class="example"></div></p>' => '<p></p><div class="example"></div><p></p>' |
|
| 14 | - ); |
|
| 10 | + $invalid = array ( |
|
| 11 | + '<p>Enclosed Value</p></p>' => '<p>Enclosed Value</p><p></p>', |
|
| 12 | + '<meta content="text/html"></meta>' => '<meta content="text/html">', |
|
| 13 | + '<p><div class="example"></div></p>' => '<p></p><div class="example"></div><p></p>' |
|
| 14 | + ); |
|
| 15 | 15 | |
| 16 | - foreach($invalid as $input => $expected) { |
|
| 17 | - $value->setContent($input); |
|
| 18 | - $this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed'); |
|
| 19 | - } |
|
| 20 | - } |
|
| 16 | + foreach($invalid as $input => $expected) { |
|
| 17 | + $value->setContent($input); |
|
| 18 | + $this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed'); |
|
| 19 | + } |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function testUtf8Saving() { |
|
| 23 | - $value = new SS_HTML5Value(); |
|
| 22 | + public function testUtf8Saving() { |
|
| 23 | + $value = new SS_HTML5Value(); |
|
| 24 | 24 | |
| 25 | - $value->setContent('<p>ö ß ā い 家</p>'); |
|
| 26 | - $this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent()); |
|
| 27 | - } |
|
| 25 | + $value->setContent('<p>ö ß ā い 家</p>'); |
|
| 26 | + $this->assertEquals('<p>ö ß ā い 家</p>', $value->getContent()); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public function testWhitespaceHandling() { |
|
| 30 | - $value = new SS_HTML5Value(); |
|
| 29 | + public function testWhitespaceHandling() { |
|
| 30 | + $value = new SS_HTML5Value(); |
|
| 31 | 31 | |
| 32 | - $value->setContent('<p></p> <p></p>'); |
|
| 33 | - $this->assertEquals('<p></p> <p></p>', $value->getContent()); |
|
| 34 | - } |
|
| 32 | + $value->setContent('<p></p> <p></p>'); |
|
| 33 | + $this->assertEquals('<p></p> <p></p>', $value->getContent()); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function testInvalidHTMLTagNames() { |
|
| 37 | - $value = new SS_HTML5Value(); |
|
| 36 | + public function testInvalidHTMLTagNames() { |
|
| 37 | + $value = new SS_HTML5Value(); |
|
| 38 | 38 | |
| 39 | - $invalid = array( |
|
| 40 | - '<p><div><a href="test-link"></p></div>', |
|
| 41 | - '<html><div><a href="test-link"></a></a></html_>' |
|
| 42 | - ); |
|
| 39 | + $invalid = array( |
|
| 40 | + '<p><div><a href="test-link"></p></div>', |
|
| 41 | + '<html><div><a href="test-link"></a></a></html_>' |
|
| 42 | + ); |
|
| 43 | 43 | |
| 44 | - foreach($invalid as $input) { |
|
| 45 | - $value->setContent($input); |
|
| 44 | + foreach($invalid as $input) { |
|
| 45 | + $value->setContent($input); |
|
| 46 | 46 | |
| 47 | - $this->assertEquals( |
|
| 48 | - 'test-link', |
|
| 49 | - $value->getElementsByTagName('a')->item(0)->getAttribute('href'), |
|
| 50 | - 'Link data can be extraced from malformed HTML' |
|
| 51 | - ); |
|
| 52 | - } |
|
| 53 | - } |
|
| 47 | + $this->assertEquals( |
|
| 48 | + 'test-link', |
|
| 49 | + $value->getElementsByTagName('a')->item(0)->getAttribute('href'), |
|
| 50 | + 'Link data can be extraced from malformed HTML' |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - public function testMixedNewlines() { |
|
| 56 | - $value = new SS_HTML5Value(); |
|
| 55 | + public function testMixedNewlines() { |
|
| 56 | + $value = new SS_HTML5Value(); |
|
| 57 | 57 | |
| 58 | - $value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>"); |
|
| 59 | - $this->assertEquals( |
|
| 60 | - "<p>paragraph</p>\n<ul><li>1</li>\n</ul>", |
|
| 61 | - $value->getContent(), |
|
| 62 | - 'Newlines get converted' |
|
| 63 | - ); |
|
| 64 | - } |
|
| 58 | + $value->setContent("<p>paragraph</p>\n<ul><li>1</li>\r\n</ul>"); |
|
| 59 | + $this->assertEquals( |
|
| 60 | + "<p>paragraph</p>\n<ul><li>1</li>\n</ul>", |
|
| 61 | + $value->getContent(), |
|
| 62 | + 'Newlines get converted' |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | 65 | } |