Skip to content

Commit a17282b

Browse files
committed
fixes
1 parent 9a7deae commit a17282b

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

1-js/05-data-types/03-string/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ It is usually not recommended to use language features in a non-obvious way, but
340340

341341
Just remember: `if (~str.indexOf(...))` reads as "if found".
342342

343-
Technically speaking, numbers are truncated to 32 bits by `~` operator, so there exist other big numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
343+
Technically speaking, numbers are truncated to 32 bits by `~` operator, so there exist other big numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
344344

345-
Right now we can see it only in the old code, as modern JavaScript provides `.includes` method (see below).
345+
Right now we can see this trick only in the old code, as modern JavaScript provides `.includes` method (see below).
346346

347347
### includes, startsWith, endsWith
348348

2-ui/3-event-details/4-mouse-drag-and-drop/article.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Drag'n'Drop is a great interface solution. Taking something, dragging and dropping is a clear and simple way to do many things, from copying and moving (see file managers) to ordering (drop into cart).
44

5-
In the modern HTML standard there's a [section about Drag Events](https://html.spec.whatwg.org/multipage/interaction.html#dnd).
5+
In the modern HTML standard there's a [section about Drag and Drop](https://html.spec.whatwg.org/multipage/interaction.html#dnd) with special events such as `dragstart`, `dragend` and so on.
66

77
They are interesting because they allow to solve simple tasks easily, and also allow to handle drag'n'drop of "external" files into the browser. So we can take a file in the OS file-manager and drop it into the browser window. Then JavaScript gains access to its contents.
88

@@ -94,7 +94,7 @@ So we should listen on `document` to catch it.
9494

9595
## Correct positioning
9696

97-
In the examples above the ball is always centered under the pointer:
97+
In the examples above the ball is always moved so, that it's center is under the pointer:
9898

9999
```js
100100
ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
@@ -119,8 +119,6 @@ For instance, if we start dragging by the edge of the ball, then the cursor shou
119119
let shiftY = event.clientY - ball.getBoundingClientRect().top;
120120
```
121121

122-
Please note that there's no method to get document-relative coordinates in JavaScript, so we use window-relative coordinates here.
123-
124122
2. Then while dragging we position the ball on the same shift relative to the pointer, like this:
125123

126124
```js
@@ -146,7 +144,8 @@ ball.onmousedown = function(event) {
146144
147145
moveAt(event.pageX, event.pageY);
148146
149-
// centers the ball at (pageX, pageY) coordinates
147+
// moves the ball at (pageX, pageY) coordinates
148+
// taking initial shifts into account
150149
function moveAt(pageX, pageY) {
151150
ball.style.left = pageX - *!*shiftX*/!* + 'px';
152151
ball.style.top = pageY - *!*shiftY*/!* + 'px';
@@ -180,17 +179,17 @@ In action (inside `<iframe>`):
180179

181180
The difference is especially noticeable if we drag the ball by its right-bottom corner. In the previous example the ball "jumps" under the pointer. Now it fluently follows the cursor from the current position.
182181

183-
## Detecting droppables
182+
## Potential drop targets (droppables)
184183

185184
In previous examples the ball could be dropped just "anywhere" to stay. In real-life we usually take one element and drop it onto another. For instance, a file into a folder, or a user into a trash can or whatever.
186185

187186
In other words, we take a "draggable" element and drop it onto "droppable" element.
188187

189-
We need to know the target droppable at the end of Drag'n'Drop -- to do the corresponding action, and, preferably, during the dragging process, to highlight it.
188+
We need to know where the element was dropped at the end of Drag'n'Drop -- to do the corresponding action, and, preferably, know the droppable we're dragging over, to highlight it.
190189
191190
The solution is kind-of interesting and just a little bit tricky, so let's cover it here.
192191

193-
What's the first idea? Probably to put `onmouseover/mouseup` handlers on potential droppables and detect when the mouse pointer appears over them. And then we know that we are dragging/dropping on that element.
192+
What may be the first idea? Probably to set `mouseover/mouseup` handlers on potential droppables and detect when the mouse pointer appears over them. And then we know that we are dragging over/dropping on that element.
194193

195194
But that doesn't work.
196195
@@ -217,7 +216,7 @@ That's why the initial idea to put handlers on potential droppables doesn't work
217216
218217
So, what to do?
219218
220-
There's a method called `document.elementFromPoint(clientX, clientY)`. It returns the most nested element on given window-relative coordinates (or `null` if coordinates are out of the window).
219+
There's a method called `document.elementFromPoint(clientX, clientY)`. It returns the most nested element on given window-relative coordinates (or `null` if given coordinates are out of the window).
221220

222221
So in any of our mouse event handlers we can detect the potential droppable under the pointer like this:
223222

@@ -226,12 +225,12 @@ So in any of our mouse event handlers we can detect the potential droppable unde
226225
ball.hidden = true; // (*)
227226
let elemBelow = document.elementFromPoint(event.clientX, event.clientY);
228227
ball.hidden = false;
229-
// elemBelow is the element below the ball. If it's droppable, we can handle it.
228+
// elemBelow is the element below the ball, may be droppable
230229
```
231230

232231
Please note: we need to hide the ball before the call `(*)`. Otherwise we'll usually have a ball on these coordinates, as it's the top element under the pointer: `elemBelow=ball`.
233232

234-
We can use that code to check what we're "flying over" at any time. And handle the drop when it happens.
233+
We can use that code to check what element we're "flying over" at any time. And handle the drop when it happens.
235234
236235
An extended code of `onMouseMove` to find "droppable" elements:
237236

6-data-storage/01-cookie/article.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ We leave it as an exercise for the reader. Also, at the end of the chapter you'l
3939

4040
## Writing to document.cookie
4141

42-
We can write to `document.cookie`. But it's not a data property, it's an accessor.
42+
We can write to `document.cookie`. But it's not a data property, it's an accessor. An assignment to it is treated specially.
4343

4444
**A write operation to `document.cookie` passes through the browser that updates cookies mentioned in it, but doesn't touch other cookies.**
4545

@@ -84,11 +84,11 @@ document.cookie = "user=John; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT"
8484

8585
- **`path=/mypath`**
8686

87-
The url path prefix, where the cookie is accessible. Must be absolute. By default, it's the current path.
87+
The url path prefix, the cookie will be accessible for pages under that path. Must be absolute. By default, it's the current path.
8888

8989
If a cookie is set with `path=/admin`, it's visible at pages `/admin` and `/admin/something`, but not at `/home` or `/adminpage`.
9090

91-
Usually, we set `path=/` to make the cookie accessible from all website pages.
91+
Usually, we should set `path` to the root: `path=/` to make the cookie accessible from all website pages.
9292

9393
## domain
9494

@@ -110,19 +110,22 @@ alert(document.cookie); // no user
110110

111111
**There's no way to let a cookie be accessible from another 2nd-level domain, so `other.com` will never receive a cookie set at `site.com`.**
112112

113-
It's a safety restriction, to allow us to store sensitive data in cookies.
113+
It's a safety restriction, to allow us to store sensitive data in cookies, that should be available only on one site.
114114

115-
...But if we'd like to grant access to subdomains like `forum.site.com`, that's possible. We should explicitly set `domain` option to the root domain: `domain=site.com`:
115+
...But if we'd like to allow subdomains like `forum.site.com` get a cookie, that's possible. When setting a cookie at `site.com`, we should explicitly set `domain` option to the root domain: `domain=site.com`:
116116

117117
```js
118-
// at site.com, make the cookie accessible on any subdomain:
118+
// at site.com
119+
// make the cookie accessible on any subdomain *.site.com:
119120
document.cookie = "user=John; domain=site.com"
120121

122+
// later
123+
121124
// at forum.site.com
122-
alert(document.cookie); // with user
125+
alert(document.cookie); // has cookie user=John
123126
```
124127

125-
For historical reasons, `domain=.site.com` (a dot at the start) also works this way, it might better to add the dot to support very old browsers.
128+
For historical reasons, `domain=.site.com` (a dot before `site.com`) also works the same way, allowing access to the cookie from subdomains. That's an old notation, should be used if we need to support very old browsers.
126129

127130
So, `domain` option allows to make a cookie accessible at subdomains.
128131

0 commit comments

Comments
 (0)