diff --git a/site/lib/src/components/common/client/copy_button.dart b/site/lib/src/components/common/client/copy_button.dart index 0b0d50fdac..fffc9dc0ab 100644 --- a/site/lib/src/components/common/client/copy_button.dart +++ b/site/lib/src/components/common/client/copy_button.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'package:jaspr/jaspr.dart'; - import 'package:universal_web/web.dart' as web; import '../button.dart'; @@ -30,23 +29,33 @@ class _CopyButtonState extends State { String? content; bool _copied = false; - static final RegExp _terminalReplacementPattern = RegExp( - r'^(\s*\$\s*)|(PS\s+)?(C:\\(.*)>\s*)', - multiLine: true, - ); - @override void initState() { if (kIsWeb) { // Extract the code content and unhide the copy button on the client. context.binding.addPostFrameCallback(() { setState(() { - content = buttonKey.currentNode + final codeElement = buttonKey.currentNode ?.closest('.code-block-wrapper') ?.querySelector('pre code') - ?.textContent - ?.replaceAll(_terminalReplacementPattern, '') - .replaceAll('\u200B', ''); // Remove zero-width spaces + ?.cloneNode(true); + if (codeElement == null) return; + + // Filter out hidden elements like the terminal sign or folding icons. + final iterator = web.document.createNodeIterator( + codeElement, + /* NodeFilter.SHOW_ELEMENT */ 1, + ); + web.Node? currentNode; + while ((currentNode = iterator.nextNode()) != null) { + final element = currentNode as web.Element; + if (element.getAttribute('aria-hidden') == 'true') { + element.remove(); + } + } + + // Remove zero-width spaces + content = codeElement.textContent?.replaceAll('\u200B', ''); }); assert(