From a4780ebaeb123ce9c793b85bb38a1701fad8f7ac Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sun, 30 Aug 2020 09:29:45 +0000 Subject: Issue #1629 - Uplift implementation of behavior for stylesheets. --- dom/html/HTMLLinkElement.cpp | 59 ++++++++++++++++++++++++++++++++++--------- dom/html/HTMLLinkElement.h | 16 +++++++++--- dom/html/HTMLStyleElement.cpp | 6 +++-- dom/html/HTMLStyleElement.h | 5 ++-- 4 files changed, 66 insertions(+), 20 deletions(-) (limited to 'dom/html') diff --git a/dom/html/HTMLLinkElement.cpp b/dom/html/HTMLLinkElement.cpp index 8afe767bd..1b78cbd69 100644 --- a/dom/html/HTMLLinkElement.cpp +++ b/dom/html/HTMLLinkElement.cpp @@ -33,6 +33,8 @@ #define LINK_ELEMENT_FLAG_BIT(n_) \ NODE_FLAG_BIT(ELEMENT_TYPE_SPECIFIC_BITS_OFFSET + (n_)) +#define LINK_DISABLED Preferences::GetBool("dom.link.disabled_attribute.enabled", true) + // Link element specific bits enum { // Indicates that a DNS Prefetch has been requested from this Link element. @@ -92,9 +94,14 @@ NS_INTERFACE_TABLE_TAIL_INHERITING(nsGenericHTMLElement) NS_IMPL_ELEMENT_CLONE(HTMLLinkElement) + bool -HTMLLinkElement::Disabled() +HTMLLinkElement::Disabled() const { + if (LINK_DISABLED) { + return GetBoolAttr(nsGkAtoms::disabled); + } + StyleSheet* ss = GetSheet(); return ss && ss->Disabled(); } @@ -107,8 +114,12 @@ HTMLLinkElement::GetMozDisabled(bool* aDisabled) } void -HTMLLinkElement::SetDisabled(bool aDisabled) -{ +HTMLLinkElement::SetDisabled(bool aDisabled, ErrorResult& aRv) +{ + if (LINK_DISABLED) { + return SetHTMLBoolAttr(nsGkAtoms::disabled, aDisabled, aRv); + } + if (StyleSheet* ss = GetSheet()) { ss->SetDisabled(aDisabled); } @@ -117,11 +128,11 @@ HTMLLinkElement::SetDisabled(bool aDisabled) NS_IMETHODIMP HTMLLinkElement::SetMozDisabled(bool aDisabled) { - SetDisabled(aDisabled); - return NS_OK; + ErrorResult rv; + SetDisabled(aDisabled, rv); + return rv.StealNSResult(); } - NS_IMPL_STRING_ATTR(HTMLLinkElement, Charset, charset) NS_IMPL_URI_ATTR(HTMLLinkElement, Href, href) NS_IMPL_STRING_ATTR(HTMLLinkElement, Hreflang, hreflang) @@ -369,7 +380,8 @@ HTMLLinkElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, aName == nsGkAtoms::rel || aName == nsGkAtoms::title || aName == nsGkAtoms::media || - aName == nsGkAtoms::type)) { + aName == nsGkAtoms::type || + (LINK_DISABLED && aName == nsGkAtoms::disabled))) { bool dropSheet = false; if (aName == nsGkAtoms::rel) { nsAutoString value; @@ -396,17 +408,24 @@ HTMLLinkElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, dropSheet || (aName == nsGkAtoms::title || aName == nsGkAtoms::media || - aName == nsGkAtoms::type)); + aName == nsGkAtoms::type || + (LINK_DISABLED && aName == nsGkAtoms::disabled))); } } else { - // Since removing href or rel makes us no longer link to a - // stylesheet, force updates for those too. + // If the disabled attribute is removed from a link element, the + // stylesheet may be explicitly enabled. if (aNameSpaceID == kNameSpaceID_None) { + if (aName == nsGkAtoms::disabled && LINK_DISABLED) { + mExplicitlyEnabled = true; + } + // Since removing href or rel makes us no longer link to a + // stylesheet, force updates for those too. if (aName == nsGkAtoms::href || aName == nsGkAtoms::rel || aName == nsGkAtoms::title || aName == nsGkAtoms::media || - aName == nsGkAtoms::type) { + aName == nsGkAtoms::type || + (LINK_DISABLED && aName == nsGkAtoms::disabled)) { UpdateStyleSheetInternal(nullptr, nullptr, true); } if (aName == nsGkAtoms::href || @@ -499,13 +518,15 @@ HTMLLinkElement::GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, nsAString& aMedia, bool* aIsScoped, - bool* aIsAlternate) + bool* aIsAlternate, + bool* aIsExplicitlyEnabled) { aTitle.Truncate(); aType.Truncate(); aMedia.Truncate(); *aIsScoped = false; *aIsAlternate = false; + *aIsExplicitlyEnabled = false; nsAutoString rel; GetAttr(kNameSpaceID_None, nsGkAtoms::rel, rel); @@ -515,6 +536,20 @@ HTMLLinkElement::GetStyleSheetInfo(nsAString& aTitle, return; } + if (LINK_DISABLED) { + + // Is the link disabled? + if (Disabled()) { + return; + } + + // Is it explicitly enabled? + if (mExplicitlyEnabled) { + *aIsExplicitlyEnabled = true; + } + + } + nsAutoString title; GetAttr(kNameSpaceID_None, nsGkAtoms::title, title); title.CompressWhitespace(); diff --git a/dom/html/HTMLLinkElement.h b/dom/html/HTMLLinkElement.h index 421b149e9..f9c832c8d 100644 --- a/dom/html/HTMLLinkElement.h +++ b/dom/html/HTMLLinkElement.h @@ -84,8 +84,8 @@ public: virtual bool HasDeferredDNSPrefetchRequest() override; // WebIDL - bool Disabled(); - void SetDisabled(bool aDisabled); + bool Disabled() const; + void SetDisabled(bool aDisabled, ErrorResult& aRv); // XPCOM GetHref is fine. void SetHref(const nsAString& aHref, ErrorResult& aRv) { @@ -179,10 +179,18 @@ protected: nsAString& aType, nsAString& aMedia, bool* aIsScoped, - bool* aIsAlternate) override; -protected: + bool* aIsAlternate, + bool* aIsExplicitlyEnabled) override; + RefPtr mRelList; + // The "explicitly enabled" flag. This flag is set whenever the 'disabled' + // attribute is explicitly unset, and makes alternate stylesheets not be + // disabled by default anymore. + // + // See https://github.com/whatwg/html/issues/3840#issuecomment-481034206. + bool mExplicitlyEnabled = false; + private: RefPtr mImportLoader; }; diff --git a/dom/html/HTMLStyleElement.cpp b/dom/html/HTMLStyleElement.cpp index 329dda648..87dc68f83 100644 --- a/dom/html/HTMLStyleElement.cpp +++ b/dom/html/HTMLStyleElement.cpp @@ -66,7 +66,7 @@ HTMLStyleElement::GetMozDisabled(bool* aDisabled) } bool -HTMLStyleElement::Disabled() +HTMLStyleElement::Disabled() const { StyleSheet* ss = GetSheet(); return ss && ss->Disabled(); @@ -222,12 +222,14 @@ HTMLStyleElement::GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, nsAString& aMedia, bool* aIsScoped, - bool* aIsAlternate) + bool* aIsAlternate, + bool* aIsExplicitlyEnabled) { aTitle.Truncate(); aType.Truncate(); aMedia.Truncate(); *aIsAlternate = false; + *aIsExplicitlyEnabled = false; nsAutoString title; GetAttr(kNameSpaceID_None, nsGkAtoms::title, title); diff --git a/dom/html/HTMLStyleElement.h b/dom/html/HTMLStyleElement.h index 6b2a12b1f..9f82b8e51 100644 --- a/dom/html/HTMLStyleElement.h +++ b/dom/html/HTMLStyleElement.h @@ -58,7 +58,7 @@ public: NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED - bool Disabled(); + bool Disabled() const; void SetDisabled(bool aDisabled); void SetMedia(const nsAString& aMedia, ErrorResult& aError) { @@ -87,7 +87,8 @@ protected: nsAString& aType, nsAString& aMedia, bool* aIsScoped, - bool* aIsAlternate) override; + bool* aIsAlternate, + bool* aIsExplicitlyEnabled) override; /** * Common method to call from the various mutation observer methods. * aContent is a content node that's either the one that changed or its -- cgit v1.2.3