Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

fixes #5310 #5351

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions library/Zend/Dom/Css2Xpath.php
Expand Up @@ -85,7 +85,7 @@ protected static function _tokenize($expression)

// arbitrary attribute strict equality
$expression = preg_replace_callback(
'|\[([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i',
'|\[@?([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i',
function ($matches) {
return '[@' . strtolower($matches[1]) . "='" . $matches[2] . "']";
},
Expand Down Expand Up @@ -113,11 +113,13 @@ function ($matches) {
);

// Classes
$expression = preg_replace(
'|\.([a-z][a-z0-9_-]*)|i',
"[contains(concat(' ', normalize-space(@class), ' '), ' \$1 ')]",
$expression
);
if(false === strpos($expression, "[@")) {
$expression = preg_replace(
'|\.([a-z][a-z0-9_-]*)|i',
"[contains(concat(' ', normalize-space(@class), ' '), ' \$1 ')]",
$expression
);
}

/** ZF-9764 -- remove double asterisk */
$expression = str_replace('**', '*', $expression);
Expand Down
12 changes: 12 additions & 0 deletions tests/ZendTest/Dom/Css2XpathTest.php
Expand Up @@ -164,4 +164,16 @@ public function testIdSelectorWithLeadingAsterix()
$test = Css2Xpath::transform('*#id');
$this->assertEquals("//*[@id='id']", $test);
}

/**
* @group ZF-5310
*/
public function testCanTransformWithAttributeAndDot()
{
$test = Css2Xpath::transform('a[href="http://example.com"]');
$this->assertEquals("//a[@href='http://example.com']", $test);

$test = Css2Xpath::transform('a[@href="http://example.com"]');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this throw a exception since @ is not a valid CSS selector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maks3w Thats a great question, the fact is, the function converts the code to include the @ symbol, I think it would be weird to throw an exception if one includes the @ symbol, seeing as the result of the function outputs the correctly formatted query.

EDIT: to clarify above

$test = Css2Xpath::transform('a[href="http://example.com"]');
echo $test; // outputs: //a[@href='http://example.com']

The reason I included that test was to ensure that when an @ was included that the double quotes were converted to a single quote, so the 2 were consistent. It does seem odd to me that the CSS2XPath is modifying this query in general, and perhaps that is the modification that needs to be made in this regard, IE> ignore attributes? Unfortunately I don't know enough of the history of this class to make that decision.

$this->assertEquals("//a[@href='http://example.com']", $test);
}
}
13 changes: 13 additions & 0 deletions tests/ZendTest/Dom/QueryTest.php
Expand Up @@ -396,4 +396,17 @@ public function testOffsetUnset()

unset($results[2]);
}

/**
* @group ZF-5310
*/
public function testCssSelectorShouldFindNodesWhenMatchingAttributeValueWithDot()
{
$this->loadHtml();
$results = $this->query->execute('a[href="http://www.about.com"]');

$this->assertEquals(1, $results->count());
$this->assertEquals('About', $results[0]->nodeValue);

}
}
2 changes: 1 addition & 1 deletion tests/ZendTest/Dom/_files/sample.xhtml
Expand Up @@ -16,7 +16,7 @@

<ul id="topnav">
<li class="current"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="http://www.about.com">About</a></li>
<li><a href="#">Misc</a></li>
<li><a href="#">Wiki</a></li>
</ul>
Expand Down