Revert "[dart:html] Throw exception if Window.open opens null window"

This reverts commit a356f71b71.

Reason for revert: This should be handled by throwing an exception when
the methods of the returned window are called, not when it is opened.
This would be a noisy breaking change that we don't want for 3.1. For
now, revert until the change that affects the individual methods is
landed.

Original change's description:
> [dart:html] Throw exception if Window.open opens null window
>
> Window.open silently allows a null window to be opened, and
> issues arise later when users try to use the non-null wrapper.
> This CL changes that to throw an exception if the window is null.
> This exception can be caught and recovered from. This avoids the
> larger breaking change of making this API nullable.
>
> CoreLibraryReviewExempt: Backend-specific library.
> Change-Id: I9a53a477cb370c3bc6bc26b2162ce66c5af166aa
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/306910
> Reviewed-by: Sigmund Cherem <sigmund@google.com>
> Commit-Queue: Srujan Gaddam <srujzs@google.com>

CoreLibraryReviewExempt: Revert in backend-specific library.
Change-Id: I5007b7d7aa608bfc8e5827b5f967af5573d0b758
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309000
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Srujan Gaddam 2023-06-28 23:21:32 +00:00 committed by Commit Queue
parent 2b250992f9
commit bd3e6fa1a3
7 changed files with 14 additions and 48 deletions

View file

@ -36,14 +36,6 @@
[#51486]: https://github.com/dart-lang/sdk/issues/51486
[#52027]: https://github.com/dart-lang/sdk/issues/52027
#### `dart:html`
- **Breaking change to Window.open**:
`Window.open` will now throw an exception that can be caught
(`NullWindowException`) if the opened window is null. Previously, this null
window would be wrapped, and there would be surprising runtime errors when any
member is used on the wrapper.
#### `dart:js_interop`
- **Object literal constructors**:

View file

@ -32154,18 +32154,17 @@ class Window extends EventTarget
/**
* Opens a new window.
*
* Throws a NullWindowException if the opened window is null.
*
* ## Other resources
*
* * [Window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window.open)
* from MDN.
*/
WindowBase open(String url, String name, [String? options]) {
final win =
options == null ? _open2(url, name) : _open3(url, name, options);
if (win == null) throw new NullWindowException();
return _DOMWindowCrossFrame._createSafe(win);
if (options == null) {
return _DOMWindowCrossFrame._createSafe(_open2(url, name));
} else {
return _DOMWindowCrossFrame._createSafe(_open3(url, name, options));
}
}
// API level getter and setter for Location.
@ -33778,13 +33777,6 @@ class Window extends EventTarget
? JS<num>('num', '#.scrollY', this).round()
: document.documentElement!.scrollTop;
}
class NullWindowException implements Exception {
@override
String toString() {
return 'Attempted to call Window.open with a null window.';
}
}
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@ -40025,7 +40017,7 @@ EventTarget? _convertNativeToDart_EventTarget(e) {
return e;
}
_convertDartToNative_EventTarget(e) {
EventTarget? _convertDartToNative_EventTarget(e) {
if (e is _DOMWindowCrossFrame) {
return e._window;
} else {
@ -40124,7 +40116,7 @@ class _DOMWindowCrossFrame implements WindowBase {
// Private window. Note, this is a window in another frame, so it
// cannot be typed as "Window" as its prototype is not patched
// properly. Its fields and methods can only be accessed via JavaScript.
final Object _window;
final _window;
// Fields.
HistoryBase get history =>

View file

@ -11,13 +11,4 @@ main() {
expect(window.scrollX, 0);
expect(window.scrollY, 0);
});
test('open', () {
window.open('', 'blank');
try {
// A blank page with no access to the original window (noopener) should
// result in null.
window.open('', 'invalid', 'noopener=true');
fail('Expected Window.open to throw.');
} on NullWindowException {}
});
}

View file

@ -33,7 +33,7 @@ EventTarget? _convertNativeToDart_EventTarget(e) {
return e;
}
_convertDartToNative_EventTarget(e) {
EventTarget? _convertDartToNative_EventTarget(e) {
if (e is _DOMWindowCrossFrame) {
return e._window;
} else {

View file

@ -9,7 +9,7 @@ class _DOMWindowCrossFrame implements WindowBase {
// Private window. Note, this is a window in another frame, so it
// cannot be typed as "Window" as its prototype is not patched
// properly. Its fields and methods can only be accessed via JavaScript.
final Object _window;
final _window;
// Fields.
HistoryBase get history =>

View file

@ -50,18 +50,17 @@ $(CLASS_MODIFIERS)class $CLASSNAME$EXTENDS$IMPLEMENTS {
/**
* Opens a new window.
*
* Throws a NullWindowException if the opened window is null.
*
* ## Other resources
*
* * [Window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window.open)
* from MDN.
*/
WindowBase open(String url, String name, [String$NULLABLE options]) {
final win =
options == null ? _open2(url, name) : _open3(url, name, options);
if (win == null) throw new NullWindowException();
return _DOMWindowCrossFrame._createSafe(win);
if (options == null) {
return _DOMWindowCrossFrame._createSafe(_open2(url, name));
} else {
return _DOMWindowCrossFrame._createSafe(_open3(url, name, options));
}
}
// API level getter and setter for Location.
@ -255,10 +254,3 @@ $!MEMBERS
JS<num>('num', '#.scrollY', this).round() :
document.documentElement$NULLASSERT.scrollTop;
}
class NullWindowException implements Exception {
@override
String toString() {
return 'Attempted to call Window.open with a null window.';
}
}

View file

@ -9581,7 +9581,6 @@ final Map<String, Set<String>> dartTypeToNativeTypes = {
'NoncedElement': {'NoncedElement'},
'Notification': {'Notification'},
'NotificationEvent': {'NotificationEvent'},
'NullWindowException': {'NullWindowException'},
'Number': {'SVGNumber'},
'NumberInputElement': {'NumberInputElement'},
'NumberList': {'SVGNumberList'},