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: content/terms/explanation/filters.md
+20-15Lines changed: 20 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,15 +7,19 @@ weight: 3
7
7
8
8
Filters solve [noise]({{< relref "/terms/guideline/declaring/#usual-noise" >}}) issues in terms versions that cannot be addressed with direct selection or removal of content using CSS selectors or range selectors.
9
9
10
-
## Why filters are needed
10
+
## When filters are needed
11
11
12
-
Web pages often contain dynamically generated content or content that cannot be targeted with CSS selectors that creates noise in the recorded version, for example:
12
+
Filters are necessary when standard CSS selectors and range selectors cannot adequately address noise in terms versions. They provide a solution for complex content manipulation that goes beyond simple selection and removal.
13
13
14
-
- Tracking parameters in URLs, for example `utm_source`, `utm_medium`, …
15
-
- Content that are date based and can change between visits, for example "Updated X days ago" can be converted to a "Last updated on YYYY-MM-DD".
16
-
- Dynamic elements with changing classes or IDs
14
+
Use filters when:
17
15
18
-
Without filters, this dynamic content creates changes that are not meaningful to the terms.
16
+
-**CSS selectors are insufficient**: When noise appears within content that can't be targeted with selectors or [range selectors]({{< relref "terms/explanation/range-selectors" >}}) with the [`select`]({{< relref "terms/reference/declaration/#ref-select" >}}) and [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) properties.
17
+
-**Content is dynamically generated**: When elements change on each page load, such as:
18
+
- Tracking parameters in URLs (e.g., `utm_source`, `utm_medium`)
19
+
- Dynamic elements with changing classes or IDs
20
+
-**Complex tasks are needed**: When content transformation is needed such as:
21
+
- Converting images to base64 to store them in the terms version.
22
+
- Converting date-based content to a more stable format (e.g., "Updated X days ago" to "Last updated on YYYY-MM-DD")
19
23
20
24
## How filters work
21
25
@@ -25,15 +29,16 @@ Filters are JavaScript functions that receive a JSDOM document instance and can
25
29
26
30
When designing filters, follow these core principles:
27
31
28
-
-**Be specific**: Target only the noise you want to remove. Avoid broad selectors that might accidentally remove important content.
29
-
-**Be safe**: Ensure your filter doesn't accidentally remove important content. Always check that the generated version still contains the whole terms content.
30
-
-**Be idempotent**: Your filter should produce the same result even if run multiple times on its own output. This ensures consistency and prevents unexpected behavior.
31
-
-**Be efficient**: Use efficient DOM queries and avoid unnecessary operations. Process only the elements you need to modify.
32
+
-**Be specific**: Target only the noise you want to remove. Avoid broad selectors that might accidentally remove important content.
32
33
33
-
## When to use filters
34
+
> For example, if your filter converts relative dates to absolute dates, use `.metadata time` not `time` which might also affect important effective dates within the terms content.
34
35
35
-
Use filters when:
36
+
-**Be idempotent**: Filters should produce the same result even if run multiple times on their own output. This ensures consistency and prevents unexpected behavior.
36
37
37
-
-**CSS selectors are insufficient**: When noise appears within content that can't be targeted with selectors or [range selectors]({{< relref "terms/explanation/range-selectors" >}}) with the [`select`]({{< relref "terms/reference/declaration/#ref-select" >}}) and [`remove`]({{< relref "terms/reference/declaration/#ref-remove" >}}) properties.
38
-
-**Meaningful content is dynamic**: When elements change on each page load, for example "Updated X days ago" can be converted to a "Last updated on YYYY-MM-DD".
39
-
-**Patterns are complex**: When simple removal isn't possible, for example removing all the tracking parameters in URLs.
38
+
> For example, if your filter adds section numbers like "1." to headings, check if numbers already exist to prevent "1. Privacy Policy" from becoming "1. 1. Privacy Policy" on repeated runs.
39
+
40
+
-**Be efficient**: Use efficient DOM queries and avoid unnecessary operations. Process only the elements you need to modify.
41
+
42
+
> For example, if your filter updates timestamp elements with a specific class, use `document.querySelector('.timestamp')` instead of `document.querySelectorAll('*')` followed by filtering for timestamp elements.
43
+
44
+
-**Be safe**: Filters should not accidentally remove important content. The generated version should always be checked after adding a filter to ensure it still contains the whole terms content.
export [async] function filterName(document, parameters, [documentDeclaration])
16
24
```
17
25
18
-
Each filter is exposed as a named function export that takes a `document` parameter and behaves like the `document` object in a browser DOM.
19
-
> The `document` parameter is actually a [JSDOM](https://github.com/jsdom/jsdom) document instance.
26
+
Each filter is exposed as a named function export that takes a `document` parameter and behaves like the `document` object in a browser DOM. The `document` parameter is actually a [JSDOM](https://github.com/jsdom/jsdom) document instance.
20
27
21
28
These functions can be `async`, but they will still run sequentially.
22
29
@@ -53,7 +60,7 @@ Can be used as follows in the declaration:
0 commit comments