Open Bug 1271534 Opened 9 years ago Updated 2 years ago

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

Categories

(WebExtensions :: General, defect, P5)

defect

Tracking

(Not tracked)

People

(Reporter: yuki, Unassigned)

References

Details

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

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.
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#
Priority: -- → P5
Whiteboard: [design-decision-needed] triaged → [design-decision-approved] triaged
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
Product: Toolkit → WebExtensions
Component: Untriaged → General
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.