1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-19 00:05:36 +00:00
elinks/src/document/css
Witold Filipczyk 3d96b0d7d7 Revert "Compile with C++."
This reverts commit 4f4df33638.
2019-04-21 12:27:40 +02:00
..
apply.c class -> class_ for C++ compatibility 2016-04-20 18:18:27 +02:00
apply.h Doxygenate src/document/css/ 2007-07-28 02:35:36 +03:00
css.c Revert "Compile with C++." 2019-04-21 12:27:40 +02:00
css.h Strings corrections from Malcolm Parsons 2008-01-27 04:19:23 +00:00
Makefile path_to_top -> top_builddir 2005-10-20 04:00:35 +02:00
parser.c Revert "Compile with C++." 2019-04-21 12:27:40 +02:00
parser.h const in scanner 2008-01-26 18:20:17 +02:00
property.c Add support for the CSS list-style-type property 2013-09-07 10:52:01 +02:00
property.h Reorganisation of code to make C++ happy 2016-04-20 22:21:31 +02:00
README Remove now useless $Id: lines. 2005-10-21 09:14:07 +02:00
scanner.c CSS: do not fail assertion on "url( )" 2008-12-25 07:16:02 +00:00
scanner.h Doxygenate src/document/css/ 2007-07-28 02:35:36 +03:00
stylesheet.c Merge branch 'elinks-0.12' into elinks-0.13 2008-11-01 22:39:17 +02:00
stylesheet.h Reorganisation of code to make C++ happy 2016-04-20 22:21:31 +02:00
value.c Add support & test for the CSS list-style property 2011-11-14 04:34:50 +00:00
value.h Add support & test for the CSS list-style property 2011-11-14 04:34:50 +00:00

This is a super-simplistic CSS micro-engine.

				    Phases

The CSS handling is divided into:

* The scanner

It takes care of composing tokens from a string containing CSS source. It
also takes care of eliminating either garbage code that was not recognized
or things like whitespace and comments. The scanner will not attempt to
recover from this garbage code but merely signal them to the upper layers.

The scanner only works with strings but is a bit more high level than
scanners in the sense of flex. The string "10em" will not just generate the
two tokens <number>, <identifier> but rather combine them into one token.
This only leads to problems with tokens of the sort #<identifier> that can
be both a hex color or hash so should not be a problem but rather mean that
we will do less scanner calls.

The scanner lives in scanner.*

* The parser

It takes a string with CSS code, composes tokens (from the scanner) into
some meaningful syntax and transforms it to an internal set of structures
describing the data (let's call it a "rawer"). It currently does no recovery
when something unexpected shows up but skips to next special control char.

The parser lives in parser.* and value.*

* The applier

It applies style info from a syntax tree (parsed ELinks or document
stylesheet) or fragment of one (in the case of style="" attributes) to the
current element.

The applier lives in apply.*


			      The current state

Currently we both check the element's 'style' attribute, content of <style>
tags and imports from either <link> tags in the HTML header or @imports from
the CSS code. But we lack a proper way to handle the cascading. Now it will
automatically scan the current element, and if a 'style' attribute is found,
it is parsed and applied to the current element.  If there is no 'style'
attribute it will look up any styles retrieved from the document stylesheet
and last try styles from the default user controlled stylesheet. TODO: We
should always look up <style> tags and only apply those not found in any
'style' attribute.

One big problem with the current way of doing things is inheritance, there
is no way we are telling the HTML engine what is going to be inherited and
what is not. The other problem is precedence, currently even global
stylesheet takes precedence over local classic-formatting attributes (we
just css_apply() like mad on various places to make sure the CSS attributes
are stuffed through HTML engine's throat). These two problems will be solved
when the HTML engine is converted to work with stylesheets natively (instead
of format + par_format).


			       The selectors tree

In order to handle any non-trivial selectors, we need them to form a certain
structure. A hierarchical one was chosen, where we initially focus on a
the most specific element, then we build the way down through ids, classes
and pseudo-classes and then back the way up through parent elements.

Assume two statements: "div#foo p a>b i:baz { color: black; }" and
"div#foo.bar p a>b i:baz { text-decoration: underline; }". The tree we build up
is:

                element[i]
                | (pseudo_classes)
                pseudo_class[baz]
                | (ancestors)
                element[b]
                | (parents)
                element[a]
                | (ancestors)
                element[p]
                | (ancestors)
                element[div]
                | (ids)
                id[foo] -> (color: black)
                | (classes)
                class[bar] -> (text-decoration: underline)

As you can see, the combinators hierarchy is reverse, while the other selectors
hierarchy is as usual. This is to aid the applier, it goes from the general to
the specific (that's how it is from the POV of applying stuff to single
elements, even though the ancestors are more "general" from the POV of the
document stucture). This approach has its deficiencies as well (it can be
expensive to match long complex combinators since we need to walk through the
ancestry each time we match an element, or even more frequently when we
consider the element with varied specificities ("i", "i#x", "i:baz",
"i#x:baz")) but it still looks like the best way (because of the varied
specificities, you can't well go the other way by narrowing down the selectors
as you descend the elements tree).

Let's close the discourse by adding another two selectors to the stylesheet:
"b i#x:baz { color: blue; }" and "a>b#foo i:baz { background: white; }".

                element[i] -------------.
                | (pseudo_classes)      | (ids)
                pseudo_class[baz]       id[x]
                | (ancestors)           | (pseudo_classes)
  .------------ element[b]              pseudo_class[baz]
  | (ids)       | (parents)             | (ancestors)
  id[foo]       element[a]              element[b] -> (color: blue)
  | (parents)   | (ancestors)
  element[a]    element[p]
   -> (b: w)    | (ancestors)
                element[div]
                | (ids)
                id[foo] -> (color: black)
                | (classes)
                class[bar] -> (text-decoration: underline)

As you can see a tiny alternation of specificity at the top of the tree will
duplicate the whole path of combinators, but you can't get away without that,
I think.

You can get probably a much better overview of how it looks by #define
DEBUG_CSS at the top of src/document/css/stylesheet.h, recompiling and then
grabbing a dumped stylesheet tree from stderr. Translate the 'type' and 'rel'
fields from numbers to actual values accordingly to the enums in the
aforementioned header file.


			    The future of selectors

XXX: I keep this here only for a historical reference now. The order matters,
the connecting lines probably not. --pasky

Specificity

111		a#id.nav
.		    |
.		.---'---.
.		V	|
101	       a#id	|
.		|	|
.		|	|
.		|	|
13		|	|		       div p a.nav
.		|	|			    |
.		|	|	    .--------------'|
.		|	|	    V		    |
12		|	|        p a.nav	    |
.		|	|	    |		    |
.		|	`-.---------'		    |
.		|	  V			    |
11		|	a.nav			    |
.		|	  |			    |
.		|	  |			    |
.		|	  |			    |
2		|	  |    p a		    |
.		|	  |	|		    |
.		`---------`-.---'---.		    |
.			    V	    V		    |
1			    a	    p	   img	   div
.			    |	      |	    |	    |
.			    `-------+---+---+-------'
.					V
0					* (universal selector)