/* * Copyright (C) 1999 Lars Knoll (knoll@kde.org) * (C) 1999 Antti Koivisto (koivisto@kde.org) * (C) 2001 Dirk Mueller (mueller@kde.org) * (C) 2006 Alexey Proskuryakov (ap@webkit.org) * Copyright (C) 2004-2009, 2011-2012, 2015 Apple Inc. All rights reserved. * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) * Copyright (C) 2008, 2009, 2011, 2012 Google Inc. All rights reserved. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) * Copyright (C) Research In Motion Limited 2010-2011. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "config.h" #include "AuthorStyleSheets.h" #include "CSSStyleSheet.h" #include "Element.h" #include "ExtensionStyleSheets.h" #include "HTMLIFrameElement.h" #include "HTMLLinkElement.h" #include "HTMLStyleElement.h" #include "InspectorInstrumentation.h" #include "Page.h" #include "PageGroup.h" #include "ProcessingInstruction.h" #include "SVGNames.h" #include "SVGStyleElement.h" #include "Settings.h" #include "ShadowRoot.h" #include "StyleInvalidationAnalysis.h" #include "StyleResolver.h" #include "StyleSheetContents.h" #include "StyleSheetList.h" #include "UserContentController.h" #include "UserContentURLPattern.h" #include "UserStyleSheet.h" namespace WebCore { using namespace ContentExtensions; using namespace HTMLNames; AuthorStyleSheets::AuthorStyleSheets(Document& document) : m_document(document) { } AuthorStyleSheets::AuthorStyleSheets(ShadowRoot& shadowRoot) : m_document(shadowRoot.documentScope()) , m_shadowRoot(&shadowRoot) { } // This method is called whenever a top-level stylesheet has finished loading. void AuthorStyleSheets::removePendingSheet(RemovePendingSheetNotificationType notification) { // Make sure we knew this sheet was pending, and that our count isn't out of sync. ASSERT(m_pendingStyleSheetCount > 0); m_pendingStyleSheetCount--; #ifdef INSTRUMENT_LAYOUT_SCHEDULING if (!ownerElement()) printf("Stylesheet loaded at time %d. %d stylesheets still remain.\n", elapsedTime(), m_pendingStylesheets); #endif if (m_pendingStyleSheetCount) return; if (notification == RemovePendingSheetNotifyLater) { m_document.setNeedsNotifyRemoveAllPendingStylesheet(); return; } if (m_shadowRoot) { m_shadowRoot->updateStyle(); return; } m_document.didRemoveAllPendingStylesheet(); } void AuthorStyleSheets::addStyleSheetCandidateNode(Node& node, bool createdByParser) { if (!node.inDocument()) return; // Until the exists, we have no choice but to compare document positions, // since styles outside of the body and head continue to be shunted into the head // (and thus can shift to end up before dynamically added DOM content that is also // outside the body). if ((createdByParser && m_document.bodyOrFrameset()) || m_styleSheetCandidateNodes.isEmpty()) { m_styleSheetCandidateNodes.add(&node); return; } // Determine an appropriate insertion point. auto begin = m_styleSheetCandidateNodes.begin(); auto end = m_styleSheetCandidateNodes.end(); auto it = end; Node* followingNode = nullptr; do { --it; Node* n = *it; unsigned short position = n->compareDocumentPosition(&node); if (position == Node::DOCUMENT_POSITION_FOLLOWING) { m_styleSheetCandidateNodes.insertBefore(followingNode, &node); return; } followingNode = n; } while (it != begin); m_styleSheetCandidateNodes.insertBefore(followingNode, &node); } void AuthorStyleSheets::removeStyleSheetCandidateNode(Node& node) { m_styleSheetCandidateNodes.remove(&node); } void AuthorStyleSheets::collectActiveStyleSheets(Vector>& sheets) { if (m_document.settings() && !m_document.settings()->authorAndUserStylesEnabled()) return; for (auto& node : m_styleSheetCandidateNodes) { StyleSheet* sheet = nullptr; if (is(*node)) { // Processing instruction (XML documents only). // We don't support linking to embedded CSS stylesheets, see for discussion. ProcessingInstruction& pi = downcast(*node); sheet = pi.sheet(); #if ENABLE(XSLT) // Don't apply XSL transforms to already transformed documents -- if (pi.isXSL() && !m_document.transformSourceDocument()) { // Don't apply XSL transforms until loading is finished. if (!m_document.parsing()) m_document.applyXSLTransform(&pi); return; } #endif } else if (is(*node) || is(*node) || is(*node)) { Element& element = downcast(*node); AtomicString title = element.fastGetAttribute(titleAttr); bool enabledViaScript = false; if (is(element)) { // element HTMLLinkElement& linkElement = downcast(element); if (linkElement.isDisabled()) continue; enabledViaScript = linkElement.isEnabledViaScript(); if (linkElement.styleSheetIsLoading()) { // it is loading but we should still decide which style sheet set to use if (!enabledViaScript && !title.isEmpty() && m_preferredStylesheetSetName.isEmpty()) { if (!linkElement.fastGetAttribute(relAttr).contains("alternate")) { m_preferredStylesheetSetName = title; m_selectedStylesheetSetName = title; } } continue; } if (!linkElement.sheet()) title = nullAtom; } // Get the current preferred styleset. This is the // set of sheets that will be enabled. if (is(element)) sheet = downcast(element).sheet(); else if (is(element)) sheet = downcast(element).sheet(); else sheet = downcast(element).sheet(); // Check to see if this sheet belongs to a styleset // (thus making it PREFERRED or ALTERNATE rather than // PERSISTENT). auto& rel = element.fastGetAttribute(relAttr); if (!enabledViaScript && !title.isEmpty()) { // Yes, we have a title. if (m_preferredStylesheetSetName.isEmpty()) { // No preferred set has been established. If // we are NOT an alternate sheet, then establish // us as the preferred set. Otherwise, just ignore // this sheet. if (is(element) || !rel.contains("alternate")) m_preferredStylesheetSetName = m_selectedStylesheetSetName = title; } if (title != m_preferredStylesheetSetName) sheet = nullptr; } if (rel.contains("alternate") && title.isEmpty()) sheet = nullptr; } if (sheet) sheets.append(sheet); } } AuthorStyleSheets::StyleResolverUpdateType AuthorStyleSheets::analyzeStyleSheetChange(UpdateFlag updateFlag, const Vector>& newStylesheets, bool& requiresFullStyleRecalc) { requiresFullStyleRecalc = true; // Stylesheets of