[CSSMQ] Use initial values as base for relative units.

Don't use computed style for root element as base for relative lengths. The
media queries spec says:

"Relative units in media queries are based on the initial value, which means
that units are never based on results of declarations."

BUG=269322

Review URL: https://chromiumcodereview.appspot.com/22258007

git-svn-id: svn://svn.chromium.org/blink/trunk@155692 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/third_party/WebKit/LayoutTests/fast/media/mq-relative-constraints-10-expected.html b/third_party/WebKit/LayoutTests/fast/media/mq-relative-constraints-10-expected.html
new file mode 100644
index 0000000..ed7b1237
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/fast/media/mq-relative-constraints-10-expected.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<p style="color:green">This text should be green.</p>
diff --git a/third_party/WebKit/LayoutTests/fast/media/mq-relative-constraints-10.html b/third_party/WebKit/LayoutTests/fast/media/mq-relative-constraints-10.html
new file mode 100644
index 0000000..7858e17
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/fast/media/mq-relative-constraints-10.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>REM lengths in media queries relative to initial value</title>
+    <style>
+        html { font-size: 10000px }
+        body { font-size: 16px }
+        @media (min-width: 2rem) {
+            #t1 { color: green }
+        }
+        @media (min-height: 2rem) {
+            #t2 { color: green }
+        }
+        @media (min-device-width: 2rem) {
+            #t3 { color: green }
+        }
+        @media (min-device-height: 2rem) {
+            #t4 { color: green }
+        }
+    </style>
+</head>
+<body>
+    <p><span id="t1">This</span> <span id="t2">text</span> <span id="t3">should<span> <span id="t4">be green.</span></p>
+</body>
+</html>
diff --git a/third_party/WebKit/Source/core/css/MediaQueryEvaluator.cpp b/third_party/WebKit/Source/core/css/MediaQueryEvaluator.cpp
index 900a627..413c80ea 100644
--- a/third_party/WebKit/Source/core/css/MediaQueryEvaluator.cpp
+++ b/third_party/WebKit/Source/core/css/MediaQueryEvaluator.cpp
@@ -337,7 +337,7 @@
     return false;
 }
 
-static bool computeLength(CSSValue* value, bool strict, RenderStyle* style, RenderStyle* rootStyle, int& result)
+static bool computeLength(CSSValue* value, bool strict, RenderStyle* initialStyle, int& result)
 {
     if (!value->isPrimitiveValue())
         return false;
@@ -350,7 +350,9 @@
     }
 
     if (primitiveValue->isLength()) {
-        result = primitiveValue->computeLength<int>(style, rootStyle, 1.0 /* multiplier */, true /* computingFontSize */);
+        // Relative (like EM) and root relative (like REM) units are always resolved against the initial values
+        // for media queries, hence the two initialStyle parameters.
+        result = primitiveValue->computeLength<int>(initialStyle, initialStyle, 1.0 /* multiplier */, true /* computingFontSize */);
         return true;
     }
 
@@ -361,11 +363,10 @@
 {
     if (value) {
         FloatRect sg = screenRect(frame->page()->mainFrame()->view());
-        RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
         int length;
         long height = sg.height();
         InspectorInstrumentation::applyScreenHeightOverride(frame, &height);
-        return computeLength(value, !frame->document()->inQuirksMode(), style, rootStyle, length) && compareValue(static_cast<int>(height), length, op);
+        return computeLength(value, !frame->document()->inQuirksMode(), style, length) && compareValue(static_cast<int>(height), length, op);
     }
     // ({,min-,max-}device-height)
     // assume if we have a device, assume non-zero
@@ -376,11 +377,10 @@
 {
     if (value) {
         FloatRect sg = screenRect(frame->page()->mainFrame()->view());
-        RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
         int length;
         long width = sg.width();
         InspectorInstrumentation::applyScreenWidthOverride(frame, &width);
-        return computeLength(value, !frame->document()->inQuirksMode(), style, rootStyle, length) && compareValue(static_cast<int>(width), length, op);
+        return computeLength(value, !frame->document()->inQuirksMode(), style, length) && compareValue(static_cast<int>(width), length, op);
     }
     // ({,min-,max-}device-width)
     // assume if we have a device, assume non-zero
@@ -395,9 +395,8 @@
     if (value) {
         if (RenderView* renderView = frame->document()->renderView())
             height = adjustForAbsoluteZoom(height, renderView);
-        RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
         int length;
-        return computeLength(value, !frame->document()->inQuirksMode(), style, rootStyle, length) && compareValue(height, length, op);
+        return computeLength(value, !frame->document()->inQuirksMode(), style, length) && compareValue(height, length, op);
     }
 
     return height;
@@ -411,9 +410,8 @@
     if (value) {
         if (RenderView* renderView = frame->document()->renderView())
             width = adjustForAbsoluteZoom(width, renderView);
-        RenderStyle* rootStyle = frame->document()->documentElement()->renderStyle();
         int length;
-        return computeLength(value, !frame->document()->inQuirksMode(), style, rootStyle, length) && compareValue(width, length, op);
+        return computeLength(value, !frame->document()->inQuirksMode(), style, length) && compareValue(width, length, op);
     }
 
     return width;