Bug 683672 - Style Inspector is counting some unmatched rules incorrectly; r=msucan

This commit is contained in:
Michael Ratcliffe 2011-09-05 15:14:52 +02:00
parent dddeb8cff6
commit 9d83e5f859
6 changed files with 183 additions and 30 deletions

View File

@ -47,7 +47,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/devtools/CssLogic.jsm");
Cu.import("resource:///modules/devtools/Templater.jsm");
var EXPORTED_SYMBOLS = ["CssHtmlTree"];
var EXPORTED_SYMBOLS = ["CssHtmlTree", "PropertyView"];
/**
* CssHtmlTree is a panel that manages the display of a table sorted by style.
@ -342,7 +342,7 @@ PropertyView.prototype = {
/**
* Compute the title of the property view. The title includes the number of
* rules that hold the current property.
* selectors that match the currently selected element.
*
* @param {nsIDOMElement} aElement reference to the DOM element where the rule
* title needs to be displayed.
@ -351,23 +351,23 @@ PropertyView.prototype = {
ruleTitle: function PropertyView_ruleTitle(aElement)
{
let result = "";
let matchedRuleCount = this.propertyInfo.matchedRuleCount;
let matchedSelectorCount = this.propertyInfo.matchedSelectors.length;
if (matchedRuleCount > 0) {
if (matchedSelectorCount > 0) {
aElement.classList.add("rule-count");
aElement.firstElementChild.className = "expander";
let str = CssHtmlTree.l10n("property.numberOfRules");
result = PluralForm.get(matchedRuleCount, str)
.replace("#1", matchedRuleCount);
let str = CssHtmlTree.l10n("property.numberOfSelectors");
result = PluralForm.get(matchedSelectorCount, str)
.replace("#1", matchedSelectorCount);
} else if (this.showUnmatchedLink) {
aElement.classList.add("rule-unmatched");
aElement.firstElementChild.className = "expander";
let unmatchedRuleCount = this.propertyInfo.unmatchedRuleCount;
let str = CssHtmlTree.l10n("property.numberOfUnmatchedRules");
result = PluralForm.get(unmatchedRuleCount, str)
.replace("#1", unmatchedRuleCount);
let unmatchedSelectorCount = this.propertyInfo.unmatchedSelectors.length;
let str = CssHtmlTree.l10n("property.numberOfUnmatchedSelectors");
result = PluralForm.get(unmatchedSelectorCount, str)
.replace("#1", unmatchedSelectorCount);
}
return result;
},
@ -429,8 +429,9 @@ PropertyView.prototype = {
get showUnmatchedLinkText()
{
let smur = CssHtmlTree.l10n("rule.showUnmatchedLink");
let plural = PluralForm.get(this.propertyInfo.unmatchedRuleCount, smur);
return plural.replace("#1", this.propertyInfo.unmatchedRuleCount);
let unmatchedSelectorCount = this.propertyInfo.unmatchedSelectors.length;
let plural = PluralForm.get(unmatchedSelectorCount, smur);
return plural.replace("#1", unmatchedSelectorCount);
},
/**

View File

@ -741,6 +741,8 @@ function CssSheet(aCssLogic, aDomSheet, aSystemSheet, aIndex)
}
CssSheet.prototype = {
_passId: null,
/**
* Get a source for a stylesheet, taking into account embedded stylesheets
* for which we need to use document.defaultView.location.href rather than
@ -950,6 +952,8 @@ function CssRule(aCssSheet, aDomRule, aElement)
}
CssRule.prototype = {
_passId: null,
/**
* Check if the parent stylesheet is allowed by the CssLogic.sourceFilter.
*
@ -1091,6 +1095,8 @@ function CssSelector(aCssRule, aSelector)
}
CssSelector.prototype = {
_matchId: null,
/**
* Retrieve the CssSelector source, which is the source of the CssSheet owning
* the selector.

View File

@ -48,11 +48,13 @@ include $(topsrcdir)/config/rules.mk
_BROWSER_TEST_FILES = \
browser_styleinspector.js \
browser_styleinspector_webconsole.js \
browser_bug683672.js \
head.js \
$(NULL)
_BROWSER_TEST_PAGES = \
browser_styleinspector_webconsole.htm \
browser_bug683672.html \
$(NULL)
libs:: $(_BROWSER_TEST_FILES)

View File

@ -0,0 +1,32 @@
<!-- Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ -->
<html>
<head>
<style>
.matched1, .matched2, .matched3, .matched4, .matched5 {
color: #000;
}
.unmatched1, .unmatched2, .unmatched3, .unmatched4, .unmatched5, .unmatched6, .unmatched7 {
color: #f00;
}
div {
position: absolute;
top: 40px;
left: 20px;
border: 1px solid #000;
color: #111;
width: 100px;
height: 50px;
}
</style>
</head>
<body>
inspectstyle($("test"));
<div id="test" class="matched1 matched2 matched3 matched4 matched5">Test div</div>
<div id="dummy">
<div></div>
</div>
</body>
</html>

View File

@ -0,0 +1,115 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Tests that the style inspector works properly
let doc;
let stylePanel;
const TEST_URI = "http://example.com/browser/browser/devtools/styleinspector/test/browser/browser_bug683672.html";
Cu.import("resource:///modules/devtools/CssHtmlTree.jsm");
function test()
{
waitForExplicitFinish();
addTab(TEST_URI);
browser.addEventListener("load", tabLoaded, true);
}
function tabLoaded()
{
ok(window.StyleInspector, "StyleInspector exists");
ok(StyleInspector.isEnabled, "style inspector preference is enabled");
stylePanel = StyleInspector.createPanel();
Services.obs.addObserver(runTests, "StyleInspector-opened", false);
stylePanel.openPopup();
}
function runTests()
{
Services.obs.removeObserver(runTests, "StyleInspector-opened", false);
ok(stylePanel.isOpen(), "style inspector is open");
testMatchedSelectors();
testUnmatchedSelectors();
info("finishing up");
Services.obs.addObserver(finishUp, "StyleInspector-closed", false);
stylePanel.hidePopup();
}
function testMatchedSelectors()
{
info("checking selector counts, matched rules and titles");
let div = content.document.getElementById("test");
ok(div, "captain, we have the div");
info("selecting the div");
stylePanel.selectNode(div);
let htmlTree = stylePanel.cssHtmlTree;
is(div, htmlTree.viewedElement,
"style inspector node matches the selected node");
let propertyView = new PropertyView(htmlTree, "color");
let numMatchedSelectors = propertyView.propertyInfo.matchedSelectors.length;
is(numMatchedSelectors, 6,
"CssLogic returns the correct number of matched selectors for div");
let dummy = content.document.getElementById("dummy");
let returnedRuleTitle = propertyView.ruleTitle(dummy);
let str = CssHtmlTree.l10n("property.numberOfSelectors");
let calculatedRuleTitle = PluralForm.get(numMatchedSelectors, str)
.replace("#1", numMatchedSelectors);
info("returnedRuleTitle: '" + returnedRuleTitle + "'");
is(returnedRuleTitle, calculatedRuleTitle,
"returned title for matched rules is correct");
}
function testUnmatchedSelectors()
{
info("checking selector counts, unmatched rules and titles");
let body = content.document.body;
ok(body, "captain, we have a body");
info("selecting content.document.body");
stylePanel.selectNode(body);
let htmlTree = stylePanel.cssHtmlTree;
is(body, htmlTree.viewedElement,
"style inspector node matches the selected node");
let propertyView = new PropertyView(htmlTree, "color");
let numUnmatchedSelectors = propertyView.propertyInfo.unmatchedSelectors.length;
is(numUnmatchedSelectors, 13,
"CssLogic returns the correct number of unmatched selectors for body");
let dummy = content.document.getElementById("dummy");
let returnedRuleTitle = propertyView.ruleTitle(dummy);
let str = CssHtmlTree.l10n("property.numberOfUnmatchedSelectors");
let calculatedRuleTitle = PluralForm.get(numUnmatchedSelectors, str)
.replace("#1", numUnmatchedSelectors);
info("returnedRuleTitle: '" + returnedRuleTitle + "'");
is(returnedRuleTitle, calculatedRuleTitle,
"returned title for unmatched rules is correct");
}
function finishUp()
{
Services.obs.removeObserver(finishUp, "StyleInspector-closed", false);
ok(!stylePanel.isOpen(), "style inspector is closed");
doc = stylePanel = null;
gBrowser.removeCurrentTab();
finish();
}

View File

@ -3,19 +3,18 @@
# LOCALIZATION NOTE (panelTitle): This is the panel title
panelTitle=Style Inspector
# LOCALIZATION NOTE (property.numberOfRules): For each style property the panel
# shows the number of rules which hold that specific property, counted from all
# of the stylesheets in the web page inspected.
# LOCALIZATION NOTE (property.numberOfSelectors): For each style property the
# panel shows the number of selectors which match the currently selected
# element, counted from all stylesheets in the web page inspected.
# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
property.numberOfRules=#1 rule;#1 rules
property.numberOfSelectors=1 selector;#1 selectors
# LOCALIZATION NOTE (property.numberOfUnmatchedRules): Each style property is
# inside a rule. A rule is a selector that can match (or not) the highlighted
# element in the web page. The property view shows no unmatched rules. If the
# user wants to expand the property to view unmatched rules, he/she must click
# this link displayed to the right of each property.
# LOCALIZATION NOTE (property.numberOfUnmatchedSelectors): For each style
# property the panel shows the number of selectors which do not match the
# currently selected element, counted from all stylesheets in the web page
# inspected.
# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
property.numberOfUnmatchedRules=One unmatched rule;#1 unmatched rules
property.numberOfUnmatchedSelectors=1 unmatched selector;#1 unmatched selectors
# LOCALIZATION NOTE (rule.status): For each style property the panel shows
# the rules which hold that specific property. For every rule, the rule status
@ -33,14 +32,12 @@ rule.status.UNMATCHED=Unmatched
rule.sourceInline=inline
rule.sourceElement=element
# LOCALIZATION NOTE (rule.showUnmatchedLink): Each style property
# is inside a rule. A rule is a selector that can match (or not) the highlighted
# element in the web page. The property view shows only a few of the unmatched
# rules. If the user wants to see all of the unmatched rules, he/she must click
# the link displayed at the bottom of the rules table. That link shows how many
# rules are not displayed. This is the string used when the link is generated.
# LOCALIZATION NOTE (rule.showUnmatchedLink): For each style
# property the panel shows the number of selectors which do not match the
# currently selected element, counted from all stylesheets in the web page
# inspected.
# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
rule.showUnmatchedLink=One unmatched rule...;#1 unmatched rules...
rule.showUnmatchedLink=1 unmatched selector…;#1 unmatched selectors…
# LOCALIZATION NOTE (group): Style properties are displayed in categories and
# these are the category names.