Add ability to get visible text as-is from DOM Range

NEW
Unassigned

Status

defect
P5
normal
3 years ago
9 months ago

People

(Reporter: yuki, Unassigned)

Tracking

Trunk
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [design-decision-approved] triaged)

Reporter

Description

3 years ago
When I tried to migrate my addon "Text Link" and "XUL/Migemo" from XUL-based to WebExtensions, I've realized that it is impossible to get actual visible text as-is from DOM Range (and selection).

For example, if there is an HTML fragment and a selection:

~~~
<p style="white-space: pre-wrap">[SELECTION START]path/to/file1.jpg<br><span style="display: none">path/to/file2.jpg<br></span>path/to/file3.jpg[SELECTION END]<br>...</p>
~~~

It will be rendered like:

~~~
[SELECTION START]path/to/file1.jpg
path/to/file3.jpg[SELECTION END]
...
~~~

I hope to get actual visible text like "path/to/file1.jpg\npath/to/file3.jpg" from the selection range, but it is impossible. `window.getSelection().getRangeAt(0).toString()` returns a string "path/to/file1.jpgpath/to/file2.jpgpath/to/file3.jpg", because DOM Range's `toString()` doesn't consider actual rendering result. The actual result doesn't have any line break produced by <br> and it has unnecessary "path/to/file2.jpg" in invisible element.

In XUL-based addon it can be done by nsIDocumentEncoder, like:

~~~
var encoder = Cc['@mozilla.org/layout/documentEncoder;1?type=text/plain'].createInstance(Ci.nsIDocumentEncoder);
encoder.init(range.startContainer.ownerDocument, 'text/plain', encoder.OutputBodyOnly | encoder.OutputLFLineBreak | encoder.SkipInvisibleContent);
encoder.setRange(range);
var result = encoder.encodeToString();
~~~

After XUL is ended we need something to alter the workaround with nsIDocumentEncoder.

Updated

3 years ago
Whiteboard: [design-decision-needed] triaged
To be discussed at January 24 WebExtensions triage meeting. 

Agenda: https://docs.google.com/document/d/1add-6FL8mzksvzbyB83HUmEkVmKERd-nt740AYr-4PE/edit#

Updated

2 years ago
Priority: -- → P5
Whiteboard: [design-decision-needed] triaged → [design-decision-approved] triaged
Reporter

Comment 2

2 years ago
As a workaround, I've implemented this function based on DOM2 TreeWalker. It possibly don't work completely for some edge cases, but seems effective for most cases.
https://github.com/piroor/textlink/blob/52b3cb102df31c18a0649ee6ff0a2903864105cb/webextensions/content_scripts/range.js#L8

Updated

11 months ago
Product: Toolkit → WebExtensions
Bulk move of bugs per https://bugzilla.mozilla.org/show_bug.cgi?id=1483958
Component: Untriaged → General
You need to log in before you can comment on or make changes to this bug.