Skip to content

Commit

Permalink
Handling simple quotes in attributes (see #49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Mar 21, 2019
1 parent b05f39f commit 936a19d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
21 changes: 11 additions & 10 deletions Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,18 @@ public function doParseTag(&$name, &$data)
break;
}
if (null !== $field) {
$data = preg_replace_callback('#="([^"]+)"#mUsi', function($matches) {
return '="'.urlencode($matches[1]).'"';
}, $data);

$attributes = explode(' ', $data);

foreach ($attributes as $attribute) {
if (preg_match("#([^=]+)(=\"(.+)\"|)#muSi", $attribute, $match)) {
$field->push($match[1], isset($match[3]) ? html_entity_decode(urldecode($match[3])) : null);
preg_replace_callback('#([^= ]+)(=("([^"]*)"|\'([^\']*)\')|)#', function($match) use ($field) {
$key = trim($match[1]);
$value = null;

if (isset($match[5])) {
$value = trim($match[5]);
} else if (isset($match[4])) {
$value = trim($match[4]);
}
}

$field->push($key, $value ? html_entity_decode($value) : $value);
}, $data);

return $field;
} else {
Expand Down
3 changes: 2 additions & 1 deletion tests/ParserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public function testBasicParse()

$fields = $parser->getFields();

$this->assertEquals(1, count($fields));
$this->assertEquals(2, count($fields));
$this->assertArrayHasKey('foo', $fields);
$this->assertArrayHasKey('foo2', $fields);

$this->assertEquals('foo', $fields['foo']->getName());
}
Expand Down
2 changes: 2 additions & 0 deletions tests/files/parser/basic.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<form method="post" action="/post.php">
<!-- Un input -->
Your name&nbsp;: <input type="text" name="foo"><br />

Your name&nbsp;: <input type="text" name='foo2'><br />
<hr />
<a href="other.html">click</a>
</form>

0 comments on commit 936a19d

Please sign in to comment.