aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.7/packages/addon-kit/docs/selection.md
blob: f2d4ca5a902a65cd6fafe0ee0dde6c14b0d36e36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!-- contributed by Eric H. Jung [eric.jung@yahoo.com] -->
<!-- contributed by Irakli Gozalishvili [gozala@mozilla.com] -->

The `selection` module provides a means to get and set text and HTML selections
in the current Firefox page.  It can also observe new selections.

Registering for Selection Notifications
---------------------------------------

To be notified when the user makes a selection, register a listener for the
"select" event. Each listener will be called after a selection is made.

    function myListener() {
      console.log("A selection has been made.");
    }
    var selection = require("selection");
    selection.on('select', myListener);

    // You can remove listeners too.
    selection.removeListener('select', myListener);

Iterating Over Discontiguous Selections
---------------------------------------

Discontiguous selections can be accessed by iterating over the `selection`
module itself. Each iteration yields a `Selection` object from which `text`,
`html`, and `isContiguous` properties can be accessed.


Examples
--------

Log the current contiguous selection as text:

    var selection = require("selection");
    if (selection.text)
      console.log(selection.text);

Log the current discontiguous selections as HTML:

    var selection = require("selection");
    if (!selection.isContiguous) {
      for (var subselection in selection) {
         console.log(subselection.html);
      }
    }

Surround HTML selections with delimiters:

    var selection = require("selection");
    selection.on('select', function () {
      selection.html = "\\\" + selection.html + "///";
    });

<api name="text">
@property {string}
  Gets or sets the current selection as plain text. Setting the selection
  removes all current selections, inserts the specified text at the location of
  the first selection, and selects the new text. Getting the selection when
  there is no current selection returns `null`. Setting the selection when there
  is no current selection throws an exception. Getting the selection when
  `isContiguous` is `true` returns the text of the first selection.
</api>

<api name="html">
@property {string}
  Gets or sets the current selection as HTML. Setting the selection removes all
  current selections, inserts the specified text at the location of the first
  selection, and selects the new text. Getting the selection when there is no
  current selection returns `null`. Setting the selection when there is no
  current selection throws an exception. Getting the selection when
  `isContiguous` is `true` returns the text of the first selection.
</api>

<api name="isContiguous">
@property {boolean}
  `true` if the current selection is a single, contiguous selection, and `false`
  if there are two or more discrete selections, each of which may or may not be
  spatially adjacent. (Discontiguous selections can be created by the user with
  Ctrl+click-and-drag.)
</api>

<api name="select">
@event
  This event is emitted whenever the user makes a new selection in a page.
</api>