Skip to content
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

Dart2JS breaks the HTML5 Selection.toString() method #27789

Open
jobukkit opened this issue Nov 9, 2016 · 8 comments
Open

Dart2JS breaks the HTML5 Selection.toString() method #27789

jobukkit opened this issue Nov 9, 2016 · 8 comments
Assignees
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-html type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-libraries Issues impacting dart:html, etc., libraries

Comments

@jobukkit
Copy link

jobukkit commented Nov 9, 2016

In both Dart and Javascript, you can get the selected/highlighted text in the browser by calling window.getSelection().toString(). However, Dart2JS breaks this. Instead of the selected text, it gives the literal string Instance of 'Selection'.

Example Dart code (works in Dartium):

String selectedText;

Selection selection = window.getSelection();
selectedText = selection.toString();

print(selectedText);

The compiled Javascript for this says something like:

selection = window.getSelection();
t2 = J.getInterceptor(selection);
t1 = t2.toString$0(selection);

After manually changing the last line to

t1 = selection.toString();

it works correctly.

@anders-sandholm anders-sandholm added web-libraries Issues impacting dart:html, etc., libraries library-html type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 9, 2016
@anders-sandholm
Copy link
Contributor

May be a dart2js thing. If so, please re-assign labels accordingly.

@jobukkit
Copy link
Author

jobukkit commented Nov 9, 2016

I can't assign labels here..

@rakudrama
Copy link
Member

@alan-knight Selection.toString() needs to be defined like Location.toString()

@alan-knight alan-knight self-assigned this Nov 9, 2016
@ghost
Copy link

ghost commented Mar 27, 2017

While I was waiting for this bug being fixed, I wrote a workaround with package:js.

get_selection_patch.dart

@JS()
library get_selection_patch;
import "package:js/js.dart";
@JS("getSelectionString")
external String getSelectionString();

get_selection_string.js

var getSelectionString = function () {
  return window.getSelection().toString();
}

@jobukkit jobukkit changed the title Dart2JS breaks the HTML5 Selection.toString() method Dart2JS breaks the HTML5 Selection.toString() method Jun 4, 2017
@alan-knight
Copy link
Contributor

This was fixed in
https://codereview.chromium.org/2669103003

but unfortunately caused test regressions and had to be reverted in
https://codereview.chromium.org/2678593002

and we haven't come back to it yet.

@rkj
Copy link

rkj commented Jan 4, 2018

@ntaoo even simpler workaround:

import 'dart:js';
...
  context.callMethod('getSelection').callMethod('toString');

@vsmenon vsmenon added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jul 22, 2019
@Tomas-kubicek
Copy link

This is still a prominent issue even after all this time. When should we expect a fix?

@binarypie
Copy link

We're also wondering if this will ever be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-html type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-libraries Issues impacting dart:html, etc., libraries
Projects
None yet
Development

No branches or pull requests

8 participants