从Chrome源码看浏览器如何计算CSS | 目的地Destination

从Chrome源码看浏览器如何计算CSS

从Chrome源码看浏览器如何计算CSS

在《Effective前端6:避免页面卡顿》这篇里面介绍了浏览器渲染页面的过程:

从Chrome源码看浏览器如何计算CSS

并且《从Chrome源码看浏览器如何构建DOM树》介绍了第一步如何解析Html构建DOM树,这个过程大概如下:

从Chrome源码看浏览器如何计算CSS

浏览器每收到一段html的文本之后,就会把它序列化成一个个的tokens,依次遍历这些token,实例化成对应的html结点并插入到DOM树里面。

我将在这一篇介绍第二步Style的过程,即CSS的处理。

1. 加载CSS

在构建DOM的过程中,如果遇到link的标签,当把它插到DOM里面之后,就会触发资源加载——根据href指明的链接:


1
<span></span>&lt;link rel="stylesheet" href="demo.css"&gt;

上面的rel指明了它是一个样式文件。这个加载是异步,不会影响DOM树的构建,只是说在CSS没处理好之前,构建好的DOM并不会显示出来。用以下的html和css做试验:


1
<span></span><span class="cp">&lt;!DOCType html&gt;</span> <span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">"stylesheet"</span> <span class="na">href</span><span class="o">=</span><span class="s">"demo.css"</span><span class="p">&gt;</span> <span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"text"</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>hello, world<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span> <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span> <span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>

demo.css如下:


1
<span></span><span class="nc">.text</span><span class="p">{</span> <span class="nb">font-size</span><span class="o">:</span> <span class="m">20px</span><span class="p">;</span> <span class="p">}</span> <span class="nc">.text</span> <span class="nt">p</span><span class="p">{</span> <span class="nb">color</span><span class="o">:</span> <span class="m">#505050</span><span class="p">;</span> <span class="p">}</span>

从打印的log可以看出(添加打印的源码略):

[DocumentLoader.cpp(558)] “<!DOCType html>/n<html>/n<head>/n<link rel=/”stylesheet/” href=/”demo.css/”> /n</head>/n<body>/n<div class=/”text/”>/n <p>hello, world</p>/n</div>/n</body>/n</html>/n”

[HTMLDocumentParser.cpp(765)] “tagName: html |type: DOCTYPE|attr: |text: “

[HTMLDocumentParser.cpp(765)] “tagName: |type: Character |attr: |text: /n”

[HTMLDocumentParser.cpp(765)] “tagName: html |type: startTag |attr: |text: “

[HTMLDocumentParser.cpp(765)] “tagName: html |type: EndTag |attr: |text: “

[HTMLDocumentParser.cpp(765)] “tagName: |type: EndOfFile|attr: |text: “

[Document.cpp(1231)] readystatechange to Interactive

[CSSParserImpl.cpp(217)] recieved and parsing stylesheet: “.text{/n font-size: 20px;/n}/n.text p{/n color: #505050;/n}/n”

在CSS没有加载好之前,DOM树已经构建好了。为什么DOM构建好了不把html放出来,因为没有样式的html直接放出来,给人看到的页面将会是乱的。所以CSS不能太大,页面一打开将会停留较长时间的白屏,所以把图片/字体等转成base64放到CSS里面是一种不太推荐的做法。

2. 解析CSS

(1)字符串 -> tokens

CSS解析和html解析有比较像的地方,都是先格式化成tokens。CSS token定义了很多种类型,如下的CSS会被拆成这么多个token:

从Chrome源码看浏览器如何计算CSS

经常看到有人建议CSS的色值使用16位的数字会优于使用rgb的表示,这个是子虚乌有,还是有根据的呢?

如下所示:

从Chrome源码看浏览器如何计算CSS

如果改成rgb,它将变成一个函数类型的token,这个函数需要再计算一下。从这里看的话,使用16位色值确实比使用rgb好。

(2)tokens -> styleRule

这里不关心它是怎么把tokens转化成style的规则的,我们只要看格式化后的styleRule是怎么样的就可以。每个styleRule主要包含两个部分,一个是选择器selectors,第二个是属性集properties。用以下CSS:


1
<span></span><span class="nc">.text</span> <span class="nc">.hello</span><span class="p">{</span> <span class="nb">color</span><span class="o">:</span> <span class="nb">rgb</span><span class="p">(</span><span class="m">200</span><span class="o">,</span> <span class="m">200</span><span class="o">,</span> <span class="m">200</span><span class="p">);</span> <span class="nb">width</span><span class="o">:</span> <span class="n">calc</span><span class="p">(</span><span class="m">100%</span> <span class="o">-</span> <span class="m">20px</span><span class="p">);</span> <span class="p">}</span> <span class="nn">#world</span><span class="p">{</span> <span class="nb">margin</span><span class="o">:</span> <span class="m">20px</span><span class="p">;</span> <span class="p">}</span>

打印出来的选择器结果为(相关打印代码省略):

selector text = “.text .hello”

value = “hello” matchType = “Class” relation = “Descendant”

tag history selector text = “.text”

value = “text” matchType = “Class” relation = “SubSelector”

selector text = “#world”

value = “world” matchType = “Id” relation = “SubSelector”

从第一个选择器可以看出,它的解析是从右往左的,这个在判断match的时候比较有用。

blink定义了几种matchType:


1
<span></span> <span class="k">enum</span> <span class="n">MatchType</span> <span class="p">{</span> <span class="n">Unknown</span><span class="p">,</span> <span class="n">Tag</span><span class="p">,</span> <span class="c1">// Example: div</span> <span class="n">Id</span><span class="p">,</span> <span class="c1">// Example: #id</span> <span class="n">Class</span><span class="p">,</span> <span class="c1">// example: .class</span> <span class="n">PseudoClass</span><span class="p">,</span> <span class="c1">// Example: :nth-child(2)</span> <span class="n">PseudoElement</span><span class="p">,</span> <span class="c1">// Example: ::first-line</span> <span class="n">PagePseudoClass</span><span class="p">,</span> <span class="c1">// ??</span> <span class="n">AttributeExact</span><span class="p">,</span> <span class="c1">// Example: E[foo="bar"]</span> <span class="n">AttributeSet</span><span class="p">,</span> <span class="c1">// Example: E[foo]</span> <span class="n">AttributeHyphen</span><span class="p">,</span> <span class="c1">// Example: E[foo|="bar"]</span> <span class="n">AttributeList</span><span class="p">,</span> <span class="c1">// Example: E[foo~="bar"]</span> <span class="n">AttributeContain</span><span class="p">,</span> <span class="c1">// css3: E[foo*="bar"]</span> <span class="n">AttributeBegin</span><span class="p">,</span> <span class="c1">// css3: E[foo^="bar"]</span> <span class="n">AttributeEnd</span><span class="p">,</span> <span class="c1">// css3: E[foo$="bar"]</span> <span class="n">FirstAttributeSelectorMatch</span> <span class="o">=</span> <span class="n">AttributeExact</span><span class="p">,</span> <span class="p">};</span>

还定义了几种选择器的类型:


1
<span></span><span class="k">enum</span> <span class="n">RelationType</span> <span class="p">{</span> <span class="n">SubSelector</span><span class="p">,</span> <span class="c1">// No combinator</span> <span class="n">Descendant</span><span class="p">,</span> <span class="c1">// "Space" combinator</span> <span class="n">Child</span><span class="p">,</span> <span class="c1">// &gt; combinator</span> <span class="n">DirectAdjacent</span><span class="p">,</span> <span class="c1">// + combinator</span> <span class="n">IndirectAdjacent</span><span class="p">,</span> <span class="c1">// ~ combinator</span> <span class="c1">// Special cases for shadow DOM related selectors.</span> <span class="n">ShadowPiercingDescendant</span><span class="p">,</span> <span class="c1">// &gt;&gt;&gt; combinator</span> <span class="n">ShadowDeep</span><span class="p">,</span> <span class="c1">// /deep/ combinator</span> <span class="n">ShadowPseudo</span><span class="p">,</span> <span class="c1">// ::shadow pseudo element</span> <span class="n">ShadowSlot</span> <span class="c1">// ::slotted() pseudo element</span> <span class="p">};</span>

.text .hello的.hello选择器的类型就是Descendant,即后代选择器。记录选择器类型的作用是协助判断当前元素是否match这个选择器。例如,由于.hello是一个父代选器,所以它从右往左的下一个选择器就是它的父选择器,于是判断当前元素的所有父元素是否匹配.text这个选择器。

第二个部分——属性打印出来是这样的:

selector text = “.text .hello”

perperty id = 15 value = “rgb(200, 200, 200)”

perperty id = 316 value = “calc(100% – 20px)”

selector text = “#world”

perperty id = 147 value = “20px”

perperty id = 146 value = “20px”

perperty id = 144 value = “20px”

perperty id = 145 value = “20px”

所有的CSS的属性都是用id标志的,上面的id依次对应:


1
<span></span><span class="k">enum</span> <span class="n">CSSPropertyID</span> <span class="p">{</span> <span class="n">CSSPropertyColor</span> <span class="o">=</span> <span class="mi">15</span><span class="p">,</span> <span class="n">CSSPropertyWidth</span> <span class="o">=</span> <span class="mi">316</span><span class="p">,</span> <span class="n">CSSPropertyMarginLeft</span> <span class="o">=</span> <span class="mi">145</span><span class="p">,</span> <span class="n">CSSPropertyMarginRight</span> <span class="o">=</span> <span class="mi">146</span><span class="p">,</span> <span class="n">CSSPropertyMarginTop</span> <span class="o">=</span> <span class="mi">147</span><span class="p">,</span> <span class="n">CSSPropertyMarkerEnd</span> <span class="o">=</span> <span class="mi">148</span><span class="p">,</span> <span class="p">}</span>

设置了margin: 20px,会转化成四个属性。从这里可以看出CSS提倡属性合并,但是最后还是会被拆成各个小属性。所以属性合并最大的作用应该在于减少CSS的代码量。

一个选择器和一个属性集就构成一条rule,同一个css表的所有rule放到同一个stylesheet对象里面,blink会把用户的样式存放到一个m_authorStyleSheets的向量里面,如下图示意:

从Chrome源码看浏览器如何计算CSS

除了autherStyleSheet,还有浏览器默认的样式DefaultStyleSheet,这里面有几张,最常见的是UAStyleSheet,其它的还有svg和全屏的默认样式表。Blink ua全部样式可见这个文件html.css,这里面有一些常见的设置,如把style/link/script等标签display: none,把div/h1/p等标签display: block,设置p/h1/h2等标签的margin值等,从这个样式表还可以看到Chrome已经支持了HTML5.1新加的标签,如dialog:


1
<span></span><span class="nt">dialog</span> <span class="p">{</span> <span class="nb">position</span><span class="o">:</span> <span class="nb">absolute</span><span class="p">;</span> <span class="nb">left</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span> <span class="nb">right</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span> <span class="nb">width</span><span class="o">:</span> <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">fit</span><span class="o">-</span><span class="nb">content</span><span class="p">;</span> <span class="nb">height</span><span class="o">:</span> <span class="o">-</span><span class="n">webkit</span><span class="o">-</span><span class="n">fit</span><span class="o">-</span><span class="nb">content</span><span class="p">;</span> <span class="nb">margin</span><span class="o">:</span> <span class="nb">auto</span><span class="p">;</span> <span class="nb">border</span><span class="o">:</span> <span class="nb">solid</span><span class="p">;</span> <span class="nb">padding</span><span class="o">:</span> <span class="m">1em</span><span class="p">;</span> <span class="nb">background</span><span class="o">:</span> <span class="nb">white</span><span class="p">;</span> <span class="nb">color</span><span class="o">:</span> <span class="nb">black</span><span class="p">;</span> <span class="p">}</span>

另外还有怪异模式的样式表:quirk.css,这个文件很小,影响比较大的主要是下面:


1
<span></span><span class="c">/* This will apply only to text fields, since all other inputs already use border box sizing */</span> <span class="nt">input</span><span class="nd">:not</span><span class="o">([</span><span class="nt">type</span><span class="o">=</span><span class="nt">image</span> <span class="nt">i</span><span class="o">]),</span> <span class="nt">textarea</span> <span class="p">{</span> <span class="n">box</span><span class="o">-</span><span class="n">sizing</span><span class="o">:</span> <span class="nb">border</span><span class="o">-</span><span class="n">box</span><span class="p">;</span> <span class="p">}</span>

blink会先去加载html.css文件,怪异模式下再接着加载quirk.css文件。

(4)生成哈希map

最后会把生成的rule集放到四个类型哈希map:


1
<span></span> <span class="n">CompactRuleMap</span> <span class="n">m_idRules</span><span class="p">;</span> <span class="n">CompactRuleMap</span> <span class="n">m_classRules</span><span class="p">;</span> <span class="n">CompactRuleMap</span> <span class="n">m_tagRules</span><span class="p">;</span> <span class="n">CompactRuleMap</span> <span class="n">m_shadowPseudoElementRules</span><span class="p">;</span>

map的类型是根据最右边的selector的类型:id、class、标签、伪类选择器区分的,这样做的目的是为了在比较的时候能够很快地取出匹配第一个选择器的所有rule,然后每条rule再检查它的下一个selector是否匹配当前元素。

3. 计算CSS

CSS表解析好之后,会触发layout tree,进行layout的时候,会把每个可视的Node结点相应地创建一个Layout结点,而创建Layout结点的时候需要计算一下得到它的style。为什么需要计算style,因为可能会有多个选择器的样式命中了它,所以需要把几个选择器的样式属性综合在一起,以及继承父元素的属性以及UA的提供的属性。这个过程包括两步:找到命中的选择器和设置样式。

(1)选择器命中判断

用以下html做为demo:


1
<span></span><span class="p">&lt;</span><span class="nt">style</span><span class="p">&gt;</span> <span class="nc">.text</span><span class="p">{</span> <span class="nb">font-size</span><span class="o">:</span> <span class="m">22em</span><span class="p">;</span> <span class="p">}</span> <span class="nc">.text</span> <span class="nt">p</span><span class="p">{</span> <span class="nb">color</span><span class="o">:</span> <span class="m">#505050</span><span class="p">;</span> <span class="p">}</span> <span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"text"</span><span class="p">&gt;</span> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>hello, world<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span> <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>

上面会生成两个rule,第一个rule会放到上面提到的四个哈希map其中的classRules里面,而第二个rule会放到tagRules里面。

当这个样式表解析好时,触发layout,这个layout会更新所有的DOM元素:


1
<span></span><span class="kt">void</span> <span class="n">ContainerNode</span><span class="o">::</span><span class="n">attachLayoutTree</span><span class="p">(</span><span class="k">const</span> <span class="n">AttachContext</span><span class="o">&amp;</span> <span class="n">context</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span> <span class="p">(</span><span class="n">Node</span><span class="o">*</span> <span class="n">child</span> <span class="o">=</span> <span class="n">firstChild</span><span class="p">();</span> <span class="n">child</span><span class="p">;</span> <span class="n">child</span> <span class="o">=</span> <span class="n">child</span><span class="o">-&gt;</span><span class="n">nextSibling</span><span class="p">())</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">child</span><span class="o">-&gt;</span><span class="n">needsAttach</span><span class="p">())</span> <span class="n">child</span><span class="o">-&gt;</span><span class="n">attachLayoutTree</span><span class="p">(</span><span class="n">childrenContext</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span>

这是一个递归,初始为document对象,即从document开始深度优先,遍历所有的dom结点,更新它们的布局。

对每个node,代码里面会依次按照id、class、伪元素、标签的顺序取出所有的selector,进行比较判断,最后是通配符,如下:


1
<span></span>//如果结点有id属性 if (element.hasID()) collectMatchingRulesForList( matchRequest.ruleSet-&gt;idRules(element.idForStyleResolution()), cascadeOrder, matchRequest); //如果结点有class属性 if (element.isStyledElement() &amp;&amp; element.hasClass()) { for (size_t i = 0; i &lt; element.classNames().size(); ++i) collectMatchingRulesForList( matchRequest.ruleSet-&gt;classRules(element.classNames()[i]), cascadeOrder, matchRequest); } //伪类的处理 ... //标签选择器处理 collectMatchingRulesForList( matchRequest.ruleSet-&gt;tagRules(element.localNameForSelectorMatching()), cascadeOrder, matchRequest); //最后是通配符 ...

在遇到div.text这个元素的时候,会去执行上面代码的取出classRules的那行。

上面domo的rule只有两个,一个是classRule,一个是tagRule。所以会对取出来的这个classRule进行检验:


1
<span></span><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">checkOne</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">subResult</span><span class="p">))</span> <span class="k">return</span> <span class="n">SelectorFailsLocally</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">context</span><span class="p">.</span><span class="n">selector</span><span class="o">-&gt;</span><span class="n">isLastInTagHistory</span><span class="p">())</span> <span class="p">{</span> <span class="k">return</span> <span class="n">SelectorMatches</span><span class="p">;</span> <span class="p">}</span>

第一行先对当前选择器(.text)进行检验,如果不通过,则直接返回不匹配,如果通过了,第三行判断当前选择器是不是最左边的选择器,如果是的话,则返回匹配成功。如果左边还有限定的话,那么再递归检查左边的选择器是否匹配。

我们先来看一下第一行的checkOne是怎么检验的:


1
<span></span><span class="k">switch</span> <span class="p">(</span><span class="n">selector</span><span class="p">.</span><span class="n">match</span><span class="p">())</span> <span class="p">{</span> <span class="k">case</span> <span class="n">CSSSelector</span><span class="o">::</span><span class="nl">Tag</span><span class="p">:</span> <span class="k">return</span> <span class="n">matchesTagName</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">selector</span><span class="p">.</span><span class="n">tagQName</span><span class="p">());</span> <span class="k">case</span> <span class="n">CSSSelector</span><span class="o">::</span><span class="nl">Class</span><span class="p">:</span> <span class="k">return</span> <span class="n">element</span><span class="p">.</span><span class="n">hasClass</span><span class="p">()</span> <span class="o">&amp;&amp;</span> <span class="n">element</span><span class="p">.</span><span class="n">classNames</span><span class="p">().</span><span class="n">contains</span><span class="p">(</span><span class="n">selector</span><span class="p">.</span><span class="n">value</span><span class="p">());</span> <span class="k">case</span> <span class="n">CSSSelector</span><span class="o">::</span><span class="nl">Id</span><span class="p">:</span> <span class="k">return</span> <span class="n">element</span><span class="p">.</span><span class="n">hasID</span><span class="p">()</span> <span class="o">&amp;&amp;</span> <span class="n">element</span><span class="p">.</span><span class="n">idForStyleResolution</span><span class="p">()</span> <span class="o">==</span> <span class="n">selector</span><span class="p">.</span><span class="n">value</span><span class="p">();</span> <span class="p">}</span>

很明显,.text将会在上面第6行匹配成功,并且它左边没有限定了,所以返回匹配成功。

到了检验p标签的时候,会取出”.text p”的rule,它的第一个选择器是p,将会在上面代码的第3行判断成立。但由于它前面还有限定,于是它还得继续检验前面的限定成不成立。

前一个选择器的检验关键是靠当前选择器和它的关系,上面提到的relationType,这里的p的relationType是Descendant即后代。上面在调了checkOne成功之后,继续往下走:


1
<span></span><span class="k">switch</span> <span class="p">(</span><span class="n">relation</span><span class="p">)</span> <span class="p">{</span> <span class="k">case</span> <span class="n">CSSSelector</span><span class="o">::</span><span class="nl">Descendant</span><span class="p">:</span> <span class="k">for</span> <span class="p">(</span><span class="n">nextContext</span><span class="p">.</span><span class="n">element</span> <span class="o">=</span> <span class="n">parentElement</span><span class="p">(</span><span class="n">context</span><span class="p">);</span> <span class="n">nextContext</span><span class="p">.</span><span class="n">element</span><span class="p">;</span> <span class="n">nextContext</span><span class="p">.</span><span class="n">element</span> <span class="o">=</span> <span class="n">parentElement</span><span class="p">(</span><span class="n">nextContext</span><span class="p">))</span> <span class="p">{</span> <span class="n">MatchStatus</span> <span class="n">match</span> <span class="o">=</span> <span class="n">matchSelector</span><span class="p">(</span><span class="n">nextContext</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">match</span> <span class="o">==</span> <span class="n">SelectorMatches</span> <span class="o">||</span> <span class="n">match</span> <span class="o">==</span> <span class="n">SelectorFailsCompletely</span><span class="p">)</span> <span class="k">return</span> <span class="n">match</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">nextSelectorExceedsScope</span><span class="p">(</span><span class="n">nextContext</span><span class="p">))</span> <span class="k">return</span> <span class="n">SelectorFailsCompletely</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">SelectorFailsCompletely</span><span class="p">;</span> <span class="k">case</span> <span class="n">CSSSelector</span><span class="o">::</span><span class="nl">Child</span><span class="p">:</span> <span class="c1">//...</span> <span class="p">}</span>

由于这里是一个后代选择器,所以它会循环当前元素所有父结点,用这个父结点和第二个选择器”.text”再执行checkOne的逻辑,checkOne将返回成功,并且它已经是最后一个选择器了,所以判断结束,返回成功匹配。

后代选择器会去查找它的父结点 ,而其它的relationType会相应地去查找关联的元素。

所以不提倡把选择器写得太长,特别是用sass/less写的时候,新手很容易写嵌套很多层,这样会增加查找匹配的负担。例如上面,它需要对下一个父代选器启动一个新的递归的过程,而递归是一种比较耗时的操作。一般是不要超过三层。

上面已经较完整地介绍了匹配的过程,接下来分析匹配之后又是如何设置style的。

(2)设置style

设置style的顺序是先继承父结点,然后使用UA的style,最后再使用用户的style:


1
<span></span><span class="n">style</span><span class="o">-&gt;</span><span class="n">inheritFrom</span><span class="p">(</span><span class="o">*</span><span class="n">state</span><span class="p">.</span><span class="n">parentStyle</span><span class="p">())</span> <span class="n">matchUARules</span><span class="p">(</span><span class="n">collector</span><span class="p">);</span> <span class="n">matchAuthorRules</span><span class="p">(</span><span class="o">*</span><span class="n">state</span><span class="p">.</span><span class="n">element</span><span class="p">(),</span> <span class="n">collector</span><span class="p">);</span>

每一步如果有styleRule匹配成功的话会把它放到当前元素的m_matchedRules的向量里面,并会去计算它的优先级,记录到m_specificity变量。这个优先级是怎么算的呢?


1
<span></span><span class="k">for</span> <span class="p">(</span><span class="k">const</span> <span class="n">CSSSelector</span><span class="o">*</span> <span class="n">selector</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span> <span class="n">selector</span><span class="p">;</span> <span class="n">selector</span> <span class="o">=</span> <span class="n">selector</span><span class="o">-&gt;</span><span class="n">tagHistory</span><span class="p">())</span> <span class="p">{</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">total</span> <span class="o">+</span> <span class="n">selector</span><span class="o">-&gt;</span><span class="n">specificityForOneSelector</span><span class="p">();</span> <span class="p">}</span> <span class="k">return</span> <span class="n">total</span><span class="p">;</span>

如上代码所示,它会从右到左取每个selector的优先级之和。不同类型的selector的优级级定义如下:


1
<span></span> <span class="k">switch</span> <span class="p">(</span><span class="n">m_match</span><span class="p">)</span> <span class="p">{</span> <span class="k">case</span> <span class="nl">Id</span><span class="p">:</span> <span class="k">return</span> <span class="mh">0x010000</span><span class="p">;</span> <span class="k">case</span> <span class="nl">PseudoClass</span><span class="p">:</span> <span class="k">return</span> <span class="mh">0x000100</span><span class="p">;</span> <span class="k">case</span> <span class="nl">Class</span><span class="p">:</span> <span class="k">case</span> <span class="nl">PseudoElement</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeExact</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeSet</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeList</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeHyphen</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeContain</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeBegin</span><span class="p">:</span> <span class="k">case</span> <span class="nl">AttributeEnd</span><span class="p">:</span> <span class="k">return</span> <span class="mh">0x000100</span><span class="p">;</span> <span class="k">case</span> <span class="nl">Tag</span><span class="p">:</span> <span class="k">return</span> <span class="mh">0x000001</span><span class="p">;</span> <span class="k">case</span> <span class="nl">Unknown</span><span class="p">:</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span>

其中id的优先级为0x10000 = 65536,类、属性、伪类的优先级为0x100 = 256,标签选择器的优先级为1。如下面计算所示:


1
<span></span><span class="c">/*优先级为257 = 265 + 1*/</span> <span class="nc">.text</span> <span class="nt">h1</span><span class="p">{</span> <span class="nb">font-size</span><span class="o">:</span> <span class="m">8em</span><span class="p">;</span> <span class="p">}</span> <span class="c">/*优先级为65537 = 65536 + 1*/</span> <span class="nn">#my-text</span> <span class="nt">h1</span><span class="p">{</span> <span class="nb">font-size</span><span class="o">:</span> <span class="m">16em</span><span class="p">;</span> <span class="p">}</span>

内联style的优先级又是怎么处理的呢?

当match完了当前元素的所有CSS规则,全部放到了collector的m_matchedRules里面,再把这个向量根据优先级从小到大排序:


1
<span></span><span class="n">collector</span><span class="p">.</span><span class="n">sortAndTransferMatchedRules</span><span class="p">();</span>

排序的规则是这样的:


1
<span></span><span class="k">static</span> <span class="kr">inline</span> <span class="kt">bool</span> <span class="nf">compareRules</span><span class="p">(</span><span class="k">const</span> <span class="n">MatchedRule</span><span class="o">&amp;</span> <span class="n">matchedRule1</span><span class="p">,</span> <span class="k">const</span> <span class="n">MatchedRule</span><span class="o">&amp;</span> <span class="n">matchedRule2</span><span class="p">)</span> <span class="p">{</span> <span class="kt">unsigned</span> <span class="n">specificity1</span> <span class="o">=</span> <span class="n">matchedRule1</span><span class="p">.</span><span class="n">specificity</span><span class="p">();</span> <span class="kt">unsigned</span> <span class="n">specificity2</span> <span class="o">=</span> <span class="n">matchedRule2</span><span class="p">.</span><span class="n">specificity</span><span class="p">();</span> <span class="k">if</span> <span class="p">(</span><span class="n">specificity1</span> <span class="o">!=</span> <span class="n">specificity2</span><span class="p">)</span> <span class="k">return</span> <span class="n">specificity1</span> <span class="o">&lt;</span> <span class="n">specificity2</span><span class="p">;</span> <span class="k">return</span> <span class="n">matchedRule1</span><span class="p">.</span><span class="n">position</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">matchedRule2</span><span class="p">.</span><span class="n">position</span><span class="p">();</span> <span class="p">}</span>

先按优先级,如果两者的优先级一样,则比较它们的位置。

把css表的样式处理完了之后,blink再去取style的内联样式(这个在已经在构建DOM的时候存放好了),把内联样式push_back到上面排好序的容器里,由于它是由小到大排序的,所以放最后面的优先级肯定是最大的。


1
<span></span><span class="n">collector</span><span class="p">.</span><span class="n">addElementStyleProperties</span><span class="p">(</span><span class="n">state</span><span class="p">.</span><span class="n">element</span><span class="p">()</span><span class="o">-&gt;</span><span class="n">inlineStyle</span><span class="p">(),</span> <span class="n">isInlineStyleCacheable</span><span class="p">);</span>

样式里面的important的优先级又是怎么处理的?

所有的样式规则都处理完毕,最后就是按照它们的优先级计算CSS了。将在下面这个函数执行:


1
<span></span>applyMatchedPropertiesAndCustomPropertyAnimations( state, collector.matchedResult(), element);

这个函数会按照下面的顺序依次设置元素的style:


1
<span></span> <span class="n">applyMatchedProperties</span><span class="o">&lt;</span><span class="n">HighPropertyPriority</span><span class="p">,</span> <span class="n">CheckNeedsApplyPass</span><span class="o">&gt;</span><span class="p">(</span> <span class="n">state</span><span class="p">,</span> <span class="n">matchResult</span><span class="p">.</span><span class="n">allRules</span><span class="p">(),</span> <span class="nb">false</span><span class="p">,</span> <span class="n">applyInheritedOnly</span><span class="p">,</span> <span class="n">needsApplyPass</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="k">auto</span> <span class="nl">range</span> <span class="p">:</span> <span class="n">ImportantAuthorRanges</span><span class="p">(</span><span class="n">matchResult</span><span class="p">))</span> <span class="p">{</span> <span class="n">applyMatchedProperties</span><span class="o">&lt;</span><span class="n">HighPropertyPriority</span><span class="p">,</span> <span class="n">CheckNeedsApplyPass</span><span class="o">&gt;</span><span class="p">(</span> <span class="n">state</span><span class="p">,</span> <span class="n">range</span><span class="p">,</span> <span class="nb">true</span><span class="p">,</span> <span class="n">applyInheritedOnly</span><span class="p">,</span> <span class="n">needsApplyPass</span><span class="p">);</span> <span class="p">}</span>

先设置正常的规则,最后再设置important的规则。所以越往后的设置的规则就会覆盖前面设置的规则。

最后生成的Style是怎么样的?

按优先级计算出来的Style会被放在一个ComputedStyle的对象里面,这个style里面的规则分成了几类,通过检查style对象可以一窥:

从Chrome源码看浏览器如何计算CSS

把它画成一张图表:

从Chrome源码看浏览器如何计算CSS

主要有几类,box是长宽,surround是margin/padding,还有不可继承的nonInheritedData和可继承的styleIneritedData一些属性。Blink还把很多比较少用的属性放到rareData的结构里面,为避免实例化这些不常用的属性占了太多的空间。

具体来说,上面设置的font-size为:22em * 16px = 352px:

从Chrome源码看浏览器如何计算CSS

而所有的色值会变成16进制的整数,如blink定义的两种颜色的色值:


1
<span></span><span class="k">static</span> <span class="k">const</span> <span class="n">RGBA32</span> <span class="n">lightenedBlack</span> <span class="o">=</span> <span class="mh">0xFF545454</span><span class="p">;</span> <span class="k">static</span> <span class="k">const</span> <span class="n">RGBA32</span> <span class="n">darkenedWhite</span> <span class="o">=</span> <span class="mh">0xFFABABAB</span><span class="p">;</span>

同时blink对rgba色值的转化算法:


1
<span></span><span class="n">RGBA32</span> <span class="nf">makeRGBA32FromFloats</span><span class="p">(</span><span class="kt">float</span> <span class="n">r</span><span class="p">,</span> <span class="kt">float</span> <span class="n">g</span><span class="p">,</span> <span class="kt">float</span> <span class="n">b</span><span class="p">,</span> <span class="kt">float</span> <span class="n">a</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">colorFloatToRGBAByte</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="mi">24</span> <span class="o">|</span> <span class="n">colorFloatToRGBAByte</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="mi">16</span> <span class="o">|</span> <span class="n">colorFloatToRGBAByte</span><span class="p">(</span><span class="n">g</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="mi">8</span> <span class="o">|</span> <span class="n">colorFloatToRGBAByte</span><span class="p">(</span><span class="n">b</span><span class="p">);</span> <span class="p">}</span>

从这里可以看到,有些CSS优化建议说要按照下面的顺序书写CSS规则:

1.位置属性(position, top, right, z-index, display, float等)
2.大小(width, height, padding, margin)
3.文字系列(font, line-height, letter-spacing, color- text-align等)
4.背景(background, border等)
5.其他(animation, transition等)

这些顺序对浏览器来说其实是一样的,因为最后都会放到computedStyle里面,而这个style里面的数据是不区分先后顺序的。所以这种建议与其说是优化,倒不如说是规范,大家都按照这个规范写的话,看CSS就可以一目了然,可以很快地看到想要了解的关键信息。

(3)调整style

最后把生成的style做一个调整:


1
<span></span><span class="n">adjustComputedStyle</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">element</span><span class="p">);</span> <span class="c1">//style在state对象里面</span>

调整的内容包括:

第一个:把absolute/fixed定位、float的元素设置成block:


1
<span></span><span class="c1">// Absolute/fixed positioned elements, floating elements and the document</span> <span class="c1">// element need block-like outside display.</span> <span class="k">if</span> <span class="p">(</span><span class="n">style</span><span class="p">.</span><span class="n">hasOutOfFlowPosition</span><span class="p">()</span> <span class="o">||</span> <span class="n">style</span><span class="p">.</span><span class="n">isFloating</span><span class="p">()</span> <span class="o">||</span> <span class="p">(</span><span class="n">element</span> <span class="o">&amp;&amp;</span> <span class="n">element</span><span class="o">-&gt;</span><span class="n">document</span><span class="p">().</span><span class="n">documentElement</span><span class="p">()</span> <span class="o">==</span> <span class="n">element</span><span class="p">))</span> <span class="n">style</span><span class="p">.</span><span class="n">setDisplay</span><span class="p">(</span><span class="n">equivalentBlockDisplay</span><span class="p">(</span><span class="n">style</span><span class="p">.</span><span class="n">display</span><span class="p">()));</span>

第二个,如果有:first-letter选择器时,会把元素display和position做调整:


1
<span></span><span class="k">static</span> <span class="kt">void</span> <span class="nf">adjustStyleForFirstLetter</span><span class="p">(</span><span class="n">ComputedStyle</span><span class="o">&amp;</span> <span class="n">style</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Force inline display (except for floating first-letters).</span> <span class="n">style</span><span class="p">.</span><span class="n">setDisplay</span><span class="p">(</span><span class="n">style</span><span class="p">.</span><span class="n">isFloating</span><span class="p">()</span> <span class="o">?</span> <span class="n">EDisplay</span><span class="o">::</span><span class="nl">Block</span> <span class="p">:</span> <span class="n">EDisplay</span><span class="o">::</span><span class="n">Inline</span><span class="p">);</span> <span class="c1">// CSS2 says first-letter can't be positioned.</span> <span class="n">style</span><span class="p">.</span><span class="n">setPosition</span><span class="p">(</span><span class="n">StaticPosition</span><span class="p">);</span> <span class="p">}</span>

还会对表格元素做一些调整。

到这里,CSS相关的解析和计算就分析完毕,笔者将尝试在下一篇介绍渲染页面的第三步layout的过程。

相关阅读:

  1. 从Chrome源码看浏览器如何构建DOM树
  2. 从Chrome源码看浏览器的事件机制

来源:知乎 www.zhihu.com
作者:李银城

【知乎日报】千万用户的选择,做朋友圈里的新鲜事分享大牛。 点击下载

分享

从Chrome源码看浏览器如何计算CSS:等您坐沙发呢!

Leave your comment