Bug 1375930: Fix rem computation on the root element. r?heycam draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 23 Jun 2017 23:06:25 +0200
changeset 600132 da93973a71b0f552818d5ef92df8e70d71a734c7
parent 599907 6e8f27c30bdd765ad82fa2d9f5f6b720211d4606
child 600133 bbc3fdae795a448cd4e5092e68b9068165dc877a
push id65680
push userbmo:emilio+bugs@crisal.io
push dateSat, 24 Jun 2017 07:47:40 +0000
reviewersheycam
bugs1375930
milestone56.0a1
Bug 1375930: Fix rem computation on the root element. r?heycam I'll be really pissed off if this doesn't make any new test pass and I have to write one... I don't actually know how could we get this so wrong for so long... MozReview-Commit-ID: DK98SS1w5nO
servo/components/style/values/specified/length.rs
--- a/servo/components/style/values/specified/length.rs
+++ b/servo/components/style/values/specified/length.rs
@@ -107,17 +107,16 @@ impl FontRelativeLength {
                                                 reference_font_size,
                                                 context.style().writing_mode,
                                                 context.in_media_query,
                                                 context.device)
         }
 
         let reference_font_size = base_size.resolve(context);
 
-        let root_font_size = context.device.root_font_size();
         match *self {
             FontRelativeLength::Em(length) => reference_font_size.scale_by(length),
             FontRelativeLength::Ex(length) => {
                 match query_font_metrics(context, reference_font_size) {
                     FontMetricsQueryResult::Available(metrics) => metrics.x_height.scale_by(length),
                     // https://drafts.csswg.org/css-values/#ex
                     //
                     //     In the cases where it is impossible or impractical to
@@ -144,17 +143,29 @@ impl FontRelativeLength {
                         if context.style().writing_mode.is_vertical() {
                             reference_font_size.scale_by(length)
                         } else {
                             reference_font_size.scale_by(0.5 * length)
                         }
                     }
                 }
             }
-            FontRelativeLength::Rem(length) => root_font_size.scale_by(length)
+            FontRelativeLength::Rem(length) => {
+                // https://drafts.csswg.org/css-values/#rem:
+                //
+                // 	 When specified on the font-size property of the root
+                // 	 element, the rem units refer to the property’s initial
+                // 	 value.
+                //
+                if context.is_root_element {
+                    reference_font_size.scale_by(length)
+                } else {
+                    context.device.root_font_size().scale_by(length)
+                }
+            }
         }
     }
 }
 
 #[derive(Clone, PartialEq, Copy, Debug)]
 #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
 /// A viewport-relative length.
 ///