Skip to content

Commit 30386fc

Browse files
authored
Merge pull request #2095 from peachesontour/patch-11
Typos
2 parents 6701d9b + cd3fb86 commit 30386fc

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

2-ui/99-ui-misc/01-mutation-observer/article.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Mutation observer
33

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.
55

66
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.
77

@@ -128,16 +128,16 @@ Such snippet in an HTML markup looks like this:
128128
...
129129
```
130130

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.
132132

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:
134134

135135
```js
136136
// highlight all code snippets on the page
137137
document.querySelectorAll('pre[class*="language"]').forEach(Prism.highlightElem);
138138
```
139139

140-
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.
141141

142142
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:
143143

@@ -162,13 +162,13 @@ snippets.forEach(Prism.highlightElem);
162162
*/!*
163163
```
164164

165-
...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.
166166

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.
168168

169169
Luckily, there's another option.
170170

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.
172172

173173
So we'll handle the highlighting functionality in one place, relieving us from the need to integrate it.
174174

@@ -236,9 +236,9 @@ There's a method to stop observing the node:
236236

237237
- `observer.disconnect()` -- stops the observation.
238238

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
240240

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.
242242

243243
These methods can be used together, like this:
244244

@@ -252,14 +252,14 @@ let mutationRecords = observer.takeRecords();
252252
```
253253

254254
```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.
256256
257257
The mere fact that a DOM node is observed doesn't prevent the garbage collection.
258258
```
259259

260260
## Summary
261261

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.
263263

264264
We can use it to track changes introduced by other parts of our code, as well as to integrate with third-party scripts.
265265

0 commit comments

Comments
 (0)