Skip to content

Commit 2a8d22e

Browse files
committed
Integrate feedback for Chapters 12-21
Issue marijnh#88
1 parent 0eda372 commit 2a8d22e

File tree

6 files changed

+68
-56
lines changed

6 files changed

+68
-56
lines changed

12_browser.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,13 @@ need to be closed. An example of this would be `<img
199199
src="http://example.com/image.jpg">`, which will display the image
200200
found at the given source URL.
201201

202-
To be able to include angular brackets in the text of a document, even though
203-
they have a special meaning in HTML, yet another form of special notation has
204-
to be introduced. A plain opening angular bracket is written as `&lt;` (“less than”),
205-
and a closing bracket as `&gt;` (“greater than”). In HTML, an ampersand (“&”) character
206-
followed by a word and a semicolon (“;”) denotes an escaped character.
202+
To be able to include angular brackets in the text of a document, even
203+
though they have a special meaning in HTML, yet another form of
204+
special notation has to be introduced. A plain opening angular bracket
205+
is written as `&lt;` (“less than”), and a closing bracket as `&gt;`
206+
(“greater than”). In HTML, an ampersand (“&”) character followed by a
207+
word and a semicolon (“;”) is called an _entity_, and will be replaced
208+
by the character it encodes.
207209

208210
This is analogous to the way backslashes are used in JavaScript strings.
209211
Since this mechanism gives ampersand characters a special meaning too,

13_dom.txt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ where elements can contain sub-elements that are similar to
6464
themselves.
6565

6666
We call a data structure a _tree_ when it has a branching structure,
67-
no cycles (a node may not contain itself, directly or
68-
indirectly), and has a single, well-defined “root”.
67+
no cycles (a node may not contain itself, directly or indirectly), and
68+
has a single, well-defined “root”. In the case of the DOM,
69+
`document.documentElement` serves as the root.
6970

7071
Trees come up a lot in computer science. Apart from representing
7172
recursive structures like HTML documents or programs, they are also
@@ -142,11 +143,10 @@ following diagram tries to illustrate these.
142143

143144
image::img/html-links.svg[alt="Links between DOM nodes",width="6cm"]
144145

145-
Although the diagram only shows one link of each type,
146-
every node has a `parentNode` property
147-
that points to its containing node. Likewise, every element
148-
node (node type 1) has a `childNodes` property that points to an
149-
array-like object holding its children.
146+
Although the diagram only shows one link of each type, every node has
147+
a `parentNode` property that points to its containing node. Likewise,
148+
every element node (node type 1) has a `childNodes` property that
149+
points to an array-like object holding its children.
150150

151151
In theory, you could move anywhere in the tree using just these
152152
parent and child links. But JavaScript also gives you access to
@@ -495,12 +495,13 @@ is called `className`. You can also access it with the
495495

496496
== Layout ==
497497

498-
You might have noticed that different types of elements are laid out differently.
499-
Some, such as paragraphs (`<p>`) or headings (`<h1>`), take up the
500-
whole width of the document, and are placed below each other.
501-
These are called _block_ elements. Others, such as links (`<a>`) or
502-
the `<strong>` element used in the example above, are treated as part
503-
of the surrounding text. Such elements are called _inline_ elements.
498+
You might have noticed that different types of elements are laid out
499+
differently. Some, such as paragraphs (`<p>`) or headings (`<h1>`),
500+
take up the whole width of the document, and are rendered on separate
501+
lines. These are called _block_ elements. Others, such as links
502+
(`<a>`) or the `<strong>` element used in the example above, are
503+
rendered on the same line with their surrounding text. Such elements
504+
are called _inline_ elements.
504505

505506
For any given document, browser are able to compute a
506507
layout, which gives each element a size and position based on its type
@@ -722,10 +723,11 @@ p.a.b#main {
722723
----
723724

724725
The precedence rule favoring the most recently defined rule only holds
725-
when the rules have the same _specificity_. Specificity depends on the
726-
amount of aspects of the tag that the rule matches. For example, `p.a`
727-
is more specific than just `p` or just `.a`, so a rule defined as such would take
728-
precedence to them.
726+
when the rules have the same _specificity_. A rule's specificity is a
727+
measure of how precisely it describes matching elements, determined by
728+
the number and kind (tag, class, or id) of element aspects it
729+
requires. For example, `p.a` is more specific than just `p` or just
730+
`.a`, so a rule defined as such would take precedence to them.
729731

730732
The notation `p > a {…}` applies the given styles to all `<a>` tags
731733
that are direct children of `<p>` tags. Similarly, `p a {…}` applies

16_canvas.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,12 @@ level. It is more difficult to use than plain HTML, but also much more
10841084
powerful.
10851085

10861086
Both SVG and HTML build up a data structure (the DOM) that represents
1087-
the picture. This makes it possible to modify elements they are drawn.
1088-
If you need to repeatedly change a small part of a big picture, in
1089-
response to what the user is doing or as part of an animation, doing
1090-
it in canvas can be needlessly expensive. The DOM also allows us to
1091-
register mouse event handlers on every element in the picture (even on
1092-
shapes drawn with SVG). This can not be done with canvas.
1087+
the picture. This makes it possible to modify elements after they are
1088+
drawn. If you need to repeatedly change a small part of a big picture,
1089+
in response to what the user is doing or as part of an animation,
1090+
doing it in canvas can be needlessly expensive. The DOM also allows us
1091+
to register mouse event handlers on every element in the picture (even
1092+
on shapes drawn with SVG). This can not be done with canvas.
10931093

10941094
But canvas’ pixel-oriented approach can be an advantage when drawing a
10951095
huge amount of tiny elements. The fact that it does not build up a

18_forms.txt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ a good idea to disable the control until the action finishes, so that
197197
when the user gets impatient and clicks it again, they don't
198198
accidentally repeat their action.
199199

200-
== A forms as a whole ==
200+
== A form as a whole ==
201201

202202
When a field is contained in a `<form>` element, its DOM element will
203203
have a property `form` linking back to the form's DOM element. The
@@ -283,13 +283,13 @@ F1, inserts the string “Srinivasa Ramanujan” for you.
283283

284284
[source,text/html]
285285
----
286-
<textarea style="width: 100%; height: 50px"></textarea>
286+
<textarea></textarea>
287287
<script>
288-
var text = document.querySelector("textarea");
289-
text.addEventListener("keydown", function(event) {
290-
// The key code for F1 happens to 112
288+
var textarea = document.querySelector("textarea");
289+
textarea.addEventListener("keydown", function(event) {
290+
// The key code for F1 happens to be 112
291291
if (event.keyCode == 112) {
292-
replaceSelection(text, "Srinivasa Ramanujan");
292+
replaceSelection(textarea, "Srinivasa Ramanujan");
293293
event.preventDefault();
294294
}
295295
});
@@ -309,7 +309,7 @@ of a text field's content with the given word, and then moves the
309309
cursor after that word, so that the user can continue typing.
310310

311311
The `"change"` event for a text field does not fire every time
312-
something is typed. Rather, it happens when the event loses focus
312+
something is typed. Rather, it happens when the field loses focus
313313
after its content was changed. To respond immediately to changes in a
314314
text field, you should register a handler for the `"input"` event
315315
instead, which fires for every character typed (or deleted, or
@@ -493,10 +493,9 @@ is initially empty. The reason there isn't simply a `file` property is
493493
that file fields also support a `multiple` attribute, which makes it
494494
possible to select multiple files at the same time.
495495

496-
For a singular file field, there will be either zero or one file
497-
objects in the `files` object. Such an object has properties like
498-
`name` (the file name), `size` (the file's size in bytes), and `type`
499-
(the media type of the file, like `text/plain` or `image/jpeg`).
496+
Objects in the `files` object have properties like `name` (the file
497+
name), `size` (the file's size in bytes), and `type` (the media type
498+
of the file, like `text/plain` or `image/jpeg`).
500499

501500
[[filereader]]
502501
What it does not have is a property that contains the content of the
@@ -615,22 +614,24 @@ Notes: <select id="list"></select>
615614
</textarea>
616615

617616
<script>
618-
var notes = JSON.parse(localStorage.getItem("notes")) ||
619-
{"shopping list": ""};
620-
function saveToStorage() {
621-
localStorage.setItem("notes", JSON.stringify(notes));
622-
}
623-
624617
var list = document.querySelector("#list");
625618
function addToList(name) {
626619
var option = document.createElement("option");
627620
option.textContent = name;
628621
list.appendChild(option);
629622
}
623+
624+
// Initialize the list from localStorage
625+
var notes = JSON.parse(localStorage.getItem("notes")) ||
626+
{"shopping list": ""};
630627
for (var name in notes)
631628
if (notes.hasOwnProperty(name))
632629
addToList(name);
633630

631+
function saveToStorage() {
632+
localStorage.setItem("notes", JSON.stringify(notes));
633+
}
634+
634635
var current = document.querySelector("#currentnote");
635636
current.value = notes[list.value];
636637

19_paint.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,14 @@ function createPaint(parent) {
122122

123123
var panel = elt("div", {class: "picturepanel"}, canvas);
124124
parent.appendChild(elt("div", null, panel, toolbar));
125-
}
125+
}
126126
----
127127

128128
Each control has access to the canvas drawing context (and, through
129129
that context's `canvas` property, to the `<canvas>` element itself).
130130
Most of the program's state lives in this canvas—it contains the
131-
current picture as well as the selected color and brush size.
131+
current picture as well as the selected color (in its `fillColor`
132+
property) and brush size (in its `lineWidth` property).
132133

133134
We wrap the canvas and the controls in `<div>` elements with classes
134135
to be able to add some styling, for example the grey border around the
@@ -137,11 +138,11 @@ picture.
137138
== Tool selection ==
138139

139140
The first control we add is the the `<select>` element that allows the
140-
user to pick a drawing tool. Again, we will use an object to collect
141-
the various tools, so that we do not have to hard-code them all in one
142-
place, and so that we can add more tools later. This object associates
143-
the names of the tools with the function that should be called when
144-
they are selected and the canvas is clicked.
141+
user to pick a drawing tool. As with `controls`, we will use an object
142+
to collect the various tools, so that we do not have to hard-code them
143+
all in one place, and so that we can add more tools later. This object
144+
associates the names of the tools with the function that should be
145+
called when they are selected and the canvas is clicked.
145146

146147
// include_code
147148

20_node.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ use complicated machines and programming languages?’. Fu-Tzu replied
1414
beautiful huts.’
1515
____
1616

17-
This chapter provides a brief introduction to Node.js, aiming to teach
18-
you the important ideas that the system builds on and give you enough
19-
information to write simple Node programs. It does not aim to be a
20-
complete, or even a thorough, treatment of Node.
17+
So far, we have learned the JavaScript language, and used it within a
18+
single environment, the browser. The following two chapters will
19+
briefly introduce you to Node.js, which allows you to apply your
20+
JavaScript skills outside of the browser, allowing you to build
21+
anything from simple command-line tools to dynamic HTTP servers.
22+
23+
These chapters aim to teach you the important ideas that Node.js
24+
builds on and give you enough information to write some useful
25+
programs for it. They do not try to be a complete, or even a thorough,
26+
treatment of Node.
2127

2228
If you want to follow along and run the code in this chapter, start by
2329
going to http://nodejs.org[_nodejs.org_], and following the

0 commit comments

Comments
 (0)