DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

/*
 * A base class which implements nsIStyleSheetLinkingElement and can
 * be subclassed by various content nodes that want to load
 * stylesheets (<style>, <link>, processing instructions, etc).
 */

#include "nsStyleLinkElement.h"

#include "mozilla/StyleSheet.h"
#include "mozilla/StyleSheetInlines.h"
#include "mozilla/css/Loader.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/FragmentOrElement.h"
#include "mozilla/dom/HTMLLinkElement.h"
#include "mozilla/dom/ShadowRoot.h"
#include "mozilla/dom/SRILogHelper.h"
#include "mozilla/Preferences.h"
#include "nsIContent.h"
#include "mozilla/dom/Document.h"
#include "nsUnicharUtils.h"
#include "nsCRT.h"
#include "nsXPCOMCIDInternal.h"
#include "nsUnicharInputStream.h"
#include "nsContentUtils.h"
#include "nsStyleUtil.h"
#include "nsQueryObject.h"

using namespace mozilla;
using namespace mozilla::dom;

nsStyleLinkElement::SheetInfo::SheetInfo(
    const Document& aDocument, nsIContent* aContent,
    already_AddRefed<nsIURI> aURI,
    already_AddRefed<nsIPrincipal> aTriggeringPrincipal,
    already_AddRefed<nsIReferrerInfo> aReferrerInfo,
    mozilla::CORSMode aCORSMode, const nsAString& aTitle,
    const nsAString& aMedia, HasAlternateRel aHasAlternateRel,
    IsInline aIsInline, IsExplicitlyEnabled aIsExplicitlyEnabled)
    : mContent(aContent),
      mURI(aURI),
      mTriggeringPrincipal(aTriggeringPrincipal),
      mReferrerInfo(aReferrerInfo),
      mCORSMode(aCORSMode),
      mTitle(aTitle),
      mMedia(aMedia),
      mHasAlternateRel(aHasAlternateRel == HasAlternateRel::Yes),
      mIsInline(aIsInline == IsInline::Yes),
      mIsExplicitlyEnabled(aIsExplicitlyEnabled) {
  MOZ_ASSERT(!mIsInline || aContent);
  MOZ_ASSERT_IF(aContent, aContent->OwnerDoc() == &aDocument);
  MOZ_ASSERT(mReferrerInfo);

  if (!mIsInline && aContent && aContent->IsElement()) {
    aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::integrity,
                                   mIntegrity);
  }
}

nsStyleLinkElement::SheetInfo::~SheetInfo() = default;

nsStyleLinkElement::nsStyleLinkElement()
    : mDontLoadStyle(false),
      mUpdatesEnabled(true),
      mLineNumber(1),
      mColumnNumber(1) {}

nsStyleLinkElement::~nsStyleLinkElement() {
  nsStyleLinkElement::SetStyleSheet(nullptr);
}

void nsStyleLinkElement::GetTitleAndMediaForElement(const Element& aSelf,
                                                    nsString& aTitle,
                                                    nsString& aMedia) {
  // Only honor title as stylesheet name for elements in the document (that is,
  // ignore for Shadow DOM), per [1] and [2]. See [3].
  //
  // [1]: https://html.spec.whatwg.org/#attr-link-title
  // [2]: https://html.spec.whatwg.org/#attr-style-title
  // [3]: https://github.com/w3c/webcomponents/issues/535
  if (aSelf.IsInUncomposedDoc()) {
    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() {
  nsStyleLinkElement::SetStyleSheet(nullptr);
}

void nsStyleLinkElement::Traverse(nsCycleCollectionTraversalCallback& cb) {
  nsStyleLinkElement* tmp = this;
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheet);
}

void nsStyleLinkElement::SetStyleSheet(StyleSheet* aStyleSheet) {
  if (mStyleSheet) {
    mStyleSheet->SetOwningNode(nullptr);
  }

  mStyleSheet = aStyleSheet;
  if (mStyleSheet) {
    nsCOMPtr<nsINode> node = do_QueryObject(this);
    if (node) {
      mStyleSheet->SetOwningNode(node);
    }
  }
}

StyleSheet* nsStyleLinkElement::GetStyleSheet() { return mStyleSheet; }

void nsStyleLinkElement::InitStyleLinkElement(bool aDontLoadStyle) {
  mDontLoadStyle = aDontLoadStyle;
}

void nsStyleLinkElement::SetEnableUpdates(bool aEnableUpdates) {
  mUpdatesEnabled = aEnableUpdates;
}

void nsStyleLinkElement::GetCharset(nsAString& aCharset) {
  aCharset.Truncate();
}

/* virtual */
void nsStyleLinkElement::OverrideBaseURI(nsIURI* aNewBaseURI) {
  MOZ_ASSERT_UNREACHABLE(
      "Base URI can't be overriden in this implementation "
      "of nsIStyleSheetLinkingElement.");
}

/* virtual */
void nsStyleLinkElement::SetLineNumber(uint32_t aLineNumber) {
  mLineNumber = aLineNumber;
}

/* virtual */
uint32_t nsStyleLinkElement::GetLineNumber() { return mLineNumber; }

/* virtual */
void nsStyleLinkElement::SetColumnNumber(uint32_t aColumnNumber) {
  mColumnNumber = aColumnNumber;
}

/* virtual */
uint32_t nsStyleLinkElement::GetColumnNumber() { return mColumnNumber; }

static uint32_t ToLinkMask(const nsAString& aLink) {
  // Keep this in sync with sRelValues in HTMLLinkElement.cpp
  if (aLink.EqualsLiteral("prefetch"))
    return nsStyleLinkElement::ePREFETCH;
  else if (aLink.EqualsLiteral("dns-prefetch"))
    return nsStyleLinkElement::eDNS_PREFETCH;
  else if (aLink.EqualsLiteral("stylesheet"))
    return nsStyleLinkElement::eSTYLESHEET;
  else if (aLink.EqualsLiteral("next"))
    return nsStyleLinkElement::eNEXT;
  else if (aLink.EqualsLiteral("alternate"))
    return nsStyleLinkElement::eALTERNATE;
  else if (aLink.EqualsLiteral("preconnect"))
    return nsStyleLinkElement::ePRECONNECT;
  else if (aLink.EqualsLiteral("preload"))
    return nsStyleLinkElement::ePRELOAD;
  else
    return 0;
}

uint32_t nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes) {
  uint32_t linkMask = 0;
  nsAString::const_iterator start, done;
  aTypes.BeginReading(start);
  aTypes.EndReading(done);
  if (start == done) return linkMask;

  nsAString::const_iterator current(start);
  bool inString = !nsContentUtils::IsHTMLWhitespace(*current);
  nsAutoString subString;

  while (current != done) {
    if (nsContentUtils::IsHTMLWhitespace(*current)) {
      if (inString) {
        nsContentUtils::ASCIIToLower(Substring(start, current), subString);
        linkMask |= ToLinkMask(subString);
        inString = false;
      }
    } else {
      if (!inString) {
        start = current;
        inString = true;
      }
    }
    ++current;
  }
  if (inString) {
    nsContentUtils::ASCIIToLower(Substring(start, current), subString);
    linkMask |= ToLinkMask(subString);
  }
  return linkMask;
}

Result<nsStyleLinkElement::Update, nsresult>
nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver) {
  return DoUpdateStyleSheet(nullptr, nullptr, aObserver, ForceUpdate::No);
}

Result<nsStyleLinkElement::Update, nsresult>
nsStyleLinkElement::UpdateStyleSheetInternal(Document* aOldDocument,
                                             ShadowRoot* aOldShadowRoot,
                                             ForceUpdate aForceUpdate) {
  return DoUpdateStyleSheet(aOldDocument, aOldShadowRoot, nullptr,
                            aForceUpdate);
}

Result<nsStyleLinkElement::Update, nsresult>
nsStyleLinkElement::DoUpdateStyleSheet(Document* aOldDocument,
                                       ShadowRoot* aOldShadowRoot,
                                       nsICSSLoaderObserver* aObserver,
                                       ForceUpdate aForceUpdate) {
  nsCOMPtr<nsIContent> thisContent = do_QueryInterface(this);
  // All instances of nsStyleLinkElement should implement nsIContent.
  MOZ_ASSERT(thisContent);

  if (thisContent->IsInSVGUseShadowTree()) {
    // Stylesheets in <use>-cloned subtrees are disabled until we figure out
    // how they should behave.
    return Update{};
  }

  if (mStyleSheet && (aOldDocument || aOldShadowRoot)) {
    MOZ_ASSERT(!(aOldDocument && aOldShadowRoot),
               "ShadowRoot content is never in document, thus "
               "there should not be a old document and old "
               "ShadowRoot simultaneously.");

    // We're removing the link element from the document or shadow tree,
    // unload the stylesheet.  We want to do this even if updates are
    // disabled, since otherwise a sheet with a stale linking element pointer
    // will be hanging around -- not good!
    if (aOldShadowRoot) {
      aOldShadowRoot->RemoveSheet(mStyleSheet);
    } else {
      aOldDocument->RemoveStyleSheet(mStyleSheet);
    }

    SetStyleSheet(nullptr);
  }

  Document* doc = thisContent->GetComposedDoc();

  // Loader could be null during unlink, see bug 1425866.
  if (!doc || !doc->CSSLoader() || !doc->CSSLoader()->GetEnabled()) {
    return Update{};
  }

  // When static documents are created, stylesheets are cloned manually.
  if (mDontLoadStyle || !mUpdatesEnabled || doc->IsStaticDocument()) {
    return Update{};
  }

  Maybe<SheetInfo> info = GetStyleSheetInfo();
  if (aForceUpdate == ForceUpdate::No && mStyleSheet && info &&
      !info->mIsInline && info->mURI) {
    if (nsIURI* oldURI = mStyleSheet->GetSheetURI()) {
      bool equal;
      nsresult rv = oldURI->Equals(info->mURI, &equal);
      if (NS_SUCCEEDED(rv) && equal) {
        return Update{};
      }
    }
  }

  if (mStyleSheet) {
    if (thisContent->IsInShadowTree()) {
      ShadowRoot* containingShadow = thisContent->GetContainingShadow();
      // Could be null only during unlink.
      if (MOZ_LIKELY(containingShadow)) {
        containingShadow->RemoveSheet(mStyleSheet);
      }
    } else {
      doc->RemoveStyleSheet(mStyleSheet);
    }

    nsStyleLinkElement::SetStyleSheet(nullptr);
  }

  if (!info) {
    return Update{};
  }

  if (!info->mURI && !info->mIsInline) {
    // If href is empty and this is not inline style then just bail
    return Update{};
  }

  if (info->mIsInline) {
    nsAutoString text;
    if (!nsContentUtils::GetNodeTextContent(thisContent, false, text,
                                            fallible)) {
      return Err(NS_ERROR_OUT_OF_MEMORY);
    }

    MOZ_ASSERT(thisContent->NodeInfo()->NameAtom() != nsGkAtoms::link,
               "<link> is not 'inline', and needs different CSP checks");
    MOZ_ASSERT(thisContent->IsElement());
    nsresult rv = NS_OK;
    if (!nsStyleUtil::CSPAllowsInlineStyle(
            thisContent->AsElement(), doc, info->mTriggeringPrincipal,
            mLineNumber, mColumnNumber, text, &rv)) {
      if (NS_FAILED(rv)) {
        return Err(rv);
      }
      return Update{};
    }

    // Parse the style sheet.
    return doc->CSSLoader()->LoadInlineStyle(*info, text, mLineNumber,
                                             aObserver);
  }
  if (thisContent->IsElement()) {
    nsAutoString integrity;
    thisContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::integrity,
                                      integrity);
    if (!integrity.IsEmpty()) {
      MOZ_LOG(SRILogHelper::GetSriLog(), mozilla::LogLevel::Debug,
              ("nsStyleLinkElement::DoUpdateStyleSheet, integrity=%s",
               NS_ConvertUTF16toUTF8(integrity).get()));
    }
  }
  auto resultOrError = doc->CSSLoader()->LoadStyleLink(*info, aObserver);
  if (resultOrError.isErr()) {
    // Don't propagate LoadStyleLink() errors further than this, since some
    // consumers (e.g. nsXMLContentSink) will completely abort on innocuous
    // things like a stylesheet load being blocked by the security system.
    return Update{};
  }
  return resultOrError;
}