Skip to content

Commit f3e4ebb

Browse files
authored
Merge pull request #1706 from dashaezhova/patch-1
Update 'Selection and Range' article.md
2 parents b2d69c1 + 7f26b91 commit f3e4ebb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

2-ui/99-ui-misc/02-selection-range/article.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ libs:
88

99
In this chapter we'll cover selection in the document, as well as selection in form fields, such as `<input>`.
1010

11-
JavaScript can do get the existing selection, select/deselect both as a whole or partially, remove the selected part from the document, wrap it into a tag, and so on.
11+
JavaScript can get the existing selection, select/deselect both as a whole or partially, remove the selected part from the document, wrap it into a tag, and so on.
1212

1313
You can get ready to use recipes at the end, in "Summary" section. But you'll get much more if you read the whole chapter. The underlying `Range` and `Selection` objects are easy to grasp, and then you'll need no recipes to make them do what you want.
1414

1515
## Range
1616

1717
The basic concept of selection is [Range](https://dom.spec.whatwg.org/#ranges): basically, a pair of "boundary points": range start and range end.
1818

19-
Each point represented as a parent DOM node with the relative offset from its start. If the parent node is an element element node, then the offset is a child number, for a text node it's the position in the text. Examples to follow.
19+
Each point represented as a parent DOM node with the relative offset from its start. If the parent node is an element node, then the offset is a child number, for a text node it's the position in the text. Examples to follow.
2020

2121
Let's select something.
2222

@@ -95,8 +95,8 @@ Let's select `"Example: <i>italic</i>"`. That's two first children of `<p>` (cou
9595
</script>
9696
```
9797

98-
- `range.setStart(p, 0)` -- sets the start at the 0th child of `<p>` (that's a text node `"Example: "`).
99-
- `range.setEnd(p, 2)` -- spans the range up to (but not including) 2nd child of `<p>` (that's a text node `" and "`, but as the end is not included, so the last selected node is `<i>`).
98+
- `range.setStart(p, 0)` -- sets the start at the 0th child of `<p>` (that's the text node `"Example: "`).
99+
- `range.setEnd(p, 2)` -- spans the range up to (but not including) 2nd child of `<p>` (that's the text node `" and "`, but as the end is not included, so the last selected node is `<i>`).
100100

101101
Here's a more flexible test stand where you try more variants:
102102

@@ -387,7 +387,7 @@ Also, there are convenience methods to manipulate the selection range directly,
387387
- `setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset)` - replace selection range with the given start `anchorNode/anchorOffset` and end `focusNode/focusOffset`. All content in-between them is selected.
388388
- `selectAllChildren(node)` -- select all children of the `node`.
389389
- `deleteFromDocument()` -- remove selected content from the document.
390-
- `containsNode(node, allowPartialContainment = false)` -- checks whether the selection contains `node` (partically if the second argument is `true`)
390+
- `containsNode(node, allowPartialContainment = false)` -- checks whether the selection contains `node` (partially if the second argument is `true`)
391391

392392
So, for many tasks we can call `Selection` methods, no need to access the underlying `Range` object.
393393

@@ -551,7 +551,7 @@ If nothing is selected, or we use equal `start` and `end` in `setRangeText`, the
551551

552552
We can also insert something "at the cursor" using `setRangeText`.
553553

554-
Here's an button that inserts `"HELLO"` at the cursor position and puts the cursor immediately after it. If the selection is not empty, then it gets replaced (we can do detect in by comparing `selectionStart!=selectionEnd` and do something else instead):
554+
Here's a button that inserts `"HELLO"` at the cursor position and puts the cursor immediately after it. If the selection is not empty, then it gets replaced (we can detect it by comparing `selectionStart!=selectionEnd` and do something else instead):
555555

556556
```html run autorun
557557
<input id="input" style="width:200px" value="Text Text Text Text Text">
@@ -583,7 +583,7 @@ To make something unselectable, there are three ways:
583583

584584
This doesn't allow the selection to start at `elem`. But the user may start the selection elsewhere and include `elem` into it.
585585

586-
Then `elem` will become a part of `document.getSelection()`, so the selection actully happens, but its content is usually ignored in copy-paste.
586+
Then `elem` will become a part of `document.getSelection()`, so the selection actually happens, but its content is usually ignored in copy-paste.
587587

588588

589589
2. Prevent default action in `onselectstart` or `mousedown` events.
@@ -632,7 +632,7 @@ The most used recipes are probably:
632632
cloned.append(selection.getRangeAt(i).cloneContents());
633633
}
634634
```
635-
2. Setting the selection
635+
2. Setting the selection:
636636
```js run
637637
let selection = document.getSelection();
638638

0 commit comments

Comments
 (0)