From 17a92ff99b7b36f0d860b77c9b239ca4fda0700f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 8 May 2018 06:51:34 +0200 Subject: [PATCH] Bug 1459844: Share more code and fix some inconsistencies between html / svg style elements. r=heycam MozReview-Commit-ID: IkTrIfJI1iK --- dom/base/nsStyleLinkElement.cpp | 28 ++++++++++++++++++++++++++++ dom/base/nsStyleLinkElement.h | 12 ++++++++++++ dom/html/HTMLLinkElement.cpp | 26 ++++++-------------------- dom/html/HTMLStyleElement.cpp | 23 +++++------------------ dom/svg/SVGStyleElement.cpp | 23 +++++++++-------------- 5 files changed, 60 insertions(+), 52 deletions(-) diff --git a/dom/base/nsStyleLinkElement.cpp b/dom/base/nsStyleLinkElement.cpp index 6043120f041c..229493233486 100644 --- a/dom/base/nsStyleLinkElement.cpp +++ b/dom/base/nsStyleLinkElement.cpp @@ -85,6 +85,34 @@ nsStyleLinkElement::~nsStyleLinkElement() nsStyleLinkElement::SetStyleSheet(nullptr); } +void +nsStyleLinkElement::GetTitleAndMediaForElement(const Element& aSelf, + nsString& aTitle, + nsString& aMedia) +{ + aSelf.GetAttr(kNameSpaceID_None, nsGkAtoms::title, aTitle); + aTitle.CompressWhitespace(); + + aSelf.GetAttr(kNameSpaceID_None, nsGkAtoms::media, aMedia); + // The HTML5 spec is formulated in terms of the CSSOM spec, which specifies + // that media queries should be ASCII lowercased during serialization. + // + // FIXME(emilio): How does it matter? This is going to be parsed anyway, CSS + // should take care of serializing it properly. + nsContentUtils::ASCIIToLower(aMedia); +} + +bool +nsStyleLinkElement::IsCSSMimeTypeAttribute(const Element& aSelf) +{ + nsAutoString type; + nsAutoString mimeType; + nsAutoString notUsed; + aSelf.GetAttr(kNameSpaceID_None, nsGkAtoms::type, type); + nsContentUtils::SplitMimeType(type, mimeType, notUsed); + return mimeType.IsEmpty() || mimeType.LowerCaseEqualsLiteral("text/css"); +} + void nsStyleLinkElement::Unlink() { diff --git a/dom/base/nsStyleLinkElement.h b/dom/base/nsStyleLinkElement.h index d9c02c7f412c..2636226bc25a 100644 --- a/dom/base/nsStyleLinkElement.h +++ b/dom/base/nsStyleLinkElement.h @@ -93,6 +93,18 @@ protected: mozilla::dom::ShadowRoot* aOldShadowRoot, ForceUpdate = ForceUpdate::No); + // Gets a suitable title and media for SheetInfo out of an element, which + // needs to be `this`. + // + // NOTE(emilio): Needs nsString instead of nsAString because of + // CompressWhitespace. + static void GetTitleAndMediaForElement(const mozilla::dom::Element&, + nsString& aTitle, + nsString& aMedia); + + // Returns whether the type attribute specifies the text/css mime type. + static bool IsCSSMimeTypeAttribute(const mozilla::dom::Element&); + virtual mozilla::Maybe GetStyleSheetInfo() = 0; // CC methods diff --git a/dom/html/HTMLLinkElement.cpp b/dom/html/HTMLLinkElement.cpp index dbe1c738f41a..368c3c468e76 100644 --- a/dom/html/HTMLLinkElement.cpp +++ b/dom/html/HTMLLinkElement.cpp @@ -430,9 +430,13 @@ HTMLLinkElement::GetStyleSheetInfo() return Nothing(); } + if (!IsCSSMimeTypeAttribute(*this)) { + return Nothing(); + } + nsAutoString title; - GetAttr(kNameSpaceID_None, nsGkAtoms::title, title); - title.CompressWhitespace(); + nsAutoString media; + GetTitleAndMediaForElement(*this, title, media); bool alternate = linkTypes & nsStyleLinkElement::eALTERNATE; if (alternate && title.IsEmpty()) { @@ -440,30 +444,12 @@ HTMLLinkElement::GetStyleSheetInfo() return Nothing(); } - nsAutoString type; - nsAutoString mimeType; - nsAutoString notUsed; - GetAttr(kNameSpaceID_None, nsGkAtoms::type, type); - nsContentUtils::SplitMimeType(type, mimeType, notUsed); - if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) { - return Nothing(); - } - nsAutoString href; GetAttr(kNameSpaceID_None, nsGkAtoms::href, href); if (href.IsEmpty()) { return Nothing(); } - nsAutoString media; - GetAttr(kNameSpaceID_None, nsGkAtoms::media, media); - // The HTML5 spec is formulated in terms of the CSSOM spec, which specifies - // that media queries should be ASCII lowercased during serialization. - // - // FIXME(emilio): How does it matter? This is going to be parsed anyway, CSS - // should take care of serializing it properly. - nsContentUtils::ASCIIToLower(media); - nsCOMPtr uri = Link::GetURI(); nsCOMPtr prin = mTriggeringPrincipal; return Some(SheetInfo { diff --git a/dom/html/HTMLStyleElement.cpp b/dom/html/HTMLStyleElement.cpp index 227035bc81d0..65ea7f7e67bc 100644 --- a/dom/html/HTMLStyleElement.cpp +++ b/dom/html/HTMLStyleElement.cpp @@ -197,27 +197,14 @@ HTMLStyleElement::SetTextContentInternal(const nsAString& aTextContent, Maybe HTMLStyleElement::GetStyleSheetInfo() { - nsAutoString title; - GetAttr(kNameSpaceID_None, nsGkAtoms::title, title); - title.CompressWhitespace(); - - nsAutoString media; - GetAttr(kNameSpaceID_None, nsGkAtoms::media, media); - // The HTML5 spec is formulated in terms of the CSSOM spec, which specifies - // that media queries should be ASCII lowercased during serialization. - // - // FIXME(emilio): Doesn't matter I'd think, style should take care of that. - nsContentUtils::ASCIIToLower(media); - - nsAutoString type; - nsAutoString mimeType; - nsAutoString notUsed; - GetAttr(kNameSpaceID_None, nsGkAtoms::type, type); - nsContentUtils::SplitMimeType(type, mimeType, notUsed); - if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) { + if (!IsCSSMimeTypeAttribute(*this)) { return Nothing(); } + nsAutoString title; + nsAutoString media; + GetTitleAndMediaForElement(*this, title, media); + nsCOMPtr prin = mTriggeringPrincipal; return Some(SheetInfo { *OwnerDoc(), diff --git a/dom/svg/SVGStyleElement.cpp b/dom/svg/SVGStyleElement.cpp index 4c026507a34e..0d42f0811402 100644 --- a/dom/svg/SVGStyleElement.cpp +++ b/dom/svg/SVGStyleElement.cpp @@ -219,29 +219,24 @@ SVGStyleElement::SetTitle(const nsAString& aTitle, ErrorResult& rv) Maybe SVGStyleElement::GetStyleSheetInfo() { - nsAutoString title; - GetAttr(kNameSpaceID_None, nsGkAtoms::title, title); - title.CompressWhitespace(); - - nsAutoString media; - GetAttr(kNameSpaceID_None, nsGkAtoms::media, media); - // The SVG spec is formulated in terms of the CSS2 spec, - // which specifies that media queries are case insensitive. - nsContentUtils::ASCIIToLower(media); - - // FIXME(emilio): Why doesn't this do the same as HTMLStyleElement? - nsAutoString type; - GetAttr(kNameSpaceID_None, nsGkAtoms::type, type); - if (!type.IsEmpty() && !type.LowerCaseEqualsLiteral("text/css")) { + if (!IsCSSMimeTypeAttribute(*this)) { return Nothing(); } + nsAutoString title; + nsAutoString media; + GetTitleAndMediaForElement(*this, title, media); + return Some(SheetInfo { *OwnerDoc(), this, nullptr, + // FIXME(bug 1459822): Why doesn't this need a principal, but + // HTMLStyleElement does? nullptr, net::ReferrerPolicy::RP_Unset, + // FIXME(bug 1459822): Why does this need a crossorigin attribute, but + // HTMLStyleElement doesn't? AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin)), title, media,