-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[cross_file] [web] Separate "Save As" implementation details from XFile web class #10396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,14 @@ | |
| // found in the LICENSE file. | ||
|
|
||
| import 'package:web/web.dart'; | ||
| import '../types/html.dart'; | ||
|
|
||
| /// Type definition for function that creates anchor elements | ||
| typedef CreateAnchorElement = | ||
| HTMLAnchorElement Function(String href, String? suggestedName); | ||
|
|
||
| /// Override for creating anchor elements for testing purposes | ||
| CreateAnchorElement? anchorElementOverride; | ||
|
|
||
| /// Create anchor element with download attribute | ||
| HTMLAnchorElement createAnchorElement(String href, String? suggestedName) => | ||
|
|
@@ -35,3 +43,23 @@ Element ensureInitialized(String id) { | |
| bool isSafari() { | ||
| return window.navigator.vendor == 'Apple Computer, Inc.'; | ||
| } | ||
|
|
||
| /// Saves the given [XFile] to user's device ("Save As" dialog). | ||
| Future<void> saveFileAs(XFile file, String path) async { | ||
| // Create container element. | ||
| final Element target = ensureInitialized('__x_file_dom_element'); | ||
|
|
||
| // Create <a> element. | ||
| final HTMLAnchorElement element = | ||
| anchorElementOverride != null | ||
| ? anchorElementOverride!(file.path, file.name) | ||
| : createAnchorElement(file.path, file.name); | ||
|
|
||
| // Clear existing children before appending new one. | ||
| while (target.children.length > 0) { | ||
| target.removeChild(target.children.item(0)!); | ||
| } | ||
|
Comment on lines
+59
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Add and click. | ||
| addElementToContainerAndClick(target, element); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import 'dart:js_interop'; | |
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:cross_file/cross_file.dart'; | ||
| import 'package:cross_file/src/web_helpers/web_helpers.dart' as helpers; | ||
| import 'package:test/test.dart'; | ||
| import 'package:web/web.dart' as html; | ||
|
|
||
|
|
@@ -147,15 +148,9 @@ void main() { | |
| final html.HTMLAnchorElement mockAnchor = | ||
| html.document.createElement('a') as html.HTMLAnchorElement; | ||
|
|
||
| final CrossFileTestOverrides overrides = CrossFileTestOverrides( | ||
| createAnchorElement: (_, __) => mockAnchor, | ||
| ); | ||
| helpers.anchorElementOverride = (_, __) => mockAnchor; | ||
|
|
||
| final XFile file = XFile.fromData( | ||
| bytes, | ||
| name: textFile.name, | ||
| overrides: overrides, | ||
| ); | ||
| final XFile file = XFile.fromData(bytes, name: textFile.name); | ||
|
Comment on lines
+151
to
+153
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modifying a global variable ( Additionally, the test may create a DOM element that should also be cleaned up to prevent side effects between tests. addTearDown(() {
helpers.anchorElementOverride = null;
html.document.querySelector('#$crossFileDomElementId')?.remove();
});
helpers.anchorElementOverride = (_, __) => mockAnchor;
final XFile file = XFile.fromData(bytes, name: textFile.name); |
||
|
|
||
| bool clicked = false; | ||
| mockAnchor.onClick.listen((html.MouseEvent event) => clicked = true); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a global variable for testing can lead to test pollution. It's good practice to:
@visibleForTestingto signal its intended use. This requires importingpackage:meta/meta.dartat the top of the file.tearDownoraddTearDown.