Bug 1459844: Share more code and fix some inconsistencies between html / svg style elements. r=heycam

MozReview-Commit-ID: IkTrIfJI1iK
This commit is contained in:
Emilio Cobos Álvarez 2018-05-08 06:51:34 +02:00
Родитель b73f5b5a53
Коммит 17a92ff99b
5 изменённых файлов: 60 добавлений и 52 удалений

Просмотреть файл

@ -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()
{

Просмотреть файл

@ -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<SheetInfo> GetStyleSheetInfo() = 0;
// CC methods

Просмотреть файл

@ -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<nsIURI> uri = Link::GetURI();
nsCOMPtr<nsIPrincipal> prin = mTriggeringPrincipal;
return Some(SheetInfo {

Просмотреть файл

@ -197,27 +197,14 @@ HTMLStyleElement::SetTextContentInternal(const nsAString& aTextContent,
Maybe<nsStyleLinkElement::SheetInfo>
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<nsIPrincipal> prin = mTriggeringPrincipal;
return Some(SheetInfo {
*OwnerDoc(),

Просмотреть файл

@ -219,29 +219,24 @@ SVGStyleElement::SetTitle(const nsAString& aTitle, ErrorResult& rv)
Maybe<nsStyleLinkElement::SheetInfo>
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,