On the Analysis of Cascading Style Sheets

Pierre Genevès, CNRS
Nabil Layaïda, INRIA
Vincent Quint, INRIA

LIG Laboratory and INRIA Grenoble, France

International Conference on World Wide Web, April 2012

Rough Timeline of Core Web Technologies

  • 1991: HTML
  • 1994: HTML2
  • 1996: CSS , Javascript
  • 1997: HTML4
  • 1998: CSS2
  • 2000: XHTML1
  • 2005: AJAX
  • 2009: CSS3, HTML5
  • Initial foundations of the web: HTML, HTTP, URL
  • First added feature: Cascading Style Sheets (CSS)
  • Nowadays: HTML 5 + CSS3 + Javascript

CSS Today


CSS in a Nutshell

CSS appears very simple at first sight:


However...

CSS has intricate facets:

CSS in Practice

Web designers' heaven... but web developpers' hell

Today, CSS debugging technique = testing


Limits of current technology


A Notion of Correctness?

What would be "correctness" for CSS anyway?

What would be an "incorrect" CSS?


Objectives and Challenges

Main objective: Find analysis methods that

Main difficulties


Proposed Approach

  1. Describe CSS mechanisms and dependencies logically
  2. Formulate the existence of style inconsistencies
  3. Use a logical reasoner for solving satisfiability

Possible outputs:

  • Formula is unsatisfiable: CSS correctness proven
  • Formula is satisfiable: style inconsistencies exist for some document(s), we generate the smallest counter-example
SVG Architecture

Which Logic?

Chosen logic:

Required adaptations:


Zoom on Logical Formulas

XML trees are seen as labeled binary trees (wlog)

Programs $\alpha \in \{ 1, 2, \overline{1}, \overline{2}\}$ for navigation

SVG tree

A formula can be:



Translating CSS in the Logic

Sample CSS rule: div p   {color: red;}
Logical formula: $p \wedge \langle\text{color}\rangle \text{'red'} \wedge \mu X. \langle\overline{1}\rangle\text{div} \vee \langle\overline{2}\rangle X \vee \langle \overline{1}\rangle X$
SVG tree

Main principles:


Example with one Property

Sample CSS

1. tr:nth-child(even) {background-color:LightGray;}
2. tr:nth-child(odd) {background-color:DarkGray;}
3. table td {font-size: 16px;}
4. td {font-size: 14px;}
5. div {color:DarkGray;}
6. div {font-size:smaller;}
  • Given cascade, priorities, inheritance, can a property reach an unacceptable value for some document?
  • Can font size go too small?
$\left.\begin{array}{l} \phi_\text{prop}(\text{<}\text{font-size}\text{>}\text{'8px'})\\ \phi_\text{CSS}(\text{<}\text{font-size}\text{>}) \\ \phi_\text{Schema} \end{array}\right\} \phi$
$\rightarrow$ SVG solver
$\rightarrow$  satisfiable  $\rightarrow$
<body>Body text
  <div font-size="smaller">text ...
    <div font-size="smaller">text ... 
      <div font-size="smaller">text ...
      </div>
    </div>
  </div>
</body>

Example with one Property (cont.)

Sample CSS

1. tr:nth-child(even) {background-color:LightGray;}
2. tr:nth-child(odd) {background-color:DarkGray;}
3. table td {font-size: 16px;}
4. td {font-size: 14px;}
5. div {color:DarkGray;}
6. div {font-size:smaller;}
7. div div div {font-size:medium;}
  • Rule #7 added
  • Can font size still go too small?
$\left.\begin{array}{l} \phi_\text{prop}(\text{<}\text{font-size}\text{>}\text{'8px'})\\ \phi_\text{CSS}(\text{<}\text{font-size}\text{>}) \\ \phi_\text{Schema} \end{array}\right\} \phi$
$\rightarrow$ SVG solver
$\rightarrow$  unsatisfiable  $\rightarrow$
No. 

There does not exist any document for which the font-size may go too small.

Example with two Properties

Sample CSS

1. tr:nth-child(even) {background-color:LightGray;}
2. tr:nth-child(odd) {background-color:DarkGray;}
3. table td {font-size: 16px;}
4. td {font-size: 14px;}
5. div {color:DarkGray;}
6. div {font-size:smaller;}
  • Consider two properties: e.g. color and background-color
  • Is there any chance that they interact in a bad way?
$\left.\begin{array}{l} \phi_\text{prop}(\text{<}\text{color}\text{>},\text{<}\text{bg-color}\text{>})\\ \phi_\text{CSS}(\text{<}\text{color}\text{>},\text{<}\text{bg-color}\text{>}) \\ \phi_\text{Schema} \end{array}\right\} \phi$
$\rightarrow$ SVG solver
$\rightarrow$  sat.  $\rightarrow$
...
<table>
  <tr style="background-color:DarkGray;">
    <td>This <div style="color:DarkGray;"> Cell</div>
    is DarkGray.</td></tr>
  <tr style="background-color:LightGray;">
    <td>The <div style="color:DarkGray;"> Cell</div>
    is DarkGray.</td></tr>
</table>...

Detecting CSS Generic Issues

Conclusion

Thank you!

Further information:

Web site:   http://wam.inrialpes.fr/websolver
These slides:   http://wam.inrialpes.fr/talks/2012/WWW12/slides.htm
Email:   pierre.geneves@inria.fr

CSS Inheritance

Compiling Main CSS Combinators


$\begin{array}{lll} \textbf{Semantics} & \textbf{CSS} & \textbf{Tree Logic} \\ \hline \text{Any element} & \text{*} & \top \\ \text{Any 'p' element} & \text{p} & \text{p} \\ \text{Any child of some p element} & \text{p > *} & \mu X. \langle\overline{1}\rangle p \vee \langle\overline{2}\rangle X \\ \text{Any descendant 'b' of some 'a' element} & \text{a b} & b \wedge \mu X. \langle \overline{1}\rangle (a \vee X) \vee \langle \overline{2}\rangle X \\ \text{Any element with class 'foo' } & \text{.foo} & \langle \text{class}\rangle '\text{foo}' \\ \text{Any element with attribute 'title' } & \text{*[title]} & \langle \text{title}\rangle \top\\ \text{Any 'p' element with an 'a' child} & \text{Not possible} \qquad & p \wedge \langle 1\rangle \mu X. a \vee \langle 2\rangle X\\ \text{Any adjacent next sibling of a 'p' element} & \text{p + *} & \langle \overline{2}\rangle p \\ \text{Any next sibling 'pre' of a 'h1' element} \qquad & \text{h1 ~ pre} & \text{pre} \wedge \mu X. \langle \overline{2}\rangle \text{h1} \vee \langle \overline{2}\rangle X \\ \text{Any 'e' whose 'foo' attribute value is 'bar'} \qquad & \text{e[foo="bar"]} & e \wedge \langle \text{foo}\rangle'\text{bar}' \end{array}$