Bug 1481961: Rewrite media queries so that they work on an evaluator function. r=xidorn
authorEmilio Cobos Álvarez <emilio@crisal.io>
Wed, 15 Aug 2018 16:07:11 +0200
changeset 489439 0214ed222b79d41b0b53bccb383115c4de74e045
parent 489438 1cee27f6b2336dfeaaeaf45e660f1614118cf7e8
child 489440 f65812990b76c09f46c09842982d7d37f7dd6e2c
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersxidorn
bugs1481961
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1481961: Rewrite media queries so that they work on an evaluator function. r=xidorn This moves most of the code to be Rust, except potentially some evaluator functions, and allows to unblock the use case from any-hover / any-pointer and remove nsMediaFeatures. Differential Revision: https://phabricator.services.mozilla.com/D2976
Cargo.lock
layout/style/ServoBindings.h
layout/style/ServoBindings.toml
layout/style/ServoStyleConsts.h
layout/style/nsCSSKeywordList.h
layout/style/nsCSSProps.cpp
layout/style/nsCSSProps.h
layout/style/nsMediaFeatures.cpp
layout/style/nsMediaFeatures.h
layout/style/nsStyleConsts.h
servo/components/style/Cargo.toml
servo/components/style/cbindgen.toml
servo/components/style/gecko/media_features.rs
servo/components/style/gecko/media_queries.rs
servo/components/style/gecko/mod.rs
servo/components/style/lib.rs
servo/components/style/media_queries/media_condition.rs
servo/components/style/media_queries/media_feature.rs
servo/components/style/media_queries/media_feature_expression.rs
servo/components/style/media_queries/mod.rs
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1474,16 +1474,27 @@ version = "0.1.40"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
  "num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
  "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
+name = "num-derive"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc-macro2 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.14.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "num-integer"
 version = "0.1.35"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -2024,16 +2035,17 @@ dependencies = [
  "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "malloc_size_of 0.0.1",
  "malloc_size_of_derive 0.0.1",
  "matches 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "new-ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "nsstring 0.1.0",
+ "num-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
  "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
  "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "parking_lot 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "rayon 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2738,16 +2750,17 @@ dependencies = [
 "checksum msdos_time 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "65ba9d75bcea84e07812618fedf284a64776c2f2ea0cad6bca7f69739695a958"
 "checksum net2 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "3a80f842784ef6c9a958b68b7516bc7e35883c614004dd94959a4dca1b716c09"
 "checksum new-ordered-float 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8ccbebba6fb53a6d2bdcfaf79cb339bc136dee3bfff54dc337a334bafe36476a"
 "checksum new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0cdc457076c78ab54d5e0d6fa7c47981757f1e34dc39ff92787f217dede586c4"
 "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
 "checksum nom 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce"
 "checksum nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b"
 "checksum num 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "a311b77ebdc5dd4cf6449d81e4135d9f0e3b153839ac90e648a8ef538f923525"
+"checksum num-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d2c31b75c36a993d30c7a13d70513cb93f02acafdd5b7ba250f9b0e18615de7"
 "checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba"
 "checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01"
 "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
 "checksum num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7de20f146db9d920c45ee8ed8f71681fd9ade71909b48c3acbd766aa504cf10"
 "checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d"
 "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
 "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
 "checksum parking_lot 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "69376b761943787ebd5cc85a5bc95958651a22609c5c1c2b65de21786baec72b"
--- a/layout/style/ServoBindings.h
+++ b/layout/style/ServoBindings.h
@@ -733,11 +733,24 @@ const nsTArray<mozilla::dom::Element*>* 
 bool Gecko_GetBoolPrefValue(const char* pref_name);
 
 // Returns true if we're currently performing the servo traversal.
 bool Gecko_IsInServoTraversal();
 
 // Returns true if we're currently on the main thread.
 bool Gecko_IsMainThread();
 
+// Media feature helpers.
+mozilla::StyleDisplayMode Gecko_MediaFeatures_GetDisplayMode(nsIDocument*);
+uint32_t Gecko_MediaFeatures_GetColorDepth(nsIDocument*);
+void Gecko_MediaFeatures_GetDeviceSize(nsIDocument*, nscoord* width, nscoord* height);
+float Gecko_MediaFeatures_GetResolution(nsIDocument*);
+bool Gecko_MediaFeatures_PrefersReducedMotion(nsIDocument*);
+float Gecko_MediaFeatures_GetDevicePixelRatio(nsIDocument*);
+bool Gecko_MediaFeatures_HasSystemMetric(nsIDocument*,
+                                         nsAtom* metric,
+                                         bool is_accessible_from_content);
+bool Gecko_MediaFeatures_IsResourceDocument(nsIDocument*);
+nsAtom* Gecko_MediaFeatures_GetOperatingSystemVersion(nsIDocument*);
+
 } // extern "C"
 
 #endif // mozilla_ServoBindings_h
--- a/layout/style/ServoBindings.toml
+++ b/layout/style/ServoBindings.toml
@@ -142,16 +142,17 @@ rusty-enums = [
     "nsStyleImageLayers_LayerType",
     "nsTimingFunction_Type",
     "mozilla::ServoElementSnapshotFlags",
     "mozilla::Side",
     "mozilla::dom::PlaybackDirection",
     "mozilla::dom::FillMode",
     "mozilla::HalfCorner",
     "mozilla::StyleDisplay",
+    "mozilla::StyleDisplayMode",
     "mozilla::StyleFloatEdge",
     "mozilla::StyleShapeRadius",
     "mozilla::StyleWindowDragging",
     "mozilla::StyleOrient",
     "mozilla::StyleBoxSizing",
     "mozilla::StyleClear",
     "mozilla::StyleColumnFill",
     "mozilla::StyleColumnSpan",
@@ -222,16 +223,17 @@ whitelist-types = [
     "mozilla::ComputedTimingFunction::BeforeFlag",
     "mozilla::SeenPtrs",
     "mozilla::ServoElementSnapshot.*",
     "mozilla::ComputedStyle",
     "mozilla::StyleSheet",
     "mozilla::ServoStyleSheetInner",
     "mozilla::ServoStyleSetSizes",
     "mozilla::ServoTraversalStatistics",
+    "mozilla::StyleDisplayMode",
     "mozilla::css::LoaderReusableStyleSheets",
     "mozilla::css::SheetLoadData",
     "mozilla::css::SheetLoadDataHolder",
     "mozilla::css::SheetParsingMode",
     "mozilla::css::DocumentMatchingFunction",
     "mozilla::dom::IterationCompositeOperation",
     "mozilla::dom::StyleChildrenIterator",
     "mozilla::HalfCorner",
@@ -465,16 +467,17 @@ structs-types = [
     "mozilla::AnonymousCounterStyle",
     "mozilla::AtomArray",
     "mozilla::FontStretch",
     "mozilla::FontSlantStyle",
     "mozilla::FontWeight",
     "mozilla::MallocSizeOf",
     "mozilla::OriginFlags",
     "mozilla::UniquePtr",
+    "mozilla::StyleDisplayMode",
     "ServoRawOffsetArc",
     "DeclarationBlockMutationClosure",
     "nsIContent",
     "nsINode",
     "nsIDocument",
     "nsIDocument_DocumentTheme",
     "nsSimpleContentList",
     "MediumFeaturesChangedResult",
--- a/layout/style/ServoStyleConsts.h
+++ b/layout/style/ServoStyleConsts.h
@@ -73,17 +73,17 @@ enum class StyleAppearance : uint8_t {
   Menuseparator,
   Menuarrow,
   // An image in the menu gutter, like in bookmarks or history.
   Menuimage,
   // A horizontal meter bar.
   Meterbar,
   // The meter bar's meter indicator.
   Meterchunk,
-  // The dropdown button(s) that open up a dropdown list.
+  // The "arrowed" part of the dropdown button that open up a dropdown list.
   MozMenulistButton,
   // For HTML's <input type=number>
   NumberInput,
   // A horizontal progress bar.
   Progressbar,
   // The progress bar's progress indicator
   Progresschunk,
   // A vertical progress bar.
@@ -300,9 +300,17 @@ enum class StyleDisplay : uint8_t {
   MozGridLine,
   MozStack,
   MozInlineStack,
   MozDeck,
   MozGroupbox,
   MozPopup,
 };
 
+// Values for the display-mode media feature.
+enum class StyleDisplayMode : uint8_t {
+  Browser = 0,
+  MinimalUi,
+  Standalone,
+  Fullscreen,
+};
+
 } // namespace mozilla
--- a/layout/style/nsCSSKeywordList.h
+++ b/layout/style/nsCSSKeywordList.h
@@ -76,17 +76,16 @@ CSS_KEY(annotation, annotation)
 CSS_KEY(auto, auto)
 CSS_KEY(baseline, baseline)
 CSS_KEY(blink, blink)
 CSS_KEY(block, block)
 CSS_KEY(blur, blur)
 CSS_KEY(border-box, border_box)
 CSS_KEY(bottom, bottom)
 CSS_KEY(brightness, brightness)
-CSS_KEY(browser, browser)
 CSS_KEY(cell, cell)
 CSS_KEY(center, center)
 CSS_KEY(character-variant, character_variant)
 CSS_KEY(circle, circle)
 CSS_KEY(clip, clip)
 CSS_KEY(closest-corner, closest_corner)
 CSS_KEY(closest-side, closest_side)
 CSS_KEY(col-resize, col_resize)
@@ -124,38 +123,35 @@ CSS_KEY(farthest-corner, farthest_corner
 CSS_KEY(fill, fill)
 CSS_KEY(filled, filled)
 CSS_KEY(fill-box, fill_box)
 CSS_KEY(flat, flat)
 CSS_KEY(flex, flex)
 CSS_KEY(flex-end, flex_end)
 CSS_KEY(flex-start, flex_start)
 CSS_KEY(flow-root, flow_root)
-CSS_KEY(fullscreen, fullscreen)
 CSS_KEY(grab, grab)
 CSS_KEY(grabbing, grabbing)
 CSS_KEY(grayscale, grayscale)
 CSS_KEY(grid, grid)
 CSS_KEY(groove, groove)
 CSS_KEY(help, help)
 CSS_KEY(hidden, hidden)
 CSS_KEY(hue-rotate, hue_rotate)
-CSS_KEY(interlace, interlace)
 CSS_KEY(infinite, infinite)
 CSS_KEY(inline, inline)
 CSS_KEY(inline-block, inline_block)
 CSS_KEY(inline-flex, inline_flex)
 CSS_KEY(inline-grid, inline_grid)
 CSS_KEY(inline-table, inline_table)
 CSS_KEY(inset, inset)
 CSS_KEY(interpolatematrix, interpolatematrix)
 CSS_KEY(accumulatematrix, accumulatematrix)
 CSS_KEY(invert, invert)
 CSS_KEY(justify, justify)
-CSS_KEY(landscape, landscape)
 CSS_KEY(last baseline, last_baseline) // only used for DevTools auto-completion
 CSS_KEY(layout, layout)
 CSS_KEY(left, left)
 CSS_KEY(legacy, legacy)
 CSS_KEY(line-through, line_through)
 CSS_KEY(linear, linear)
 CSS_KEY(list-item, list_item)
 CSS_KEY(mandatory, mandatory)
@@ -167,17 +163,16 @@ CSS_KEY(matrix3d, matrix3d)
 CSS_KEY(max-content, max_content)
 CSS_KEY(middle, middle)
 CSS_KEY(min-content, min_content)
 CSS_KEY(move, move)
 CSS_KEY(n-resize, n_resize)
 CSS_KEY(ne-resize, ne_resize)
 CSS_KEY(nesw-resize, nesw_resize)
 CSS_KEY(no-drop, no_drop)
-CSS_KEY(no-preference, no_preference)
 CSS_KEY(no-repeat, no_repeat)
 CSS_KEY(none, none)
 CSS_KEY(normal, normal)
 CSS_KEY(not-allowed, not_allowed)
 CSS_KEY(ns-resize, ns_resize)
 CSS_KEY(nw-resize, nw_resize)
 CSS_KEY(nwse-resize, nwse_resize)
 CSS_KEY(opacity, opacity)
@@ -188,22 +183,19 @@ CSS_KEY(over, over)
 CSS_KEY(overline, overline)
 CSS_KEY(paint, paint)
 CSS_KEY(padding-box, padding_box)
 CSS_KEY(pan-x, pan_x)
 CSS_KEY(pan-y, pan_y)
 CSS_KEY(perspective, perspective)
 CSS_KEY(pointer, pointer)
 CSS_KEY(polygon, polygon)
-CSS_KEY(portrait, portrait)
 CSS_KEY(preserve-3d, preserve_3d)
 CSS_KEY(progress, progress)
-CSS_KEY(progressive, progressive)
 CSS_KEY(proximity, proximity)
-CSS_KEY(reduce, reduce)
 CSS_KEY(repeat, repeat)
 CSS_KEY(repeat-x, repeat_x)
 CSS_KEY(repeat-y, repeat_y)
 CSS_KEY(ridge, ridge)
 CSS_KEY(right, right)
 CSS_KEY(rotate, rotate)
 CSS_KEY(rotate3d, rotate3d)
 CSS_KEY(rotatex, rotatex)
@@ -237,17 +229,16 @@ CSS_KEY(skewx, skewx)
 CSS_KEY(skewy, skewy)
 CSS_KEY(small-caps, small_caps)
 CSS_KEY(solid, solid)
 CSS_KEY(space-around, space_around)
 CSS_KEY(space-between, space_between)
 CSS_KEY(space-evenly, space_evenly)
 CSS_KEY(span, span)
 CSS_KEY(start, start)
-CSS_KEY(standalone, standalone)
 CSS_KEY(step-end, step_end)
 CSS_KEY(step-start, step_start)
 CSS_KEY(stretch, stretch)
 CSS_KEY(strict, strict)
 CSS_KEY(stroke-box, stroke_box)
 CSS_KEY(style, style)
 CSS_KEY(styleset, styleset)
 CSS_KEY(stylistic, stylistic)
@@ -283,13 +274,12 @@ CSS_KEY(view-box, view_box)
 CSS_KEY(visible, visible)
 CSS_KEY(w-resize, w_resize)
 CSS_KEY(wait, wait)
 CSS_KEY(wavy, wavy)
 CSS_KEY(zoom-in, zoom_in)
 CSS_KEY(zoom-out, zoom_out)
 
 // Appearance keywords for widget styles
-CSS_KEY(minimal-ui, minimal_ui)
 //CSS_KEY(middle, middle)
 //CSS_KEY(start, start)
 CSS_KEY(space, space)
 
--- a/layout/style/nsCSSProps.cpp
+++ b/layout/style/nsCSSProps.cpp
@@ -487,26 +487,16 @@ const KTableEntry nsCSSProps::kAutoCompl
 // </NOTE>
 
 const KTableEntry nsCSSProps::kFontSmoothingKTable[] = {
   { eCSSKeyword_auto, NS_FONT_SMOOTHING_AUTO },
   { eCSSKeyword_grayscale, NS_FONT_SMOOTHING_GRAYSCALE },
   { eCSSKeyword_UNKNOWN, -1 }
 };
 
-const KTableEntry nsCSSProps::kFontVariantAlternatesFuncsKTable[] = {
-  { eCSSKeyword_stylistic, NS_FONT_VARIANT_ALTERNATES_STYLISTIC },
-  { eCSSKeyword_styleset, NS_FONT_VARIANT_ALTERNATES_STYLESET },
-  { eCSSKeyword_character_variant, NS_FONT_VARIANT_ALTERNATES_CHARACTER_VARIANT },
-  { eCSSKeyword_swash, NS_FONT_VARIANT_ALTERNATES_SWASH },
-  { eCSSKeyword_ornaments, NS_FONT_VARIANT_ALTERNATES_ORNAMENTS },
-  { eCSSKeyword_annotation, NS_FONT_VARIANT_ALTERNATES_ANNOTATION },
-  { eCSSKeyword_UNKNOWN, -1 }
-};
-
 const KTableEntry nsCSSProps::kGridAutoFlowKTable[] = {
   { eCSSKeyword_row, NS_STYLE_GRID_AUTO_FLOW_ROW },
   { eCSSKeyword_column, NS_STYLE_GRID_AUTO_FLOW_COLUMN },
   { eCSSKeyword_dense, NS_STYLE_GRID_AUTO_FLOW_DENSE },
   { eCSSKeyword_UNKNOWN, -1 }
 };
 
 const KTableEntry nsCSSProps::kGridTrackBreadthKTable[] = {
--- a/layout/style/nsCSSProps.h
+++ b/layout/style/nsCSSProps.h
@@ -334,17 +334,16 @@ public:
   static const KTableEntry kAlignContentDistribution[]; // <content-distribution>
   static const KTableEntry kAlignContentPosition[]; // <content-position>
   // -- tables for auto-completion of the {align,justify}-{content,items,self} properties --
   static const KTableEntry kAutoCompletionAlignJustifySelf[];
   static const KTableEntry kAutoCompletionAlignItems[];
   static const KTableEntry kAutoCompletionAlignJustifyContent[];
   // ------------------------------------------------------------------
   static const KTableEntry kFontSmoothingKTable[];
-  static const KTableEntry kFontVariantAlternatesFuncsKTable[];
   static const KTableEntry kGridAutoFlowKTable[];
   static const KTableEntry kGridTrackBreadthKTable[];
   static const KTableEntry kLineHeightKTable[];
   static const KTableEntry kContainKTable[];
   static const KTableEntry kOutlineStyleKTable[];
   static const KTableEntry kOverflowKTable[];
   static const KTableEntry kOverflowSubKTable[];
   static const KTableEntry kOverflowClipBoxKTable[];
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -22,43 +22,16 @@
 #include "nsContentUtils.h"
 #include "mozilla/StyleSheet.h"
 #include "mozilla/StyleSheetInlines.h"
 
 using namespace mozilla;
 
 static nsTArray<RefPtr<nsAtom>>* sSystemMetrics = nullptr;
 
-static const nsCSSKTableEntry kOrientationKeywords[] = {
-  { eCSSKeyword_portrait,                 StyleOrientation::Portrait },
-  { eCSSKeyword_landscape,                StyleOrientation::Landscape },
-  { eCSSKeyword_UNKNOWN,                  -1 }
-};
-
-static const nsCSSKTableEntry kScanKeywords[] = {
-  { eCSSKeyword_progressive,              StyleScan::Progressive },
-  { eCSSKeyword_interlace,                StyleScan::Interlace },
-  { eCSSKeyword_UNKNOWN,                  -1 }
-};
-
-static const nsCSSKTableEntry kDisplayModeKeywords[] = {
-  { eCSSKeyword_browser,                 StyleDisplayMode::Browser },
-  { eCSSKeyword_minimal_ui,              StyleDisplayMode::MinimalUi },
-  { eCSSKeyword_standalone,              StyleDisplayMode::Standalone },
-  { eCSSKeyword_fullscreen,              StyleDisplayMode::Fullscreen },
-  { eCSSKeyword_UNKNOWN,                 -1 }
-};
-
-static const nsCSSKeywordAndBoolTableEntry kPrefersReducedMotionKeywords[] = {
-  // NOTE: The third boolean value is the value in the Boolean Context.
-  { eCSSKeyword_no_preference, StylePrefersReducedMotion::NoPreference, false },
-  { eCSSKeyword_reduce,        StylePrefersReducedMotion::Reduce,       true },
-  { eCSSKeyword_UNKNOWN,       -1 }
-};
-
 #ifdef XP_WIN
 struct OperatingSystemVersionInfo {
   LookAndFeel::OperatingSystemVersion mId;
   nsStaticAtom** mName;
 };
 
 // Os version identities used in the -moz-os-version media query.
 const OperatingSystemVersionInfo kOsVersionStrings[] = {
@@ -88,32 +61,16 @@ GetSize(nsIDocument* aDocument)
     //
     // FIXME(emilio, bug 1414600): Not quite!
     return pc->GetPageSize();
   }
 
   return pc->GetVisibleArea().Size();
 }
 
-static void
-GetWidth(nsIDocument* aDocument, const nsMediaFeature*,
-         nsCSSValue& aResult)
-{
-  nsSize size = GetSize(aDocument);
-  aResult.SetFloatValue(CSSPixel::FromAppUnits(size.width), eCSSUnit_Pixel);
-}
-
-static void
-GetHeight(nsIDocument* aDocument, const nsMediaFeature*,
-          nsCSSValue& aResult)
-{
-  nsSize size = GetSize(aDocument);
-  aResult.SetFloatValue(CSSPixel::FromAppUnits(size.height), eCSSUnit_Pixel);
-}
-
 static bool
 IsDeviceSizePageSize(nsIDocument* aDocument)
 {
   nsIDocShell* docShell = aDocument->GetDocShell();
   if (!docShell) {
     return false;
   }
   return docShell->GetDeviceSizeIsPageSize();
@@ -143,347 +100,193 @@ GetDeviceSize(nsIDocument* aDocument)
     return pc->GetPageSize();
   }
 
   nsSize size;
   pc->DeviceContext()->GetDeviceSurfaceDimensions(size.width, size.height);
   return size;
 }
 
-static void
-GetDeviceWidth(nsIDocument* aDocument, const nsMediaFeature*,
-               nsCSSValue& aResult)
-{
-  nsSize size = GetDeviceSize(aDocument);
-  aResult.SetFloatValue(CSSPixel::FromAppUnits(size.width), eCSSUnit_Pixel);
-}
-
-static void
-GetDeviceHeight(nsIDocument* aDocument, const nsMediaFeature*,
-                nsCSSValue& aResult)
-{
-  nsSize size = GetDeviceSize(aDocument);
-  aResult.SetFloatValue(CSSPixel::FromAppUnits(size.height), eCSSUnit_Pixel);
-}
-
-static void
-GetOrientation(nsIDocument* aDocument, const nsMediaFeature*,
-               nsCSSValue& aResult)
-{
-  nsSize size = GetSize(aDocument);
-  // Per spec, square viewports should be 'portrait'
-  auto orientation = size.width > size.height
-    ? StyleOrientation::Landscape : StyleOrientation::Portrait;
-  aResult.SetEnumValue(orientation);
-}
-
-static void
-GetDeviceOrientation(nsIDocument* aDocument, const nsMediaFeature*,
-                     nsCSSValue& aResult)
+bool
+Gecko_MediaFeatures_IsResourceDocument(nsIDocument* aDocument)
 {
-  nsSize size = GetDeviceSize(aDocument);
-  // Per spec, square viewports should be 'portrait'
-  auto orientation = size.width > size.height
-    ? StyleOrientation::Landscape : StyleOrientation::Portrait;
-  aResult.SetEnumValue(orientation);
-}
-
-static void
-GetIsResourceDocument(nsIDocument* aDocument, const nsMediaFeature*,
-                      nsCSSValue& aResult)
-{
-  aResult.SetIntValue(aDocument->IsResourceDoc() ? 1 : 0, eCSSUnit_Integer);
-}
-
-// Helper for two features below
-static void
-MakeArray(const nsSize& aSize, nsCSSValue& aResult)
-{
-  RefPtr<nsCSSValue::Array> a = nsCSSValue::Array::Create(2);
-
-  a->Item(0).SetIntValue(aSize.width, eCSSUnit_Integer);
-  a->Item(1).SetIntValue(aSize.height, eCSSUnit_Integer);
-
-  aResult.SetArrayValue(a, eCSSUnit_Array);
-}
-
-static void
-GetAspectRatio(nsIDocument* aDocument, const nsMediaFeature*,
-               nsCSSValue& aResult)
-{
-  MakeArray(GetSize(aDocument), aResult);
-}
-
-static void
-GetDeviceAspectRatio(nsIDocument* aDocument, const nsMediaFeature*,
-                     nsCSSValue& aResult)
-{
-  MakeArray(GetDeviceSize(aDocument), aResult);
+  return aDocument->IsResourceDoc();
 }
 
 static nsDeviceContext*
 GetDeviceContextFor(nsIDocument* aDocument)
 {
   nsPresContext* pc = aDocument->GetPresContext();
   if (!pc) {
     return nullptr;
   }
 
   // It would be nice to call nsLayoutUtils::GetDeviceContextForScreenInfo here,
   // except for two things:  (1) it can flush, and flushing is bad here, and (2)
   // it doesn't really get us consistency in multi-monitor situations *anyway*.
   return pc->DeviceContext();
 }
 
-static void
-GetColor(nsIDocument* aDocument, const nsMediaFeature*,
-         nsCSSValue& aResult)
+void
+Gecko_MediaFeatures_GetDeviceSize(nsIDocument* aDocument,
+                                  nscoord* aWidth,
+                                  nscoord* aHeight)
+{
+  nsSize size = GetDeviceSize(aDocument);
+  *aWidth = size.width;
+  *aHeight = size.height;
+}
+
+uint32_t
+Gecko_MediaFeatures_GetColorDepth(nsIDocument* aDocument)
 {
   // Use depth of 24 when resisting fingerprinting, or when we're not being
   // rendered.
   uint32_t depth = 24;
 
   if (!nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     if (nsDeviceContext* dx = GetDeviceContextFor(aDocument)) {
       // FIXME: On a monochrome device, return 0!
       dx->GetDepth(depth);
     }
   }
 
   // The spec says to use bits *per color component*, so divide by 3,
   // and round down, since the spec says to use the smallest when the
   // color components differ.
-  depth /= 3;
-  aResult.SetIntValue(int32_t(depth), eCSSUnit_Integer);
+  return depth / 3;
 }
 
-static void
-GetColorIndex(nsIDocument* aDocument, const nsMediaFeature*,
-              nsCSSValue& aResult)
-{
-  // We should return zero if the device does not use a color lookup
-  // table.  Stuart says that our handling of displays with 8-bit
-  // color is bad enough that we never change the lookup table to
-  // match what we're trying to display, so perhaps we should always
-  // return zero.  Given that there isn't any better information
-  // exposed, we don't have much other choice.
-  aResult.SetIntValue(0, eCSSUnit_Integer);
-}
-
-static void
-GetMonochrome(nsIDocument* aDocument, const nsMediaFeature*,
-              nsCSSValue& aResult)
-{
-  // For color devices we should return 0.
-  // FIXME: On a monochrome device, return the actual color depth, not
-  // 0!
-  aResult.SetIntValue(0, eCSSUnit_Integer);
-}
-
-static void
-GetResolution(nsIDocument* aDocument, const nsMediaFeature*,
-              nsCSSValue& aResult)
+float
+Gecko_MediaFeatures_GetResolution(nsIDocument* aDocument)
 {
   // We're returning resolution in terms of device pixels per css pixel, since
   // that is the preferred unit for media queries of resolution. This avoids
   // introducing precision error from conversion to and from less-used
   // physical units like inches.
-
-  float dppx = 1.;
-
-  if (nsDeviceContext* dx = GetDeviceContextFor(aDocument)) {
-    if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
-      dppx = dx->GetFullZoom();
-    } else {
-      // Get the actual device pixel ratio, which also takes zoom into account.
-      dppx =
-        float(AppUnitsPerCSSPixel()) / dx->AppUnitsPerDevPixel();
-    }
+  nsPresContext* pc = aDocument->GetPresContext();
+  if (!pc) {
+    return 1.;
   }
 
-  aResult.SetFloatValue(dppx, eCSSUnit_Pixel);
-}
+  if (pc->GetOverrideDPPX() > 0.) {
+    return pc->GetOverrideDPPX();
+  }
 
-static void
-GetScan(nsIDocument* aDocument, const nsMediaFeature*,
-        nsCSSValue& aResult)
-{
-  // Since Gecko doesn't support the 'tv' media type, the 'scan'
-  // feature is never present.
-  aResult.Reset();
+  if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
+    return pc->DeviceContext()->GetFullZoom();
+  }
+  // Get the actual device pixel ratio, which also takes zoom into account.
+  return float(AppUnitsPerCSSPixel()) / pc->DeviceContext()->AppUnitsPerDevPixel();
 }
 
 static nsIDocument*
 TopDocument(nsIDocument* aDocument)
 {
   nsIDocument* current = aDocument;
   while (nsIDocument* parent = current->GetParentDocument()) {
     current = parent;
   }
   return current;
 }
 
-static void
-GetDisplayMode(nsIDocument* aDocument, const nsMediaFeature*,
-               nsCSSValue& aResult)
+StyleDisplayMode
+Gecko_MediaFeatures_GetDisplayMode(nsIDocument* aDocument)
 {
   nsIDocument* rootDocument = TopDocument(aDocument);
 
   nsCOMPtr<nsISupports> container = rootDocument->GetContainer();
   if (nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container)) {
     nsCOMPtr<nsIWidget> mainWidget;
     baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
     if (mainWidget && mainWidget->SizeMode() == nsSizeMode_Fullscreen) {
-      aResult.SetEnumValue(StyleDisplayMode::Fullscreen);
-      return;
+      return StyleDisplayMode::Fullscreen;
     }
   }
 
   static_assert(nsIDocShell::DISPLAY_MODE_BROWSER == static_cast<int32_t>(StyleDisplayMode::Browser) &&
                 nsIDocShell::DISPLAY_MODE_MINIMAL_UI == static_cast<int32_t>(StyleDisplayMode::MinimalUi) &&
                 nsIDocShell::DISPLAY_MODE_STANDALONE == static_cast<int32_t>(StyleDisplayMode::Standalone) &&
                 nsIDocShell::DISPLAY_MODE_FULLSCREEN == static_cast<int32_t>(StyleDisplayMode::Fullscreen),
                 "nsIDocShell display modes must mach nsStyleConsts.h");
 
   uint32_t displayMode = nsIDocShell::DISPLAY_MODE_BROWSER;
   if (nsIDocShell* docShell = rootDocument->GetDocShell()) {
     docShell->GetDisplayMode(&displayMode);
   }
 
-  aResult.SetEnumValue(static_cast<StyleDisplayMode>(displayMode));
+  return static_cast<StyleDisplayMode>(displayMode);
 }
 
-static void
-GetGrid(nsIDocument* aDocument, const nsMediaFeature*, nsCSSValue& aResult)
-{
-  // Gecko doesn't support grid devices (e.g., ttys), so the 'grid'
-  // feature is always 0.
-  aResult.SetIntValue(0, eCSSUnit_Integer);
-}
-
-static void
-GetDevicePixelRatio(nsIDocument* aDocument, const nsMediaFeature*,
-                    nsCSSValue& aResult)
+float
+Gecko_MediaFeatures_GetDevicePixelRatio(nsIDocument* aDocument)
 {
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
-    aResult.SetFloatValue(1.0, eCSSUnit_Number);
-    return;
+    return 1.0;
   }
 
   nsIPresShell* presShell = aDocument->GetShell();
   if (!presShell) {
-    aResult.SetFloatValue(1.0, eCSSUnit_Number);
-    return;
+    return 1.0;
   }
 
   nsPresContext* pc = presShell->GetPresContext();
   if (!pc) {
-    aResult.SetFloatValue(1.0, eCSSUnit_Number);
-    return;
+    return 1.0;
   }
 
-  float ratio = pc->CSSPixelsToDevPixels(1.0f);
-  aResult.SetFloatValue(ratio, eCSSUnit_Number);
+  return pc->CSSPixelsToDevPixels(1.0f);
 }
 
-static void
-GetTransform3d(nsIDocument* aDocument, const nsMediaFeature*,
-               nsCSSValue& aResult)
-{
-  // Gecko supports 3d transforms, so this feature is always 1.
-  aResult.SetIntValue(1, eCSSUnit_Integer);
-}
-
-static bool
-HasSystemMetric(nsIDocument* aDocument,
-                nsAtom* aMetric,
-                bool aIsAccessibleFromContent)
+bool
+Gecko_MediaFeatures_HasSystemMetric(nsIDocument* aDocument,
+                                    nsAtom* aMetric,
+                                    bool aIsAccessibleFromContent)
 {
   if (aIsAccessibleFromContent &&
       nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return false;
   }
 
   nsMediaFeatures::InitSystemMetrics();
   return sSystemMetrics->IndexOf(aMetric) != sSystemMetrics->NoIndex;
 }
 
-static void
-GetSystemMetric(nsIDocument* aDocument, const nsMediaFeature* aFeature,
-                nsCSSValue& aResult)
+nsAtom*
+Gecko_MediaFeatures_GetOperatingSystemVersion(nsIDocument* aDocument)
 {
-  const bool isAccessibleFromContentPages =
-    !(aFeature->mReqFlags & nsMediaFeature::eUserAgentAndChromeOnly);
-
-  MOZ_ASSERT(!isAccessibleFromContentPages ||
-             *aFeature->mName == nsGkAtoms::_moz_touch_enabled);
-  MOZ_ASSERT(aFeature->mValueType == nsMediaFeature::eBoolInteger,
-             "unexpected type");
-
-  nsAtom* metricAtom = *aFeature->mData.mMetric;
-  bool hasMetric =
-    HasSystemMetric(aDocument, metricAtom, isAccessibleFromContentPages);
-  aResult.SetIntValue(hasMetric ? 1 : 0, eCSSUnit_Integer);
-}
-
-static void
-GetOperatingSystemVersion(nsIDocument* aDocument, const nsMediaFeature* aFeature,
-                         nsCSSValue& aResult)
-{
-  aResult.Reset();
-
-  MOZ_ASSERT(aFeature->mReqFlags & nsMediaFeature::eUserAgentAndChromeOnly);
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
-    return;
+    return nullptr;
   }
 
 #ifdef XP_WIN
   int32_t metricResult;
   if (NS_SUCCEEDED(
         LookAndFeel::GetInt(LookAndFeel::eIntID_OperatingSystemVersionIdentifier,
                             &metricResult))) {
     for (const auto& osVersion : kOsVersionStrings) {
       if (metricResult == osVersion.mId) {
-        aResult.SetAtomIdentValue((*osVersion.mName)->ToAddRefed());
-        break;
+        return *osVersion.mName;
       }
     }
   }
 #endif
+
+  return nullptr;
 }
 
-static void
-GetIsGlyph(nsIDocument* aDocument, const nsMediaFeature* aFeature,
-           nsCSSValue& aResult)
-{
-  MOZ_ASSERT(aFeature->mReqFlags & nsMediaFeature::eUserAgentAndChromeOnly);
-  aResult.SetIntValue(aDocument->IsSVGGlyphsDocument() ? 1 : 0, eCSSUnit_Integer);
-}
-
-static bool
-PrefersReducedMotion(nsIDocument* aDocument)
+bool
+Gecko_MediaFeatures_PrefersReducedMotion(nsIDocument* aDocument)
 {
   if (nsContentUtils::ShouldResistFingerprinting(aDocument)) {
     return false;
   }
   return LookAndFeel::GetInt(LookAndFeel::eIntID_PrefersReducedMotion, 0) == 1;
 }
 
-static void
-GetPrefersReducedMotion(nsIDocument* aDocument,
-                        const nsMediaFeature*,
-                        nsCSSValue& aResult)
-{
-  auto prefersReducedMotion = PrefersReducedMotion(aDocument)
-    ? StylePrefersReducedMotion::Reduce
-    : StylePrefersReducedMotion::NoPreference;
-
-  aResult.SetEnumValue(prefersReducedMotion);
-}
-
 /* static */ void
 nsMediaFeatures::InitSystemMetrics()
 {
   if (sSystemMetrics)
     return;
 
   MOZ_ASSERT(NS_IsMainThread());
 
@@ -611,392 +414,8 @@ nsMediaFeatures::FreeSystemMetrics()
   sSystemMetrics = nullptr;
 }
 
 /* static */ void
 nsMediaFeatures::Shutdown()
 {
   FreeSystemMetrics();
 }
-
-/*
- * Adding new media features requires (1) adding the new feature to this
- * array, with appropriate entries (and potentially any new code needed
- * to support new types in these entries and (2) ensuring that either
- * nsPresContext::MediaFeatureValuesChanged or
- * nsPresContext::PostMediaFeatureValuesChangedEvent is called when the
- * value that would be returned by the entry's mGetter changes.
- */
-
-/* static */ const nsMediaFeature
-nsMediaFeatures::features[] = {
-  {
-    &nsGkAtoms::width,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eLength,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetWidth
-  },
-  {
-    &nsGkAtoms::height,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eLength,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetHeight
-  },
-  {
-    &nsGkAtoms::deviceWidth,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eLength,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetDeviceWidth
-  },
-  {
-    &nsGkAtoms::deviceHeight,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eLength,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetDeviceHeight
-  },
-  {
-    &nsGkAtoms::orientation,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eEnumerated,
-    nsMediaFeature::eNoRequirements,
-    { kOrientationKeywords },
-    GetOrientation
-  },
-  {
-    &nsGkAtoms::aspectRatio,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eIntRatio,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetAspectRatio
-  },
-  {
-    &nsGkAtoms::deviceAspectRatio,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eIntRatio,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetDeviceAspectRatio
-  },
-  {
-    &nsGkAtoms::color,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eInteger,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetColor
-  },
-  {
-    &nsGkAtoms::colorIndex,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eInteger,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetColorIndex
-  },
-  {
-    &nsGkAtoms::monochrome,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eInteger,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetMonochrome
-  },
-  {
-    &nsGkAtoms::resolution,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eResolution,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetResolution
-  },
-  {
-    &nsGkAtoms::scan,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eEnumerated,
-    nsMediaFeature::eNoRequirements,
-    { kScanKeywords },
-    GetScan
-  },
-  {
-    &nsGkAtoms::grid,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetGrid
-  },
-  {
-    &nsGkAtoms::displayMode,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eEnumerated,
-    nsMediaFeature::eNoRequirements,
-    { kDisplayModeKeywords },
-    GetDisplayMode
-  },
-  {
-    &nsGkAtoms::prefersReducedMotion,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolEnumerated,
-    nsMediaFeature::eNoRequirements,
-    { kPrefersReducedMotionKeywords },
-    GetPrefersReducedMotion
-  },
-
-  // Webkit extensions that we support for de-facto web compatibility
-  // -webkit-{min|max}-device-pixel-ratio (controlled with its own pref):
-  {
-    &nsGkAtoms::devicePixelRatio,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eFloat,
-    nsMediaFeature::eHasWebkitPrefix |
-      nsMediaFeature::eWebkitDevicePixelRatioPrefEnabled,
-    { nullptr },
-    GetDevicePixelRatio
-  },
-  // -webkit-transform-3d:
-  {
-    &nsGkAtoms::transform_3d,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eHasWebkitPrefix,
-    { nullptr },
-    GetTransform3d
-  },
-
-  // Mozilla extensions
-  {
-    &nsGkAtoms::_moz_device_pixel_ratio,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eFloat,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    GetDevicePixelRatio
-  },
-  {
-    &nsGkAtoms::_moz_device_orientation,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eEnumerated,
-    nsMediaFeature::eNoRequirements,
-    { kOrientationKeywords },
-    GetDeviceOrientation
-  },
-  {
-    &nsGkAtoms::_moz_is_resource_document,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { nullptr },
-    GetIsResourceDocument
-  },
-  {
-    &nsGkAtoms::_moz_scrollbar_start_backward,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::scrollbar_start_backward },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_scrollbar_start_forward,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::scrollbar_start_forward },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_scrollbar_end_backward,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::scrollbar_end_backward },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_scrollbar_end_forward,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::scrollbar_end_forward },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_scrollbar_thumb_proportional,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::scrollbar_thumb_proportional },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_overlay_scrollbars,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::overlay_scrollbars },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_windows_default_theme,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::windows_default_theme },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_mac_graphite_theme,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::mac_graphite_theme },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_mac_yosemite_theme,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::mac_yosemite_theme },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_windows_accent_color_in_titlebar,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::windows_accent_color_in_titlebar },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_windows_compositor,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::windows_compositor },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_windows_classic,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::windows_classic },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_windows_glass,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::windows_glass },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_touch_enabled,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    // FIXME(emilio): Restrict (or remove?) when bug 1035774 lands.
-    nsMediaFeature::eNoRequirements,
-    { &nsGkAtoms::touch_enabled },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_menubar_drag,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::menubar_drag },
-    GetSystemMetric
-  },
-  {
-    &nsGkAtoms::_moz_os_version,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eIdent,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { nullptr },
-    GetOperatingSystemVersion
-  },
-
-  {
-    &nsGkAtoms::_moz_swipe_animation_enabled,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::swipe_animation_enabled },
-    GetSystemMetric
-  },
-
-  {
-    &nsGkAtoms::_moz_gtk_csd_available,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::gtk_csd_available },
-    GetSystemMetric
-  },
-
-  {
-    &nsGkAtoms::_moz_gtk_csd_minimize_button,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::gtk_csd_minimize_button },
-    GetSystemMetric
-  },
-
-  {
-    &nsGkAtoms::_moz_gtk_csd_maximize_button,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::gtk_csd_maximize_button },
-    GetSystemMetric
-  },
-
-  {
-    &nsGkAtoms::_moz_gtk_csd_close_button,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::gtk_csd_close_button },
-    GetSystemMetric
-  },
-
-  {
-    &nsGkAtoms::_moz_system_dark_theme,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { &nsGkAtoms::system_dark_theme },
-    GetSystemMetric
-  },
-
-  // Internal -moz-is-glyph media feature: applies only inside SVG glyphs.
-  // Internal because it is really only useful in the user agent anyway
-  //  and therefore not worth standardizing.
-  {
-    &nsGkAtoms::_moz_is_glyph,
-    nsMediaFeature::eMinMaxNotAllowed,
-    nsMediaFeature::eBoolInteger,
-    nsMediaFeature::eUserAgentAndChromeOnly,
-    { nullptr },
-    GetIsGlyph
-  },
-  // Null-mName terminator:
-  {
-    nullptr,
-    nsMediaFeature::eMinMaxAllowed,
-    nsMediaFeature::eInteger,
-    nsMediaFeature::eNoRequirements,
-    { nullptr },
-    nullptr
-  },
-};
--- a/layout/style/nsMediaFeatures.h
+++ b/layout/style/nsMediaFeatures.h
@@ -7,118 +7,17 @@
 /* the features that media queries can test */
 
 #ifndef nsMediaFeatures_h_
 #define nsMediaFeatures_h_
 
 #include <stdint.h>
 #include "nsCSSProps.h"
 
-class nsAtom;
-class nsIDocument;
-class nsCSSValue;
-class nsStaticAtom;
-
-struct nsCSSKeywordAndBoolTableEntry : public nsCSSKTableEntry {
-  constexpr nsCSSKeywordAndBoolTableEntry(nsCSSKeyword aKeyword, int16_t aValue)
-    : nsCSSKTableEntry(aKeyword, aValue)
-    , mValueInBooleanContext(true)
-  {
-  }
-
-  template<typename T,
-           typename = typename std::enable_if<std::is_enum<T>::value>::type>
-  constexpr nsCSSKeywordAndBoolTableEntry(
-      nsCSSKeyword aKeyword,
-      T aValue,
-      bool aValueInBooleanContext)
-    : nsCSSKTableEntry(aKeyword, aValue)
-    , mValueInBooleanContext(aValueInBooleanContext)
-  {
-  }
-
-  bool mValueInBooleanContext;
-};
-
-struct nsMediaFeature;
-typedef void (*nsMediaFeatureValueGetter)(nsIDocument* aDocument,
-                                          const nsMediaFeature* aFeature,
-                                          nsCSSValue& aResult);
-
-struct nsMediaFeature
-{
-  nsStaticAtom** mName; // extra indirection to point to nsGkAtoms members
-
-  enum RangeType { eMinMaxAllowed, eMinMaxNotAllowed };
-  RangeType mRangeType;
-
-  enum ValueType {
-    // All value types allow eCSSUnit_Null to indicate that no value
-    // was given (in addition to the types listed below).
-    eLength,         // values are eCSSUnit_Pixel
-    eInteger,        // values are eCSSUnit_Integer
-    eFloat,          // values are eCSSUnit_Number
-    eBoolInteger,    // values are eCSSUnit_Integer (0, -0, or 1 only)
-    eIntRatio,       // values are eCSSUnit_Array of two eCSSUnit_Integer
-    eResolution,     // values are in eCSSUnit_Inch (for dpi),
-                     //   eCSSUnit_Pixel (for dppx), or
-                     //   eCSSUnit_Centimeter (for dpcm)
-    eEnumerated,     // values are eCSSUnit_Enumerated (uses keyword table)
-    eBoolEnumerated, // values are eCSSUnit_Enumerated (uses keyword and boolean
-                     // pair table)
-    eIdent           // values are eCSSUnit_Ident
-    // Note that a number of pieces of code (both for parsing and
-    // for matching of valueless expressions) assume that all numeric
-    // value types cannot be negative.  The parsing code also does
-    // not allow zeros in eIntRatio types.
-  };
-  ValueType mValueType;
-
-  enum RequirementFlags : uint8_t {
-    // Bitfield of requirements that must be satisfied in order for this
-    // media feature to be active.
-    eNoRequirements = 0,
-    eHasWebkitPrefix = 1 << 0, // Feature name must start w/ "-webkit-", even
-                               // before any "min-"/"max-" qualifier.
-
-    // Feature is only supported if the pref
-    // "layout.css.prefixes.device-pixel-ratio-webkit" is enabled.
-    // (Should only be used for -webkit-device-pixel-ratio.)
-    eWebkitDevicePixelRatioPrefEnabled = 1 << 1,
-    // Feature is only usable from UA sheets and chrome:// urls.
-    eUserAgentAndChromeOnly = 1 << 2,
-  };
-  uint8_t mReqFlags;
-
-  union {
-    // In static arrays, it's the first member that's initialized.  We
-    // need that to be void* so we can initialize both other types.
-    // This member should never be accessed by name.
-    const void* mInitializer_;
-    // If mValueType == eEnumerated:  const int32_t*: keyword table in
-    //   the same format as the keyword tables in nsCSSProps.
-    const nsCSSKTableEntry* mKeywordTable;
-    // If mValueType == eBoolEnumerated:  similar to the above but having a
-    // boolean field representing the value in Boolean context.
-    const nsCSSKeywordAndBoolTableEntry* mKeywordAndBoolTable;
-    // If mGetter == GetSystemMetric (which implies mValueType ==
-    //   eBoolInteger): nsAtom * const *, for the system metric.
-    nsAtom * const * mMetric;
-  } mData;
-
-  // A function that returns the current value for this feature for a
-  // given presentation.  If it returns eCSSUnit_Null, the feature is
-  // not present.
-  nsMediaFeatureValueGetter mGetter;
-};
-
 class nsMediaFeatures
 {
 public:
   static void InitSystemMetrics();
   static void FreeSystemMetrics();
   static void Shutdown();
-
-  // Terminated with an entry whose mName is null.
-  static const nsMediaFeature features[];
 };
 
 #endif /* !defined(nsMediaFeatures_h_) */
--- a/layout/style/nsStyleConsts.h
+++ b/layout/style/nsStyleConsts.h
@@ -314,22 +314,16 @@ enum class StyleImageLayerRepeat : uint8
   NoRepeat = 0x00,
   RepeatX,
   RepeatY,
   Repeat,
   Space,
   Round
 };
 
-enum class StylePrefersReducedMotion : uint8_t {
-  NoPreference,
-  Reduce,
-};
-
-
 // See nsStyleImageLayers
 #define NS_STYLE_IMAGELAYER_SIZE_CONTAIN             0
 #define NS_STYLE_IMAGELAYER_SIZE_COVER               1
 
 // Mask mode
 #define NS_STYLE_MASK_MODE_ALPHA                0
 #define NS_STYLE_MASK_MODE_LUMINANCE            1
 #define NS_STYLE_MASK_MODE_MATCH_SOURCE         2
@@ -1158,35 +1152,11 @@ enum class StyleOverscrollBehavior : uin
   None,
 };
 
 // See nsStyleDisplay::mScrollSnapType{X,Y}
 #define NS_STYLE_SCROLL_SNAP_TYPE_NONE              0
 #define NS_STYLE_SCROLL_SNAP_TYPE_MANDATORY         1
 #define NS_STYLE_SCROLL_SNAP_TYPE_PROXIMITY         2
 
-/*****************************************************************************
- * Constants for media features.                                             *
- *****************************************************************************/
-
-// orientation
-enum class StyleOrientation : uint8_t {
-  Portrait = 0,
-  Landscape,
-};
-
-// scan
-enum class StyleScan : uint8_t {
-  Progressive = 0,
-  Interlace,
-};
-
-// display-mode
-enum class StyleDisplayMode : uint8_t {
-  Browser = 0,
-  MinimalUi,
-  Standalone,
-  Fullscreen,
-};
-
 } // namespace mozilla
 
 #endif /* nsStyleConsts_h___ */
--- a/servo/components/style/Cargo.toml
+++ b/servo/components/style/Cargo.toml
@@ -43,18 +43,19 @@ itertools = "0.7.6"
 itoa = "0.4"
 lazy_static = "1"
 log = "0.4"
 malloc_size_of = { path = "../malloc_size_of" }
 malloc_size_of_derive = { path = "../malloc_size_of_derive" }
 matches = "0.1"
 nsstring = {path = "../../support/gecko/nsstring", optional = true}
 num_cpus = {version = "1.1.0", optional = true}
-num-integer = "0.1.32"
-num-traits = "0.1.32"
+num-integer = "0.1"
+num-traits = "0.1"
+num-derive = "0.2"
 new-ordered-float = "1.0"
 owning_ref = "0.3.3"
 parking_lot = "0.6"
 precomputed-hash = "0.1.1"
 rayon = "1"
 selectors = { path = "../selectors" }
 serde = {version = "1.0", optional = true, features = ["derive"]}
 servo_arc = { path = "../servo_arc" }
--- a/servo/components/style/cbindgen.toml
+++ b/servo/components/style/cbindgen.toml
@@ -17,10 +17,10 @@ namespaces = ["mozilla"]
 [struct]
 derive_eq = true
 
 [enum]
 derive_helper_methods = true
 
 [export]
 prefix = "Style"
-include = ["StyleDisplay", "StyleAppearance"]
+include = ["StyleDisplay", "StyleAppearance", "StyleDisplayMode"]
 item_types = ["enums"]
new file mode 100644
--- /dev/null
+++ b/servo/components/style/gecko/media_features.rs
@@ -0,0 +1,610 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+//! Gecko's media feature list and evaluator.
+
+use Atom;
+use app_units::Au;
+use euclid::Size2D;
+use gecko_bindings::bindings;
+use values::computed::CSSPixelLength;
+use values::computed::Resolution;
+
+use media_queries::Device;
+use media_queries::media_feature::{MediaFeatureDescription, Evaluator};
+use media_queries::media_feature::{AllowsRanges, ParsingRequirements};
+use media_queries::media_feature_expression::{AspectRatio, RangeOrOperator};
+
+macro_rules! feature {
+    ($name:expr, $allows_ranges:expr, $evaluator:expr, $reqs:expr,) => {
+        MediaFeatureDescription {
+            name: $name,
+            allows_ranges: $allows_ranges,
+            evaluator: $evaluator,
+            requirements: $reqs,
+        }
+    }
+}
+
+fn viewport_size(device: &Device) -> Size2D<Au> {
+    let pc = device.pres_context();
+    if pc.mIsRootPaginatedDocument() != 0 {
+        // We want the page size, including unprintable areas and margins.
+        // FIXME(emilio, bug 1414600): Not quite!
+        let area = &pc.mPageSize;
+        return Size2D::new(Au(area.width), Au(area.height))
+    }
+    device.au_viewport_size()
+}
+
+fn device_size(device: &Device) -> Size2D<Au> {
+    let mut width = 0;
+    let mut height = 0;
+    unsafe {
+        bindings::Gecko_MediaFeatures_GetDeviceSize(
+            device.document(),
+            &mut width,
+            &mut height,
+        );
+    }
+    Size2D::new(Au(width), Au(height))
+}
+
+fn eval_width(
+    device: &Device,
+    value: Option<CSSPixelLength>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        value.map(Au::from),
+        viewport_size(device).width,
+    )
+}
+
+fn eval_device_width(
+    device: &Device,
+    value: Option<CSSPixelLength>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        value.map(Au::from),
+        device_size(device).width,
+    )
+}
+
+fn eval_height(
+    device: &Device,
+    value: Option<CSSPixelLength>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        value.map(Au::from),
+        viewport_size(device).height,
+    )
+}
+
+fn eval_device_height(
+    device: &Device,
+    value: Option<CSSPixelLength>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        value.map(Au::from),
+        device_size(device).height,
+    )
+}
+
+fn eval_aspect_ratio_for<F>(
+    device: &Device,
+    query_value: Option<AspectRatio>,
+    range_or_operator: Option<RangeOrOperator>,
+    get_size: F,
+) -> bool
+where
+    F: FnOnce(&Device) -> Size2D<Au>,
+{
+    let query_value = match query_value {
+        Some(v) => v,
+        None => return true,
+    };
+
+    let size = get_size(device);
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        Some(size.height.0 as u64 * query_value.0 as u64),
+        size.width.0 as u64 * query_value.1 as u64,
+    )
+}
+
+fn eval_aspect_ratio(
+    device: &Device,
+    query_value: Option<AspectRatio>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    eval_aspect_ratio_for(device, query_value, range_or_operator, viewport_size)
+}
+
+fn eval_device_aspect_ratio(
+    device: &Device,
+    query_value: Option<AspectRatio>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    eval_aspect_ratio_for(device, query_value, range_or_operator, device_size)
+}
+
+fn eval_device_pixel_ratio(
+    device: &Device,
+    query_value: Option<f32>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    let ratio = unsafe {
+        bindings::Gecko_MediaFeatures_GetDevicePixelRatio(device.document())
+    };
+
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        query_value,
+        ratio,
+    )
+}
+
+#[derive(Debug, Copy, Clone, FromPrimitive, ToCss, Parse)]
+#[repr(u8)]
+enum Orientation {
+    Landscape,
+    Portrait,
+}
+
+fn eval_orientation_for<F>(
+    device: &Device,
+    value: Option<Orientation>,
+    get_size: F,
+) -> bool
+where
+    F: FnOnce(&Device) -> Size2D<Au>,
+{
+    let query_orientation = match value {
+        Some(v) => v,
+        None => return true,
+    };
+
+    let size = get_size(device);
+
+    // Per spec, square viewports should be 'portrait'
+    let is_landscape = size.width > size.height;
+    match query_orientation {
+        Orientation::Landscape => is_landscape,
+        Orientation::Portrait => !is_landscape,
+    }
+}
+
+fn eval_orientation(
+    device: &Device,
+    value: Option<Orientation>,
+) -> bool {
+    eval_orientation_for(device, value, viewport_size)
+}
+
+fn eval_device_orientation(
+    device: &Device,
+    value: Option<Orientation>,
+) -> bool {
+    eval_orientation_for(device, value, device_size)
+}
+
+/// Values for the display-mode media feature.
+#[derive(Debug, Copy, Clone, FromPrimitive, ToCss, Parse)]
+#[repr(u8)]
+#[allow(missing_docs)]
+pub enum DisplayMode {
+  Browser = 0,
+  MinimalUi,
+  Standalone,
+  Fullscreen,
+}
+
+fn eval_display_mode(
+    device: &Device,
+    query_value: Option<DisplayMode>,
+) -> bool {
+    let query_value = match query_value {
+        Some(v) => v,
+        None => return true,
+    };
+
+    let gecko_display_mode = unsafe {
+        bindings::Gecko_MediaFeatures_GetDisplayMode(device.document())
+    };
+
+    // NOTE: cbindgen guarantees the same representation.
+    gecko_display_mode as u8 == query_value as u8
+}
+
+fn eval_grid(_: &Device, query_value: Option<bool>, _: Option<RangeOrOperator>) -> bool {
+    // Gecko doesn't support grid devices (e.g., ttys), so the 'grid' feature
+    // is always 0.
+    let supports_grid = false;
+    query_value.map_or(supports_grid, |v| v == supports_grid)
+}
+
+fn eval_transform_3d(
+    _: &Device,
+    query_value: Option<bool>,
+    _: Option<RangeOrOperator>,
+) -> bool {
+    let supports_transforms = true;
+    query_value.map_or(supports_transforms, |v| v == supports_transforms)
+}
+
+#[derive(Debug, Copy, Clone, FromPrimitive, ToCss, Parse)]
+#[repr(u8)]
+enum Scan {
+    Progressive,
+    Interlace,
+}
+
+fn eval_scan(_: &Device, _: Option<Scan>) -> bool {
+    // Since Gecko doesn't support the 'tv' media type, the 'scan' feature never
+    // matches.
+    false
+}
+
+fn eval_color(
+    device: &Device,
+    query_value: Option<u32>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    let color_bits_per_channel =
+        unsafe { bindings::Gecko_MediaFeatures_GetColorDepth(device.document()) };
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        query_value,
+        color_bits_per_channel,
+    )
+}
+
+fn eval_color_index(
+    _: &Device,
+    query_value: Option<u32>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    // We should return zero if the device does not use a color lookup
+    // table.
+    let index = 0;
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        query_value,
+        index,
+    )
+}
+
+fn eval_monochrome(
+    _: &Device,
+    query_value: Option<u32>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    // For color devices we should return 0.
+    // FIXME: On a monochrome device, return the actual color depth, not 0!
+    let depth = 0;
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        query_value,
+        depth,
+    )
+}
+
+fn eval_resolution(
+    device: &Device,
+    query_value: Option<Resolution>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool {
+    let resolution_dppx =
+        unsafe { bindings::Gecko_MediaFeatures_GetResolution(device.document()) };
+    RangeOrOperator::evaluate(
+        range_or_operator,
+        query_value.map(|r| r.dppx()),
+        resolution_dppx,
+    )
+}
+
+#[derive(Debug, Copy, Clone, FromPrimitive, ToCss, Parse)]
+#[repr(u8)]
+enum PrefersReducedMotion {
+    NoPreference,
+    Reduce,
+}
+
+fn eval_prefers_reduced_motion(
+    device: &Device,
+    query_value: Option<PrefersReducedMotion>,
+) -> bool {
+    let prefers_reduced =
+        unsafe { bindings::Gecko_MediaFeatures_PrefersReducedMotion(device.document()) };
+    let query_value = match query_value {
+        Some(v) => v,
+        None => return prefers_reduced,
+    };
+
+    match query_value {
+        PrefersReducedMotion::NoPreference => !prefers_reduced,
+        PrefersReducedMotion::Reduce => prefers_reduced,
+    }
+}
+
+fn eval_moz_is_glyph(
+    device: &Device,
+    query_value: Option<bool>,
+    _: Option<RangeOrOperator>,
+) -> bool {
+    let is_glyph = unsafe { (*device.document()).mIsSVGGlyphsDocument() };
+    query_value.map_or(is_glyph, |v| v == is_glyph)
+}
+
+fn eval_moz_is_resource_document(
+    device: &Device,
+    query_value: Option<bool>,
+    _: Option<RangeOrOperator>,
+) -> bool {
+    let is_resource_doc  = unsafe {
+        bindings::Gecko_MediaFeatures_IsResourceDocument(device.document())
+    };
+    query_value.map_or(is_resource_doc, |v| v == is_resource_doc)
+}
+
+fn eval_system_metric(
+    device: &Device,
+    query_value: Option<bool>,
+    metric: Atom,
+    accessible_from_content: bool,
+) -> bool {
+    let supports_metric = unsafe {
+        bindings::Gecko_MediaFeatures_HasSystemMetric(
+            device.document(),
+            metric.as_ptr(),
+            accessible_from_content,
+        )
+    };
+    query_value.map_or(supports_metric, |v| v == supports_metric)
+}
+
+fn eval_moz_touch_enabled(
+    device: &Device,
+    query_value: Option<bool>,
+    _: Option<RangeOrOperator>,
+) -> bool {
+    eval_system_metric(
+        device,
+        query_value,
+        atom!("touch-enabled"),
+        /* accessible_from_content = */ true,
+    )
+}
+
+fn eval_moz_os_version(
+    device: &Device,
+    query_value: Option<Atom>,
+    _: Option<RangeOrOperator>,
+) -> bool {
+    let query_value = match query_value {
+        Some(v) => v,
+        None => return false,
+    };
+
+    let os_version = unsafe {
+        bindings::Gecko_MediaFeatures_GetOperatingSystemVersion(device.document())
+    };
+
+    query_value.as_ptr() == os_version
+}
+
+macro_rules! system_metric_feature {
+    ($feature_name:expr, $metric_name:expr) => {
+        {
+            fn __eval(
+                device: &Device,
+                query_value: Option<bool>,
+                _: Option<RangeOrOperator>,
+            ) -> bool {
+                eval_system_metric(
+                    device,
+                    query_value,
+                    $metric_name,
+                    /* accessible_from_content = */ false,
+                )
+            }
+
+            feature!(
+                $feature_name,
+                AllowsRanges::No,
+                Evaluator::BoolInteger(__eval),
+                ParsingRequirements::CHROME_AND_UA_ONLY,
+            )
+        }
+    }
+}
+
+lazy_static! {
+    /// Adding new media features requires (1) adding the new feature to this
+    /// array, with appropriate entries (and potentially any new code needed
+    /// to support new types in these entries and (2) ensuring that either
+    /// nsPresContext::MediaFeatureValuesChanged is called when the value that
+    /// would be returned by the evaluator function could change.
+    pub static ref MEDIA_FEATURES: [MediaFeatureDescription; 43] = [
+        feature!(
+            atom!("width"),
+            AllowsRanges::Yes,
+            Evaluator::Length(eval_width),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("height"),
+            AllowsRanges::Yes,
+            Evaluator::Length(eval_height),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("aspect-ratio"),
+            AllowsRanges::Yes,
+            Evaluator::IntRatio(eval_aspect_ratio),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("orientation"),
+            AllowsRanges::No,
+            keyword_evaluator!(eval_orientation, Orientation),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("device-width"),
+            AllowsRanges::Yes,
+            Evaluator::Length(eval_device_width),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("device-height"),
+            AllowsRanges::Yes,
+            Evaluator::Length(eval_device_height),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("device-aspect-ratio"),
+            AllowsRanges::Yes,
+            Evaluator::IntRatio(eval_device_aspect_ratio),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("-moz-device-orientation"),
+            AllowsRanges::No,
+            keyword_evaluator!(eval_device_orientation, Orientation),
+            ParsingRequirements::empty(),
+        ),
+        // Webkit extensions that we support for de-facto web compatibility.
+        // -webkit-{min|max}-device-pixel-ratio (controlled with its own pref):
+        feature!(
+            atom!("device-pixel-ratio"),
+            AllowsRanges::Yes,
+            Evaluator::Float(eval_device_pixel_ratio),
+            ParsingRequirements::WEBKIT_PREFIX |
+                ParsingRequirements::WEBKIT_DEVICE_PIXEL_RATIO_PREF_ENABLED,
+        ),
+        // -webkit-transform-3d.
+        feature!(
+            atom!("transform-3d"),
+            AllowsRanges::No,
+            Evaluator::BoolInteger(eval_transform_3d),
+            ParsingRequirements::WEBKIT_PREFIX,
+        ),
+        feature!(
+            atom!("-moz-device-pixel-ratio"),
+            AllowsRanges::Yes,
+            Evaluator::Float(eval_device_pixel_ratio),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("resolution"),
+            AllowsRanges::Yes,
+            Evaluator::Resolution(eval_resolution),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("display-mode"),
+            AllowsRanges::No,
+            keyword_evaluator!(eval_display_mode, DisplayMode),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("grid"),
+            AllowsRanges::No,
+            Evaluator::BoolInteger(eval_grid),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("scan"),
+            AllowsRanges::No,
+            keyword_evaluator!(eval_scan, Scan),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("color"),
+            AllowsRanges::Yes,
+            Evaluator::Integer(eval_color),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("color-index"),
+            AllowsRanges::Yes,
+            Evaluator::Integer(eval_color_index),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("monochrome"),
+            AllowsRanges::Yes,
+            Evaluator::Integer(eval_monochrome),
+            ParsingRequirements::empty(),
+        ),
+        feature!(
+            atom!("prefers-reduced-motion"),
+            AllowsRanges::No,
+            keyword_evaluator!(eval_prefers_reduced_motion, PrefersReducedMotion),
+            ParsingRequirements::empty(),
+        ),
+
+        // Internal -moz-is-glyph media feature: applies only inside SVG glyphs.
+        // Internal because it is really only useful in the user agent anyway
+        // and therefore not worth standardizing.
+        feature!(
+            atom!("-moz-is-glyph"),
+            AllowsRanges::No,
+            Evaluator::BoolInteger(eval_moz_is_glyph),
+            ParsingRequirements::CHROME_AND_UA_ONLY,
+        ),
+        feature!(
+            atom!("-moz-is-resource-document"),
+            AllowsRanges::No,
+            Evaluator::BoolInteger(eval_moz_is_resource_document),
+            ParsingRequirements::CHROME_AND_UA_ONLY,
+        ),
+        feature!(
+            atom!("-moz-os-version"),
+            AllowsRanges::No,
+            Evaluator::Ident(eval_moz_os_version),
+            ParsingRequirements::CHROME_AND_UA_ONLY,
+        ),
+        // FIXME(emilio): make system metrics store the -moz- atom, and remove
+        // some duplication here.
+        system_metric_feature!(atom!("-moz-scrollbar-start-backward"), atom!("scrollbar-start-backward")),
+        system_metric_feature!(atom!("-moz-scrollbar-start-forward"), atom!("scrollbar-start-forward")),
+        system_metric_feature!(atom!("-moz-scrollbar-end-backward"), atom!("scrollbar-end-backward")),
+        system_metric_feature!(atom!("-moz-scrollbar-end-forward"), atom!("scrollbar-end-forward")),
+        system_metric_feature!(atom!("-moz-scrollbar-thumb-proportional"), atom!("scrollbar-thumb-proportional")),
+        system_metric_feature!(atom!("-moz-overlay-scrollbars"), atom!("overlay-scrollbars")),
+        system_metric_feature!(atom!("-moz-windows-default-theme"), atom!("windows-default-theme")),
+        system_metric_feature!(atom!("-moz-mac-graphite-theme"), atom!("mac-graphite-theme")),
+        system_metric_feature!(atom!("-moz-mac-yosemite-theme"), atom!("mac-yosemite-theme")),
+        system_metric_feature!(atom!("-moz-windows-accent-color-in-titlebar"), atom!("windows-accent-color-in-titlebar")),
+        system_metric_feature!(atom!("-moz-windows-compositor"), atom!("windows-compositor")),
+        system_metric_feature!(atom!("-moz-windows-classic"), atom!("windows-classic")),
+        system_metric_feature!(atom!("-moz-windows-glass"), atom!("windows-glass")),
+        system_metric_feature!(atom!("-moz-menubar-drag"), atom!("menubar-drag")),
+        system_metric_feature!(atom!("-moz-swipe-animation-enabled"), atom!("swipe-animation-enabled")),
+        system_metric_feature!(atom!("-moz-gtk-csd-available"), atom!("gtk-csd-available")),
+        system_metric_feature!(atom!("-moz-gtk-csd-minimize-button"), atom!("gtk-csd-minimize-button")),
+        system_metric_feature!(atom!("-moz-gtk-csd-maximize-button"), atom!("gtk-csd-maximize-button")),
+        system_metric_feature!(atom!("-moz-gtk-csd-close-button"), atom!("gtk-csd-close-button")),
+        system_metric_feature!(atom!("-moz-system-dark-theme"), atom!("system-dark-theme")),
+        // This is the only system-metric media feature that's accessible to
+        // content as of today.
+        // FIXME(emilio): Restrict (or remove?) when bug 1035774 lands.
+        feature!(
+            atom!("-moz-touch-enabled"),
+            AllowsRanges::No,
+            Evaluator::BoolInteger(eval_moz_touch_enabled),
+            ParsingRequirements::empty(),
+        ),
+    ];
+}
--- a/servo/components/style/gecko/media_queries.rs
+++ b/servo/components/style/gecko/media_queries.rs
@@ -1,44 +1,33 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! Gecko's media-query device and expression representation.
 
 use app_units::AU_PER_PX;
 use app_units::Au;
-use context::QuirksMode;
-use cssparser::{Parser, RGBA, Token};
+use cssparser::RGBA;
 use euclid::Size2D;
 use euclid::TypedScale;
 use gecko::values::{convert_nscolor_to_rgba, convert_rgba_to_nscolor};
 use gecko_bindings::bindings;
 use gecko_bindings::structs;
-use gecko_bindings::structs::{nsCSSKTableEntry, nsCSSKeyword, nsCSSUnit, nsCSSValue};
-use gecko_bindings::structs::{nsMediaFeature, nsMediaFeature_RangeType};
-use gecko_bindings::structs::{nsMediaFeature_ValueType, nsPresContext};
-use gecko_bindings::structs::RawGeckoPresContextOwned;
-use gecko_bindings::structs::nsCSSKeywordAndBoolTableEntry;
+use gecko_bindings::structs::{nsPresContext, RawGeckoPresContextOwned};
 use media_queries::MediaType;
-use parser::{Parse, ParserContext};
 use properties::ComputedValues;
 use servo_arc::Arc;
-use std::fmt::{self, Write};
+use std::fmt;
 use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
-use str::{starts_with_ignore_ascii_case, string_as_ascii_lowercase};
 use string_cache::Atom;
-use style_traits::{CSSPixel, CssWriter, DevicePixel};
-use style_traits::{ParseError, StyleParseErrorKind, ToCss};
+use style_traits::{CSSPixel, DevicePixel};
 use style_traits::viewport::ViewportConstraints;
-use stylesheets::Origin;
-use values::{serialize_atom_identifier, CSSFloat, CustomIdent, KeyframesName};
-use values::computed::{self, ToComputedValue};
+use values::{CustomIdent, KeyframesName};
 use values::computed::font::FontSize;
-use values::specified::{Integer, Length, Number, Resolution};
 
 /// The `Device` in Gecko wraps a pres context, has a default values computed,
 /// and contains all the viewport rule state.
 pub struct Device {
     /// NB: The pres context lifetime is tied to the styleset, who owns the
     /// stylist, and thus the `Device`, so having a raw pres context pointer
     /// here is fine.
     pres_context: RawGeckoPresContextOwned,
@@ -66,21 +55,18 @@ pub struct Device {
 }
 
 impl fmt::Debug for Device {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         use nsstring::nsCString;
 
         let mut doc_uri = nsCString::new();
         unsafe {
-            let doc =
-                &*self.pres_context().mDocument.raw::<structs::nsIDocument>();
-
             bindings::Gecko_nsIURI_Debug(
-                doc.mDocumentURI.raw::<structs::nsIURI>(),
+                (*self.document()).mDocumentURI.raw::<structs::nsIURI>(),
                 &mut doc_uri,
             )
         };
 
         f.debug_struct("Device")
             .field("document_url", &doc_uri)
             .finish()
     }
@@ -152,20 +138,27 @@ impl Device {
     }
 
     /// Returns the body text color.
     pub fn body_text_color(&self) -> RGBA {
         convert_nscolor_to_rgba(self.body_text_color.load(Ordering::Relaxed) as u32)
     }
 
     /// Gets the pres context associated with this document.
+    #[inline]
     pub fn pres_context(&self) -> &nsPresContext {
         unsafe { &*self.pres_context }
     }
 
+    /// Gets the document pointer.
+    #[inline]
+    pub fn document(&self) -> *mut structs::nsIDocument {
+        self.pres_context().mDocument.raw::<structs::nsIDocument>()
+    }
+
     /// Recreates the default computed values.
     pub fn reset_computed_values(&mut self) {
         self.default_values = ComputedValues::default_values(self.pres_context());
     }
 
     /// Rebuild all the cached data.
     pub fn rebuild_cached_data(&mut self) {
         self.reset_computed_values();
@@ -243,763 +236,8 @@ impl Device {
     pub fn zoom_text(&self, size: Au) -> Au {
         size.scale_by(self.pres_context().mEffectiveTextZoom)
     }
     /// Un-apply text zoom (see nsStyleFont::UnzoomText).
     pub fn unzoom_text(&self, size: Au) -> Au {
         size.scale_by(1. / self.pres_context().mEffectiveTextZoom)
     }
 }
-
-/// The kind of matching that should be performed on a media feature value.
-#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
-pub enum Range {
-    /// At least the specified value.
-    Min,
-    /// At most the specified value.
-    Max,
-}
-
-/// The operator that was specified in this media feature.
-#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
-enum Operator {
-    Equal,
-    GreaterThan,
-    GreaterThanEqual,
-    LessThan,
-    LessThanEqual,
-}
-
-impl ToCss for Operator {
-    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
-    where
-        W: fmt::Write,
-    {
-        dest.write_str(match *self {
-            Operator::Equal => "=",
-            Operator::LessThan => "<",
-            Operator::LessThanEqual => "<=",
-            Operator::GreaterThan => ">",
-            Operator::GreaterThanEqual => ">=",
-        })
-    }
-}
-
-/// Either a `Range` or an `Operator`.
-///
-/// Ranged media features are not allowed with operations (that'd make no
-/// sense).
-#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
-enum RangeOrOperator {
-    Range(Range),
-    Operator(Operator),
-}
-
-/// A feature expression for gecko contains a reference to the media feature,
-/// the value the media query contained, and the range to evaluate.
-#[derive(Clone, Debug, MallocSizeOf)]
-pub struct MediaFeatureExpression {
-    feature: &'static nsMediaFeature,
-    value: Option<MediaExpressionValue>,
-    range_or_operator: Option<RangeOrOperator>,
-}
-
-impl ToCss for MediaFeatureExpression {
-    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
-    where
-        W: fmt::Write,
-    {
-        dest.write_str("(")?;
-
-        if (self.feature.mReqFlags & structs::nsMediaFeature_RequirementFlags_eHasWebkitPrefix) != 0
-        {
-            dest.write_str("-webkit-")?;
-        }
-
-        if let Some(RangeOrOperator::Range(range)) = self.range_or_operator {
-            match range {
-                Range::Min => dest.write_str("min-")?,
-                Range::Max => dest.write_str("max-")?,
-            }
-        }
-
-        // NB: CssStringWriter not needed, feature names are under control.
-        write!(dest, "{}", unsafe {
-            Atom::from_static(*self.feature.mName)
-        })?;
-
-        if let Some(RangeOrOperator::Operator(op)) = self.range_or_operator {
-            dest.write_char(' ')?;
-            op.to_css(dest)?;
-            dest.write_char(' ')?;
-        } else if self.value.is_some() {
-            dest.write_str(": ")?;
-        }
-
-        if let Some(ref val) = self.value {
-            val.to_css(dest, self)?;
-        }
-
-        dest.write_str(")")
-    }
-}
-
-impl PartialEq for MediaFeatureExpression {
-    fn eq(&self, other: &Self) -> bool {
-        self.feature.mName == other.feature.mName && self.value == other.value &&
-            self.range_or_operator == other.range_or_operator
-    }
-}
-
-/// A value found or expected in a media expression.
-///
-/// FIXME(emilio): How should calc() serialize in the Number / Integer /
-/// BoolInteger / IntRatio case, as computed or as specified value?
-///
-/// If the first, this would need to store the relevant values.
-///
-/// See: https://github.com/w3c/csswg-drafts/issues/1968
-#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
-pub enum MediaExpressionValue {
-    /// A length.
-    Length(Length),
-    /// A (non-negative) integer.
-    Integer(u32),
-    /// A floating point value.
-    Float(CSSFloat),
-    /// A boolean value, specified as an integer (i.e., either 0 or 1).
-    BoolInteger(bool),
-    /// Two integers separated by '/', with optional whitespace on either side
-    /// of the '/'.
-    IntRatio(u32, u32),
-    /// A resolution.
-    Resolution(Resolution),
-    /// An enumerated value, defined by the variant keyword table in the
-    /// feature's `mData` member.
-    Enumerated(i16),
-    /// Similar to the above Enumerated but the variant keyword table has an
-    /// additional field what the keyword value means in the Boolean Context.
-    BoolEnumerated(i16),
-    /// An identifier.
-    Ident(Atom),
-}
-
-impl MediaExpressionValue {
-    fn from_css_value(
-        for_expr: &MediaFeatureExpression,
-        css_value: &nsCSSValue,
-    ) -> Option<Self> {
-        // NB: If there's a null value, that means that we don't support the
-        // feature.
-        if css_value.mUnit == nsCSSUnit::eCSSUnit_Null {
-            return None;
-        }
-
-        match for_expr.feature.mValueType {
-            nsMediaFeature_ValueType::eLength => {
-                debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Pixel);
-                let pixels = css_value.float_unchecked();
-                Some(MediaExpressionValue::Length(Length::from_px(pixels)))
-            },
-            nsMediaFeature_ValueType::eInteger => {
-                let i = css_value.integer_unchecked();
-                debug_assert!(i >= 0);
-                Some(MediaExpressionValue::Integer(i as u32))
-            },
-            nsMediaFeature_ValueType::eFloat => {
-                debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Number);
-                Some(MediaExpressionValue::Float(css_value.float_unchecked()))
-            },
-            nsMediaFeature_ValueType::eBoolInteger => {
-                debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Integer);
-                let i = css_value.integer_unchecked();
-                debug_assert!(i == 0 || i == 1);
-                Some(MediaExpressionValue::BoolInteger(i == 1))
-            },
-            nsMediaFeature_ValueType::eResolution => {
-                debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Pixel);
-                Some(MediaExpressionValue::Resolution(Resolution::Dppx(
-                    css_value.float_unchecked(),
-                )))
-            },
-            nsMediaFeature_ValueType::eEnumerated => {
-                let value = css_value.integer_unchecked() as i16;
-                Some(MediaExpressionValue::Enumerated(value))
-            },
-            nsMediaFeature_ValueType::eBoolEnumerated => {
-                let value = css_value.integer_unchecked() as i16;
-                Some(MediaExpressionValue::BoolEnumerated(value))
-            },
-            nsMediaFeature_ValueType::eIdent => {
-                debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_AtomIdent);
-                Some(MediaExpressionValue::Ident(unsafe {
-                    Atom::from_raw(*css_value.mValue.mAtom.as_ref())
-                }))
-            },
-            nsMediaFeature_ValueType::eIntRatio => {
-                let array = unsafe { css_value.array_unchecked() };
-                debug_assert_eq!(array.len(), 2);
-                let first = array[0].integer_unchecked();
-                let second = array[1].integer_unchecked();
-
-                debug_assert!(first >= 0 && second >= 0);
-                Some(MediaExpressionValue::IntRatio(first as u32, second as u32))
-            },
-        }
-    }
-}
-
-impl MediaExpressionValue {
-    fn to_css<W>(&self, dest: &mut CssWriter<W>, for_expr: &MediaFeatureExpression) -> fmt::Result
-    where
-        W: fmt::Write,
-    {
-        match *self {
-            MediaExpressionValue::Length(ref l) => l.to_css(dest),
-            MediaExpressionValue::Integer(v) => v.to_css(dest),
-            MediaExpressionValue::Float(v) => v.to_css(dest),
-            MediaExpressionValue::BoolInteger(v) => dest.write_str(if v { "1" } else { "0" }),
-            MediaExpressionValue::IntRatio(a, b) => {
-                a.to_css(dest)?;
-                dest.write_char('/')?;
-                b.to_css(dest)
-            },
-            MediaExpressionValue::Resolution(ref r) => r.to_css(dest),
-            MediaExpressionValue::Ident(ref ident) => serialize_atom_identifier(ident, dest),
-            MediaExpressionValue::Enumerated(value) => unsafe {
-                let keyword = find_in_table(
-                    *for_expr.feature.mData.mKeywordTable.as_ref(),
-                    |_kw, val| val == value,
-                    |e| e.keyword(),
-                ).expect("Value not found in the keyword table?");
-
-                MediaExpressionValue::keyword_to_css(keyword, dest)
-            },
-            MediaExpressionValue::BoolEnumerated(value) => unsafe {
-                let keyword = find_in_table(
-                    *for_expr.feature.mData.mKeywordAndBoolTable.as_ref(),
-                    |_kw, val| val == value,
-                    |e| e.keyword(),
-                ).expect("Value not found in the keyword table?");
-
-                MediaExpressionValue::keyword_to_css(keyword, dest)
-            }
-        }
-    }
-
-    fn keyword_to_css<W>(keyword: nsCSSKeyword, dest: &mut CssWriter<W>) -> fmt::Result
-    where
-        W: fmt::Write,
-    {
-        use std::{slice, str};
-        use std::os::raw::c_char;
-
-        // NB: All the keywords on nsMediaFeatures are static,
-        // well-formed utf-8.
-        let mut length = 0;
-        unsafe {
-            let buffer: *const c_char = bindings::Gecko_CSSKeywordString(keyword, &mut length);
-            let buffer = slice::from_raw_parts(buffer as *const u8, length as usize);
-
-            let string = str::from_utf8_unchecked(buffer);
-
-            dest.write_str(string)
-        }
-    }
-}
-
-fn find_feature<F>(mut f: F) -> Option<&'static nsMediaFeature>
-where
-    F: FnMut(&'static nsMediaFeature) -> bool,
-{
-    unsafe {
-        let mut features = structs::nsMediaFeatures_features.as_ptr();
-        while !(*features).mName.is_null() {
-            if f(&*features) {
-                return Some(&*features);
-            }
-            features = features.offset(1);
-        }
-    }
-    None
-}
-
-trait TableEntry {
-    fn value(&self) -> i16;
-    fn keyword(&self) -> nsCSSKeyword;
-}
-
-impl TableEntry for nsCSSKTableEntry {
-    fn value(&self) -> i16 {
-        self.mValue
-    }
-    fn keyword(&self) -> nsCSSKeyword {
-        self.mKeyword
-    }
-}
-
-impl TableEntry for nsCSSKeywordAndBoolTableEntry {
-    fn value(&self) -> i16 {
-        self._base.mValue
-    }
-    fn keyword(&self) -> nsCSSKeyword {
-        self._base.mKeyword
-    }
-}
-
-unsafe fn find_in_table<T, R, FindFunc, ResultFunc>(
-    current_entry: *const T,
-    find: FindFunc,
-    result_func: ResultFunc,
-) -> Option<R>
-where
-    T: TableEntry,
-    FindFunc: Fn(nsCSSKeyword, i16) -> bool,
-    ResultFunc: Fn(&T) -> R,
-{
-    let mut current_entry = current_entry;
-
-    loop {
-        let value = (*current_entry).value();
-        let keyword = (*current_entry).keyword();
-
-        if value == -1 {
-            return None; // End of the table.
-        }
-
-        if find(keyword, value) {
-            return Some(result_func(&*current_entry));
-        }
-
-        current_entry = current_entry.offset(1);
-    }
-}
-
-fn parse_feature_value<'i, 't>(
-    feature: &nsMediaFeature,
-    feature_value_type: nsMediaFeature_ValueType,
-    context: &ParserContext,
-    input: &mut Parser<'i, 't>,
-) -> Result<MediaExpressionValue, ParseError<'i>> {
-    let value = match feature_value_type {
-        nsMediaFeature_ValueType::eLength => {
-            let length = Length::parse_non_negative(context, input)?;
-            MediaExpressionValue::Length(length)
-        },
-        nsMediaFeature_ValueType::eInteger => {
-            let integer = Integer::parse_non_negative(context, input)?;
-            MediaExpressionValue::Integer(integer.value() as u32)
-        },
-        nsMediaFeature_ValueType::eBoolInteger => {
-            let integer = Integer::parse_non_negative(context, input)?;
-            let value = integer.value();
-            if value > 1 {
-                return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
-            }
-            MediaExpressionValue::BoolInteger(value == 1)
-        },
-        nsMediaFeature_ValueType::eFloat => {
-            let number = Number::parse(context, input)?;
-            MediaExpressionValue::Float(number.get())
-        },
-        nsMediaFeature_ValueType::eIntRatio => {
-            let a = Integer::parse_positive(context, input)?;
-            input.expect_delim('/')?;
-            let b = Integer::parse_positive(context, input)?;
-            MediaExpressionValue::IntRatio(a.value() as u32, b.value() as u32)
-        },
-        nsMediaFeature_ValueType::eResolution => {
-            MediaExpressionValue::Resolution(Resolution::parse(context, input)?)
-        },
-        nsMediaFeature_ValueType::eEnumerated => {
-            let first_table_entry: *const nsCSSKTableEntry =
-                unsafe { *feature.mData.mKeywordTable.as_ref() };
-
-            let value = parse_keyword(input, first_table_entry)?;
-
-            MediaExpressionValue::Enumerated(value)
-        },
-        nsMediaFeature_ValueType::eBoolEnumerated => {
-            let first_table_entry: *const nsCSSKeywordAndBoolTableEntry =
-                unsafe { *feature.mData.mKeywordAndBoolTable.as_ref() };
-
-            let value = parse_keyword(input, first_table_entry)?;
-
-            MediaExpressionValue::BoolEnumerated(value)
-        },
-        nsMediaFeature_ValueType::eIdent => {
-            MediaExpressionValue::Ident(Atom::from(input.expect_ident()?.as_ref()))
-        },
-    };
-
-    Ok(value)
-}
-
-/// Parse a keyword and returns the corresponding i16 value.
-fn parse_keyword<'i, 't, T>(
-    input: &mut Parser<'i, 't>,
-    first_table_entry: *const T,
-) -> Result<i16, ParseError<'i>>
-where
-    T: TableEntry,
-{
-    let location = input.current_source_location();
-    let keyword = input.expect_ident()?;
-    let keyword = unsafe {
-        bindings::Gecko_LookupCSSKeyword(keyword.as_bytes().as_ptr(), keyword.len() as u32)
-    };
-
-    let value = unsafe {
-        find_in_table(first_table_entry, |kw, _| kw == keyword, |e| e.value())
-    };
-
-    match value {
-        Some(value) => Ok(value),
-        None => Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
-    }
-}
-
-/// Consumes an operation or a colon, or returns an error.
-fn consume_operation_or_colon(
-    input: &mut Parser,
-) -> Result<Option<Operator>, ()> {
-    let first_delim = {
-        let next_token = match input.next() {
-            Ok(t) => t,
-            Err(..) => return Err(()),
-        };
-
-        match *next_token {
-            Token::Colon => return Ok(None),
-            Token::Delim(oper) => oper,
-            _ => return Err(()),
-        }
-    };
-    Ok(Some(match first_delim {
-        '=' => Operator::Equal,
-        '>' => {
-            if input.try(|i| i.expect_delim('=')).is_ok() {
-                Operator::GreaterThanEqual
-            } else {
-                Operator::GreaterThan
-            }
-        }
-        '<' => {
-            if input.try(|i| i.expect_delim('=')).is_ok() {
-                Operator::LessThanEqual
-            } else {
-                Operator::LessThan
-            }
-        }
-        _ => return Err(()),
-    }))
-}
-
-impl MediaFeatureExpression {
-    /// Trivially construct a new expression.
-    fn new(
-        feature: &'static nsMediaFeature,
-        value: Option<MediaExpressionValue>,
-        range_or_operator: Option<RangeOrOperator>,
-    ) -> Self {
-        Self {
-            feature,
-            value,
-            range_or_operator,
-        }
-    }
-
-    /// Parse a media expression of the form:
-    ///
-    /// ```
-    /// (media-feature: media-value)
-    /// ```
-    pub fn parse<'i, 't>(
-        context: &ParserContext,
-        input: &mut Parser<'i, 't>,
-    ) -> Result<Self, ParseError<'i>> {
-        input.expect_parenthesis_block()?;
-        input.parse_nested_block(|input| {
-            Self::parse_in_parenthesis_block(context, input)
-        })
-    }
-
-    /// Parse a media feature expression where we've already consumed the
-    /// parenthesis.
-    pub fn parse_in_parenthesis_block<'i, 't>(
-        context: &ParserContext,
-        input: &mut Parser<'i, 't>,
-    ) -> Result<Self, ParseError<'i>> {
-        // FIXME: remove extra indented block when lifetimes are non-lexical
-        let feature;
-        let range;
-        {
-            let location = input.current_source_location();
-            let ident = input.expect_ident()?;
-
-            let mut flags = 0;
-
-            if context.chrome_rules_enabled() || context.stylesheet_origin == Origin::UserAgent
-            {
-                flags |= structs::nsMediaFeature_RequirementFlags_eUserAgentAndChromeOnly;
-            }
-
-            let result = {
-                let mut feature_name = &**ident;
-
-                if unsafe { structs::StaticPrefs_sVarCache_layout_css_prefixes_webkit } &&
-                    starts_with_ignore_ascii_case(feature_name, "-webkit-")
-                {
-                    feature_name = &feature_name[8..];
-                    flags |= structs::nsMediaFeature_RequirementFlags_eHasWebkitPrefix;
-                    if unsafe {
-                        structs::StaticPrefs_sVarCache_layout_css_prefixes_device_pixel_ratio_webkit
-                    } {
-                        flags |= structs::nsMediaFeature_RequirementFlags_eWebkitDevicePixelRatioPrefEnabled;
-                    }
-                }
-
-                let range = if starts_with_ignore_ascii_case(feature_name, "min-") {
-                    feature_name = &feature_name[4..];
-                    Some(Range::Min)
-                } else if starts_with_ignore_ascii_case(feature_name, "max-") {
-                    feature_name = &feature_name[4..];
-                    Some(Range::Max)
-                } else {
-                    None
-                };
-
-                let atom = Atom::from(string_as_ascii_lowercase(feature_name));
-                match find_feature(|f| atom.as_ptr() == unsafe { *f.mName as *mut _ }) {
-                    Some(f) => Ok((f, range)),
-                    None => Err(()),
-                }
-            };
-
-            match result {
-                Ok((f, r)) => {
-                    feature = f;
-                    range = r;
-                },
-                Err(()) => {
-                    return Err(location.new_custom_error(
-                        StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
-                    ))
-                },
-            }
-
-            if (feature.mReqFlags & !flags) != 0 {
-                return Err(location.new_custom_error(
-                    StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
-                ));
-            }
-
-            if range.is_some() &&
-                feature.mRangeType != nsMediaFeature_RangeType::eMinMaxAllowed
-            {
-                return Err(location.new_custom_error(
-                    StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
-                ));
-            }
-        }
-
-        let feature_allows_ranges =
-            feature.mRangeType == nsMediaFeature_RangeType::eMinMaxAllowed;
-
-        let operator = input.try(consume_operation_or_colon);
-        let operator = match operator {
-            Err(..) => {
-                // If there's no colon, this is a media query of the
-                // form '(<feature>)', that is, there's no value
-                // specified.
-                //
-                // Gecko doesn't allow ranged expressions without a
-                // value, so just reject them here too.
-                if range.is_some() {
-                    return Err(input.new_custom_error(
-                        StyleParseErrorKind::RangedExpressionWithNoValue
-                    ));
-                }
-
-                return Ok(Self::new(feature, None, None));
-            }
-            Ok(operator) => operator,
-        };
-
-        let range_or_operator = match range {
-            Some(range) => {
-                if operator.is_some() {
-                    return Err(input.new_custom_error(
-                        StyleParseErrorKind::MediaQueryUnexpectedOperator
-                    ));
-                }
-                Some(RangeOrOperator::Range(range))
-            }
-            None => {
-                match operator {
-                    Some(operator) => {
-                        if !feature_allows_ranges {
-                            return Err(input.new_custom_error(
-                                StyleParseErrorKind::MediaQueryUnexpectedOperator
-                            ));
-                        }
-                        Some(RangeOrOperator::Operator(operator))
-                    }
-                    None => None,
-                }
-            }
-        };
-
-        let value =
-            parse_feature_value(feature, feature.mValueType, context, input).map_err(|err| {
-                err.location
-                    .new_custom_error(StyleParseErrorKind::MediaQueryExpectedFeatureValue)
-            })?;
-
-        Ok(Self::new(feature, Some(value), range_or_operator))
-    }
-
-    /// Returns whether this media query evaluates to true for the given device.
-    pub fn matches(&self, device: &Device, quirks_mode: QuirksMode) -> bool {
-        let mut css_value = nsCSSValue::null();
-        unsafe {
-            (self.feature.mGetter.unwrap())(
-                device
-                    .pres_context()
-                    .mDocument
-                    .raw::<structs::nsIDocument>(),
-                self.feature,
-                &mut css_value,
-            )
-        };
-
-        let value = match MediaExpressionValue::from_css_value(self, &css_value) {
-            Some(v) => v,
-            None => return false,
-        };
-
-        self.evaluate_against(device, &value, quirks_mode)
-    }
-
-    fn evaluate_against(
-        &self,
-        device: &Device,
-        actual_value: &MediaExpressionValue,
-        quirks_mode: QuirksMode,
-    ) -> bool {
-        use self::MediaExpressionValue::*;
-        use std::cmp::Ordering;
-
-        debug_assert!(
-            self.feature.mRangeType == nsMediaFeature_RangeType::eMinMaxAllowed ||
-            self.range_or_operator.is_none(),
-            "Whoops, wrong range"
-        );
-
-        // http://dev.w3.org/csswg/mediaqueries3/#units
-        // em units are relative to the initial font-size.
-        let required_value = match self.value {
-            Some(ref v) => v,
-            None => {
-                // If there's no value, always match unless it's a zero length
-                // or a zero integer or boolean.
-                return match *actual_value {
-                    BoolInteger(v) => v,
-                    Integer(v) => v != 0,
-                    Length(ref l) => computed::Context::for_media_query_evaluation(
-                        device,
-                        quirks_mode,
-                        |context| l.to_computed_value(&context).px() != 0.,
-                    ),
-                    BoolEnumerated(value) => {
-                        let value = unsafe {
-                            find_in_table(
-                                *self.feature.mData.mKeywordAndBoolTable.as_ref(),
-                                |_kw, val| val == value,
-                                |e| e.mValueInBooleanContext,
-                            )
-                        };
-                        value.expect("Value not found in the keyword table?")
-                    },
-                    _ => true,
-                };
-            },
-        };
-
-        // FIXME(emilio): Handle the possible floating point errors?
-        let cmp = match (actual_value, required_value) {
-            (&Length(ref one), &Length(ref other)) => {
-                computed::Context::for_media_query_evaluation(device, quirks_mode, |context| {
-                    one.to_computed_value(&context)
-                        .to_i32_au()
-                        .cmp(&other.to_computed_value(&context).to_i32_au())
-                })
-            },
-            (&Integer(one), &Integer(ref other)) => one.cmp(other),
-            (&BoolInteger(one), &BoolInteger(ref other)) => one.cmp(other),
-            (&Float(one), &Float(ref other)) => one.partial_cmp(other).unwrap(),
-            (&IntRatio(one_num, one_den), &IntRatio(other_num, other_den)) => {
-                // Extend to avoid overflow.
-                (one_num as u64 * other_den as u64).cmp(&(other_num as u64 * one_den as u64))
-            },
-            (&Resolution(ref one), &Resolution(ref other)) => {
-                let actual_dpi = unsafe {
-                    if (*device.pres_context).mOverrideDPPX > 0.0 {
-                        self::Resolution::Dppx((*device.pres_context).mOverrideDPPX).to_dpi()
-                    } else {
-                        one.to_dpi()
-                    }
-                };
-
-                actual_dpi.partial_cmp(&other.to_dpi()).unwrap()
-            },
-            (&Ident(ref one), &Ident(ref other)) => {
-                debug_assert_ne!(
-                    self.feature.mRangeType,
-                    nsMediaFeature_RangeType::eMinMaxAllowed
-                );
-                return one == other;
-            },
-            (&Enumerated(one), &Enumerated(other)) => {
-                debug_assert_ne!(
-                    self.feature.mRangeType,
-                    nsMediaFeature_RangeType::eMinMaxAllowed
-                );
-                return one == other;
-            },
-            (&BoolEnumerated(one), &BoolEnumerated(other)) => {
-                debug_assert_ne!(
-                    self.feature.mRangeType,
-                    nsMediaFeature_RangeType::eMinMaxAllowed
-                );
-                return one == other;
-            },
-            _ => unreachable!(),
-        };
-
-        let range_or_op = match self.range_or_operator {
-            Some(r) => r,
-            None => return cmp == Ordering::Equal,
-        };
-
-        match range_or_op {
-            RangeOrOperator::Range(range) => {
-                cmp == Ordering::Equal || match range {
-                    Range::Min => cmp == Ordering::Greater,
-                    Range::Max => cmp == Ordering::Less,
-                }
-            }
-            RangeOrOperator::Operator(op) => {
-                match op {
-                    Operator::Equal => cmp == Ordering::Equal,
-                    Operator::GreaterThan => cmp == Ordering::Greater,
-                    Operator::GreaterThanEqual => {
-                        cmp == Ordering::Equal || cmp == Ordering::Greater
-                    }
-                    Operator::LessThan => cmp == Ordering::Less,
-                    Operator::LessThanEqual => {
-                        cmp == Ordering::Equal || cmp == Ordering::Less
-                    }
-                }
-            }
-        }
-    }
-}
--- a/servo/components/style/gecko/mod.rs
+++ b/servo/components/style/gecko/mod.rs
@@ -6,16 +6,17 @@
 
 #[macro_use]
 mod non_ts_pseudo_class_list;
 
 pub mod arc_types;
 pub mod conversions;
 pub mod data;
 pub mod global_style_data;
+pub mod media_features;
 pub mod media_queries;
 pub mod pseudo_element;
 pub mod restyle_damage;
 pub mod rules;
 pub mod selector_parser;
 pub mod snapshot;
 pub mod snapshot_helpers;
 pub mod traversal;
--- a/servo/components/style/lib.rs
+++ b/servo/components/style/lib.rs
@@ -62,16 +62,18 @@ extern crate malloc_size_of;
 extern crate malloc_size_of_derive;
 #[allow(unused_extern_crates)]
 #[macro_use]
 extern crate matches;
 #[cfg(feature = "gecko")]
 pub extern crate nsstring;
 #[cfg(feature = "gecko")]
 extern crate num_cpus;
+#[macro_use]
+extern crate num_derive;
 extern crate num_integer;
 extern crate num_traits;
 extern crate ordered_float;
 extern crate owning_ref;
 extern crate parking_lot;
 extern crate precomputed_hash;
 extern crate rayon;
 extern crate selectors;
@@ -123,25 +125,23 @@ pub mod driver;
 pub mod element_state;
 #[cfg(feature = "servo")]
 mod encoding_support;
 pub mod error_reporting;
 pub mod font_face;
 pub mod font_metrics;
 #[cfg(feature = "gecko")]
 #[allow(unsafe_code)]
-pub mod gecko;
-#[cfg(feature = "gecko")]
-#[allow(unsafe_code)]
 pub mod gecko_bindings;
 pub mod hash;
 pub mod invalidation;
 #[allow(missing_docs)] // TODO.
 pub mod logical_geometry;
 pub mod matching;
+#[macro_use]
 pub mod media_queries;
 pub mod parallel;
 pub mod parser;
 pub mod rule_cache;
 pub mod rule_tree;
 pub mod scoped_tls;
 pub mod selector_map;
 pub mod selector_parser;
@@ -185,21 +185,26 @@ pub use html5ever::Namespace;
 /// Generated from the properties.mako.rs template by build.rs
 #[macro_use]
 #[allow(unsafe_code)]
 #[deny(missing_docs)]
 pub mod properties {
     include!(concat!(env!("OUT_DIR"), "/properties.rs"));
 }
 
+#[cfg(feature = "gecko")]
+#[allow(unsafe_code)]
+pub mod gecko;
+
 // uses a macro from properties
 #[cfg(feature = "servo")]
 #[allow(unsafe_code)]
 pub mod servo;
 
+
 #[cfg(feature = "gecko")]
 #[allow(unsafe_code, missing_docs)]
 pub mod gecko_properties {
     include!(concat!(env!("OUT_DIR"), "/gecko_properties.rs"));
 }
 
 macro_rules! reexport_computed_values {
     ( $( { $name: ident, $boxed: expr } )+ ) => {
--- a/servo/components/style/media_queries/media_condition.rs
+++ b/servo/components/style/media_queries/media_condition.rs
@@ -8,17 +8,16 @@
 
 use context::QuirksMode;
 use cssparser::{Parser, Token};
 use parser::ParserContext;
 use std::fmt::{self, Write};
 use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
 use super::{Device, MediaFeatureExpression};
 
-
 /// A binary `and` or `or` operator.
 #[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
 #[allow(missing_docs)]
 pub enum Operator {
     And,
     Or,
 }
 
new file mode 100644
--- /dev/null
+++ b/servo/components/style/media_queries/media_feature.rs
@@ -0,0 +1,172 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+//! Media features.
+
+use super::Device;
+use super::media_feature_expression::{AspectRatio, RangeOrOperator};
+
+use Atom;
+use cssparser::Parser;
+use parser::ParserContext;
+use std::fmt;
+use style_traits::ParseError;
+use values::computed::{CSSPixelLength, Resolution};
+
+/// A generic discriminant for an enum value.
+pub type KeywordDiscriminant = u8;
+
+type MediaFeatureEvaluator<T> = fn(
+    device: &Device,
+    // null == no value was given in the query.
+    value: Option<T>,
+    range_or_operator: Option<RangeOrOperator>,
+) -> bool;
+
+/// Serializes a given discriminant.
+///
+/// FIXME(emilio): we could prevent this allocation if the ToCss code would
+/// generate a method for keywords to get the static string or something.
+pub type KeywordSerializer = fn(KeywordDiscriminant) -> String;
+
+/// Parses a given identifier.
+pub type KeywordParser = for <'a, 'i, 't> fn(
+    context: &'a ParserContext,
+    input: &'a mut Parser<'i, 't>,
+) -> Result<KeywordDiscriminant, ParseError<'i>>;
+
+/// An evaluator for a given media feature.
+///
+/// This determines the kind of values that get parsed, too.
+#[allow(missing_docs)]
+pub enum Evaluator {
+    Length(MediaFeatureEvaluator<CSSPixelLength>),
+    Integer(MediaFeatureEvaluator<u32>),
+    Float(MediaFeatureEvaluator<f32>),
+    BoolInteger(MediaFeatureEvaluator<bool>),
+    /// An integer ratio, such as the one from device-pixel-ratio.
+    IntRatio(MediaFeatureEvaluator<AspectRatio>),
+    /// A resolution.
+    Resolution(MediaFeatureEvaluator<Resolution>),
+    /// A keyword value.
+    Enumerated {
+        /// The parser to get a discriminant given a string.
+        parser: KeywordParser,
+        /// The serializer to get a string from a discriminant.
+        ///
+        /// This is guaranteed to be called with a keyword that `parser` has
+        /// produced.
+        serializer: KeywordSerializer,
+        /// The evaluator itself. This is guaranteed to be called with a
+        /// keyword that `parser` has produced.
+        evaluator: MediaFeatureEvaluator<KeywordDiscriminant>,
+    },
+    Ident(MediaFeatureEvaluator<Atom>),
+}
+
+/// A simple helper macro to create a keyword evaluator.
+///
+/// This assumes that keyword feature expressions don't accept ranges, and
+/// asserts if that's not true. As of today there's nothing like that (does that
+/// even make sense?).
+macro_rules! keyword_evaluator {
+    ($actual_evaluator:ident, $keyword_type:ty) => {
+        {
+            fn __parse<'i, 't>(
+                context: &$crate::parser::ParserContext,
+                input: &mut $crate::cssparser::Parser<'i, 't>,
+            ) -> Result<
+                $crate::media_queries::media_feature::KeywordDiscriminant,
+                ::style_traits::ParseError<'i>,
+            > {
+                let kw = <$keyword_type as $crate::parser::Parse>::parse(context, input)?;
+                Ok(kw as $crate::media_queries::media_feature::KeywordDiscriminant)
+            }
+
+            fn __serialize(kw: $crate::media_queries::media_feature::KeywordDiscriminant) -> String {
+                // This unwrap is ok because the only discriminants that get
+                // back to us is the ones that `parse` produces.
+                let value: $keyword_type =
+                    ::num_traits::cast::FromPrimitive::from_u8(kw).unwrap();
+                <$keyword_type as ::style_traits::ToCss>::to_css_string(&value)
+            }
+
+            fn __evaluate(
+                device: &$crate::media_queries::Device,
+                value: Option<$crate::media_queries::media_feature::KeywordDiscriminant>,
+                range_or_operator: Option<$crate::media_queries::media_feature_expression::RangeOrOperator>,
+            ) -> bool {
+                debug_assert!(
+                    range_or_operator.is_none(),
+                    "Since when do keywords accept ranges?"
+                );
+                // This unwrap is ok because the only discriminants that get
+                // back to us is the ones that `parse` produces.
+                let value: Option<$keyword_type> =
+                    value.map(|kw| ::num_traits::cast::FromPrimitive::from_u8(kw).unwrap());
+                $actual_evaluator(device, value)
+            }
+
+            $crate::media_queries::media_feature::Evaluator::Enumerated {
+                parser: __parse,
+                serializer: __serialize,
+                evaluator: __evaluate,
+            }
+        }
+    }
+}
+
+bitflags! {
+    /// Different requirements or toggles that change how a expression is
+    /// parsed.
+    pub struct ParsingRequirements: u8 {
+        /// The feature should only be parsed in chrome and ua sheets.
+        const CHROME_AND_UA_ONLY = 1 << 0;
+        /// The feature requires a -webkit- prefix.
+        const WEBKIT_PREFIX = 1 << 1;
+        /// The feature requires the webkit-device-pixel-ratio preference to be
+        /// enabled.
+        const WEBKIT_DEVICE_PIXEL_RATIO_PREF_ENABLED = 1 << 2;
+    }
+}
+
+/// Whether a media feature allows ranges or not.
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+#[allow(missing_docs)]
+pub enum AllowsRanges {
+    Yes,
+    No,
+}
+
+/// A description of a media feature.
+pub struct MediaFeatureDescription {
+    /// The media feature name, in ascii lowercase.
+    pub name: Atom,
+    /// Whether min- / max- prefixes are allowed or not.
+    pub allows_ranges: AllowsRanges,
+    /// The evaluator, which we also use to determine which kind of value to
+    /// parse.
+    pub evaluator: Evaluator,
+    /// Different requirements that need to hold for the feature to be
+    /// successfully parsed.
+    pub requirements: ParsingRequirements,
+}
+
+impl MediaFeatureDescription {
+    /// Whether this media feature allows ranges.
+    #[inline]
+    pub fn allows_ranges(&self) -> bool {
+        self.allows_ranges == AllowsRanges::Yes
+    }
+}
+
+impl fmt::Debug for MediaFeatureDescription {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        f.debug_struct("MediaFeatureExpression")
+            .field("name", &self.name)
+            .field("allows_ranges", &self.allows_ranges)
+            .field("requirements", &self.requirements)
+            .finish()
+    }
+}
new file mode 100644
--- /dev/null
+++ b/servo/components/style/media_queries/media_feature_expression.rs
@@ -0,0 +1,562 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+//! Parsing for media feature expressions, like `(foo: bar)` or
+//! `(width >= 400px)`.
+
+use super::Device;
+use super::media_feature::{Evaluator, MediaFeatureDescription};
+use super::media_feature::{ParsingRequirements, KeywordDiscriminant};
+
+use Atom;
+use cssparser::{Parser, Token};
+use context::QuirksMode;
+use num_traits::Zero;
+use parser::{Parse, ParserContext};
+use std::fmt::{self, Write};
+use str::{starts_with_ignore_ascii_case, string_as_ascii_lowercase};
+use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
+use stylesheets::Origin;
+use values::{serialize_atom_identifier, CSSFloat};
+use values::specified::{Integer, Length, Number, Resolution};
+use values::computed::{self, ToComputedValue};
+
+#[cfg(feature = "gecko")]
+use gecko_bindings::structs;
+
+#[cfg(feature = "gecko")]
+use gecko::media_features::MEDIA_FEATURES;
+#[cfg(feature = "servo")]
+use servo::media_queries::MEDIA_FEATURES;
+
+/// An aspect ratio, with a numerator and denominator.
+#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
+pub struct AspectRatio(pub u32, pub u32);
+
+impl ToCss for AspectRatio {
+    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
+    where
+        W: fmt::Write,
+    {
+        self.0.to_css(dest)?;
+        dest.write_char('/')?;
+        self.1.to_css(dest)
+    }
+}
+
+/// The kind of matching that should be performed on a media feature value.
+#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
+pub enum Range {
+    /// At least the specified value.
+    Min,
+    /// At most the specified value.
+    Max,
+}
+
+/// The operator that was specified in this media feature.
+#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
+pub enum Operator {
+    /// =
+    Equal,
+    /// >
+    GreaterThan,
+    /// >=
+    GreaterThanEqual,
+    /// <
+    LessThan,
+    /// <=
+    LessThanEqual,
+}
+
+impl ToCss for Operator {
+    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
+    where
+        W: fmt::Write,
+    {
+        dest.write_str(match *self {
+            Operator::Equal => "=",
+            Operator::LessThan => "<",
+            Operator::LessThanEqual => "<=",
+            Operator::GreaterThan => ">",
+            Operator::GreaterThanEqual => ">=",
+        })
+    }
+}
+
+/// Either a `Range` or an `Operator`.
+///
+/// Ranged media features are not allowed with operations (that'd make no
+/// sense).
+#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
+pub enum RangeOrOperator {
+    /// A `Range`.
+    Range(Range),
+    /// An `Operator`.
+    Operator(Operator),
+}
+
+impl RangeOrOperator {
+    /// Evaluate a given range given a query value and a value from the browser.
+    pub fn evaluate<T>(
+        range_or_op: Option<Self>,
+        query_value: Option<T>,
+        value: T,
+    ) -> bool
+    where
+        T: PartialOrd + Zero
+    {
+        use std::cmp::Ordering;
+
+        let query_value = match query_value {
+            Some(v) => v,
+            None => return value != Zero::zero(),
+        };
+
+        let cmp = match value.partial_cmp(&query_value) {
+            Some(c) => c,
+            None => return false,
+        };
+
+        let range_or_op = match range_or_op {
+            Some(r) => r,
+            None => return cmp == Ordering::Equal,
+        };
+
+        match range_or_op {
+            RangeOrOperator::Range(range) => {
+                cmp == Ordering::Equal || match range {
+                    Range::Min => cmp == Ordering::Greater,
+                    Range::Max => cmp == Ordering::Less,
+                }
+            }
+            RangeOrOperator::Operator(op) => {
+                match op {
+                    Operator::Equal => cmp == Ordering::Equal,
+                    Operator::GreaterThan => cmp == Ordering::Greater,
+                    Operator::GreaterThanEqual => {
+                        cmp == Ordering::Equal || cmp == Ordering::Greater
+                    }
+                    Operator::LessThan => cmp == Ordering::Less,
+                    Operator::LessThanEqual => {
+                        cmp == Ordering::Equal || cmp == Ordering::Less
+                    }
+                }
+            }
+        }
+    }
+}
+
+/// A feature expression contains a reference to the media feature, the value
+/// the media query contained, and the range to evaluate.
+#[derive(Clone, Debug, MallocSizeOf)]
+pub struct MediaFeatureExpression {
+    feature: &'static MediaFeatureDescription,
+    value: Option<MediaExpressionValue>,
+    range_or_operator: Option<RangeOrOperator>,
+}
+
+impl PartialEq for MediaFeatureExpression {
+    fn eq(&self, other: &Self) -> bool {
+        self.feature as *const _ == other.feature as *const _ &&
+        self.value == other.value &&
+        self.range_or_operator == other.range_or_operator
+    }
+}
+
+impl ToCss for MediaFeatureExpression {
+    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
+    where
+        W: fmt::Write,
+    {
+        dest.write_str("(")?;
+
+        if self.feature.requirements.contains(ParsingRequirements::WEBKIT_PREFIX) {
+            dest.write_str("-webkit-")?;
+        }
+
+        if let Some(RangeOrOperator::Range(range)) = self.range_or_operator {
+            match range {
+                Range::Min => dest.write_str("min-")?,
+                Range::Max => dest.write_str("max-")?,
+            }
+        }
+
+        // NB: CssStringWriter not needed, feature names are under control.
+        write!(dest, "{}", self.feature.name)?;
+
+        if let Some(RangeOrOperator::Operator(op)) = self.range_or_operator {
+            dest.write_char(' ')?;
+            op.to_css(dest)?;
+            dest.write_char(' ')?;
+        } else if self.value.is_some() {
+            dest.write_str(": ")?;
+        }
+
+        if let Some(ref val) = self.value {
+            val.to_css(dest, self)?;
+        }
+
+        dest.write_str(")")
+    }
+}
+
+/// Consumes an operation or a colon, or returns an error.
+fn consume_operation_or_colon(
+    input: &mut Parser,
+) -> Result<Option<Operator>, ()> {
+    let first_delim = {
+        let next_token = match input.next() {
+            Ok(t) => t,
+            Err(..) => return Err(()),
+        };
+
+        match *next_token {
+            Token::Colon => return Ok(None),
+            Token::Delim(oper) => oper,
+            _ => return Err(()),
+        }
+    };
+    Ok(Some(match first_delim {
+        '=' => Operator::Equal,
+        '>' => {
+            if input.try(|i| i.expect_delim('=')).is_ok() {
+                Operator::GreaterThanEqual
+            } else {
+                Operator::GreaterThan
+            }
+        }
+        '<' => {
+            if input.try(|i| i.expect_delim('=')).is_ok() {
+                Operator::LessThanEqual
+            } else {
+                Operator::LessThan
+            }
+        }
+        _ => return Err(()),
+    }))
+}
+
+impl MediaFeatureExpression {
+    fn new(
+        feature: &'static MediaFeatureDescription,
+        value: Option<MediaExpressionValue>,
+        range_or_operator: Option<RangeOrOperator>,
+    ) -> Self {
+        Self { feature, value, range_or_operator }
+    }
+
+    /// Parse a media expression of the form:
+    ///
+    /// ```
+    /// (media-feature: media-value)
+    /// ```
+    pub fn parse<'i, 't>(
+        context: &ParserContext,
+        input: &mut Parser<'i, 't>,
+    ) -> Result<Self, ParseError<'i>> {
+        input.expect_parenthesis_block()?;
+        input.parse_nested_block(|input| {
+            Self::parse_in_parenthesis_block(context, input)
+        })
+    }
+
+    /// Parse a media feature expression where we've already consumed the
+    /// parenthesis.
+    pub fn parse_in_parenthesis_block<'i, 't>(
+        context: &ParserContext,
+        input: &mut Parser<'i, 't>,
+    ) -> Result<Self, ParseError<'i>> {
+        // FIXME: remove extra indented block when lifetimes are non-lexical
+        let feature;
+        let range;
+        {
+            let location = input.current_source_location();
+            let ident = input.expect_ident()?;
+
+            let mut requirements = ParsingRequirements::empty();
+
+            if context.chrome_rules_enabled() ||
+                context.stylesheet_origin == Origin::UserAgent
+            {
+                requirements.insert(ParsingRequirements::CHROME_AND_UA_ONLY);
+            }
+
+            let result = {
+                let mut feature_name = &**ident;
+
+                #[cfg(feature = "gecko")]
+                {
+                    if unsafe { structs::StaticPrefs_sVarCache_layout_css_prefixes_webkit } &&
+                        starts_with_ignore_ascii_case(feature_name, "-webkit-")
+                    {
+                        feature_name = &feature_name[8..];
+                        requirements.insert(ParsingRequirements::WEBKIT_PREFIX);
+                        if unsafe {
+                            structs::StaticPrefs_sVarCache_layout_css_prefixes_device_pixel_ratio_webkit
+                        } {
+                            requirements.insert(ParsingRequirements::WEBKIT_DEVICE_PIXEL_RATIO_PREF_ENABLED);
+                        }
+                    }
+                }
+
+                let range = if starts_with_ignore_ascii_case(feature_name, "min-") {
+                    feature_name = &feature_name[4..];
+                    Some(Range::Min)
+                } else if starts_with_ignore_ascii_case(feature_name, "max-") {
+                    feature_name = &feature_name[4..];
+                    Some(Range::Max)
+                } else {
+                    None
+                };
+
+                let atom = Atom::from(string_as_ascii_lowercase(feature_name));
+                match MEDIA_FEATURES.iter().find(|f| f.name == atom) {
+                    Some(f) => Ok((f, range)),
+                    None => Err(()),
+                }
+            };
+
+            match result {
+                Ok((f, r)) => {
+                    feature = f;
+                    range = r;
+                },
+                Err(()) => {
+                    return Err(location.new_custom_error(
+                        StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
+                    ))
+                },
+            }
+
+            if !(feature.requirements & !requirements).is_empty() {
+                return Err(location.new_custom_error(
+                    StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
+                ));
+            }
+
+            if range.is_some() && !feature.allows_ranges() {
+                return Err(location.new_custom_error(
+                    StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
+                ));
+            }
+        }
+
+        let operator = input.try(consume_operation_or_colon);
+        let operator = match operator {
+            Err(..) => {
+                // If there's no colon, this is a media query of the
+                // form '(<feature>)', that is, there's no value
+                // specified.
+                //
+                // Gecko doesn't allow ranged expressions without a
+                // value, so just reject them here too.
+                if range.is_some() {
+                    return Err(input.new_custom_error(
+                        StyleParseErrorKind::RangedExpressionWithNoValue
+                    ));
+                }
+
+                return Ok(Self::new(feature, None, None));
+            }
+            Ok(operator) => operator,
+        };
+
+        let range_or_operator = match range {
+            Some(range) => {
+                if operator.is_some() {
+                    return Err(input.new_custom_error(
+                        StyleParseErrorKind::MediaQueryUnexpectedOperator
+                    ));
+                }
+                Some(RangeOrOperator::Range(range))
+            }
+            None => {
+                match operator {
+                    Some(operator) => {
+                        if !feature.allows_ranges() {
+                            return Err(input.new_custom_error(
+                                StyleParseErrorKind::MediaQueryUnexpectedOperator
+                            ));
+                        }
+                        Some(RangeOrOperator::Operator(operator))
+                    }
+                    None => None,
+                }
+            }
+        };
+
+        let value =
+            MediaExpressionValue::parse(feature, context, input).map_err(|err| {
+                err.location
+                    .new_custom_error(StyleParseErrorKind::MediaQueryExpectedFeatureValue)
+            })?;
+
+        Ok(Self::new(feature, Some(value), range_or_operator))
+    }
+
+    /// Returns whether this media query evaluates to true for the given device.
+    pub fn matches(&self, device: &Device, quirks_mode: QuirksMode) -> bool {
+        let value = self.value.as_ref();
+
+        macro_rules! expect {
+            ($variant:ident) => {
+                value.map(|value| {
+                    match *value {
+                        MediaExpressionValue::$variant(ref v) => v,
+                        _ => unreachable!("Unexpected MediaExpressionValue"),
+                    }
+                })
+            }
+        }
+
+        match self.feature.evaluator {
+            Evaluator::Length(eval) => {
+                let computed = expect!(Length).map(|specified| {
+                    computed::Context::for_media_query_evaluation(device, quirks_mode, |context| {
+                        specified.to_computed_value(context)
+                    })
+                });
+                eval(device, computed, self.range_or_operator)
+            }
+            Evaluator::Integer(eval) => {
+                eval(device, expect!(Integer).cloned(), self.range_or_operator)
+            }
+            Evaluator::Float(eval) => {
+                eval(device, expect!(Float).cloned(), self.range_or_operator)
+            }
+            Evaluator::IntRatio(eval) => {
+                eval(device, expect!(IntRatio).cloned(), self.range_or_operator)
+            },
+            Evaluator::Resolution(eval) => {
+                let computed = expect!(Resolution).map(|specified| {
+                    computed::Context::for_media_query_evaluation(device, quirks_mode, |context| {
+                        specified.to_computed_value(context)
+                    })
+                });
+                eval(device, computed, self.range_or_operator)
+            }
+            Evaluator::Enumerated { evaluator, .. } => {
+                evaluator(
+                    device,
+                    expect!(Enumerated).cloned(),
+                    self.range_or_operator,
+                )
+            }
+            Evaluator::Ident(eval) => {
+                eval(device, expect!(Ident).cloned(), self.range_or_operator)
+            }
+            Evaluator::BoolInteger(eval) => {
+                eval(device, expect!(BoolInteger).cloned(), self.range_or_operator)
+            }
+        }
+    }
+}
+
+/// A value found or expected in a media expression.
+///
+/// FIXME(emilio): How should calc() serialize in the Number / Integer /
+/// BoolInteger / IntRatio case, as computed or as specified value?
+///
+/// If the first, this would need to store the relevant values.
+///
+/// See: https://github.com/w3c/csswg-drafts/issues/1968
+#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
+pub enum MediaExpressionValue {
+    /// A length.
+    Length(Length),
+    /// A (non-negative) integer.
+    Integer(u32),
+    /// A floating point value.
+    Float(CSSFloat),
+    /// A boolean value, specified as an integer (i.e., either 0 or 1).
+    BoolInteger(bool),
+    /// Two integers separated by '/', with optional whitespace on either side
+    /// of the '/'.
+    IntRatio(AspectRatio),
+    /// A resolution.
+    Resolution(Resolution),
+    /// An enumerated value, defined by the variant keyword table in the
+    /// feature's `mData` member.
+    Enumerated(KeywordDiscriminant),
+    /// An identifier.
+    Ident(Atom),
+}
+
+impl MediaExpressionValue {
+    fn to_css<W>(
+        &self,
+        dest: &mut CssWriter<W>,
+        for_expr: &MediaFeatureExpression,
+    ) -> fmt::Result
+    where
+        W: fmt::Write,
+    {
+        match *self {
+            MediaExpressionValue::Length(ref l) => l.to_css(dest),
+            MediaExpressionValue::Integer(v) => v.to_css(dest),
+            MediaExpressionValue::Float(v) => v.to_css(dest),
+            MediaExpressionValue::BoolInteger(v) => dest.write_str(if v { "1" } else { "0" }),
+            MediaExpressionValue::IntRatio(ratio) => {
+                ratio.to_css(dest)
+            },
+            MediaExpressionValue::Resolution(ref r) => r.to_css(dest),
+            MediaExpressionValue::Ident(ref ident) => serialize_atom_identifier(ident, dest),
+            MediaExpressionValue::Enumerated(value) => {
+                match for_expr.feature.evaluator {
+                    Evaluator::Enumerated { serializer, .. } => {
+                        dest.write_str(&*serializer(value))
+                    }
+                    _ => unreachable!(),
+                }
+            },
+        }
+    }
+
+    fn parse<'i, 't>(
+        for_feature: &MediaFeatureDescription,
+        context: &ParserContext,
+        input: &mut Parser<'i, 't>,
+    ) -> Result<MediaExpressionValue, ParseError<'i>> {
+        Ok(match for_feature.evaluator {
+            Evaluator::Length(..) => {
+                let length = Length::parse_non_negative(context, input)?;
+                MediaExpressionValue::Length(length)
+            }
+            Evaluator::Integer(..) => {
+                let integer = Integer::parse_non_negative(context, input)?;
+                MediaExpressionValue::Integer(integer.value() as u32)
+            }
+            Evaluator::BoolInteger(..) => {
+                let integer = Integer::parse_non_negative(context, input)?;
+                let value = integer.value();
+                if value > 1 {
+                    return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
+                }
+                MediaExpressionValue::BoolInteger(value == 1)
+            }
+            Evaluator::Float(..) => {
+                let number = Number::parse(context, input)?;
+                MediaExpressionValue::Float(number.get())
+            }
+            Evaluator::IntRatio(..) => {
+                let a = Integer::parse_positive(context, input)?;
+                input.expect_delim('/')?;
+                let b = Integer::parse_positive(context, input)?;
+                MediaExpressionValue::IntRatio(AspectRatio(
+                    a.value() as u32,
+                    b.value() as u32
+                ))
+            }
+            Evaluator::Resolution(..) => {
+                MediaExpressionValue::Resolution(Resolution::parse(context, input)?)
+            }
+            Evaluator::Enumerated { parser, .. } => {
+                MediaExpressionValue::Enumerated(parser(context, input)?)
+            }
+            Evaluator::Ident(..) => {
+                MediaExpressionValue::Ident(Atom::from(input.expect_ident()?.as_ref()))
+            }
+        })
+    }
+}
--- a/servo/components/style/media_queries/mod.rs
+++ b/servo/components/style/media_queries/mod.rs
@@ -4,17 +4,21 @@
 
 //! [Media queries][mq].
 //!
 //! [mq]: https://drafts.csswg.org/mediaqueries/
 
 mod media_condition;
 mod media_list;
 mod media_query;
+#[macro_use]
+pub mod media_feature;
+pub mod media_feature_expression;
 
 pub use self::media_condition::MediaCondition;
 pub use self::media_list::MediaList;
 pub use self::media_query::{MediaQuery, MediaQueryType, MediaType, Qualifier};
+pub use self::media_feature_expression::MediaFeatureExpression;
 
 #[cfg(feature = "servo")]
-pub use servo::media_queries::{Device, MediaFeatureExpression};
+pub use servo::media_queries::Device;
 #[cfg(feature = "gecko")]
-pub use gecko::media_queries::{Device, MediaFeatureExpression};
+pub use gecko::media_queries::Device;