content/base/src/nsStyleLinkElement.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * A base class which implements nsIStyleSheetLinkingElement and can
michael@0 9 * be subclassed by various content nodes that want to load
michael@0 10 * stylesheets (<style>, <link>, processing instructions, etc).
michael@0 11 */
michael@0 12
michael@0 13 #include "nsStyleLinkElement.h"
michael@0 14
michael@0 15 #include "mozilla/css/Loader.h"
michael@0 16 #include "mozilla/dom/Element.h"
michael@0 17 #include "mozilla/dom/ShadowRoot.h"
michael@0 18 #include "nsCSSStyleSheet.h"
michael@0 19 #include "nsIContent.h"
michael@0 20 #include "nsIDocument.h"
michael@0 21 #include "nsIDOMComment.h"
michael@0 22 #include "nsIDOMNode.h"
michael@0 23 #include "nsIDOMStyleSheet.h"
michael@0 24 #include "nsNetUtil.h"
michael@0 25 #include "nsUnicharUtils.h"
michael@0 26 #include "nsCRT.h"
michael@0 27 #include "nsXPCOMCIDInternal.h"
michael@0 28 #include "nsUnicharInputStream.h"
michael@0 29 #include "nsContentUtils.h"
michael@0 30 #include "nsStyleUtil.h"
michael@0 31
michael@0 32 using namespace mozilla;
michael@0 33 using namespace mozilla::dom;
michael@0 34
michael@0 35 nsStyleLinkElement::nsStyleLinkElement()
michael@0 36 : mDontLoadStyle(false)
michael@0 37 , mUpdatesEnabled(true)
michael@0 38 , mLineNumber(1)
michael@0 39 {
michael@0 40 }
michael@0 41
michael@0 42 nsStyleLinkElement::~nsStyleLinkElement()
michael@0 43 {
michael@0 44 nsStyleLinkElement::SetStyleSheet(nullptr);
michael@0 45 }
michael@0 46
michael@0 47 void
michael@0 48 nsStyleLinkElement::Unlink()
michael@0 49 {
michael@0 50 mStyleSheet = nullptr;
michael@0 51 }
michael@0 52
michael@0 53 void
michael@0 54 nsStyleLinkElement::Traverse(nsCycleCollectionTraversalCallback &cb)
michael@0 55 {
michael@0 56 nsStyleLinkElement* tmp = this;
michael@0 57 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheet);
michael@0 58 }
michael@0 59
michael@0 60 NS_IMETHODIMP
michael@0 61 nsStyleLinkElement::SetStyleSheet(nsCSSStyleSheet* aStyleSheet)
michael@0 62 {
michael@0 63 if (mStyleSheet) {
michael@0 64 mStyleSheet->SetOwningNode(nullptr);
michael@0 65 }
michael@0 66
michael@0 67 mStyleSheet = aStyleSheet;
michael@0 68 if (mStyleSheet) {
michael@0 69 nsCOMPtr<nsINode> node = do_QueryObject(this);
michael@0 70 if (node) {
michael@0 71 mStyleSheet->SetOwningNode(node);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 return NS_OK;
michael@0 76 }
michael@0 77
michael@0 78 NS_IMETHODIMP
michael@0 79 nsStyleLinkElement::GetStyleSheet(nsIStyleSheet*& aStyleSheet)
michael@0 80 {
michael@0 81 aStyleSheet = mStyleSheet;
michael@0 82 NS_IF_ADDREF(aStyleSheet);
michael@0 83
michael@0 84 return NS_OK;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMETHODIMP
michael@0 88 nsStyleLinkElement::InitStyleLinkElement(bool aDontLoadStyle)
michael@0 89 {
michael@0 90 mDontLoadStyle = aDontLoadStyle;
michael@0 91
michael@0 92 return NS_OK;
michael@0 93 }
michael@0 94
michael@0 95 NS_IMETHODIMP
michael@0 96 nsStyleLinkElement::SetEnableUpdates(bool aEnableUpdates)
michael@0 97 {
michael@0 98 mUpdatesEnabled = aEnableUpdates;
michael@0 99
michael@0 100 return NS_OK;
michael@0 101 }
michael@0 102
michael@0 103 NS_IMETHODIMP
michael@0 104 nsStyleLinkElement::GetCharset(nsAString& aCharset)
michael@0 105 {
michael@0 106 // descendants have to implement this themselves
michael@0 107 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 108 }
michael@0 109
michael@0 110 /* virtual */ void
michael@0 111 nsStyleLinkElement::OverrideBaseURI(nsIURI* aNewBaseURI)
michael@0 112 {
michael@0 113 NS_NOTREACHED("Base URI can't be overriden in this implementation "
michael@0 114 "of nsIStyleSheetLinkingElement.");
michael@0 115 }
michael@0 116
michael@0 117 /* virtual */ void
michael@0 118 nsStyleLinkElement::SetLineNumber(uint32_t aLineNumber)
michael@0 119 {
michael@0 120 mLineNumber = aLineNumber;
michael@0 121 }
michael@0 122
michael@0 123 static uint32_t ToLinkMask(const nsAString& aLink)
michael@0 124 {
michael@0 125 if (aLink.EqualsLiteral("prefetch"))
michael@0 126 return nsStyleLinkElement::ePREFETCH;
michael@0 127 else if (aLink.EqualsLiteral("dns-prefetch"))
michael@0 128 return nsStyleLinkElement::eDNS_PREFETCH;
michael@0 129 else if (aLink.EqualsLiteral("stylesheet"))
michael@0 130 return nsStyleLinkElement::eSTYLESHEET;
michael@0 131 else if (aLink.EqualsLiteral("next"))
michael@0 132 return nsStyleLinkElement::eNEXT;
michael@0 133 else if (aLink.EqualsLiteral("alternate"))
michael@0 134 return nsStyleLinkElement::eALTERNATE;
michael@0 135 else
michael@0 136 return 0;
michael@0 137 }
michael@0 138
michael@0 139 uint32_t nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes)
michael@0 140 {
michael@0 141 uint32_t linkMask = 0;
michael@0 142 nsAString::const_iterator start, done;
michael@0 143 aTypes.BeginReading(start);
michael@0 144 aTypes.EndReading(done);
michael@0 145 if (start == done)
michael@0 146 return linkMask;
michael@0 147
michael@0 148 nsAString::const_iterator current(start);
michael@0 149 bool inString = !nsContentUtils::IsHTMLWhitespace(*current);
michael@0 150 nsAutoString subString;
michael@0 151
michael@0 152 while (current != done) {
michael@0 153 if (nsContentUtils::IsHTMLWhitespace(*current)) {
michael@0 154 if (inString) {
michael@0 155 nsContentUtils::ASCIIToLower(Substring(start, current), subString);
michael@0 156 linkMask |= ToLinkMask(subString);
michael@0 157 inString = false;
michael@0 158 }
michael@0 159 }
michael@0 160 else {
michael@0 161 if (!inString) {
michael@0 162 start = current;
michael@0 163 inString = true;
michael@0 164 }
michael@0 165 }
michael@0 166 ++current;
michael@0 167 }
michael@0 168 if (inString) {
michael@0 169 nsContentUtils::ASCIIToLower(Substring(start, current), subString);
michael@0 170 linkMask |= ToLinkMask(subString);
michael@0 171 }
michael@0 172 return linkMask;
michael@0 173 }
michael@0 174
michael@0 175 NS_IMETHODIMP
michael@0 176 nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
michael@0 177 bool* aWillNotify,
michael@0 178 bool* aIsAlternate)
michael@0 179 {
michael@0 180 return DoUpdateStyleSheet(nullptr, nullptr, aObserver, aWillNotify,
michael@0 181 aIsAlternate, false);
michael@0 182 }
michael@0 183
michael@0 184 nsresult
michael@0 185 nsStyleLinkElement::UpdateStyleSheetInternal(nsIDocument *aOldDocument,
michael@0 186 ShadowRoot *aOldShadowRoot,
michael@0 187 bool aForceUpdate)
michael@0 188 {
michael@0 189 bool notify, alternate;
michael@0 190 return DoUpdateStyleSheet(aOldDocument, aOldShadowRoot, nullptr, &notify,
michael@0 191 &alternate, aForceUpdate);
michael@0 192 }
michael@0 193
michael@0 194 static bool
michael@0 195 IsScopedStyleElement(nsIContent* aContent)
michael@0 196 {
michael@0 197 // This is quicker than, say, QIing aContent to nsStyleLinkElement
michael@0 198 // and then calling its virtual GetStyleSheetInfo method to find out
michael@0 199 // if it is scoped.
michael@0 200 return (aContent->IsHTML(nsGkAtoms::style) ||
michael@0 201 aContent->IsSVG(nsGkAtoms::style)) &&
michael@0 202 aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::scoped);
michael@0 203 }
michael@0 204
michael@0 205 static void
michael@0 206 SetIsElementInStyleScopeFlagOnSubtree(Element* aElement)
michael@0 207 {
michael@0 208 if (aElement->IsElementInStyleScope()) {
michael@0 209 return;
michael@0 210 }
michael@0 211
michael@0 212 aElement->SetIsElementInStyleScope();
michael@0 213
michael@0 214 nsIContent* n = aElement->GetNextNode(aElement);
michael@0 215 while (n) {
michael@0 216 if (n->IsElementInStyleScope()) {
michael@0 217 n = n->GetNextNonChildNode(aElement);
michael@0 218 } else {
michael@0 219 if (n->IsElement()) {
michael@0 220 n->SetIsElementInStyleScope();
michael@0 221 }
michael@0 222 n = n->GetNextNode(aElement);
michael@0 223 }
michael@0 224 }
michael@0 225 }
michael@0 226
michael@0 227 static bool
michael@0 228 HasScopedStyleSheetChild(nsIContent* aContent)
michael@0 229 {
michael@0 230 for (nsIContent* n = aContent->GetFirstChild(); n; n = n->GetNextSibling()) {
michael@0 231 if (IsScopedStyleElement(n)) {
michael@0 232 return true;
michael@0 233 }
michael@0 234 }
michael@0 235 return false;
michael@0 236 }
michael@0 237
michael@0 238 // Called when aElement has had a <style scoped> child removed.
michael@0 239 static void
michael@0 240 UpdateIsElementInStyleScopeFlagOnSubtree(Element* aElement)
michael@0 241 {
michael@0 242 NS_ASSERTION(aElement->IsElementInStyleScope(),
michael@0 243 "only call UpdateIsElementInStyleScopeFlagOnSubtree on a "
michael@0 244 "subtree that has IsElementInStyleScope boolean flag set");
michael@0 245
michael@0 246 if (HasScopedStyleSheetChild(aElement)) {
michael@0 247 return;
michael@0 248 }
michael@0 249
michael@0 250 aElement->ClearIsElementInStyleScope();
michael@0 251
michael@0 252 nsIContent* n = aElement->GetNextNode(aElement);
michael@0 253 while (n) {
michael@0 254 if (HasScopedStyleSheetChild(n)) {
michael@0 255 n = n->GetNextNonChildNode(aElement);
michael@0 256 } else {
michael@0 257 if (n->IsElement()) {
michael@0 258 n->ClearIsElementInStyleScope();
michael@0 259 }
michael@0 260 n = n->GetNextNode(aElement);
michael@0 261 }
michael@0 262 }
michael@0 263 }
michael@0 264
michael@0 265 static Element*
michael@0 266 GetScopeElement(nsIStyleSheet* aSheet)
michael@0 267 {
michael@0 268 nsRefPtr<nsCSSStyleSheet> cssStyleSheet = do_QueryObject(aSheet);
michael@0 269 if (!cssStyleSheet) {
michael@0 270 return nullptr;
michael@0 271 }
michael@0 272
michael@0 273 return cssStyleSheet->GetScopeElement();
michael@0 274 }
michael@0 275
michael@0 276 nsresult
michael@0 277 nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument,
michael@0 278 ShadowRoot* aOldShadowRoot,
michael@0 279 nsICSSLoaderObserver* aObserver,
michael@0 280 bool* aWillNotify,
michael@0 281 bool* aIsAlternate,
michael@0 282 bool aForceUpdate)
michael@0 283 {
michael@0 284 *aWillNotify = false;
michael@0 285
michael@0 286 nsCOMPtr<nsIContent> thisContent;
michael@0 287 CallQueryInterface(this, getter_AddRefs(thisContent));
michael@0 288
michael@0 289 // All instances of nsStyleLinkElement should implement nsIContent.
michael@0 290 NS_ENSURE_TRUE(thisContent, NS_ERROR_FAILURE);
michael@0 291
michael@0 292 // Check for a ShadowRoot because link elements are inert in a
michael@0 293 // ShadowRoot.
michael@0 294 ShadowRoot* containingShadow = thisContent->GetContainingShadow();
michael@0 295 if (thisContent->IsHTML(nsGkAtoms::link) &&
michael@0 296 (aOldShadowRoot || containingShadow)) {
michael@0 297 return NS_OK;
michael@0 298 }
michael@0 299
michael@0 300 Element* oldScopeElement = GetScopeElement(mStyleSheet);
michael@0 301
michael@0 302 if (mStyleSheet && aOldDocument) {
michael@0 303 // We're removing the link element from the document, unload the
michael@0 304 // stylesheet. We want to do this even if updates are disabled, since
michael@0 305 // otherwise a sheet with a stale linking element pointer will be hanging
michael@0 306 // around -- not good!
michael@0 307 if (aOldShadowRoot) {
michael@0 308 aOldShadowRoot->RemoveSheet(mStyleSheet);
michael@0 309 } else {
michael@0 310 aOldDocument->BeginUpdate(UPDATE_STYLE);
michael@0 311 aOldDocument->RemoveStyleSheet(mStyleSheet);
michael@0 312 aOldDocument->EndUpdate(UPDATE_STYLE);
michael@0 313 }
michael@0 314
michael@0 315 nsStyleLinkElement::SetStyleSheet(nullptr);
michael@0 316 if (oldScopeElement) {
michael@0 317 UpdateIsElementInStyleScopeFlagOnSubtree(oldScopeElement);
michael@0 318 }
michael@0 319 }
michael@0 320
michael@0 321 // When static documents are created, stylesheets are cloned manually.
michael@0 322 if (mDontLoadStyle || !mUpdatesEnabled ||
michael@0 323 thisContent->OwnerDoc()->IsStaticDocument()) {
michael@0 324 return NS_OK;
michael@0 325 }
michael@0 326
michael@0 327 nsCOMPtr<nsIDocument> doc = thisContent->GetDocument();
michael@0 328
michael@0 329 if (!doc || !doc->CSSLoader()->GetEnabled()) {
michael@0 330 return NS_OK;
michael@0 331 }
michael@0 332
michael@0 333 bool isInline;
michael@0 334 nsCOMPtr<nsIURI> uri = GetStyleSheetURL(&isInline);
michael@0 335
michael@0 336 if (!aForceUpdate && mStyleSheet && !isInline && uri) {
michael@0 337 nsIURI* oldURI = mStyleSheet->GetSheetURI();
michael@0 338 if (oldURI) {
michael@0 339 bool equal;
michael@0 340 nsresult rv = oldURI->Equals(uri, &equal);
michael@0 341 if (NS_SUCCEEDED(rv) && equal) {
michael@0 342 return NS_OK; // We already loaded this stylesheet
michael@0 343 }
michael@0 344 }
michael@0 345 }
michael@0 346
michael@0 347 if (mStyleSheet) {
michael@0 348 if (thisContent->HasFlag(NODE_IS_IN_SHADOW_TREE)) {
michael@0 349 ShadowRoot* containingShadow = thisContent->GetContainingShadow();
michael@0 350 containingShadow->RemoveSheet(mStyleSheet);
michael@0 351 } else {
michael@0 352 doc->BeginUpdate(UPDATE_STYLE);
michael@0 353 doc->RemoveStyleSheet(mStyleSheet);
michael@0 354 doc->EndUpdate(UPDATE_STYLE);
michael@0 355 }
michael@0 356
michael@0 357 nsStyleLinkElement::SetStyleSheet(nullptr);
michael@0 358 }
michael@0 359
michael@0 360 if (!uri && !isInline) {
michael@0 361 return NS_OK; // If href is empty and this is not inline style then just bail
michael@0 362 }
michael@0 363
michael@0 364 nsAutoString title, type, media;
michael@0 365 bool isScoped;
michael@0 366 bool isAlternate;
michael@0 367
michael@0 368 GetStyleSheetInfo(title, type, media, &isScoped, &isAlternate);
michael@0 369
michael@0 370 if (!type.LowerCaseEqualsLiteral("text/css")) {
michael@0 371 return NS_OK;
michael@0 372 }
michael@0 373
michael@0 374 Element* scopeElement = isScoped ? thisContent->GetParentElement() : nullptr;
michael@0 375 if (scopeElement) {
michael@0 376 NS_ASSERTION(isInline, "non-inline style must not have scope element");
michael@0 377 SetIsElementInStyleScopeFlagOnSubtree(scopeElement);
michael@0 378 }
michael@0 379
michael@0 380 bool doneLoading = false;
michael@0 381 nsresult rv = NS_OK;
michael@0 382 if (isInline) {
michael@0 383 nsAutoString text;
michael@0 384 if (!nsContentUtils::GetNodeTextContent(thisContent, false, text)) {
michael@0 385 return NS_ERROR_OUT_OF_MEMORY;
michael@0 386 }
michael@0 387
michael@0 388 MOZ_ASSERT(thisContent->Tag() != nsGkAtoms::link,
michael@0 389 "<link> is not 'inline', and needs different CSP checks");
michael@0 390 if (!nsStyleUtil::CSPAllowsInlineStyle(thisContent,
michael@0 391 thisContent->NodePrincipal(),
michael@0 392 doc->GetDocumentURI(),
michael@0 393 mLineNumber, text, &rv))
michael@0 394 return rv;
michael@0 395
michael@0 396 // Parse the style sheet.
michael@0 397 rv = doc->CSSLoader()->
michael@0 398 LoadInlineStyle(thisContent, text, mLineNumber, title, media,
michael@0 399 scopeElement, aObserver, &doneLoading, &isAlternate);
michael@0 400 }
michael@0 401 else {
michael@0 402 // XXXbz clone the URI here to work around content policies modifying URIs.
michael@0 403 nsCOMPtr<nsIURI> clonedURI;
michael@0 404 uri->Clone(getter_AddRefs(clonedURI));
michael@0 405 NS_ENSURE_TRUE(clonedURI, NS_ERROR_OUT_OF_MEMORY);
michael@0 406 rv = doc->CSSLoader()->
michael@0 407 LoadStyleLink(thisContent, clonedURI, title, media, isAlternate,
michael@0 408 GetCORSMode(), aObserver, &isAlternate);
michael@0 409 if (NS_FAILED(rv)) {
michael@0 410 // Don't propagate LoadStyleLink() errors further than this, since some
michael@0 411 // consumers (e.g. nsXMLContentSink) will completely abort on innocuous
michael@0 412 // things like a stylesheet load being blocked by the security system.
michael@0 413 doneLoading = true;
michael@0 414 isAlternate = false;
michael@0 415 rv = NS_OK;
michael@0 416 }
michael@0 417 }
michael@0 418
michael@0 419 NS_ENSURE_SUCCESS(rv, rv);
michael@0 420
michael@0 421 *aWillNotify = !doneLoading;
michael@0 422 *aIsAlternate = isAlternate;
michael@0 423
michael@0 424 return NS_OK;
michael@0 425 }
michael@0 426
michael@0 427 void
michael@0 428 nsStyleLinkElement::UpdateStyleSheetScopedness(bool aIsNowScoped)
michael@0 429 {
michael@0 430 if (!mStyleSheet) {
michael@0 431 return;
michael@0 432 }
michael@0 433
michael@0 434 nsCOMPtr<nsIContent> thisContent;
michael@0 435 CallQueryInterface(this, getter_AddRefs(thisContent));
michael@0 436
michael@0 437 Element* oldScopeElement = mStyleSheet->GetScopeElement();
michael@0 438 Element* newScopeElement = aIsNowScoped ?
michael@0 439 thisContent->GetParentElement() :
michael@0 440 nullptr;
michael@0 441
michael@0 442 if (oldScopeElement == newScopeElement) {
michael@0 443 return;
michael@0 444 }
michael@0 445
michael@0 446 nsIDocument* document = thisContent->GetOwnerDocument();
michael@0 447
michael@0 448 if (thisContent->HasFlag(NODE_IS_IN_SHADOW_TREE)) {
michael@0 449 ShadowRoot* containingShadow = thisContent->GetContainingShadow();
michael@0 450 containingShadow->RemoveSheet(mStyleSheet);
michael@0 451
michael@0 452 mStyleSheet->SetScopeElement(newScopeElement);
michael@0 453
michael@0 454 containingShadow->InsertSheet(mStyleSheet, thisContent);
michael@0 455 } else {
michael@0 456 document->BeginUpdate(UPDATE_STYLE);
michael@0 457 document->RemoveStyleSheet(mStyleSheet);
michael@0 458
michael@0 459 mStyleSheet->SetScopeElement(newScopeElement);
michael@0 460
michael@0 461 document->AddStyleSheet(mStyleSheet);
michael@0 462 document->EndUpdate(UPDATE_STYLE);
michael@0 463 }
michael@0 464
michael@0 465 if (oldScopeElement) {
michael@0 466 UpdateIsElementInStyleScopeFlagOnSubtree(oldScopeElement);
michael@0 467 }
michael@0 468 if (newScopeElement) {
michael@0 469 SetIsElementInStyleScopeFlagOnSubtree(newScopeElement);
michael@0 470 }
michael@0 471 }

mercurial