You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/99-ui-misc/01-mutation-observer/article.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
2
2
# Mutation observer
3
3
4
-
`MutationObserver` is a built-in object that observes a DOM element and fires a callback in case of changes.
4
+
`MutationObserver` is a built-in object that observes a DOM element and fires a callback when it detects a change.
5
5
6
6
We'll first take a look at the syntax, and then explore a real-world use case, to see where such thing may be useful.
7
7
@@ -128,16 +128,16 @@ Such snippet in an HTML markup looks like this:
128
128
...
129
129
```
130
130
131
-
Also we'll use a JavaScript highlighting library on our site, e.g.[Prism.js](https://prismjs.com/). A call to `Prism.highlightElem(pre)` examines the contents of such `pre` elements and adds into them special tags and styles for colored syntax highlighting, similar to what you see in examples here, at this page.
131
+
For better readability and at the same time, to beautify it, we'll be using a JavaScript syntax highlighting library on our site, like[Prism.js](https://prismjs.com/). To get syntax highlighting for above snippet in Prism, `Prism.highlightElem(pre)`is called, which examines the contents of such `pre` elements and adds special tags and styles for colored syntax highlighting into those elements, similar to what you see in examples here, on this page.
132
132
133
-
When exactly to run that highlighting method? We can do it on `DOMContentLoaded` event, or at the bottom of the page. At that moment we have our DOM ready, can search for elements `pre[class*="language"]` and call `Prism.highlightElem` on them:
133
+
When exactly should we run that highlighting method? Well, we can do it on `DOMContentLoaded` event, or put the script at the bottom of the page. The moment our DOM is ready, we can search for elements `pre[class*="language"]` and call `Prism.highlightElem` on them:
Everything's simple so far, right? There are `<pre>` code snippets in HTML, we highlight them.
140
+
Everything's simple so far, right? Where there are `<pre>` code snippets in HTML, we highlight them.
141
141
142
142
Now let's go on. Let's say we're going to dynamically fetch materials from a server. We'll study methods for that [later in the tutorial](info:fetch). For now it only matters that we fetch an HTML article from a webserver and display it on demand:
...But imagine, we have many places in the code where we load contents: articles, quizzes, forum posts. Do we need to put the highlighting call everywhere? That's not very convenient, and also easy to forget.
165
+
...But, imagine if we have many places in the code where we load our content - articles, quizzes, forum posts, etc. Do we need to put the highlighting call everywhere? That's not very convenient, right? It is also easy to forget where we put them and therefore, making it harder if we want to make some changes to the code later.
166
166
167
-
And what if the content is loaded by a third-party module? E.g. we have a forum written by someone else, that loads contents dynamically, and we'd like to add syntax highlighting to it. No one likes to patch third-party scripts.
167
+
And what if the content is loaded by a third-party module? For example, we have a forum written by someone else, that loads content dynamically, and we'd like to add syntax highlighting to it. No one likes patching third-party scripts.
168
168
169
169
Luckily, there's another option.
170
170
171
-
We can use `MutationObserver` to automatically detect when code snippets are inserted in the page and highlight them.
171
+
We can use `MutationObserver` to automatically detect when code snippets are inserted into the page and highlight them.
172
172
173
173
So we'll handle the highlighting functionality in one place, relieving us from the need to integrate it.
174
174
@@ -236,9 +236,9 @@ There's a method to stop observing the node:
236
236
237
237
-`observer.disconnect()` -- stops the observation.
238
238
239
-
When we stop the observing, it might be possible that some changes were not processed by the observer yet.
239
+
When we stop the observing, it might be possible that some changes were not yet processed by the observer. In such cases, we use
240
240
241
-
-`observer.takeRecords()` -- gets a list of unprocessed mutation records, those that happened, but the callback did not handle them.
241
+
-`observer.takeRecords()` -- gets a list of unprocessed mutation records - those that happened, but the callback has not handled them.
242
242
243
243
These methods can be used together, like this:
244
244
@@ -252,14 +252,14 @@ let mutationRecords = observer.takeRecords();
252
252
```
253
253
254
254
```smart header="Garbage collection interaction"
255
-
Observers use weak references to nodes internally. That is: if a node is removed from DOM, and becomes unreachable, then it becomes garbage collected.
255
+
Observers use weak references to nodes internally. That is, if a node is removed from the DOM, and becomes unreachable, then it can be garbage collected.
256
256
257
257
The mere fact that a DOM node is observed doesn't prevent the garbage collection.
258
258
```
259
259
260
260
## Summary
261
261
262
-
`MutationObserver` can react on changes in DOM: attributes, added/removed elements, text content.
262
+
`MutationObserver` can react to changes in DOM - attributes, text content and adding/removing elements.
263
263
264
264
We can use it to track changes introduced by other parts of our code, as well as to integrate with third-party scripts.
0 commit comments