Permalink
Browse files

Option values now have tags stripped, more closely mirroring browser …

…behaviour. Also, added work-around for html-tidys over-zealous empty tag stripping.
  • Loading branch information...
1 parent d3fdabd commit f5dd95e33b6a95555c63c7c0640fbdbf22422507 dgheath committed Jul 31, 2009
Showing with 43 additions and 18 deletions.
  1. +2 −2 tag.php
  2. +27 −14 test/parsing_test.php
  3. +14 −2 tidy_parser.php
View
@@ -1017,7 +1017,7 @@ function isValue($compare) {
if (trim($this->getValue()) == $compare) {
return true;
}
- return trim($this->getContent()) == $compare;
+ return trim(strip_tags($this->getContent())) == $compare;
}
/**
@@ -1028,7 +1028,7 @@ function isValue($compare) {
*/
function getDefault() {
if ($this->getAttribute('value') === false) {
- return $this->getContent();
+ return strip_tags($this->getContent());
}
return $this->getAttribute('value');
}
View
@@ -425,6 +425,31 @@ function testSettingSelectionField() {
$this->assertEqual($page->getField(new SimpleByName('a')), 'aaa');
}
+ function testSelectionOptionsAreNormalised() {
+ $raw = '<form>' .
+ '<select name="a">' .
+ '<option selected><b>Big</b> bold</option>' .
+ '<option>small <em>italic</em></option>' .
+ '</select>' .
+ '</form>';
+ $page = $this->whenVisiting('http://host', $raw);
+ $this->assertEqual($page->getField(new SimpleByName('a')), 'Big bold');
+ $this->assertTrue($page->setField(new SimpleByName('a'), 'small italic'));
+ $this->assertEqual($page->getField(new SimpleByName('a')), 'small italic');
+ }
+
+ function testCanParseBlankOptions() {
+ $raw = '<form>
+ <select id=4 name="d">
+ <option value="d1">D1</option>
+ <option value="d2">D2</option>
+ <option></option>
+ </select>
+ </form>';
+ $page = $this->whenVisiting('http://host', $raw);
+ $this->assertTrue($page->setField(new SimpleByName('d'), ''));
+ }
+
function testSettingSelectionFieldByEnclosingLabel() {
$raw = '<form>' .
'<label>Stuff' .
@@ -470,7 +495,7 @@ function assertSameFrameset($actual, $expected) {
}
}
-abstract class TestOfParsingUsingPhpParser extends TestOfParsing {
+class TestOfParsingUsingPhpParser extends TestOfParsing {
function whenVisiting($url, $content) {
$response = new MockSimpleHttpResponse();
@@ -486,20 +511,9 @@ function testNastyTitle() {
$this->assertEqual($page->getTitle(), "Me&Me");
}
- function testCanParseBlankOptions() {
- $raw = '<form>
- <select id=4 name="d">
- <option value="d1">D1</option>
- <option value="d2">D2</option>
- <option></option>
- </select>
- </form>';
- $page = $this->whenVisiting('http://host', $raw);
- $this->assertTrue($page->setField(new SimpleByName('d'), ''));
- }
}
-class TestOfParsingUsingTidyParser extends TestOfParsing {
+abstract class TestOfParsingUsingTidyParser extends TestOfParsing {
function skip() {
$this->skipUnless(extension_loaded('tidy'), 'Install \'tidy\' php extension to enable html tidy based parser');
@@ -512,6 +526,5 @@ function whenVisiting($url, $content) {
$builder = new SimpleTidyPageBuilder();
return $builder->parse($response);
}
-
}
?>
View
@@ -46,7 +46,9 @@ function can() {
*/
function parse($response) {
$this->page = new SimplePage($response);
- $tidied = tidy_parse_string($response->getContent(), array('output-html' => true), 'latin1');
+ $tidied = tidy_parse_string($this->guardEmptyTags($response->getContent()),
+ array('output-html' => true, 'new-empty-tags' => 'empty', 'new-inline-tags' => 'empty'),
+ 'latin1');
if ($tidied->errorBuffer) {
foreach(explode("\n", $tidied->errorBuffer) as $notice) {
//user_error($notice, E_USER_NOTICE);
@@ -60,6 +62,16 @@ function parse($response) {
}
/**
+ * HTML tidy strips out empty tags such as <option> which we
+ * need to preserve. This method inserts an additional tag.
+ * @param string The raw html.
+ * @return string The html with guard tags inserted.
+ */
+ private function guardEmptyTags($html) {
+ return preg_replace('|<([^>/][^>]*)>(\s*)</|', '<\1><empty>\2</', $html);
+ }
+
+ /**
* Visits the given node and all children
*/
private function walkTree($node) {
@@ -127,7 +139,7 @@ private function collectSelectOptions($node) {
$options = array();
if ($node->name == 'option') {
$options[] = $this->tags()->createTag($node->name, $this->attributes($node))
- ->addContent($this->innerHtml($node));
+ ->addContent($this->innerHtml($node));
}
if ($node->hasChildren()) {
foreach ($node->child as $child) {

0 comments on commit f5dd95e

Please sign in to comment.