import 'dart:js_interop'; import 'dart:js_interop_unsafe'; @JS('document') external _Document get _document; extension type _Document._(JSObject _) implements JSObject { external JSObject? get activeElement; } extension type _Event._(JSObject _) implements JSObject { external JSObject? get target; external JSArray composedPath(); } /// Never take a paste owned by an external editor. Flutter field ownership is /// checked by the composer's Flutter FocusNode before this DOM check; the /// engine's shared flt-text-editing element is not itself a composer identity. bool chatPasteHasExternalEditable(JSObject event) { final paste = _Event._(event); final candidates = [...paste.composedPath().toDart]; if (paste.target case final target?) candidates.add(target); var active = _document.activeElement; for (var depth = 0; active != null && depth < 32; depth++) { candidates.add(active); final shadow = active.getProperty('shadowRoot'.toJS); final next = shadow?.getProperty('activeElement'.toJS); if (next == null) break; active = next; } for (final node in candidates) { final tag = node .getProperty('tagName'.toJS) ?.toDart .toUpperCase(); final editable = tag == 'INPUT' || tag == 'TEXTAREA' || node.getProperty('isContentEditable'.toJS)?.toDart == true; if (editable) { final classes = node.getProperty('className'.toJS)?.toDart ?? ''; final parent = node.getProperty('parentElement'.toJS); final semanticsOwned = parent ?.getProperty('tagName'.toJS) ?.toDart .toUpperCase() == 'FLT-SEMANTICS'; if (!classes.split(RegExp(r'\s+')).contains('flt-text-editing') && !semanticsOwned) { return true; } } } // A focused embedded document or opaque custom-element editor owns its // keyboard. Closed shadow roots cannot expose their private editable node. if (active != null) { final tag = active.getProperty('tagName'.toJS)?.toDart.toUpperCase() ?? ''; if (tag == 'IFRAME' || (tag.contains('-') && !tag.startsWith('FLT-'))) { return true; } } return false; }