Bug 632817 - Cannot filter search for NET events in the Web console; f=pwalton,mihai.sucan r=sdwilsh approval2.0=beltzner

This commit is contained in:
Rob Campbell 2011-02-18 11:46:59 -08:00
Родитель 54f590344d
Коммит f3ba40966c
3 изменённых файлов: 210 добавлений и 56 удалений

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

@ -1606,36 +1606,25 @@ HUD_SERVICE.prototype =
},
/**
* Returns the source code of the XPath contains() function necessary to
* match the given query string.
* Check that the passed string matches the filter arguments.
*
* @param string The query string to convert.
* @returns string
* @param String aString
* to search for filter words in.
* @param String aFilter
* is a string containing all of the words to filter on.
* @returns boolean
*/
buildXPathFunctionForString: function HS_buildXPathFunctionForString(aStr)
stringMatchesFilters: function stringMatchesFilters(aString, aFilter)
{
let words = aStr.split(/\s+/), results = [];
for (let i = 0; i < words.length; i++) {
let word = words[i];
if (word === "") {
continue;
}
let result;
if (word.indexOf('"') === -1) {
result = '"' + word + '"';
}
else if (word.indexOf("'") === -1) {
result = "'" + word + "'";
}
else {
result = 'concat("' + word.replace(/"/g, "\", '\"', \"") + '")';
}
results.push("contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), " + result.toLowerCase() + ")");
if (!aFilter || !aString) {
return true;
}
return (results.length === 0) ? "true()" : results.join(" and ");
let searchStr = aString.toLowerCase();
let filterStrings = aFilter.toLowerCase().split(/\s+/);
return !filterStrings.some(function (f) {
return searchStr.indexOf(f) == -1;
});
},
/**
@ -1651,31 +1640,25 @@ HUD_SERVICE.prototype =
adjustVisibilityOnSearchStringChange:
function HS_adjustVisibilityOnSearchStringChange(aHUDId, aSearchString)
{
let fn = this.buildXPathFunctionForString(aSearchString);
let displayNode = this.getOutputNodeById(aHUDId);
let outputNode = displayNode.querySelector(".hud-output-node");
let doc = outputNode.ownerDocument;
// Look for message nodes ("hud-msg-node") that *aren't* filtered and
// that don't contain the string, and hide them.
let xpath = './/*[contains(@class, "hud-msg-node") and ' +
'not(contains(@class, "hud-filtered-by-string")) and not(' + fn + ')]';
let result = doc.evaluate(xpath, outputNode, null,
Ci.nsIDOMXPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < result.snapshotLength; i++) {
let node = result.snapshotItem(i);
node.classList.add("hud-filtered-by-string");
}
let nodes = outputNode.querySelectorAll(".hud-msg-node");
// Look for message nodes ("hud-msg-node") that *are* filtered and that
// *do* contain the string, and unhide them.
xpath = './/*[contains(@class, "hud-msg-node") and contains(@class, ' +
'"hud-filtered-by-string") and ' + fn + ']';
result = doc.evaluate(xpath, outputNode, null,
Ci.nsIDOMXPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < result.snapshotLength; i++) {
let node = result.snapshotItem(i);
node.classList.remove("hud-filtered-by-string");
for (let i = 0; i < nodes.length; ++i) {
let node = nodes[i];
// hide nodes that match the strings
let text = node.clipboardText;
// if the text matches the words in aSearchString...
if (this.stringMatchesFilters(text, aSearchString)) {
node.classList.remove("hud-filtered-by-string");
}
else {
node.classList.add("hud-filtered-by-string");
}
}
this.regroupOutput(outputNode);
@ -5083,23 +5066,21 @@ ConsoleUtils = {
* The ID of the HUD which this node is to be inserted into.
*/
filterMessageNode: function(aNode, aHUDId) {
// Filter on the search string.
let search = HUDService.getFilterStringByHUDId(aHUDId);
let xpath = ".[" + HUDService.buildXPathFunctionForString(search) + "]";
let doc = aNode.ownerDocument;
let result = doc.evaluate(xpath, aNode, null,
Ci.nsIDOMXPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (result.snapshotLength == 0) {
// The string filter didn't match, so the node is filtered.
aNode.classList.add("hud-filtered-by-string");
}
// Filter by the message type.
let prefKey = MESSAGE_PREFERENCE_KEYS[aNode.category][aNode.severity];
if (prefKey && !HUDService.getFilterState(aHUDId, prefKey)) {
// The node is filtered by type.
aNode.classList.add("hud-filtered-by-type");
}
// Filter on the search string.
let search = HUDService.getFilterStringByHUDId(aHUDId);
let text = aNode.clipboardText;
// if string matches the filter text
if (!HUDService.stringMatchesFilters(text, search)) {
aNode.classList.add("hud-filtered-by-string");
}
},
/**

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

@ -122,6 +122,7 @@ _BROWSER_TEST_FILES = \
browser_webconsole_bug_613280_jsterm_copy.js \
browser_webconsole_bug_630733_response_redirect_headers.js \
browser_webconsole_bug_621644_jsterm_dollar.js \
browser_webconsole_bug_632817.js \
head.js \
$(NULL)

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

@ -0,0 +1,172 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Tests that network log messages bring up the network panel.
const TEST_NETWORK_REQUEST_URI = "http://example.com/browser/toolkit/components/console/hudservice/tests/browser/test-network-request.html";
const TEST_IMG = "http://example.com/browser/toolkit/components/console/hudservice/tests/browser/test-image.png";
const TEST_DATA_JSON_CONTENT =
'{ id: "test JSON data", myArray: [ "foo", "bar", "baz", "biff" ] }';
let lastRequest = null;
let requestCallback = null;
function test()
{
addTab("data:text/html,Web Console network logging tests");
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee, true);
openConsole();
is(HUDService.displaysIndex().length, 1, "Web Console was opened");
hudId = HUDService.displaysIndex()[0];
hud = HUDService.getHeadsUpDisplay(hudId);
HUDService.lastFinishedRequestCallback = function(aRequest) {
lastRequest = aRequest;
if (requestCallback) {
requestCallback();
}
};
executeSoon(testPageLoad);
}, true);
}
function testPageLoad()
{
browser.addEventListener("load", function(aEvent) {
browser.removeEventListener(aEvent.type, arguments.callee, true);
// Check if page load was logged correctly.
ok(lastRequest, "Page load was logged");
is(lastRequest.url, TEST_NETWORK_REQUEST_URI,
"Logged network entry is page load");
is(lastRequest.method, "GET", "Method is correct");
lastRequest = null;
executeSoon(testPageLoadBody);
}, true);
content.location = TEST_NETWORK_REQUEST_URI;
}
function testPageLoadBody()
{
// Turn off logging of request bodies and check again.
browser.addEventListener("load", function(aEvent) {
browser.removeEventListener(aEvent.type, arguments.callee, true);
ok(lastRequest, "Page load was logged again");
lastRequest = null;
executeSoon(testXhrGet);
}, true);
content.location.reload();
}
function testXhrGet()
{
requestCallback = function() {
ok(lastRequest, "testXhrGet() was logged");
is(lastRequest.method, "GET", "Method is correct");
lastRequest = null;
requestCallback = null;
executeSoon(testXhrPost);
};
// Start the XMLHttpRequest() GET test.
content.wrappedJSObject.testXhrGet();
}
function testXhrPost()
{
requestCallback = function() {
ok(lastRequest, "testXhrPost() was logged");
is(lastRequest.method, "POST", "Method is correct");
lastRequest = null;
requestCallback = null;
executeSoon(testFormSubmission);
};
// Start the XMLHttpRequest() POST test.
content.wrappedJSObject.testXhrPost();
}
function testFormSubmission()
{
// Start the form submission test. As the form is submitted, the page is
// loaded again. Bind to the load event to catch when this is done.
browser.addEventListener("load", function(aEvent) {
browser.removeEventListener(aEvent.type, arguments.callee, true);
ok(lastRequest, "testFormSubmission() was logged");
is(lastRequest.method, "POST", "Method is correct");
executeSoon(testLiveFilteringOnSearchStrings);
}, true);
let form = content.document.querySelector("form");
ok(form, "we have the HTML form");
form.submit();
}
function testLiveFilteringOnSearchStrings() {
browser.removeEventListener("DOMContentLoaded",
testLiveFilteringOnSearchStrings, false);
setStringFilter("http");
isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
"search string is set to \"http\"");
setStringFilter("HTTP");
isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
"search string is set to \"HTTP\"");
setStringFilter("hxxp");
is(countMessageNodes(), 0, "the log nodes are hidden when the search " +
"string is set to \"hxxp\"");
setStringFilter("ht tp");
isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
"search string is set to \"ht tp\"");
setStringFilter("");
isnot(countMessageNodes(), 0, "the log nodes are not hidden when the " +
"search string is removed");
setStringFilter("json");
is(countMessageNodes(), 2, "the log nodes show only the nodes with \"json\"");
setStringFilter("'foo'");
is(countMessageNodes(), 0, "the log nodes are hidden when searching for " +
"the string 'foo'");
setStringFilter("foo\"bar'baz\"boo'");
is(countMessageNodes(), 0, "the log nodes are hidden when searching for " +
"the string \"foo\"bar'baz\"boo'\"");
finishTest();
}
function countMessageNodes() {
let outputNode = hud.querySelector(".hud-output-node");
let messageNodes = outputNode.querySelectorAll(".hud-msg-node");
let displayedMessageNodes = 0;
let view = outputNode.ownerDocument.defaultView;
for (let i = 0; i < messageNodes.length; i++) {
let computedStyle = view.getComputedStyle(messageNodes[i], null);
if (computedStyle.display !== "none")
displayedMessageNodes++;
}
return displayedMessageNodes;
}
function setStringFilter(aValue)
{
hud.querySelector(".hud-filter-box").value = aValue;
HUDService.adjustVisibilityOnSearchStringChange(hudId, aValue);
}