Skip to content

Commit 902a7e7

Browse files
committed
minor
1 parent d63c27b commit 902a7e7

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

2-ui/1-document/05-basic-dom-node-properties/4-where-document-in-hierarchy/solution.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ alert(HTMLDocument.prototype.__proto__.constructor.name); // Document
3535
alert(HTMLDocument.prototype.__proto__.__proto__.constructor.name); // Node
3636
```
3737

38+
That's the hierarchy.
39+
3840
We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally.

2-ui/1-document/05-basic-dom-node-properties/article.md

+34-28
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
Let's get a more in-depth look at DOM nodes.
44

5-
In this chapter we'll see more into what they are and their most used properties.
5+
In this chapter we'll see more into what they are and learn their most used properties.
66

77
## DOM node classes
88

9-
DOM nodes have different properties depending on their class. For instance, an element node corresponding to tag `<a>` has link-related properties, and the one corresponding to `<input>` has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
9+
Different DOM nodes may have different properties. For instance, an element node corresponding to tag `<a>` has link-related properties, and the one corresponding to `<input>` has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
1010

1111
Each DOM node belongs to the corresponding built-in class.
1212

@@ -21,22 +21,24 @@ The classes are:
2121
- [EventTarget](https://dom.spec.whatwg.org/#eventtarget) -- is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
2222
- [Node](http://dom.spec.whatwg.org/#interface-node) -- is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
2323
- [Element](http://dom.spec.whatwg.org/#interface-element) -- is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. A browser supports not only HTML, but also XML and SVG. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
24-
- [HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by various HTML elements:
24+
- [HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) -- is finally the basic class for all HTML elements. It is inherited by concrete HTML elements:
2525
- [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) -- the class for `<input>` elements,
2626
- [HTMLBodyElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlbodyelement) -- the class for `<body>` elements,
27-
- [HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements
27+
- [HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements,
2828
- ...and so on, each tag has its own class that may provide specific properties and methods.
2929

3030
So, the full set of properties and methods of a given node comes as the result of the inheritance.
3131

32-
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class. It gets properties and methods as a superposition of:
32+
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class.
3333

34-
- `HTMLInputElement` -- this class provides input-specific properties, and inherits from...
35-
- `HTMLElement` -- it provides common HTML element methods (and getters/setters) and inherits from...
36-
- `Element` -- provides generic element methods and inherits from...
37-
- `Node` -- provides common DOM node properties and inherits from...
34+
It gets properties and methods as a superposition of (listed in inheritance order):
35+
36+
- `HTMLInputElement` -- this class provides input-specific properties,
37+
- `HTMLElement` -- it provides common HTML element methods (and getters/setters),
38+
- `Element` -- provides generic element methods,
39+
- `Node` -- provides common DOM node properties,.
3840
- `EventTarget` -- gives the support for events (to be covered),
39-
- ...and finally it inherits from `Object`, so "pure object" methods like `hasOwnProperty` are also available.
41+
- ...and finally it inherits from `Object`, so "plain object" methods like `hasOwnProperty` are also available.
4042

4143
To see the DOM node class name, we can recall that an object usually has the `constructor` property. It references the class constructor, and `constructor.name` is its name:
4244

@@ -91,7 +93,7 @@ interface HTMLInputElement: HTMLElement {
9193
// here go properties and methods of <input> elements
9294
9395
*!*
94-
// "DOMString" means that the value of these properties are strings
96+
// "DOMString" means that the value of a property is a string
9597
*/!*
9698
attribute DOMString accept;
9799
attribute DOMString alt;
@@ -110,13 +112,11 @@ interface HTMLInputElement: HTMLElement {
110112
...
111113
}
112114
```
113-
114-
Other classes are somewhat similar.
115115
````
116116
117117
## The "nodeType" property
118118
119-
The `nodeType` property provides an old-fashioned way to get the "type" of a DOM node.
119+
The `nodeType` property provides one more, "old-fashioned" way to get the "type" of a DOM node.
120120
121121
It has a numeric value:
122122
- `elem.nodeType == 1` for element nodes,
@@ -185,8 +185,7 @@ For instance, let's compare `tagName` and `nodeName` for the `document` and a co
185185
</body>
186186
```
187187
188-
If we only deal with elements, then `tagName` is the only thing we should use.
189-
188+
If we only deal with elements, then we can use both `tagName` and `nodeName` - there's no difference.
190189
191190
```smart header="The tag name is always uppercase except XHTML"
192191
The browser has two modes of processing documents: HTML and XML. Usually the HTML-mode is used for webpages. XML-mode is enabled when the browser receives an XML-document with the header: `Content-Type: application/xml+xhtml`.
@@ -300,30 +299,35 @@ Consider the example:
300299
*!*
301300
// replace div.outerHTML with <p>...</p>
302301
*/!*
303-
div.outerHTML = '<p>A new element!</p>'; // (*)
302+
div.outerHTML = '<p>A new element</p>'; // (*)
304303
305304
*!*
306305
// Wow! The div is still the same!
307306
*/!*
308-
alert(div.outerHTML); // <div>Hello, world!</div>
307+
alert(div.outerHTML); // <div>Hello, world!</div> (**)
309308
</script>
310309
```
311310
312-
In the line `(*)` we take the full HTML of `<div>...</div>` and replace it by `<p>...</p>`. In the outer document we can see the new content instead of the `<div>`. But the old `div` variable is still the same.
311+
Looks really odd, right?
313312
314-
The `outerHTML` assignment does not modify the DOM element, but extracts it from the outer context and inserts a new piece of HTML instead of it.
313+
In the line `(*)` we replaced `div` with `<p>A new element</p>`. In the outer document we can see the new content instead of the `<div>`. But, as we can see in line `(**)`, the old `div` variable is still the same!
315314
316-
Novice developers sometimes make an error here: they modify `div.outerHTML` and then continue to work with `div` as if it had the new content in it.
315+
The `outerHTML` assignment does not modify the DOM element, but removes it from the outer context and inserts a new piece of HTML instead of it.
317316
318-
That's possible with `innerHTML`, but not with `outerHTML`.
317+
So what happened in `div.outerHTML=...` is:
318+
- `div` was removed from the document.
319+
- Another HTML `<p>A new element</p>` was inserted instead.
320+
- `div` still has the old value. The new HTML wasn't saved to any variable.
319321
320-
We can write to `outerHTML`, but should keep in mind that it doesn't change the element we're writing to. It creates the new content on its place instead. We can get a reference to new elements by querying DOM.
322+
It's so easy to make an error here: modify `div.outerHTML` and then continue to work with `div` as if it had the new content in it. But it doesn't. Such thing is correct for `innerHTML`, but not for `outerHTML`.
323+
324+
We can write to `elem.outerHTML`, but should keep in mind that it doesn't change the element we're writing to. It creates the new HTML on its place instead. We can get references to new elements by querying DOM.
321325
322326
## nodeValue/data: text node content
323327
324328
The `innerHTML` property is only valid for element nodes.
325329
326-
Other node types have their counterpart: `nodeValue` and `data` properties. These two are almost the same for practical use, there are only minor specification differences. So we'll use `data`, because it's shorter.
330+
Other node types, such as text nodes, have their counterpart: `nodeValue` and `data` properties. These two are almost the same for practical use, there are only minor specification differences. So we'll use `data`, because it's shorter.
327331
328332
An example of reading the content of a text node and a comment:
329333
@@ -345,15 +349,17 @@ An example of reading the content of a text node and a comment:
345349
</body>
346350
```
347351
348-
For text nodes we can imagine a reason to read or modify them, but why comments? Usually, they are not interesting at all, but sometimes developers embed information or template instructions into HTML in them, like this:
352+
For text nodes we can imagine a reason to read or modify them, but why comments?
353+
354+
Sometimes developers embed information or template instructions into HTML in them, like this:
349355
350356
```html
351357
<!-- if isAdmin -->
352358
<div>Welcome, Admin!</div>
353359
<!-- /if -->
354360
```
355361
356-
...Then JavaScript can read it and process embedded instructions.
362+
...Then JavaScript can read it from `data` property and process embedded instructions.
357363
358364
## textContent: pure text
359365
@@ -436,7 +442,7 @@ Here's a blinking element:
436442
437443
## More properties
438444
439-
DOM elements also have additional properties, many of them provided by the class:
445+
DOM elements also have additional properties, in particular those that depend on the class:
440446
441447
- `value` -- the value for `<input>`, `<select>` and `<textarea>` (`HTMLInputElement`, `HTMLSelectElement`...).
442448
- `href` -- the "href" for `<a href="...">` (`HTMLAnchorElement`).
@@ -457,7 +463,7 @@ For instance:
457463
458464
Most standard HTML attributes have the corresponding DOM property, and we can access it like that.
459465
460-
If we want to know the full list of supported properties for a given class, we can find them in the specification. For instance, HTMLInputElement is documented at <https://html.spec.whatwg.org/#htmlinputelement>.
466+
If we want to know the full list of supported properties for a given class, we can find them in the specification. For instance, `HTMLInputElement` is documented at <https://html.spec.whatwg.org/#htmlinputelement>.
461467
462468
Or if we'd like to get them fast or are interested in a concrete browser specification -- we can always output the element using `console.dir(elem)` and read the properties. Or explore "DOM properties" in the Elements tab of the browser developer tools.
463469

0 commit comments

Comments
 (0)