Skip to content

filter out hidden elements when copying code#13256

Open
schultek wants to merge 2 commits intomainfrom
fix/copy-no-hidden
Open

filter out hidden elements when copying code#13256
schultek wants to merge 2 commits intomainfrom
fix/copy-no-hidden

Conversation

@schultek
Copy link
Copy Markdown
Contributor

@schultek schultek commented Apr 7, 2026

Fixes #13253

Presubmit checklist

  • If you are unwilling, or unable, to sign the CLA, even for a tiny, one-word PR, please file an issue instead of a PR.
  • If this PR is not meant to land until a future stable release, mark it as draft with an explanation.
  • This PR follows the Google Developer Documentation Style Guidelines—for example, it doesn't use i.e. or e.g., and it avoids I and we (first-person pronouns).
  • This PR uses semantic line breaks
    of 80 characters or fewer.

@schultek schultek requested a review from parlough April 7, 2026 14:30
@schultek schultek requested review from a team and sfshaza2 as code owners April 7, 2026 14:30
Copy link
Copy Markdown
Contributor

@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 replaces regex-based text filtering in the CopyButton component with a DOM-based approach that removes elements marked as hidden. Review feedback identifies a potential compilation error where the remove method is called on a generic Node type and suggests optimizing the node iterator to target elements specifically. It also notes that the newly added js_interop import would become redundant if these suggestions are adopted.

Comment on lines +46 to +54
final iterator = web.document.createNodeIterator(codeElement);
web.Node? currentNode;
while ((currentNode = iterator.nextNode()) != null) {
if (currentNode.isA<web.Element>() &&
(currentNode as web.Element).getAttribute('aria-hidden') ==
'true') {
currentNode.remove();
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current implementation contains a bug and can be optimized:

  1. Bug: currentNode.remove() is called on a variable of type web.Node?. In package:web (which universal_web wraps), the remove() method is defined on the ChildNode interface (implemented by Element and CharacterData), but not on the base Node class. This will likely cause a compilation error.
  2. Optimization: By default, createNodeIterator visits all nodes, including text nodes. Since you only need to check attributes on elements, using web.NodeFilter.SHOW_ELEMENT is more efficient.
  3. Type Safety: Using a direct cast (safe when using SHOW_ELEMENT) or the Dart is operator for type promotion is cleaner than repeated isA checks.
Suggested change
final iterator = web.document.createNodeIterator(codeElement);
web.Node? currentNode;
while ((currentNode = iterator.nextNode()) != null) {
if (currentNode.isA<web.Element>() &&
(currentNode as web.Element).getAttribute('aria-hidden') ==
'true') {
currentNode.remove();
}
}
// Filter out hidden elements like the terminal sign or folding icons.
final iterator = web.document.createNodeIterator(
codeElement,
web.NodeFilter.SHOW_ELEMENT,
);
web.Node? currentNode;
while ((currentNode = iterator.nextNode()) != null) {
final element = currentNode as web.Element;
if (element.getAttribute('aria-hidden') == 'true') {
element.remove();
}
}


import 'package:jaspr/jaspr.dart';

import 'package:universal_web/js_interop.dart';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This import is only used for the isA extension method. If you apply the suggested fix for the iteration logic below (which uses a direct cast or the is operator), this import will become unused and should be removed.

@flutter-website-bot
Copy link
Copy Markdown
Collaborator

flutter-website-bot commented Apr 7, 2026

Visit the preview URL for this PR (updated for commit 2511c9c):

https://flutter-docs-prod--pr13256-fix-copy-no-hidden-wcg56xbe.web.app

Copy link
Copy Markdown
Contributor

@sfshaza2 sfshaza2 left a comment

Choose a reason for hiding this comment

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

LGTM, but I will leave it to you to resolve the bot's complaints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy example code keyboard_arrow_right

3 participants