css

A CSS parser.

We also use this parser inside parce, to be able to store default highlighting formats in css files.

In this module:

Language

Name (Aliases)

Description

Filename(s)

Mime Type(s)

Css

CSS

Cascading Style Sheet

*.css

text/css

class Css[source]

Bases: parce.language.Language

root

Toplevel items: at-rules, comments, normal rules.

classmethod toplevel()[source]

Find toplevel items: at-rules, comments, normal rules.

prelude

The prelude of a rule: one or more selectors. On { parse the rule.

classmethod selectors()[source]

Yield selectors, used in prelude and selector_list.

selector

All types of CSS selectors

selector_list

The list of selectors in :is(bla, bla), etc.

rule

Declarations of a qualified rule between { and }.

inline

CSS in a rule block, or in an HTML style attribute.

declaration

A property: value; declaration.

classmethod common()[source]

Find stuff that can be everywhere, string, comment, color, identifier

unit

Unit directly after a number, e.g. the px in 100px, also %.

classmethod identifier_common(action)[source]

Yield an ident-token and give it the specified action.

element_selector

A tag name used as selector.

property

A CSS property.

attribute

An attribute name.

id_selector

An ID selecter: #id.

class_selector

A class selector: .classname.

attribute_selector

Stuff between [ and ].

pseudo_class

Things like :first-child etc.

pseudo_element

Things like ::first-letter etc.

atrule

Contents following ‘@’.

atrule_nested

An atrule that has nested toplevel contents (@media, etc.)

atrule_keyword

The first identifier word in an @-rule.

atrule_block

A { } block from an @-rule.

atrule_nested_block

A { } block from @media, @document or @supports.

classmethod atrule_common()[source]

Find common stuff inside @-rules.

ident_token

An ident-token where quoted or unquoted text is allowed.

identifier

An ident-token that could be a color or a function().

function

Contents between identifier( … ).

url_function

The url function: url(…``)``.

dqstring

A double-quoted string.

sqstring

A single-quoted string.

classmethod string()[source]

Common rules for string.

comment

A comment.

class CssIndent[source]

Bases: parce.indent.Indent

Indenter for Css.

events(block, tokens, prev_indents)[source]

Implement this to yield indent events for the tokens.

class CssIO[source]

Bases: parce.docio.IO

I/O handling for Css.

default_encoding()[source]

Return “utf-8” by default.

find_encoding(text)[source]

Find encoding in Css.

Example:

Root lexicon Css.root and text:

Text rendered using default theme

/* css example */
h1.main {
    color: red;
    background: grey url(bla.png);
}

Result tree:

<Context Css.root at 0-80 (4 children)>
 ├╴<Token '/*' at 0:2 (Comment)>
 ├╴<Context Css.comment at 2-17 (2 children)>
 │  ├╴<Token ' css example ' at 2:15 (Comment)>
 │  ╰╴<Token '*/' at 15:17 (Comment)>
 ├╴<Context Css.prelude at 18-27 (2 children)>
 │  ├╴<Context Css.selector at 18-25 (3 children)>
 │  │  ├╴<Context Css.element_selector at 18-20 (1 child)>
 │  │  │  ╰╴<Token 'h1' at 18:20 (Name.Tag)>
 │  │  ├╴<Token '.' at 20:21 (Keyword)>
 │  │  ╰╴<Context Css.class_selector at 21-25 (1 child)>
 │  │     ╰╴<Token 'main' at 21:25 (Name.Class)>
 │  ╰╴<Token '{' at 26:27 (Delimiter.Bracket)>
 ╰╴<Context Css.rule at 32-80 (3 children)>
    ├╴<Context Css.declaration at 32-43 (4 children)>
    │  ├╴<Context Css.property at 32-37 (1 child)>
    │  │  ╰╴<Token 'color' at 32:37 (Name.Property.Definition)>
    │  ├╴<Token ':' at 37:38 (Delimiter)>
    │  ├╴<Context Css.identifier at 39-42 (1 child)>
    │  │  ╰╴<Token 'red' at 39:42 (Literal.Color)>
    │  ╰╴<Token ';' at 42:43 (Delimiter)>
    ├╴<Context Css.declaration at 48-78 (7 children)>
    │  ├╴<Context Css.property at 48-58 (1 child)>
    │  │  ╰╴<Token 'background' at 48:58 (Name.Property.Definition)>
    │  ├╴<Token ':' at 58:59 (Delimiter)>
    │  ├╴<Context Css.identifier at 60-64 (1 child)>
    │  │  ╰╴<Token 'grey' at 60:64 (Literal.Color)>
    │  ├╴<Token 'url' at 65:68 (Name)>
    │  ├╴<Token '(' at 68:69 (Delimiter)>
    │  ├╴<Context Css.url_function at 69-77 (2 children)>
    │  │  ├╴<Token 'bla.png' at 69:76 (Literal.Url)>
    │  │  ╰╴<Token ')' at 76:77 (Delimiter)>
    │  ╰╴<Token ';' at 77:78 (Delimiter)>
    ╰╴<Token '}' at 79:80 (Delimiter.Bracket)>