Cosmetic rules
This document describes the syntax for the cosmetic rules Zen supports. Cosmetic rules hide or restyle elements on web pages, or add CSS to pages.
Top-level syntax
A rule consists of a domain list (optional), a marker, and a body:
<domain-list><marker><body>
! for example:
example.com,example.org##.ad-bannerThe marker sets the rule type and the format of the body:
| Marker | Rule type | Body |
|---|---|---|
## | Element hiding | CSS selector |
#?# | Extended CSS | CSS selector that may contain extended pseudo-classes |
#$# | CSS injection | CSS rules |
Adding @ after the first # makes a rule an exception rule: #@#, #@?#, #@$#.
The first # in a line always starts the marker. The body may contain # and text that looks like a marker: example.com##a[title="##"] is an element hiding rule with the selector a[title="##"].
Element hiding rules
An element hiding rule hides the elements that match a CSS selector:
example.com##.sidebar-ad
##div[id^="google_ads_"]Matching elements are hidden with display: none !important. The selectors that apply to a page are combined into CSS rules of up to 100 selectors each. If one selector in a CSS rule is invalid, the browser ignores the whole CSS rule, including the valid selectors in it.
A selector is rejected if it contains any of the following:
</style>- unbalanced quotes, parentheses, or square brackets
- a trailing backslash
/*,*/,{,},;, or@outside quotes
For example, example.com##div[style*="display: block;"] is accepted. example.com##.ad { color: red }, the uBlock Origin syntax for styling elements, is rejected. To style elements, see :style().
If the selector contains an extended pseudo-class other than :has(), :is(), or :not(), the rule is processed as an extended CSS rule. :has(), :is(), and :not() in element hiding rules are evaluated by the browser.
Extended CSS rules
An extended CSS rule hides or restyles elements using a selector that may contain extended pseudo-classes. Most of them are pseudo-classes that CSS does not have, such as one that matches elements by their text. The others, :has(), :is(), and :not(), exist in CSS, but in extended CSS rules they may contain selectors with extended pseudo-classes.
For example:
example.com#?#div:contains(Sponsored)
example.com##.post:has(> .label:contains(Ad))
example.com#?#.banner:style(height: 0 !important)An extended CSS rule must have a non-empty domain list. A domain list of * matches every domain.
Extended CSS rules are evaluated in the page by Zen’s own JavaScript engine. The engine evaluates the rules when the page loads and again when the page changes: when nodes are added or removed, or when the id or class attribute of any element on the page changes. It does so at most once every 100 milliseconds. The effect of a rule is removed from elements that stop matching it.
If a rule cannot be parsed or uses an unsupported pseudo-class, it has no effect.
The extended CSS engine logs messages to the browser console with the prefix Zen/extended-css/engine.
Extended pseudo-classes
Where an argument accepts a regular expression, it is written as a regular expression literal: /pattern/ or /pattern/flags, where the pattern contains no unescaped /: /ad\d+/, /sponsored/i. It is read as a JavaScript regular expression. Any other argument is a plain string.
Arguments that accept a wildcard pattern treat * as any number of characters. A pattern may match any part of the string: ad* matches bad.
:has()
Matches an element if the selector in the argument, evaluated relative to that element, matches at least one element, like the standard CSS :has(). The selector may start with a combinator: div:has(> img). It may contain selectors with extended pseudo-classes. May also be written as :-abp-has().
:contains()
Matches an element whose text content contains the argument. If the argument is a regular expression, the text content must match it instead. The match is case-sensitive unless a regular expression with the i flag is used. May also be written as :has-text() or :-abp-contains().
For example, div:contains(Sponsored) matches a div whose text includes Sponsored, and div:contains(/^ad$/i) matches a div whose whole text is ad, Ad, or AD.
:matches-attr()
Matches an element by the name, and optionally the value, of one of its attributes. The argument is name or name=value. Each side may be quoted and may be:
- a plain string, matched in full:
:matches-attr("data-type"="ad") - a wildcard pattern:
:matches-attr("data-*"="*ad*") - a regular expression:
:matches-attr(/^data-\d+$/)
Neither the name nor the value may contain =.
:matches-css()
Matches an element by the value of a CSS property, as returned by getComputedStyle(). This value may differ from the one in the page’s CSS: for example, bold is returned as 700.
The argument is property: value, optionally preceded by a pseudo-element and a comma: :matches-css(::before, display: block). The value may be:
- a plain string, matched in full:
:matches-css(position: fixed) - a wildcard pattern:
:matches-css(background-image: *ads*) - a regular expression:
:matches-css(z-index: /^\d{4}/)
The pseudo-element, the property, and the value must not contain ,.
:matches-path()
Matches every element if the path of the page’s URL contains the argument. If the argument is a regular expression, the path must match it instead. The path does not include the query string or the fragment.
For example, example.com#?#.sidebar:matches-path(/news) hides .sidebar only on pages whose path contains /news.
:min-text-length()
Matches an element whose text content is at least as long as the argument, in characters. The argument must be an integer of 1 or more.
:upward()
Selects an ancestor of the matched element instead of the element itself. The argument may be:
- an integer from 1 to 255, which selects the ancestor that many levels up:
.ad-label:upward(2)selects the parent of the parent of.ad-label - a selector, which selects the nearest ancestor that matches it:
.ad-label:upward(article)
:is() and :not()
Behave like the standard CSS :is() and :not(), but may also contain selectors with extended pseudo-classes: div:not(:contains(Article)).
:style()
Applies CSS declarations to matching elements instead of hiding them. The argument is a list of declarations separated by ;:
example.com#?#.banner:style(height: 0 !important; margin: 0 !important)The selector must not be a list: .a:style(color: red) styles .a, while .a, .b:style(color: red) has no effect.
Unlike CSS injection rules, :style() works in rules from any filter list.
CSS injection rules
A CSS injection rule adds CSS to matching pages. The body is one or more CSS rules, including at-rules like @media:
example.com#$#.banner { height: 0 !important; }
example.com#$#@media (min-width: 800px) { .sidebar { display: none !important; } }These rules apply only from trusted filter lists.
The body is not validated or changed. The bodies of all rules that apply to a page are joined into one stylesheet, so a body with a missing } can break the rules that follow it.
Exception rules
An exception rule cancels other rules on the pages it applies to. It has the same syntax as other rules, with @ added after the first # of the marker:
##.banner
example.com#@#.bannerHere, the first rule hides .banner on every domain, and the second one cancels it on example.com and its subdomains.
An exception rule cancels a rule only if both have exactly the same body, character for character.
A generic exception rule cancels matching rules on every domain. An exception rule with a domain list cancels both generic rules and rules with a domain list, on the pages it applies to. Like extended CSS rules, extended CSS exception rules must have a non-empty domain list.
An exception rule cancels only rules of its own type:
#@#cancels element hiding rules.#@?#cancels extended CSS rules. An element hiding exception rule (#@#) whose body contains an extended pseudo-class other than:has(),:is(), or:not()is equivalent to the same rule with#@?#.#@$#cancels CSS injection rules.
Page injection
Cosmetic rules are applied to HTML responses by inserting tags that load the rules from https://local.irbis.sh right before the first </head> tag. A page without a </head> tag gets no cosmetic rules. When the browser requests these files, the rules are chosen by the hostname in the request’s Referer header, or in its Origin header if Referer is absent. A request with neither header gets no rules.
The page’s Content-Security-Policy, both in headers and in <meta> tags, is changed to allow these files.