Bug 1230491 - add CSSStyleSheet::parsingMode; r=heycam,bz

This commit is contained in:
Tom Tromey 2015-12-21 08:52:45 -07:00
parent 34dd38d85e
commit 6bd5c2185f
7 changed files with 123 additions and 29 deletions

View File

@ -60,6 +60,7 @@ skip-if = os == 'linux'
# disabled on OS X for intermittent failures--bug-798848
skip-if = toolkit == 'cocoa'
[test_nodesFromRect.html]
[test_parsingMode.html]
[test_popup_blocker_chrome.xul]
[test_queryCaretRect.html]
[test_resize_move_windows.xul]

View File

@ -0,0 +1,54 @@
<!DOCTYPE HTML>
<html>
<head>
<title>CSSStyleSheet parsingMode test - bug 1230491</title>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
function run() {
const Cc = Components.classes;
const Ci = Components.interfaces;
const sss = Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
const domutils = Cc["@mozilla.org/inspector/dom-utils;1"]
.getService(Ci.inIDOMUtils);
const utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
const userUrl = encodeURI("data:text/css,body { color: seagreen; }");
utils.loadSheetUsingURIString(userUrl, sss.USER_SHEET);
const agentUrl = encodeURI("data:text/css,body { color: tomato; }");
utils.loadSheetUsingURIString(agentUrl, sss.AGENT_SHEET);
const authorUrl = "chrome://mochikit/content/tests/SimpleTest/test.css";
let results = [];
for (let sheet of domutils.getAllStyleSheets(document)) {
if (sheet.href === agentUrl) {
is(sheet.parsingMode, "agent", "agent sheet has expected mode");
results[sss.AGENT_SHEET] = 1;
} else if (sheet.href === userUrl) {
is(sheet.parsingMode, "user", "user sheet has expected mode");
results[sss.USER_SHEET] = 1;
} else if (sheet.href === authorUrl) {
is(sheet.parsingMode, "author",
"author sheet has expected mode");
results[sss.AUTHOR_SHEET] = 1;
}
}
ok(results[sss.AGENT_SHEET] && results[sss.USER_SHEET] &&
results[sss.AUTHOR_SHEET],
"all sheets seen");
SimpleTest.finish();
}
</script>
</head>
<body onload="run()">
<div> What? </div>
</body>
</html>

View File

@ -9,11 +9,19 @@
interface CSSRule;
enum CSSStyleSheetParsingMode {
"author",
"user",
"agent"
};
interface CSSStyleSheet : StyleSheet {
[Pure]
readonly attribute CSSRule? ownerRule;
[Throws]
readonly attribute CSSRuleList cssRules;
[ChromeOnly]
readonly attribute CSSStyleSheetParsingMode parsingMode;
[Throws]
unsigned long insertRule(DOMString rule, unsigned long index);
[Throws]

View File

@ -43,11 +43,11 @@
#include "nsMediaFeatures.h"
#include "nsDOMClassInfoID.h"
#include "mozilla/Likely.h"
#include "mozilla/dom/CSSStyleSheetBinding.h"
#include "nsComponentManagerUtils.h"
#include "nsNullPrincipal.h"
#include "mozilla/RuleProcessorCache.h"
#include "nsIStyleSheetLinkingElement.h"
#include "nsDOMWindowUtils.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -1104,6 +1104,7 @@ CSSStyleSheet::CSSStyleSheet(CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy)
mDisabled(false),
mDirty(false),
mInRuleProcessorCache(false),
mParsingMode(css::eUserSheetFeatures),
mScopeElement(nullptr),
mRuleProcessors(nullptr)
{
@ -1122,6 +1123,7 @@ CSSStyleSheet::CSSStyleSheet(CORSMode aCORSMode,
mDisabled(false),
mDirty(false),
mInRuleProcessorCache(false),
mParsingMode(css::eUserSheetFeatures),
mScopeElement(nullptr),
mRuleProcessors(nullptr)
{
@ -1142,6 +1144,7 @@ CSSStyleSheet::CSSStyleSheet(const CSSStyleSheet& aCopy,
mDisabled(aCopy.mDisabled),
mDirty(aCopy.mDirty),
mInRuleProcessorCache(false),
mParsingMode(aCopy.mParsingMode),
mScopeElement(nullptr),
mInner(aCopy.mInner),
mRuleProcessors(nullptr)
@ -1189,6 +1192,22 @@ CSSStyleSheet::~CSSStyleSheet()
}
}
mozilla::dom::CSSStyleSheetParsingMode
CSSStyleSheet::ParsingMode()
{
#define CHECK(X, Y) \
static_assert(static_cast<int>(X) == static_cast<int>(Y), \
"mozilla::dom::CSSStyleSheetParsingMode and mozilla::css::SheetParsingMode should have identical values");
CHECK(mozilla::dom::CSSStyleSheetParsingMode::Agent, css::eAgentSheetFeatures);
CHECK(mozilla::dom::CSSStyleSheetParsingMode::User, css::eUserSheetFeatures);
CHECK(mozilla::dom::CSSStyleSheetParsingMode::Author, css::eAuthorSheetFeatures);
#undef CHECK
return static_cast<mozilla::dom::CSSStyleSheetParsingMode>(mParsingMode);
}
void
CSSStyleSheet::DropRuleCollection()
{

View File

@ -13,6 +13,7 @@
#include "mozilla/IncrementalClearCOMRuleArray.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/CSSStyleSheetBinding.h"
#include "nscore.h"
#include "nsCOMPtr.h"
@ -49,7 +50,36 @@ namespace dom {
class CSSRuleList;
} // namespace dom
// -------------------------------
namespace css {
/**
* Enum defining the mode in which a sheet is to be parsed. This is
* usually, but not always, the same as the cascade level at which the
* sheet will apply (see nsStyleSet.h). Most of the Loader APIs only
* support loading of author sheets.
*
* Author sheets are the normal case: styles embedded in or linked
* from HTML pages. They are also the most restricted.
*
* User sheets can do anything author sheets can do, and also get
* access to a few CSS extensions that are not yet suitable for
* exposure on the public Web, but are very useful for expressing
* user style overrides, such as @-moz-document rules.
*
* Agent sheets have access to all author- and user-sheet features
* plus more extensions that are necessary for internal use but,
* again, not yet suitable for exposure on the public Web. Some of
* these are outright unsafe to expose; in particular, incorrect
* styling of anonymous box pseudo-elements can violate layout
* invariants.
*/
enum SheetParsingMode {
eAuthorSheetFeatures = 0,
eUserSheetFeatures,
eAgentSheetFeatures
};
}
// -------------------------------
// CSS Style Sheet Inner Data Container
//
@ -337,6 +367,12 @@ public:
void WillDirty();
void DidDirty();
mozilla::dom::CSSStyleSheetParsingMode ParsingMode();
void SetParsingMode(css::SheetParsingMode aParsingMode) {
mParsingMode = aParsingMode;
}
private:
CSSStyleSheet(const CSSStyleSheet& aCopy,
CSSStyleSheet* aParentToUse,
@ -385,6 +421,7 @@ protected:
bool mDisabled;
bool mDirty; // has been modified
bool mInRuleProcessorCache;
css::SheetParsingMode mParsingMode;
RefPtr<dom::Element> mScopeElement;
CSSStyleSheetInner* mInner;

View File

@ -183,33 +183,6 @@ enum StyleSheetState {
eSheetComplete
};
/**
* Enum defining the mode in which a sheet is to be parsed. This is
* usually, but not always, the same as the cascade level at which the
* sheet will apply (see nsStyleSet.h). Most of the Loader APIs only
* support loading of author sheets.
*
* Author sheets are the normal case: styles embedded in or linked
* from HTML pages. They are also the most restricted.
*
* User sheets can do anything author sheets can do, and also get
* access to a few CSS extensions that are not yet suitable for
* exposure on the public Web, but are very useful for expressing
* user style overrides, such as @-moz-document rules.
*
* Agent sheets have access to all author- and user-sheet features
* plus more extensions that are necessary for internal use but,
* again, not yet suitable for exposure on the public Web. Some of
* these are outright unsafe to expose; in particular, incorrect
* styling of anonymous box pseudo-elements can violate layout
* invariants.
*/
enum SheetParsingMode {
eAuthorSheetFeatures = 0,
eUserSheetFeatures,
eAgentSheetFeatures
};
class Loader final {
typedef mozilla::net::ReferrerPolicy ReferrerPolicy;

View File

@ -1694,6 +1694,8 @@ CSSParserImpl::ParseSheet(const nsAString& aInput,
mIsChrome = false;
mReusableSheets = nullptr;
mSheet->SetParsingMode(aParsingMode);
return NS_OK;
}