Commit 2abadb62 authored by Momoko Sumida's avatar Momoko Sumida Committed by Commit Bot

Implement Document.createEmptyCSSStyleSheet

Currently, empty CSSStyleSheets can only be constructed using a constructor method.
This CL adds Document.createEmptyCSSStyleSheet so that it can be tied to document in the future.
Tying it to document restricts the use of a CSSStyleSheet in different documents, which means CSSStyleSheets can only be used in the documents where they are constructed. This will reduce security risk.

Note:
The constructed CSSStyleSheet is not currently tied to the Document yet
Constructor method will be deleted in another CL
createEmptyCSSStyleSheet(CSSStyleSheetInit) produces an empty CSSStyleSheet, while createCSSStyleSheet(text, CSSStyleSheetInit) creates a CSSStyleSheet with text

Link to related comments in discussion:
https://github.com/WICG/construct-stylesheets/issues/23#issuecomment-379180786
https://github.com/WICG/construct-stylesheets/issues/15#issuecomment-391216056

Bug: 807560

Change-Id: I94ea6f795deaf0dee67fba5c2705c8749ac72da8
Reviewed-on: https://chromium-review.googlesource.com/1160422
Commit-Queue: Momoko Sumida <momon@google.com>
Reviewed-by: default avatarHayato Ito <hayato@chromium.org>
Reviewed-by: default avatarRakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581169}
parent 3634ea3a
......@@ -28,6 +28,56 @@ test(() => {
assert_equals(sheet.cssRules[0].cssText, redStyleTexts[1]);
}, 'Empty CSSStyleSheet can be constructed using script');
test(() => {
const sheet = document.createEmptyCSSStyleSheet({title: "Red", disabled: true, media: "screen, print"});
assert_equals(sheet.title, "Red");
assert_equals(sheet.ownerNode, null);
assert_equals(sheet.ownerRule, null);
assert_equals(sheet.media.length, 2);
assert_equals(sheet.media.item(0), "screen");
assert_equals(sheet.media.item(1), "print");
assert_true(sheet.disabled);
assert_equals(sheet.cssRules.length, 0);
sheet.insertRule(redStyleTexts[0]);
assert_equals(sheet.cssRules.length, 1);
assert_equals(sheet.cssRules[0].cssText, redStyleTexts[0]);
sheet.insertRule(redStyleTexts[1]);
assert_equals(sheet.cssRules.length, 2);
assert_equals(sheet.cssRules[0].cssText, redStyleTexts[1]);
const sheet2 = document.createEmptyCSSStyleSheet({});
assert_equals(sheet2.title, "")
assert_equals(sheet2.ownerNode, null);
assert_equals(sheet2.ownerRule, null);
assert_equals(sheet2.media.length, 0);
assert_false(sheet2.disabled);
assert_equals(sheet2.cssRules.length, 0);
sheet2.insertRule(redStyleTexts[1]);
assert_equals(sheet2.cssRules.length, 1);
assert_equals(sheet2.cssRules[0].cssText, redStyleTexts[1]);
sheet2.deleteRule(0);
assert_equals(sheet2.cssRules.length, 0);
const sheet3 = document.createEmptyCSSStyleSheet();
assert_equals(sheet3.title, "")
assert_equals(sheet3.ownerNode, null);
assert_equals(sheet3.ownerRule, null);
assert_equals(sheet3.media.length, 0);
assert_false(sheet3.disabled);
assert_equals(sheet3.cssRules.length, 0);
sheet3.insertRule(redStyleTexts[1]);
assert_equals(sheet3.cssRules.length, 1);
assert_equals(sheet3.cssRules[0].cssText, redStyleTexts[1]);
sheet3.deleteRule(0);
assert_equals(sheet3.cssRules.length, 0);
}, 'Document.createEmptyCSSStyleSheet produces empty CSSStyleSheet');
promise_test(() => {
const promise_sheet = document.createCSSStyleSheet(redStyleTexts[0], {title: "Red", disabled: true, media: "screen, print"});
return promise_sheet.then(function(sheet) {
......
......@@ -1635,6 +1635,7 @@ interface Document : Node
method createDocumentFragment
method createElement
method createElementNS
method createEmptyCSSStyleSheet
method createEvent
method createExpression
method createNSResolver
......
......@@ -1144,6 +1144,20 @@ ScriptPromise Document::createCSSStyleSheet(ScriptState* script_state,
ScriptValue::From(script_state, sheet));
}
CSSStyleSheet* Document::createEmptyCSSStyleSheet(
ScriptState* script_state,
const CSSStyleSheetInit& options,
ExceptionState& exception_state) {
return CSSStyleSheet::Create(*this, options, exception_state);
}
CSSStyleSheet* Document::createEmptyCSSStyleSheet(
ScriptState* script_state,
ExceptionState& exception_state) {
return Document::createEmptyCSSStyleSheet(script_state, CSSStyleSheetInit(),
exception_state);
}
ScriptValue Document::registerElement(ScriptState* script_state,
const AtomicString& name,
const ElementRegistrationOptions& options,
......
......@@ -362,6 +362,12 @@ class CORE_EXPORT Document : public ContainerNode,
Element* CreateRawElement(const QualifiedName&,
const CreateElementFlags = CreateElementFlags());
CSSStyleSheet* createEmptyCSSStyleSheet(ScriptState*,
const CSSStyleSheetInit&,
ExceptionState&);
CSSStyleSheet* createEmptyCSSStyleSheet(ScriptState*, ExceptionState&);
ScriptPromise createCSSStyleSheet(ScriptState*,
const String&,
ExceptionState&);
......
......@@ -76,6 +76,8 @@ typedef (HTMLScriptElement or SVGScriptElement) HTMLOrSVGScriptElement;
[NewObject] Range createRange();
[CallWith=ScriptState, NewObject, RaisesException, RuntimeEnabled=ConstructableStylesheets] Promise<CSSStyleSheet> createCSSStyleSheet(DOMString text, optional CSSStyleSheetInit options);
[CallWith=ScriptState, NewObject, RaisesException, RuntimeEnabled=ConstructableStylesheets] CSSStyleSheet createEmptyCSSStyleSheet(optional CSSStyleSheetInit options);
// NodeFilter.SHOW_ALL = 0xFFFFFFFF
[NewObject] NodeIterator createNodeIterator(Node root, optional unsigned long whatToShow = 0xFFFFFFFF, optional NodeFilter? filter = null);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment