Bug 1427419 - Part 5: Move nsIDOMUtils.getCSSLexer to InspectorUtils. r=bz
MozReview-Commit-ID: 4UGiS3I2V6B
--- a/devtools/client/shared/test/browser_filter-editor-01.js
+++ b/devtools/client/shared/test/browser_filter-editor-01.js
@@ -1,26 +1,25 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Tests that the Filter Editor Widget parses filter values correctly (setCssValue)
const {CSSFilterEditorWidget} = require("devtools/client/shared/widgets/FilterWidget");
-const DOMUtils =
- Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
+const InspectorUtils = require("InspectorUtils");
const TEST_URI = CHROME_URL_ROOT + "doc_filter-editor-01.html";
const {getClientCssProperties} = require("devtools/shared/fronts/css-properties");
// Verify that the given string consists of a valid CSS URL token.
// Return true on success, false on error.
function verifyURL(string) {
- let lexer = DOMUtils.getCSSLexer(string);
+ let lexer = InspectorUtils.getCSSLexer(string);
let token = lexer.nextToken();
if (!token || token.tokenType !== "url") {
return false;
}
return lexer.nextToken() === null;
}
--- a/devtools/server/actors/styles.js
+++ b/devtools/server/actors/styles.js
@@ -1579,17 +1579,17 @@ exports.getFontPreviewData = getFontPrev
*/
function getRuleText(initialText, line, column) {
if (typeof line === "undefined" || typeof column === "undefined") {
throw new Error("Location information is missing");
}
let {offset: textOffset, text} =
getTextAtLineColumn(initialText, line, column);
- let lexer = DOMUtils.getCSSLexer(text);
+ let lexer = InspectorUtils.getCSSLexer(text);
// Search forward for the opening brace.
while (true) {
let token = lexer.nextToken();
if (!token) {
throw new Error("couldn't find start of the rule");
}
if (token.tokenType === "symbol" && token.text === "{") {
@@ -1653,17 +1653,17 @@ exports.getRuleText = getRuleText;
*/
function getSelectorOffsets(initialText, line, column) {
if (typeof line === "undefined" || typeof column === "undefined") {
throw new Error("Location information is missing");
}
let {offset: textOffset, text} =
getTextAtLineColumn(initialText, line, column);
- let lexer = DOMUtils.getCSSLexer(text);
+ let lexer = InspectorUtils.getCSSLexer(text);
// Search forward for the opening brace.
let endOffset;
while (true) {
let token = lexer.nextToken();
if (!token) {
break;
}
--- a/devtools/shared/tests/unit/test_csslexer.js
+++ b/devtools/shared/tests/unit/test_csslexer.js
@@ -5,24 +5,23 @@
// This file is a copy of layout/style/test/test_csslexer.js, modified
// to use both our pure-JS lexer and the DOMUtils lexer for
// cross-checking.
"use strict";
const jsLexer = require("devtools/shared/css/lexer");
-const domutils = Components.classes["@mozilla.org/inspector/dom-utils;1"]
- .getService(Components.interfaces.inIDOMUtils);
+const InspectorUtils = require("InspectorUtils");
// An object that acts like a CSSLexer but verifies that the DOM lexer
// and the JS lexer do the same thing.
function DoubleLexer(input) {
info("DoubleLexer input: " + input);
- this.domLexer = domutils.getCSSLexer(input);
+ this.domLexer = InspectorUtils.getCSSLexer(input);
this.jsLexer = jsLexer.getCSSLexer(input);
}
DoubleLexer.prototype = {
checkState: function () {
equal(this.domLexer.lineNumber, this.jsLexer.lineNumber,
"check line number");
equal(this.domLexer.columnNumber, this.jsLexer.columnNumber,
--- a/dom/webidl/CSSLexer.webidl
+++ b/dom/webidl/CSSLexer.webidl
@@ -103,17 +103,17 @@ dictionary CSSToken {
// symbol The symbol text.
DOMString text;
};
/**
* CSSLexer is an interface to the CSS lexer. It tokenizes an
* input stream and returns CSS tokens.
*
- * @see inIDOMUtils.getCSSLexer to create an instance of the lexer.
+ * @see InspectorUtils.getCSSLexer to create an instance of the lexer.
*/
[ChromeOnly]
interface CSSLexer
{
/**
* The line number of the most recently returned token. Line
* numbers are 0-based.
*/
--- a/dom/webidl/InspectorUtils.webidl
+++ b/dom/webidl/InspectorUtils.webidl
@@ -13,16 +13,17 @@
namespace InspectorUtils {
sequence<StyleSheet> getAllStyleSheets(Document document);
sequence<CSSRule> getCSSStyleRules(
Element element,
[TreatNullAs=EmptyString] optional DOMString pseudo = "");
unsigned long getRuleLine(CSSRule rule);
unsigned long getRuleColumn(CSSRule rule);
unsigned long getRelativeRuleLine(CSSRule rule);
+ [NewObject] CSSLexer getCSSLexer(DOMString text);
};
dictionary InspectorRGBTriple {
/*
* NOTE: Using octet for RGB components is not generally OK, because
* they can be outside the 0-255 range, but for backwards-compatible
* named colors (which is what we use this dictionary for) the 0-255
* assumption is fine.
--- a/layout/inspector/InspectorUtils.h
+++ b/layout/inspector/InspectorUtils.h
@@ -62,16 +62,18 @@ public:
* returns a line number relative to the start of the element.
*
* @param aRule the rule to examine
* @return the line number of the rule, possibly relative to the
* <style> element
*/
static uint32_t GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule);
+ static CSSLexer* GetCSSLexer(GlobalObject& aGlobal, const nsAString& aText);
+
private:
static already_AddRefed<nsStyleContext>
GetCleanStyleContextForElement(Element* aElement, nsAtom* aPseudo);
};
} // namespace dom
} // namespace mozilla
--- a/layout/inspector/inDOMUtils.cpp
+++ b/layout/inspector/inDOMUtils.cpp
@@ -358,33 +358,26 @@ InspectorUtils::GetRelativeRuleLine(Glob
lineNumber -= link->GetLineNumber() - 1;
}
}
}
return lineNumber;
}
+/* static */ CSSLexer*
+InspectorUtils::GetCSSLexer(GlobalObject& aGlobal, const nsAString& aText)
+{
+ return new CSSLexer(aText);
+}
+
} // namespace dom
} // namespace mozilla
NS_IMETHODIMP
-inDOMUtils::GetCSSLexer(const nsAString& aText, JSContext* aCx,
- JS::MutableHandleValue aResult)
-{
- MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
- JS::Rooted<JSObject*> scope(aCx, JS::CurrentGlobalOrNull(aCx));
- nsAutoPtr<CSSLexer> lexer(new CSSLexer(aText));
- if (!WrapNewBindingNonWrapperCachedObject(aCx, scope, lexer, aResult)) {
- return NS_ERROR_FAILURE;
- }
- return NS_OK;
-}
-
-NS_IMETHODIMP
inDOMUtils::GetSelectorCount(nsIDOMCSSStyleRule* aRule, uint32_t *aCount)
{
ErrorResult rv;
RefPtr<BindingStyleRule> rule = GetRuleFromDOMRule(aRule, rv);
if (rv.Failed()) {
return rv.StealNSResult();
}
--- a/layout/inspector/inIDOMUtils.idl
+++ b/layout/inspector/inIDOMUtils.idl
@@ -15,19 +15,16 @@ interface nsIDOMNode;
interface nsIDOMNodeList;
interface nsIDOMFontFaceList;
interface nsIDOMRange;
interface nsIDOMCSSStyleSheet;
[scriptable, uuid(362e98c3-82c2-4ad8-8dcb-00e8e4eab497)]
interface inIDOMUtils : nsISupports
{
- [implicit_jscontext]
- jsval getCSSLexer(in DOMString aText);
-
// Utilities for working with selectors. We don't have a JS OM representation
// of a single selector or a selector list yet, but given a rule we can index
// into the selector list.
//
// This is a somewhat backwards API; once we move StyleRule to WebIDL we
// should consider using [ChromeOnly] APIs on that.
unsigned long getSelectorCount(in nsIDOMCSSStyleRule aRule);
// For all three functions below, aSelectorIndex is 0-based
--- a/layout/style/test/test_csslexer.js
+++ b/layout/style/test/test_csslexer.js
@@ -1,15 +1,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
-function test_lexer(domutils, cssText, tokenTypes) {
- let lexer = domutils.getCSSLexer(cssText);
+Components.utils.importGlobalProperties(["InspectorUtils"]);
+
+function test_lexer(cssText, tokenTypes) {
+ let lexer = InspectorUtils.getCSSLexer(cssText);
let reconstructed = '';
let lastTokenEnd = 0;
let i = 0;
while (true) {
let token = lexer.nextToken();
if (!token) {
break;
}
@@ -71,18 +73,18 @@ var LEX_TESTS = [
"whitespace", "ident:comment", "whitespace",
"htmlcomment"]],
// earlier versions of CSS had "bad comment" tokens, but in level 3,
// unterminated comments are just comments.
["/* bad comment", ["comment"]]
];
-function test_lexer_linecol(domutils, cssText, locations) {
- let lexer = domutils.getCSSLexer(cssText);
+function test_lexer_linecol(cssText, locations) {
+ let lexer = InspectorUtils.getCSSLexer(cssText);
let i = 0;
while (true) {
let token = lexer.nextToken();
let startLine = lexer.lineNumber;
let startColumn = lexer.columnNumber;
// We do this in a bit of a funny way so that we can also test the
// location of the EOF.
@@ -96,19 +98,19 @@ function test_lexer_linecol(domutils, cs
if (!token) {
break;
}
}
// Ensure that we saw the correct number of tokens.
equal(i, locations.length);
}
-function test_lexer_eofchar(domutils, cssText, argText, expectedAppend,
+function test_lexer_eofchar(cssText, argText, expectedAppend,
expectedNoAppend) {
- let lexer = domutils.getCSSLexer(cssText);
+ let lexer = InspectorUtils.getCSSLexer(cssText);
while (lexer.nextToken()) {
// Nothing.
}
info("EOF char test, input = " + cssText);
let result = lexer.performEOFFixup(argText, true);
equal(result, expectedAppend);
@@ -139,32 +141,29 @@ var EOFCHAR_TESTS = [
["url(\"hello", "url(\"hello\")"],
["url(hello\\", "url(hello\\\\)", "url(hello\\\uFFFD)"],
["url('hello\\", "url('hello\\\\')", "url('hello')"],
["url(\"hello\\", "url(\"hello\\\\\")", "url(\"hello\")"],
];
function run_test()
{
- let domutils = Components.classes["@mozilla.org/inspector/dom-utils;1"]
- .getService(Components.interfaces.inIDOMUtils);
-
let text, result;
for ([text, result] of LEX_TESTS) {
- test_lexer(domutils, text, result);
+ test_lexer(text, result);
}
for ([text, result] of LINECOL_TESTS) {
- test_lexer_linecol(domutils, text, result);
+ test_lexer_linecol(text, result);
}
for ([text, expectedAppend, expectedNoAppend] of EOFCHAR_TESTS) {
if (!expectedNoAppend) {
expectedNoAppend = expectedAppend;
}
- test_lexer_eofchar(domutils, text, text, expectedAppend, expectedNoAppend);
+ test_lexer_eofchar(text, text, expectedAppend, expectedNoAppend);
}
// Ensure that passing a different inputString to performEOFFixup
// doesn't cause an assertion trying to strip a backslash from the
// end of an empty string.
- test_lexer_eofchar(domutils, "'\\", "", "\\'", "'");
+ test_lexer_eofchar("'\\", "", "\\'", "'");
}