Skip to content

Node properties: type, tag and contents #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2020
Merged
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.
هناك صيد هنا.

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.
في وقت تنفيذ `<script>` ، كانت العقدة DOM الأخيرة هي `<script>` بالضبط ، لأن المستعرض لم يقم بمعالجة باقي الصفحة بعد.

So the result is `1` (element node).
لذلك تكون النتيجة `1` (عقدة العنصر).

```html run height=60
<html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 5
درجة الأهمية: 5

---

# What's in the nodeType?
# ماذا يوجد في العقدة؟

What does the script show?
ماذا يظهر النص؟

```html
<html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
Let's make a loop over `<li>`:
لنقم بعمل iteration علي `<li>`:

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

In the loop we need to get the text inside every `li`.

We can read the text from the first child node of `li`, that is the text node:
في الحلقة ، نحتاج إلى إدخال النص داخل كل "li".

يمكننا قراءة النص من العقدة الفرعية الأولى لـ `li` ، وهي العقدة النصية:
```js
for (let li of document.querySelectorAll('li')) {
let title = li.firstChild.data;
Expand All @@ -18,4 +17,5 @@ for (let li of document.querySelectorAll('li')) {
}
```

Then we can get the number of descendants as `li.getElementsByTagName('li').length`.
ثم يمكننا الحصول على عدد الchildren items
`li.getElementsByTagName('li').length`.
13 changes: 6 additions & 7 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
@@ -1,14 +1,13 @@
importance: 5
الأهمية: 5

---

# Count descendants
# عد الأحفاد

There's a tree structured as nested `ul/li`.
هناك شجرة منظمة على أنها متداخلة `ul / li`.

Write the code that for each `<li>` shows:

1. What's the text inside it (without the subtree)
2. The number of nested `<li>` -- all descendants, including the deeply nested ones.
اكتب الرمز الذي يظهر لكل `<li>` ما يلي:

1. ما هو النص الموجود بداخله (بدون الشجرة الفرعية)
2. عدد "<li>" المتداخلة - جميع الأحفاد ، بما في ذلك المتداخلون بعمق.
[demo src="solution"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: **`BODY`**.
الإجابة: **`BODY`**.

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

What's going on step by step:
ما يحدث خطوة بخطوة:

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. يتم استبدال محتوى "<body>" بالتعليق. التعليق هو `<! - BODY ->` ، لأن `body.tagName ==" BODY "`. كما نتذكر ، فإن `tagName` دائمًا ما تكون أحرفًا كبيرة بتنسيق HTML.
2. التعليق هو الآن العقدة الفرعية الوحيدة ، لذلك نحصل عليه في `body.firstChild`.
3. خاصية "البيانات" للتعليق هي محتوياته (داخل `<! --...-->`): "" BODY "`.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 3
درجة الأهمية: 3

---

# Tag in comment
# تاج في تعليق

What does this code show?
ماذا يبين هذا الكود؟

```html
<script>
Expand Down
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:
Wيمكننا معرفة أي فئة ينتمي إليها بإخراجها ، مثل:

```js run
alert(document); // [object HTMLDocument]
```
تشغيل شبيبة
تنبيه (وثيقة) ؛ // [كائن HTMLDocument]
``

Or:
أو:

```js run
alert(document.constructor.name); // HTMLDocument
```
تشغيل شبيبة
تنبيه (document.constructor.name) ؛ // HTMLDocument
``

So, `document` is an instance of `HTMLDocument` class.
لذا ، `المستند` هو مثيل لفئة` HTMLDocument`.

What's its place in the hierarchy?
ما مكانها في التسلسل الهرمي؟

Yeah, we could browse the specification, but it would be faster to figure out manually.
نعم ، يمكننا تصفح المواصفات ، ولكن سيكون من الأسرع معرفة ذلك يدويًا.

Let's traverse the prototype chain via `__proto__`.
دعنا نجتاز سلسلة النموذج عبر "__proto__".

As we know, methods of a class are in the `prototype` of the constructor. For instance, `HTMLDocument.prototype` has methods for documents.
كما نعلم ، فإن طرق الصف موجودة في "النموذج الأولي" للمنشئ. على سبيل المثال ، يحتوي "HTMLDocument.prototype" على طرق للمستندات.

Also, there's a reference to the constructor function inside the `prototype`:
أيضًا ، هناك إشارة إلى وظيفة المُنشئ داخل `النموذج الأولي`:

```js run
alert(HTMLDocument.prototype.constructor === HTMLDocument); // true
```
تشغيل شبيبة
تنبيه (HTMLDocument.prototype.constructor === HTMLDocument) ؛ // صحيح
``

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`:
للحصول على اسم الفئة كسلسلة ، يمكننا استخدام `buildor.name`. لنفعل ذلك من أجل سلسلة النموذج "المستند" بالكامل ، حتى الفئة "العقدة":

```js run
alert(HTMLDocument.prototype.constructor.name); // HTMLDocument
alert(HTMLDocument.prototype.__proto__.constructor.name); // Document
alert(HTMLDocument.prototype.__proto__.__proto__.constructor.name); // Node
```
تشغيل شبيبة
تنبيه (HTMLDocument.prototype.constructor.name) ؛ // HTMLDocument
تنبيه (HTMLDocument.prototype .__ proto__.constructor.name) ؛ // المستند
تنبيه (HTMLDocument.prototype .__ proto__.__proto__.constructor.name) ؛ // العقدة
``

That's the hierarchy.
هذا هو التسلسل الهرمي.

We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally.
يمكننا أيضًا فحص الكائن باستخدام `console.dir (مستند)` ورؤية هذه الأسماء من خلال فتح "__proto__". تأخذهم وحدة التحكم من `المنشئ` داخليًا.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
importance: 4
الأهمية: 4

---

# Where's the "document" in the hierarchy?
# أين "الوثيقة" في التسلسل الهرمي؟

Which class does the `document` belong to?
إلى أي فئة ينتمي "المستند"؟

What's its place in the DOM hierarchy?
ما مكانها في التسلسل الهرمي لـ DOM؟

Does it inherit from `Node` or `Element`, or maybe `HTMLElement`?
هل ترث من "العقدة" أو "العنصر" ، أو ربما "HTMLElement
Loading