blob: a233aceffbaf819d7f2f9e5cbeb5de8b225b8ec6 [file] [log] [blame]
darin@apple.com877ce5b2010-05-28 15:38:58 +00001/*
eseidel1bdb6f52006-05-12 20:44:25 +00002 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
antti@apple.com73ce3fe2013-08-19 10:36:11 +00005 * Copyright (C) 2003, 2010, 2013 Apple Inc. All rights reserved.
bdashe0f7f142007-07-16 08:38:41 +00006 * (C) 2007 Rob Buis (buis@kde.org)
eseidel1bdb6f52006-05-12 20:44:25 +00007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000020 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
eseidel1bdb6f52006-05-12 20:44:25 +000022 */
darin@apple.com9a925fa2009-05-04 18:00:34 +000023
eseidel1bdb6f52006-05-12 20:44:25 +000024#include "config.h"
25#include "HTMLStyleElement.h"
26
antti@apple.com804e8152016-09-29 19:36:39 +000027#include "CachedResource.h"
eseidel1bdb6f52006-05-12 20:44:25 +000028#include "Document.h"
dbates@webkit.org03278c12012-02-24 16:50:55 +000029#include "Event.h"
cdumez@apple.com06ee4a52016-06-15 20:59:07 +000030#include "EventNames.h"
bdakin@apple.com26cc0332014-10-03 20:08:32 +000031#include "EventSender.h"
eseidel1bdb6f52006-05-12 20:44:25 +000032#include "HTMLNames.h"
commit-queue@webkit.org209c8cc2012-10-09 21:53:22 +000033#include "MediaList.h"
megan_gardner@apple.com06160632018-03-16 01:12:39 +000034#include "MediaQueryParser.h"
akling@apple.comb621d002013-09-17 09:27:21 +000035#include "RuntimeEnabledFeatures.h"
simon.fraser@apple.com5b0a15f2011-01-09 17:13:06 +000036#include "ScriptableDocumentParser.h"
commit-queue@webkit.orgb17e2892012-06-12 12:28:29 +000037#include "ShadowRoot.h"
antti@apple.com215ce402016-10-07 16:55:11 +000038#include "StyleScope.h"
antti@apple.com069713a2012-05-24 08:54:22 +000039#include "StyleSheetContents.h"
fpizlo@apple.com197cd322018-03-17 06:11:00 +000040#include <wtf/IsoMallocInlines.h>
darin@apple.com9ab51c782017-07-18 01:53:34 +000041#include <wtf/NeverDestroyed.h>
eseidel1bdb6f52006-05-12 20:44:25 +000042
43namespace WebCore {
44
fpizlo@apple.com197cd322018-03-17 06:11:00 +000045WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLStyleElement);
46
eseidel1bdb6f52006-05-12 20:44:25 +000047using namespace HTMLNames;
48
bdakin@apple.com26cc0332014-10-03 20:08:32 +000049static StyleEventSender& styleLoadEventSender()
50{
akling@apple.comee1218c2016-01-09 13:13:41 +000051 static NeverDestroyed<StyleEventSender> sharedLoadEventSender(eventNames().loadEvent);
bdakin@apple.com26cc0332014-10-03 20:08:32 +000052 return sharedLoadEventSender;
53}
dbates@webkit.org03278c12012-02-24 16:50:55 +000054
weinig@apple.com49178832013-09-15 00:39:29 +000055inline HTMLStyleElement::HTMLStyleElement(const QualifiedName& tagName, Document& document, bool createdByParser)
weinig@apple.comdedf67e2013-09-15 05:23:01 +000056 : HTMLElement(tagName, document)
antti@apple.com73ce3fe2013-08-19 10:36:11 +000057 , m_styleSheetOwner(document, createdByParser)
eseidel1bdb6f52006-05-12 20:44:25 +000058{
jchaffraix@webkit.org74dae402008-12-01 23:07:04 +000059 ASSERT(hasTagName(styleTag));
eseidel1bdb6f52006-05-12 20:44:25 +000060}
61
ap@apple.com1fe90892010-11-10 21:12:39 +000062HTMLStyleElement::~HTMLStyleElement()
63{
antti@apple.comdff10722016-10-06 20:53:08 +000064 m_styleSheetOwner.clearDocumentData(*this);
dbates@webkit.org03278c12012-02-24 16:50:55 +000065
bdakin@apple.com26cc0332014-10-03 20:08:32 +000066 styleLoadEventSender().cancelEvent(*this);
ap@apple.com1fe90892010-11-10 21:12:39 +000067}
68
weinig@apple.com02f433a2015-01-06 22:32:48 +000069Ref<HTMLStyleElement> HTMLStyleElement::create(const QualifiedName& tagName, Document& document, bool createdByParser)
darin@apple.com877ce5b2010-05-28 15:38:58 +000070{
weinig@apple.com02f433a2015-01-06 22:32:48 +000071 return adoptRef(*new HTMLStyleElement(tagName, document, createdByParser));
darin@apple.com877ce5b2010-05-28 15:38:58 +000072}
73
darin@apple.comefecd252016-11-12 22:30:22 +000074Ref<HTMLStyleElement> HTMLStyleElement::create(Document& document)
75{
76 return adoptRef(*new HTMLStyleElement(styleTag, document, false));
77}
78
akling@apple.com43e9d042012-11-18 16:55:06 +000079void HTMLStyleElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
eseidel1bdb6f52006-05-12 20:44:25 +000080{
antti@apple.com04c7fa32013-08-19 08:56:27 +000081 if (name == titleAttr && sheet())
82 sheet()->setTitle(value);
antti@apple.com7d5d8532013-08-19 17:42:47 +000083 else if (name == mediaAttr) {
antti@apple.com73ce3fe2013-08-19 10:36:11 +000084 m_styleSheetOwner.setMedia(value);
antti@apple.com7d5d8532013-08-19 17:42:47 +000085 if (sheet()) {
megan_gardner@apple.com06160632018-03-16 01:12:39 +000086 sheet()->setMediaQueries(MediaQuerySet::create(value, MediaQueryParserContext(document())));
antti@apple.com215ce402016-10-07 16:55:11 +000087 if (auto* scope = m_styleSheetOwner.styleScope())
antti@apple.com4f9ac5b2016-10-21 13:36:45 +000088 scope->didChangeStyleSheetContents();
antti@apple.comdff10722016-10-06 20:53:08 +000089 } else
90 m_styleSheetOwner.childrenChanged(*this);
antti@apple.com04c7fa32013-08-19 08:56:27 +000091 } else if (name == typeAttr)
antti@apple.com73ce3fe2013-08-19 10:36:11 +000092 m_styleSheetOwner.setContentType(value);
antti@apple.com04c7fa32013-08-19 08:56:27 +000093 else
akling@apple.com43e9d042012-11-18 16:55:06 +000094 HTMLElement::parseAttribute(name, value);
eseidel1bdb6f52006-05-12 20:44:25 +000095}
96
akling@apple.comb7b8be22013-09-17 08:13:32 +000097void HTMLStyleElement::finishParsingChildren()
98{
weinig@apple.com65031822013-09-25 18:09:47 +000099 m_styleSheetOwner.finishParsingChildren(*this);
akling@apple.comb7b8be22013-09-17 08:13:32 +0000100 HTMLElement::finishParsingChildren();
101}
102
rniwa@webkit.orgddc3f392017-10-21 00:49:21 +0000103Node::InsertedIntoAncestorResult HTMLStyleElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree)
akling@apple.comb7b8be22013-09-17 08:13:32 +0000104{
rniwa@webkit.orgddc3f392017-10-21 00:49:21 +0000105 auto result = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree);
rniwa@webkit.orga8490de2017-10-18 21:34:25 +0000106 if (insertionType.connectedToDocument)
antti@apple.comdff10722016-10-06 20:53:08 +0000107 m_styleSheetOwner.insertedIntoDocument(*this);
rniwa@webkit.org31b42da2016-11-04 22:53:33 +0000108 return result;
akling@apple.comb7b8be22013-09-17 08:13:32 +0000109}
110
rniwa@webkit.orgddc3f392017-10-21 00:49:21 +0000111void HTMLStyleElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree)
akling@apple.comb7b8be22013-09-17 08:13:32 +0000112{
rniwa@webkit.orgddc3f392017-10-21 00:49:21 +0000113 HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree);
rniwa@webkit.orgf2fe9c42017-10-19 10:05:21 +0000114 if (removalType.disconnectedFromDocument)
antti@apple.comdff10722016-10-06 20:53:08 +0000115 m_styleSheetOwner.removedFromDocument(*this);
akling@apple.comb7b8be22013-09-17 08:13:32 +0000116}
117
118void HTMLStyleElement::childrenChanged(const ChildChange& change)
119{
120 HTMLElement::childrenChanged(change);
weinig@apple.com65031822013-09-25 18:09:47 +0000121 m_styleSheetOwner.childrenChanged(*this);
akling@apple.comb7b8be22013-09-17 08:13:32 +0000122}
rolandsteiner@chromium.orgab7f9752011-12-05 11:48:28 +0000123
bdakin@apple.com26cc0332014-10-03 20:08:32 +0000124void HTMLStyleElement::dispatchPendingLoadEvents()
dbates@webkit.org03278c12012-02-24 16:50:55 +0000125{
bdakin@apple.com26cc0332014-10-03 20:08:32 +0000126 styleLoadEventSender().dispatchPendingEvents();
127}
dbates@webkit.org03278c12012-02-24 16:50:55 +0000128
bdakin@apple.com26cc0332014-10-03 20:08:32 +0000129void HTMLStyleElement::dispatchPendingEvent(StyleEventSender* eventSender)
130{
131 ASSERT_UNUSED(eventSender, eventSender == &styleLoadEventSender());
dbates@webkit.org03278c12012-02-24 16:50:55 +0000132 if (m_loadedSheet)
rniwa@webkit.org6c403742018-08-17 21:24:40 +0000133 dispatchEvent(Event::create(eventNames().loadEvent, Event::CanBubble::No, Event::IsCancelable::No));
dbates@webkit.org03278c12012-02-24 16:50:55 +0000134 else
rniwa@webkit.org6c403742018-08-17 21:24:40 +0000135 dispatchEvent(Event::create(eventNames().errorEvent, Event::CanBubble::No, Event::IsCancelable::No));
dbates@webkit.org03278c12012-02-24 16:50:55 +0000136}
137
138void HTMLStyleElement::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred)
139{
140 if (m_firedLoad)
141 return;
142 m_loadedSheet = !errorOccurred;
bdakin@apple.com26cc0332014-10-03 20:08:32 +0000143 styleLoadEventSender().dispatchEventSoon(*this);
dbates@webkit.org03278c12012-02-24 16:50:55 +0000144 m_firedLoad = true;
145}
146
darin@apple.com5ffbb5c2013-09-27 16:39:41 +0000147void HTMLStyleElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
beidson@apple.com96bfd56fe2008-04-03 05:56:21 +0000148{
commit-queue@webkit.orgbd9bc9a2012-05-30 20:50:31 +0000149 HTMLElement::addSubresourceAttributeURLs(urls);
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000150
jiewen_tan@apple.com667a08c2017-11-03 09:32:05 +0000151 if (auto styleSheet = makeRefPtr(this->sheet())) {
antti@apple.com804e8152016-09-29 19:36:39 +0000152 styleSheet->contents().traverseSubresources([&] (auto& resource) {
153 urls.add(resource.url());
154 return false;
155 });
156 }
beidson@apple.com96bfd56fe2008-04-03 05:56:21 +0000157}
158
simon.fraser@apple.com5b0a15f2011-01-09 17:13:06 +0000159bool HTMLStyleElement::disabled() const
160{
antti@apple.com04c7fa32013-08-19 08:56:27 +0000161 if (!sheet())
simon.fraser@apple.com5b0a15f2011-01-09 17:13:06 +0000162 return false;
163
antti@apple.com04c7fa32013-08-19 08:56:27 +0000164 return sheet()->disabled();
simon.fraser@apple.com5b0a15f2011-01-09 17:13:06 +0000165}
166
167void HTMLStyleElement::setDisabled(bool setDisabled)
168{
commit-queue@webkit.orgf4471b32011-10-24 01:49:50 +0000169 if (CSSStyleSheet* styleSheet = sheet())
simon.fraser@apple.com5b0a15f2011-01-09 17:13:06 +0000170 styleSheet->setDisabled(setDisabled);
171}
172
eseidel1bdb6f52006-05-12 20:44:25 +0000173}