Closed Bug 581982 Opened 10 years ago Closed 8 years ago

selection.text returns null instead of the selected text when the selection is in a text box or text area


(Add-on SDK Graveyard :: General, defect, P1, major)


(Not tracked)



(Reporter: emre.sevinc, Assigned: zer0)


(Depends on 1 open bug)



(2 files, 2 obsolete files)

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20100715 Ubuntu/10.04 (lucid) Firefox/3.6.7
Build Identifier: 0.6

I'm trying to get the value of selected text in a text box or a text area, I use the following piece of code after having read the documentation:

var selection = require("selection");
console.log("selected text: " + selection.text);

When I run and select a piece of text inside an input box or a text
area I always get this:

info: selected text: null 

Reproducible: Always

Steps to Reproduce:
1. Create the simplest Jetpack SDK example that comes with documentation.
2. Add the following line to the beginning:
3. var selection = require("selection");
4. Add the following line to the item body:
5. context: "input[type=text], textarea",
6. Add the following line to onClick:
7. console.log("selected text: " + selection.text);
8. cfx run -a firefox
9. Go to any web sites that has an input box, e.g.
10. Write something into the Username box.
11. Select what you have written.
12. Click on the relevant context menu item.
13. And see that selection.text returns null on the command line.

Actual Results:  
I received null value instead of the text I have selected within the input box.

Expected Results:  
I expected to receive the text I selected.
Attached file a simpler testcase (obsolete) —
I can confirm the bug. I'm also attaching a simplified testcase. Is a main.js that adds a simple context menu entry to log the current selection to the console.
Attachment #460265 - Attachment is obsolete: true
Confirming per comment 2.
Ever confirmed: true
The Add-on SDK is no longer a Mozilla Labs experiment and has become a big enough project to warrant its own Bugzilla product, so the "Add-on SDK" product has been created for it, and I am moving its bugs to that product.

To filter bugmail related to this change, filter on the word "looptid".
Component: Jetpack SDK → General
Product: Mozilla Labs → Add-on SDK
QA Contact: jetpack-sdk → general
Priority: -- → P2
Target Milestone: --- → 1.0
Assignee: nobody → rFobic
Assignee: rFobic → mferretti
The last comment was actually a mistake, sorry :)
However, the selection module uses window.getSelection() in order to get the selection, but window.getSelection() doesn't works inside textareas.
A possible workaround it could be use selectionStart and selectionEnd on the window.document.activeElement to retreive the text selected, and make it transparent to the user.
Attached file a simpler testcase
Updated the previous testcase in accordance with the addon-sdk changes.
Attachment #460527 - Attachment is obsolete: true
(automatic reprioritization of 1.0 bugs)
Priority: P2 → P1
Target Milestone: 1.0 → 1.1
I used this bug to implement the general support for text fields in the selection module, through several changes:

- `selection.text` can be used to get and set in a selection inside a text field

- `selection.isContiguous` returns `true` in case of selection in a text field (unfortunately text field selection APIs doesn't support discontiguous selections, so at least this is consistent because we can modify only the first selection in any case).
Notice that, as discussed with Myk, `selection.isContiguous` doesn't return `null` anymore, but always a boolean value. If a selection doesn't exists, it returns `false`.

- The "select" event now is fired also for text field's selection.

- Iterate over a selection object, took in account also the selection in text fields.
Attachment #556950 - Flags: review?(myk)
Comment on attachment 556950 [details]
Pointer to Github pull request:

Looks great!  Only a few minor issues, noted in the pull request.
Attachment #556950 - Flags: review?(myk) → review-
(Pushing all open bugs to the --- milestone for the new triage system)
Target Milestone: 1.1 → ---
D'oh!  Merged!

Thanks ZER0!
Closed: 8 years ago
OS: Linux → All
Hardware: x86 → All
Resolution: --- → FIXED
Commit pushed to
Bug 581982 - selection.text returns null instead of the selected text when the selection is in a text box or text area; r=myk
(cherry picked from commit 46177cb8e9c2f63b9c03e6ea21c03641e7599d17)
You need to log in before you can comment on or make changes to this bug.