Skip to content

Commit 2f24557

Browse files
committed
Change the way client code is built
1 parent 4699ecb commit 2f24557

28 files changed

+1327
-1394
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
/book.pdf
1111
/book_mobile.pdf
1212
/html/[012]*.html
13-
/html/js/chapter_info.js
14-
/html/js/[012]*.js
15-
/html/js/acorn_codemirror.js
13+
/html/ejs.js
1614
/code/chapter/*
1715
/code/file_server.js
1816
/code/skillsharing.zip

13_browser.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ HTML, which stands for _Hypertext Markup Language_, is the document format used
100100

101101
A short HTML document might look like this:
102102

103-
```{lang: "text/html"}
103+
```{lang: "html"}
104104
<!doctype html>
105105
<html>
106106
<head>
@@ -158,7 +158,7 @@ HTML is parsed in a remarkably error-tolerant way. When tags that should be ther
158158

159159
The following document will be treated just like the one shown previously:
160160

161-
```{lang: "text/html"}
161+
```{lang: "html"}
162162
<!doctype html>
163163
164164
<meta charset=utf-8>
@@ -188,7 +188,7 @@ I will also usually omit the ((doctype)) and `charset` declaration. This is not
188188

189189
In the context of this book, the most important HTML tag is `<script>`. This tag allows us to include a piece of JavaScript in a document.
190190

191-
```{lang: "text/html"}
191+
```{lang: "html"}
192192
<h1>Testing alert</h1>
193193
<script>alert("hello!");</script>
194194
```
@@ -201,7 +201,7 @@ Such a script will run as soon as its `<script>` tag is encountered while the br
201201

202202
Including large programs directly in HTML documents is often impractical. The `<script>` tag can be given an `src` attribute to fetch a script file (a text file containing a JavaScript program) from a URL.
203203

204-
```{lang: "text/html"}
204+
```{lang: "html"}
205205
<h1>Testing alert</h1>
206206
<script src="code/hello.js"></script>
207207
```
@@ -220,7 +220,7 @@ You can load ((ES modules)) (see [Chapter ?](modules#es)) in the browser by givi
220220

221221
Some attributes can also contain a JavaScript program. The `<button>` tag shown next (which shows up as a button) has an `onclick` attribute. The attribute's value will be run whenever the button is clicked.
222222

223-
```{lang: "text/html"}
223+
```{lang: "html"}
224224
<button onclick="alert('Boom!');">DO NOT PRESS</button>
225225
```
226226

14_dom.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This representation of the ((document)) is one of the toys that a JavaScript pro
2222

2323
You can imagine an HTML document as a nested set of ((box))es. Tags such as `<body>` and `</body>` enclose other ((tag))s, which in turn contain other tags or ((text)). Here's the example document from the [previous chapter](browser):
2424

25-
```{lang: "text/html", sandbox: "homepage"}
25+
```{lang: "html", sandbox: "homepage"}
2626
<!doctype html>
2727
<html>
2828
<head>
@@ -176,7 +176,7 @@ All element nodes have a `getElementsByTagName` method, which collects all eleme
176176

177177
To find a specific _single_ node, you can give it an `id` attribute and use `document.getElementById` instead.
178178

179-
```{lang: "text/html"}
179+
```{lang: "html"}
180180
<p>My ostrich Gertrude:</p>
181181
<p><img id="gertrude" src="img/ostrich.png"></p>
182182
@@ -196,7 +196,7 @@ A third, similar method is `getElementsByClassName`, which, like `getElementsByT
196196

197197
Almost everything about the DOM data structure can be changed. The shape of the document tree can be modified by changing parent-child relationships. Nodes have a `remove` method to remove them from their current parent node. To add a child node to an element node, we can use `appendChild`, which puts it at the end of the list of children, or `insertBefore`, which inserts the node given as the first argument before the node given as the second argument.
198198

199-
```{lang: "text/html"}
199+
```{lang: "html"}
200200
<p>One</p>
201201
<p>Two</p>
202202
<p>Three</p>
@@ -223,7 +223,7 @@ Say we want to write a script that replaces all ((image))s (`<img>` tags) in the
223223

224224
This involves not only removing the images but adding a new text node to replace them. Text nodes are created with the `document.createTextNode` method.
225225

226-
```{lang: "text/html"}
226+
```{lang: "html"}
227227
<p>The <img src="img/cat.png" alt="Cat"> in the
228228
<img src="img/hat.png" alt="Hat">.</p>
229229
@@ -272,7 +272,7 @@ To create ((element)) nodes, you can use the `document.createElement` method. Th
272272

273273
The following example defines a utility `elt`, which creates an element node and treats the rest of its arguments as children to that node. This function is then used to add an attribution to a quote.
274274

275-
```{lang: "text/html"}
275+
```{lang: "html"}
276276
<blockquote id="quote">
277277
No book can ever be finished. While working on it we learn
278278
just enough to find it immature the moment we turn away
@@ -316,7 +316,7 @@ Some element ((attribute))s, such as `href` for links, can be accessed through a
316316

317317
But HTML allows you to set any attribute you want on nodes. This can be useful because it allows you to store extra information in a document. If you make up your own attribute names, though, such attributes will not be present as properties on the element's node. Instead, you have to use the `getAttribute` and `setAttribute` methods to work with them.
318318

319-
```{lang: "text/html"}
319+
```{lang: "html"}
320320
<p data-classified="secret">The launch code is 00000000.</p>
321321
<p data-classified="unclassified">I have two feet.</p>
322322
@@ -352,7 +352,7 @@ The size and position of an element can be accessed from JavaScript. The `offset
352352

353353
Similarly, `clientWidth` and `clientHeight` give you the size of the space _inside_ the element, ignoring border width.
354354

355-
```{lang: "text/html"}
355+
```{lang: "html"}
356356
<p style="border: 3px solid red">
357357
I'm boxed in
358358
</p>
@@ -386,7 +386,7 @@ Laying out a document can be quite a lot of work. In the interest of speed, brow
386386

387387
A program that repeatedly alternates between reading DOM layout information and changing the DOM forces a lot of layout computations to happen and will consequently run very slowly. The following code is an example of this. It contains two different programs that build up a line of _X_ characters 2,000 pixels wide and measures the time each one takes.
388388

389-
```{lang: "text/html", test: nonumbers}
389+
```{lang: "html", test: nonumbers}
390390
<p><span id="one"></span></p>
391391
<p><span id="two"></span></p>
392392
@@ -425,7 +425,7 @@ We have seen that different HTML elements are drawn differently. Some are displa
425425

426426
The way an `<img>` tag shows an image or an `<a>` tag causes a link to be followed when it is clicked is strongly tied to the element type. But we can change the styling associated with an element, such as the text color or underline. Here is an example that uses the `style` property:
427427

428-
```{lang: "text/html"}
428+
```{lang: "html"}
429429
<p><a href=".">Normal link</a></p>
430430
<p><a href="." style="color: green">Green link</a></p>
431431
```
@@ -446,7 +446,7 @@ A style attribute may contain one or more _((declaration))s_, which are a proper
446446

447447
A lot of aspects of the document can be influenced by styling. For example, the `display` property controls whether an element is displayed as a block or an inline element.
448448

449-
```{lang: "text/html"}
449+
```{lang: "html"}
450450
This text is displayed <strong>inline</strong>,
451451
<strong style="display: block">as a block</strong>, and
452452
<strong style="display: none">not at all</strong>.
@@ -466,7 +466,7 @@ if}}
466466

467467
JavaScript code can directly manipulate the style of an element through the element's `style` property. This property holds an object that has properties for all possible style properties. The values of these properties are strings, which we can write to in order to change a particular aspect of the element's style.
468468

469-
```{lang: "text/html"}
469+
```{lang: "html"}
470470
<p id="para" style="color: purple">
471471
Nice text
472472
</p>
@@ -491,7 +491,7 @@ Some style property names contain hyphens, such as `font-family`. Because such p
491491

492492
The styling system for HTML is called ((CSS)), for _Cascading Style Sheets_. A _style sheet_ is a set of rules for how to style elements in a document. It can be given inside a `<style>` tag.
493493

494-
```{lang: "text/html"}
494+
```{lang: "html"}
495495
<style>
496496
strong {
497497
font-style: italic;
@@ -550,7 +550,7 @@ The main reason I introduced _((selector))_ syntax—the notation used in style
550550

551551
The `querySelectorAll` method, which is defined both on the `document` object and on element nodes, takes a selector string and returns a `NodeList` containing all the elements that it matches.
552552

553-
```{lang: "text/html"}
553+
```{lang: "html"}
554554
<p>And if you go chasing
555555
<span class="animal">rabbits</span></p>
556556
<p>And you know you're going to fall</p>
@@ -593,7 +593,7 @@ The `position` style property influences layout in a powerful way. By default it
593593

594594
We can use this to create an animation. The following document displays a picture of a cat that moves around in an ((ellipse)):
595595

596-
```{lang: "text/html", startCode: true}
596+
```{lang: "html", startCode: true}
597597
<p style="text-align: center">
598598
<img src="img/cat.png" style="position: relative">
599599
</p>
@@ -680,7 +680,7 @@ The way a document is displayed can be influenced by _styling_, both by attachin
680680

681681
An HTML table is built with the following tag structure:
682682

683-
```{lang: "text/html"}
683+
```{lang: "html"}
684684
<table>
685685
<tr>
686686
<th>name</th>
@@ -711,7 +711,7 @@ Once you have this working, right-align cells that contain number values by sett
711711

712712
{{if interactive
713713

714-
```{test: no, lang: "text/html"}
714+
```{test: no, lang: "html"}
715715
<h1>Mountains</h1>
716716
717717
<div id="mountains"></div>
@@ -761,7 +761,7 @@ To find the tag name of an element, use its `nodeName` property. But note that t
761761

762762
{{if interactive
763763

764-
```{lang: "text/html", test: no}
764+
```{lang: "html", test: no}
765765
<h1>Heading with a <span>span</span> element.</h1>
766766
<p>A paragraph with <span>one</span>, <span>two</span>
767767
spans.</p>
@@ -812,7 +812,7 @@ To make positioning multiple objects easier, it is probably a good idea to switc
812812

813813
{{if interactive
814814

815-
```{lang: "text/html", test: no}
815+
```{lang: "html", test: no}
816816
<style>body { min-height: 200px }</style>
817817
<img src="img/cat.png" id="cat" style="position: absolute">
818818
<img src="img/hat.png" id="hat" style="position: absolute">

15_event.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Of course, it has to remember to look at the queue, and to do it often, because
2828

2929
A better mechanism is for the system to actively notify our code when an event occurs. Browsers do this by allowing us to register functions as _handlers_ for specific events.
3030

31-
```{lang: "text/html"}
31+
```{lang: "html"}
3232
<p>Click this document to activate the handler.</p>
3333
<script>
3434
window.addEventListener("click", () => {
@@ -47,7 +47,7 @@ The `window` binding refers to a built-in object provided by the browser. It rep
4747

4848
Each browser event handler is registered in a context. In the previous example we called `addEventListener` on the `window` object to register a handler for the whole window. Such a method can also be found on DOM elements and some other types of objects. Event listeners are called only when the event happens in the context of the object they are registered on.
4949

50-
```{lang: "text/html"}
50+
```{lang: "html"}
5151
<button>Click me</button>
5252
<p>No handler here.</p>
5353
<script>
@@ -72,7 +72,7 @@ But a node can have only one `onclick` attribute, so you can register only one h
7272

7373
The `removeEventListener` method, called with arguments similar to `addEventListener`, removes a handler.
7474

75-
```{lang: "text/html"}
75+
```{lang: "html"}
7676
<button>Act-once button</button>
7777
<script>
7878
let button = document.querySelector("button");
@@ -94,7 +94,7 @@ The function given to `removeEventListener` has to be the same function value th
9494

9595
Though we have ignored it so far, event handler functions are passed an argument: the _((event object))_. This object holds additional information about the event. For example, if we want to know _which_ ((mouse button)) was pressed, we can look at the event object's `button` property.
9696

97-
```{lang: "text/html"}
97+
```{lang: "html"}
9898
<button>Click me any way you want</button>
9999
<script>
100100
let button = document.querySelector("button");
@@ -136,7 +136,7 @@ At any point, an event handler can call the `stopPropagation` method on the even
136136

137137
The following example registers `"mousedown"` handlers on both a button and the paragraph around it. When clicked with the right mouse button, the handler for the button calls `stopPropagation`, which will prevent the handler on the paragraph from running. When the button is clicked with another ((mouse button)), both handlers will run.
138138

139-
```{lang: "text/html"}
139+
```{lang: "html"}
140140
<p>A paragraph with a <button>button</button>.</p>
141141
<script>
142142
let para = document.querySelector("p");
@@ -157,7 +157,7 @@ Most event objects have a `target` property that refers to the node where they o
157157

158158
It is also possible to use the `target` property to cast a wide net for a specific type of event. For example, if you have a node containing a long list of buttons, it may be more convenient to register a single click handler on the outer node and have it use the `target` property to figure out whether a button was clicked, rather than register individual handlers on all of the buttons.
159159

160-
```{lang: "text/html"}
160+
```{lang: "html"}
161161
<button>A</button>
162162
<button>B</button>
163163
<button>C</button>
@@ -184,7 +184,7 @@ For most types of events, the JavaScript event handlers are called _before_ the
184184

185185
This can be used to implement your own ((keyboard)) shortcuts or ((context menu)). It can also be used to obnoxiously interfere with the behavior that users expect. For example, here is a link that cannot be followed:
186186

187-
```{lang: "text/html"}
187+
```{lang: "html"}
188188
<a href="https://developer.mozilla.org/">MDN</a>
189189
<script>
190190
let link = document.querySelector("a");
@@ -207,7 +207,7 @@ Depending on the browser, some events can't be intercepted at all. On Chrome, fo
207207

208208
When a key on the keyboard is pressed, your browser fires a `"keydown"` event. When it is released, you get a `"keyup"` event.
209209

210-
```{lang: "text/html", focus: true}
210+
```{lang: "html", focus: true}
211211
<p>This page turns violet when you hold the V key.</p>
212212
<script>
213213
window.addEventListener("keydown", event => {
@@ -235,7 +235,7 @@ The example looked at the `key` property of the event object to see which key th
235235

236236
Modifier keys such as [shift]{keyname}, [control]{keyname}, [alt]{keyname}, and [meta]{keyname} ([command]{keyname} on Mac) generate key events just like normal keys. But when looking for key combinations, you can also find out whether these keys are held down by looking at the `shiftKey`, `ctrlKey`, `altKey`, and `metaKey` properties of keyboard and mouse events.
237237

238-
```{lang: "text/html", focus: true}
238+
```{lang: "html", focus: true}
239239
<p>Press Control-Space to continue.</p>
240240
<script>
241241
window.addEventListener("keydown", event => {
@@ -282,7 +282,7 @@ To get precise information about the place where a mouse event happened, you can
282282

283283
The following implements a primitive drawing program. Every time you click the document, it adds a dot under your mouse pointer. See [Chapter ?](paint) for a less primitive drawing program.
284284

285-
```{lang: "text/html"}
285+
```{lang: "html"}
286286
<style>
287287
body {
288288
height: 200px;
@@ -316,7 +316,7 @@ Every time the mouse pointer moves, a `"mousemove"` event is fired. This event c
316316

317317
As an example, the following program displays a bar and sets up event handlers so that dragging to the left or right on this bar makes it narrower or wider:
318318

319-
```{lang: "text/html", startCode: true}
319+
```{lang: "html", startCode: true}
320320
<p>Drag the bar to change its width:</p>
321321
<div style="background: orange; width: 60px; height: 20px">
322322
</div>
@@ -382,7 +382,7 @@ Because many touchscreens can detect multiple fingers at the same time, these ev
382382

383383
You could do something like this to show red circles around every touching finger:
384384

385-
```{lang: "text/html"}
385+
```{lang: "html"}
386386
<style>
387387
dot { position: absolute; display: block;
388388
border: 2px solid red; border-radius: 50px;
@@ -420,7 +420,7 @@ Whenever an element is scrolled, a `"scroll"` event is fired on it. This has var
420420

421421
The following example draws a ((progress bar)) above the document and updates it to fill up as you scroll down:
422422

423-
```{lang: "text/html"}
423+
```{lang: "html"}
424424
<style>
425425
#progress {
426426
border-bottom: 2px solid blue;
@@ -469,7 +469,7 @@ Unlike the events discussed earlier, these two events do not propagate. A handle
469469

470470
The following example displays help text for the ((text field)) that currently has focus:
471471

472-
```{lang: "text/html"}
472+
```{lang: "html"}
473473
<p>Name: <input type="text" data-help="Your full name"></p>
474474
<p>Age: <input type="text" data-help="Your age in years"></p>
475475
<p id="help"></p>
@@ -606,7 +606,7 @@ If you do need to do something nontrivial in such a handler, you can use `setTim
606606

607607
In the first example, we want to react when the user has typed something, but we don't want to do it immediately for every input event. When they are ((typing)) quickly, we just want to wait until a pause occurs. Instead of immediately performing an action in the event handler, we set a timeout. We also clear the previous timeout (if any) so that when events occur close together (closer than our timeout delay), the timeout from the previous event will be canceled.
608608

609-
```{lang: "text/html"}
609+
```{lang: "html"}
610610
<textarea>Type something here...</textarea>
611611
<script>
612612
let textarea = document.querySelector("textarea");
@@ -626,7 +626,7 @@ Giving an undefined value to `clearTimeout` or calling it on a timeout that has
626626

627627
We can use a slightly different pattern if we want to space responses so that they're separated by at least a certain length of ((time)) but want to fire them _during_ a series of events, not just afterward. For example, we might want to respond to `"mousemove"` events by showing the current coordinates of the mouse but only every 250 milliseconds.
628628

629-
```{lang: "text/html"}
629+
```{lang: "html"}
630630
<script>
631631
let scheduled = null;
632632
window.addEventListener("mousemove", event => {
@@ -672,7 +672,7 @@ When that works, add a feature where, if you blow up the balloon past a certain
672672

673673
{{if interactive
674674

675-
```{test: no, lang: "text/html", focus: yes}
675+
```{test: no, lang: "html", focus: yes}
676676
<p>🎈</p>
677677
678678
<script>
@@ -714,7 +714,7 @@ There are various possible approaches here. You can make your solution as simple
714714

715715
{{if interactive
716716

717-
```{lang: "text/html", test: no}
717+
```{lang: "html", test: no}
718718
<style>
719719
.trail { /* className for the trail elements */
720720
position: absolute;
@@ -764,7 +764,7 @@ When that works, extend it to style the button for the currently selected tab di
764764

765765
{{if interactive
766766

767-
```{lang: "text/html", test: no}
767+
```{lang: "html", test: no}
768768
<tab-panel>
769769
<div data-tabname="one">Tab one</div>
770770
<div data-tabname="two">Tab two</div>

0 commit comments

Comments
 (0)