Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
There's a catch here.
Há uma pegadinha aqui.

At the time of `<script>` execution the last DOM node is exactly `<script>`, because the browser did not process the rest of the page yet.
No momento da execução do `<script>`, o último nó do DOM é exatamente o `<script>`, porque o navegador ainda não processou o resto da página.

So the result is `1` (element node).
Então o resultado é `1` (nó do tipo elemento).

```html run height=60
<html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What's in the nodeType?
# O que há no nodeType?

What does the script show?
O que o script mostra?

```html
<html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
Let's make a loop over `<li>`:
Vamos fazer um loop sobre os `<li>`:

```js
for (let li of document.querySelectorAll('li')) {
...
}
```

In the loop we need to get the text inside every `li`.
No loop, precisamos obter o texto dentro de cada `li`.

We can read the text from the first child node of `li`, that is the text node:
Podemos ler o texto a partir do primeiro nó filho do `li`, que é o nó de texto:

```js
for (let li of document.querySelectorAll('li')) {
let title = li.firstChild.data;

// title is the text in <li> before any other nodes
// title é o texto no <li> antes de quaisquer outros nós
}
```

Then we can get the number of descendants as `li.getElementsByTagName('li').length`.
Depois podemos obter o número de descendentes como `li.getElementsByTagName('li').length`.
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@
<body>

<ul>
<li>Animals
<li>Animais
<ul>
<li>Mammals
<li>Mamíferos
<ul>
<li>Cows</li>
<li>Donkeys</li>
<li>Dogs</li>
<li>Tigers</li>
<li>Vacas</li>
<li>Burros</li>
<li>Cachorros</li>
<li>Tigres</li>
</ul>
</li>
<li>Other
<li>Outros
<ul>
<li>Snakes</li>
<li>Birds</li>
<li>Lizards</li>
<li>Cobras</li>
<li>Pássaros</li>
<li>Lagartos</li>
</ul>
</li>
</ul>
</li>
<li>Fishes
<li>Peixes
<ul>
<li>Aquarium
<li>Aquário
<ul>
<li>Guppy</li>
<li>Angelfish</li>
<li>Peixe-anjo</li>
</ul>
</li>
<li>Sea
<li>Mar
<ul>
<li>Sea trout</li>
<li>Truta marinha</li>
</ul>
</li>
</ul>
Expand All @@ -41,12 +41,12 @@

<script>
for (let li of document.querySelectorAll('li')) {
// get the title from the text node
// obtém o título a partir do nó de texto
let title = li.firstChild.data;

title = title.trim(); // remove extra spaces from ends
title = title.trim(); // remove espaços extras das pontas

// get the descendants count
// obtém a contagem de descendentes
let count = li.getElementsByTagName('li').length;

alert(title + ': ' + count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
<body>

<ul>
<li>Animals
<li>Animais
<ul>
<li>Mammals
<li>Mamíferos
<ul>
<li>Cows</li>
<li>Donkeys</li>
<li>Dogs</li>
<li>Tigers</li>
<li>Vacas</li>
<li>Burros</li>
<li>Cachorros</li>
<li>Tigres</li>
</ul>
</li>
<li>Other
<li>Outros
<ul>
<li>Snakes</li>
<li>Birds</li>
<li>Lizards</li>
<li>Cobras</li>
<li>Pássaros</li>
<li>Lagartos</li>
</ul>
</li>
</ul>
</li>
<li>Fishes
<li>Peixes
<ul>
<li>Aquarium
<li>Aquário
<ul>
<li>Guppy</li>
<li>Angelfish</li>
<li>Peixe-anjo</li>
</ul>
</li>
<li>Sea
<li>Mar
<ul>
<li>Sea trout</li>
<li>Truta marinha</li>
</ul>
</li>
</ul>
</li>
</ul>

<script>
// ... your code...
// ... seu código...
</script>

</body>
Expand Down
10 changes: 5 additions & 5 deletions 2-ui/1-document/05-basic-dom-node-properties/2-tree-info/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Count descendants
# Contar descendentes

There's a tree structured as nested `ul/li`.
Há uma árvore estruturada como `ul/li` aninhados.

Write the code that for each `<li>` shows:
Escreva o código que, para cada `<li>`, mostra:

1. What's the text inside it (without the subtree)
2. The number of nested `<li>` -- all descendants, including the deeply nested ones.
1. Qual é o texto dentro dele (sem a subárvore)
2. O número de `<li>` aninhados -- todos os descendentes, incluindo os mais profundamente aninhados.

[demo src="solution"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: **`BODY`**.
A resposta: **`BODY`**.

```html run
<script>
Expand All @@ -10,8 +10,8 @@ The answer: **`BODY`**.
</script>
```

What's going on step by step:
O que está acontecendo, passo a passo:

1. The content of `<body>` is replaced with the comment. The comment is `<!--BODY-->`, because `body.tagName == "BODY"`. As we remember, `tagName` is always uppercase in HTML.
2. The comment is now the only child node, so we get it in `body.firstChild`.
3. The `data` property of the comment is its contents (inside `<!--...-->`): `"BODY"`.
1. O conteúdo de `<body>` é substituído pelo comentário. O comentário é `<!--BODY-->`, porque `body.tagName == "BODY"`. Como lembramos, `tagName` é sempre em maiúsculas em HTML.
2. O comentário agora é o único nó filho, então o obtemos em `body.firstChild`.
3. A propriedade `data` do comentário é o seu conteúdo (dentro de `<!--...-->`): `"BODY"`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ importance: 3

---

# Tag in comment
# Tag em comentário

What does this code show?
O que este código mostra?

```html
<script>
let body = document.body;

body.innerHTML = "<!--" + body.tagName + "-->";

alert( body.firstChild.data ); // what's here?
alert( body.firstChild.data ); // o que há aqui?
</script>
```
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@

We can see which class it belongs by outputting it, like:
Podemos ver a qual classe ele pertence exibindo-o, assim:

```js run
alert(document); // [object HTMLDocument]
```

Or:
Ou:

```js run
alert(document.constructor.name); // HTMLDocument
```

So, `document` is an instance of `HTMLDocument` class.
Então, `document` é uma instância da classe `HTMLDocument`.

What's its place in the hierarchy?
Qual é o seu lugar na hierarquia?

Yeah, we could browse the specification, but it would be faster to figure out manually.
Sim, poderíamos navegar pela especificação, mas seria mais rápido descobrir manualmente.

Let's traverse the prototype chain via `__proto__`.
Vamos percorrer a cadeia de protótipos via `__proto__`.

As we know, methods of a class are in the `prototype` of the constructor. For instance, `HTMLDocument.prototype` has methods for documents.
Como sabemos, os métodos de uma classe estão no `prototype` do construtor. Por exemplo, `HTMLDocument.prototype` tem métodos para documentos.

Also, there's a reference to the constructor function inside the `prototype`:
Além disso, há uma referência à função construtora dentro do `prototype`:

```js run
alert(HTMLDocument.prototype.constructor === HTMLDocument); // true
```

To get a name of the class as a string, we can use `constructor.name`. Let's do it for the whole `document` prototype chain, till class `Node`:
Para obter o nome da classe como uma string, podemos usar `constructor.name`. Vamos fazer isso para toda a cadeia de protótipos do `document`, até a classe `Node`:

```js run
alert(HTMLDocument.prototype.constructor.name); // HTMLDocument
alert(HTMLDocument.prototype.__proto__.constructor.name); // Document
alert(HTMLDocument.prototype.__proto__.__proto__.constructor.name); // Node
```

That's the hierarchy.
Essa é a hierarquia.

We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally.
Também poderíamos examinar o objeto usando `console.dir(document)` e ver esses nomes abrindo `__proto__`. O console obtém esses nomes internamente a partir de `constructor`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ importance: 4

---

# Where's the "document" in the hierarchy?
# Qual é o lugar do "document" na hierarquia?

Which class does the `document` belong to?
A qual classe o `document` pertence?

What's its place in the DOM hierarchy?
Qual é o seu lugar na hierarquia do DOM?

Does it inherit from `Node` or `Element`, or maybe `HTMLElement`?
Ele herda de `Node` ou `Element`, ou talvez `HTMLElement`?
Loading