Fix greediness of A ~ B C selector matching. b=420814 r+sr=bzbarsky a=beltzner

This commit is contained in:
dbaron@dbaron.org 2008-03-05 16:06:15 -08:00
Родитель 0d8b560733
Коммит 2fd8318753
3 изменённых файлов: 91 добавлений и 2 удалений

Просмотреть файл

@ -1620,10 +1620,15 @@ static PRBool SelectorMatchesTree(RuleProcessorData& aPrevData,
}
if (SelectorMatches(*data, selector, 0, nsnull)) {
// to avoid greedy matching, we need to recur if this is a
// descendant combinator and the next combinator is not
// descendant or general sibling combinator and the next
// combinator is different, but we can make an exception for
// sibling, then parent, since a sibling's parent is always the
// same.
if ((NS_IS_GREEDY_OPERATOR(selector->mOperator)) &&
(selector->mNext) &&
(!NS_IS_GREEDY_OPERATOR(selector->mNext->mOperator))) {
(selector->mNext->mOperator != selector->mOperator) &&
!(selector->mOperator == '~' &&
selector->mNext->mOperator == PRUnichar(0))) {
// pretend the selector didn't match, and step through content
// while testing the same selector

Просмотреть файл

@ -102,6 +102,7 @@ _TEST_FILES = test_bug73586.html \
test_parse_rule.html \
test_property_database.html \
test_property_syntax_errors.html \
test_selectors.html \
test_style_struct_copy_constructors.html \
test_value_storage.html \
test_value_computation.html \

Просмотреть файл

@ -0,0 +1,83 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for CSS Selectors</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="run()">
<p id="display"><iframe id="iframe" src="about:blank"></iframe></p>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
function run() {
var iframe = document.getElementById("iframe");
var ifwin = iframe.contentWindow;
var ifdoc = iframe.contentDocument;
function setup_style() {
var style_elem = ifdoc.createElement("style");
style_elem.setAttribute("type", "text/css");
ifdoc.getElementsByTagName("head")[0].appendChild(style_elem);
var style_text = ifdoc.createTextNode("");
style_elem.appendChild(style_text);
return style_text;
}
var style_text = setup_style();
var gCounter = 0;
/*
* selector: the selector to test
* body_contents: what to set the body's innerHTML to
* match_fn: a function that, given the document object into which
* body_contents has been inserted, produces an array of nodes that
* should match selector
* notmatch_fn: likewise, but for nodes that should not match
*/
function test_selector_in_html(selector, body_contents, match_fn, notmatch_fn)
{
var zi = ++gCounter;
ifdoc.body.innerHTML = body_contents;
style_text.data = selector + "{ z-index: " + zi + " }";
var should_match = match_fn(ifdoc);
var should_not_match = notmatch_fn(ifdoc);
if (should_match.length + should_not_match.length == 0) {
ok(false, "nothing to check");
}
for (var i = 0; i < should_match.length; ++i) {
var e = should_match[i];
is(ifwin.getComputedStyle(e, "").zIndex, zi,
"element in " + body_contents + " matched " + selector);
}
for (var i = 0; i < should_not_match.length; ++i) {
var e = should_not_match[i];
is(ifwin.getComputedStyle(e, "").zIndex, "auto",
"element in " + body_contents + " did not match " + selector);
}
ifdoc.body.innerHTML = "";
style_text.data = "";
}
// Bug 420814
test_selector_in_html(
"div ~ div p",
"<div></div><div><div><p>match</p></div></div>",
function(doc) { return doc.getElementsByTagName("p"); },
function(doc) { return []; }
);
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>