2. add-rule
| Chapter 3. Commands which are specific to w2x |

## 2. add-rule

    add-rule(selector, before-selector?, properties?)

Add CSS rule having selector `selector` to the list of “interned” CSS rules of the document being edited. Adds this rule before rule having selector `before-selector`.

Does nothing at all if a CSS rule having selector `selector` already exists in the list of “interned” CSS rules of the document being edited.

For this command to work, command `parse-styles` must have been invoked in order to “intern” the CSS classes found in all the elements of the document being edited.

`selector`

  Specifies the selector of the CSS rule to be added.

`before-selector`

  Specifies the selector of the CSS rule before which a new CSS rule is to be added. Defaults to the empty string "", which means at the end of the list.

  - `before-selector` may be the empty string "", which means: add it at the end of the list.

  - `before-selector` may be the index of a CSS rule (possibly in string form) within the document being edited. First index is 0. Examples: `0`, "`43`". The new CSS rule is added before the rule #`before-selector`, if any, at the end of the list otherwise.

  - `before-selector` may be a selector. Examples: "`p`", "`.c-Code`". The new CSS rule is added before the rule having selector `before-selector`, if any, at the end of the list otherwise.

  - `before-selector` may be a selector pattern. Examples: "`^\.c-.+$`", "`/n-\d+/`". The new CSS rule is added before first rule having a selector matching this pattern, if any, at the end of the list otherwise.

    A selector pattern has the following syntax: `/`pattern`/` or `^`pattern`$` (which is equivalent to `/^`pattern`$/`), where `pattern` is a regular expression pattern.

`properties`

  Specifies the CSS style properties of the newly added CSS rule. Defaults to the empty string "", in which case, the newly added CSS rule has no style properties.

Examples:

    (: Add an empty ".important" rule after all the other rules. :)
    add-rule(".important");

    (: Add a ".warning" rule after all the other rules. :)
    add-rule(".warning", "", "color: red; font-weight: bold;");

    (: Add an empty "a[href]" rule before rule #1. :)
    add-rule("a[href]", 1);

    (: Add an "a[href]" rule before rule "p". :)
    add-rule("a[href]", "p", "color: navy; text-decoration: underline");

    (: Add an empty ".link" rule before any rule having
       a selector starting with ".". :)
    add-rule(".link", "/^\./");

See also `remove-rule`, `set-rule`.

Related XPath extension functions: `find-rule`, `get-rule`.

1. add-class |  | 3. add-script