Skip to content

Conversation

@raju-muliyashiya
Copy link
Contributor

@raju-muliyashiya raju-muliyashiya commented Nov 11, 2025

fixes Issue #91400

Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.

List which issues are fixed by this PR. You must list at least one issue.

Pre-Review Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran [the auto-formatter].
  • I signed the [CLA].
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I [linked to at least one issue that this PR fixes] in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy], or I have commented below to indicate which [version change exemption] this PR falls under[^1].
  • I updated CHANGELOG.md to add a description of the change, [following repository CHANGELOG style], or I have commented below to indicate which [CHANGELOG exemption] this PR falls under[^1].
  • I updated/added any relevant documentation (doc comments with ///).
  • I added updated tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1].
  • All existing and new tests are passing.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the 'Save As' functionality for the web implementation of XFile. The implementation details are moved from the XFile class into a new saveFileAs helper function in web_helpers.dart, which improves separation of concerns. The testing mechanism is also updated to use a global override instead of passing an override object.

My review includes suggestions to improve the new helper function for efficiency and clarity, and to ensure test hygiene by properly cleaning up the global test override.

Comment on lines +151 to 152
helpers.anchorElementOverride = (_, __) => mockAnchor;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

It's a good practice to clean up global state modifications within tests to prevent them from affecting other tests. Using addTearDown ensures that the override is reset even if the test fails.

        addTearDown(() => helpers.anchorElementOverride = null);
        helpers.anchorElementOverride = (_, __) => mockAnchor;

Comment on lines +47 to +65
/// 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)!);
}

// Add and click.
addElementToContainerAndClick(target, element);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function can be improved for clarity and efficiency. The path parameter is unused and can be removed. Additionally, clearing the target element's children can be done more efficiently and expressively using replaceChildren. This also removes the need for the addElementToContainerAndClick helper for this use case. Remember to update the call site in XFile.saveTo.

/// Saves the given [XFile] to user's device ("Save As" dialog).
Future<void> saveFileAs(XFile file) 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);

  // The new element replaces all the existing children of the container.
  target.replaceChildren(element);
  // Click the element to trigger the download.
  element.click();
}

@stuartmorgan-g stuartmorgan-g added the triage-web Should be looked at in web triage label Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: cross_file triage-web Should be looked at in web triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cross_file] [web] Separate "Save As" implementation details from XFile web class

2 participants