Bug 1447009: Fix StyleSheet title getter to comply with the spec. r?heycam draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Tue, 08 May 2018 18:53:51 +0200
changeset 792590 bbe547db6afcaa91607220078f2b43d3bae15525
parent 792589 e8aa1691a695acba92795f8a5eddd73ef09485f5
child 792591 b0bd0bb1c6083d6f147d66b56b190dcd47f77846
push id109153
push userbmo:emilio@crisal.io
push dateTue, 08 May 2018 17:16:51 +0000
reviewersheycam
bugs1447009
milestone62.0a1
Bug 1447009: Fix StyleSheet title getter to comply with the spec. r?heycam Also to match Blink. MozReview-Commit-ID: Lh4iLhVEUKI
layout/style/StyleSheet.cpp
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/css/cssom/stylesheet-title-001.html
--- a/layout/style/StyleSheet.cpp
+++ b/layout/style/StyleSheet.cpp
@@ -390,17 +390,26 @@ StyleSheet::GetHref(nsAString& aHref, Er
   } else {
     SetDOMStringToNull(aHref);
   }
 }
 
 void
 StyleSheet::GetTitle(nsAString& aTitle)
 {
-  aTitle.Assign(mTitle);
+  // From https://drafts.csswg.org/cssom/#dom-stylesheet-title:
+  //
+  //    The title attribute must return the title or null if title is the empty
+  //    string.
+  //
+  if (!mTitle.IsEmpty()) {
+    aTitle.Assign(mTitle);
+  } else {
+    SetDOMStringToNull(aTitle);
+  }
 }
 
 void
 StyleSheet::WillDirty()
 {
   if (mInner->mComplete) {
     EnsureUniqueInner();
   }
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -323579,16 +323579,22 @@
     ]
    ],
    "css/cssom/stylesheet-same-origin.sub.html": [
     [
      "/css/cssom/stylesheet-same-origin.sub.html",
      {}
     ]
    ],
+   "css/cssom/stylesheet-title-001.html": [
+    [
+     "/css/cssom/stylesheet-title-001.html",
+     {}
+    ]
+   ],
    "css/cssom/ttwf-cssom-doc-ext-load-count.html": [
     [
      "/css/cssom/ttwf-cssom-doc-ext-load-count.html",
      {}
     ]
    ],
    "css/cssom/ttwf-cssom-doc-ext-load-tree-order.html": [
     [
@@ -540812,16 +540818,20 @@
   "css/cssom/stylesheet-same-origin.css": [
    "268fb9a72d33b3d18bbb82aaaac48bb15c89a88e",
    "support"
   ],
   "css/cssom/stylesheet-same-origin.sub.html": [
    "cab23bdf8b92c8194cc71b1a8ce34155f89f42cf",
    "testharness"
   ],
+  "css/cssom/stylesheet-title-001.html": [
+   "69938dc0f6d1a266e028d14950a7265124ff0733",
+   "testharness"
+  ],
   "css/cssom/support/1x1-green.png": [
    "51e7b6974a09eda6cb31337717c5eaeb9c44b443",
    "support"
   ],
   "css/cssom/support/1x1-lime.png": [
    "b040eb633a35c0648ad72a2902361faf25bc419d",
    "support"
   ],
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/css/cssom/stylesheet-title-001.html
@@ -0,0 +1,39 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>CSS Test: StyleSheet's title attribute</title>
+<link rel="author" title="Emilio Cobos Álvarez" href="mailto:emilio@crisal.io">
+<link rel="help" href="https://drafts.csswg.org/cssom/#preferred-css-style-sheet-set-name">
+<link rel="help" href="https://html.spec.whatwg.org/multipage/#attr-style-title">
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<style></style>
+<style title=""></style>
+<style title="Preferred">
+  p { color: green; }
+</style>
+<style title="Not preferred">
+  p { color: red; }
+</style>
+<p id="test-element">Should be green</p>
+<script>
+test(function() {
+  assert_equals(
+    getComputedStyle(document.getElementById("test-element")).color,
+    "rgb(0, 128, 0)",
+    "Preferred style should apply"
+  );
+}, "Preferred style sheet name");
+
+test(function() {
+  let sheets = document.styleSheets;
+  let styleElements = Array.from(document.querySelectorAll("style"));
+  assert_equals(sheets.length, styleElements.length);
+  for (let i = 0; i < sheets.length; ++i) {
+    let titleAttr = styleElements[i].getAttribute("title");
+    if (titleAttr === null || titleAttr === "")
+      assert_equals(sheets[i].title, null, "Empty title returns null");
+    else
+      assert_equals(sheets[i].title, titleAttr, "Selected title is properly reflected");
+  }
+}, "StyleSheet.title");
+</script>