darin@apple.com | 877ce5b | 2010-05-28 15:38:58 +0000 | [diff] [blame] | 1 | /* |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 2 | * 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.com | 73ce3fe | 2013-08-19 10:36:11 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003, 2010, 2013 Apple Inc. All rights reserved. |
bdash | e0f7f14 | 2007-07-16 08:38:41 +0000 | [diff] [blame] | 6 | * (C) 2007 Rob Buis (buis@kde.org) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 7 | * |
| 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | * Boston, MA 02110-1301, USA. |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 22 | */ |
darin@apple.com | 9a925fa | 2009-05-04 18:00:34 +0000 | [diff] [blame] | 23 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 24 | #include "config.h" |
| 25 | #include "HTMLStyleElement.h" |
| 26 | |
antti@apple.com | 804e815 | 2016-09-29 19:36:39 +0000 | [diff] [blame] | 27 | #include "CachedResource.h" |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 28 | #include "Document.h" |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 29 | #include "Event.h" |
cdumez@apple.com | 06ee4a5 | 2016-06-15 20:59:07 +0000 | [diff] [blame] | 30 | #include "EventNames.h" |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 31 | #include "EventSender.h" |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 32 | #include "HTMLNames.h" |
commit-queue@webkit.org | 209c8cc | 2012-10-09 21:53:22 +0000 | [diff] [blame] | 33 | #include "MediaList.h" |
megan_gardner@apple.com | 0616063 | 2018-03-16 01:12:39 +0000 | [diff] [blame] | 34 | #include "MediaQueryParser.h" |
akling@apple.com | b621d00 | 2013-09-17 09:27:21 +0000 | [diff] [blame] | 35 | #include "RuntimeEnabledFeatures.h" |
simon.fraser@apple.com | 5b0a15f | 2011-01-09 17:13:06 +0000 | [diff] [blame] | 36 | #include "ScriptableDocumentParser.h" |
commit-queue@webkit.org | b17e289 | 2012-06-12 12:28:29 +0000 | [diff] [blame] | 37 | #include "ShadowRoot.h" |
antti@apple.com | 215ce40 | 2016-10-07 16:55:11 +0000 | [diff] [blame] | 38 | #include "StyleScope.h" |
antti@apple.com | 069713a | 2012-05-24 08:54:22 +0000 | [diff] [blame] | 39 | #include "StyleSheetContents.h" |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 40 | #include <wtf/IsoMallocInlines.h> |
darin@apple.com | 9ab51c78 | 2017-07-18 01:53:34 +0000 | [diff] [blame] | 41 | #include <wtf/NeverDestroyed.h> |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 42 | |
| 43 | namespace WebCore { |
| 44 | |
fpizlo@apple.com | 197cd32 | 2018-03-17 06:11:00 +0000 | [diff] [blame] | 45 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLStyleElement); |
| 46 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 47 | using namespace HTMLNames; |
| 48 | |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 49 | static StyleEventSender& styleLoadEventSender() |
| 50 | { |
akling@apple.com | ee1218c | 2016-01-09 13:13:41 +0000 | [diff] [blame] | 51 | static NeverDestroyed<StyleEventSender> sharedLoadEventSender(eventNames().loadEvent); |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 52 | return sharedLoadEventSender; |
| 53 | } |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 54 | |
weinig@apple.com | 4917883 | 2013-09-15 00:39:29 +0000 | [diff] [blame] | 55 | inline HTMLStyleElement::HTMLStyleElement(const QualifiedName& tagName, Document& document, bool createdByParser) |
weinig@apple.com | dedf67e | 2013-09-15 05:23:01 +0000 | [diff] [blame] | 56 | : HTMLElement(tagName, document) |
antti@apple.com | 73ce3fe | 2013-08-19 10:36:11 +0000 | [diff] [blame] | 57 | , m_styleSheetOwner(document, createdByParser) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 58 | { |
jchaffraix@webkit.org | 74dae40 | 2008-12-01 23:07:04 +0000 | [diff] [blame] | 59 | ASSERT(hasTagName(styleTag)); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
ap@apple.com | 1fe9089 | 2010-11-10 21:12:39 +0000 | [diff] [blame] | 62 | HTMLStyleElement::~HTMLStyleElement() |
| 63 | { |
antti@apple.com | dff1072 | 2016-10-06 20:53:08 +0000 | [diff] [blame] | 64 | m_styleSheetOwner.clearDocumentData(*this); |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 65 | |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 66 | styleLoadEventSender().cancelEvent(*this); |
ap@apple.com | 1fe9089 | 2010-11-10 21:12:39 +0000 | [diff] [blame] | 67 | } |
| 68 | |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 69 | Ref<HTMLStyleElement> HTMLStyleElement::create(const QualifiedName& tagName, Document& document, bool createdByParser) |
darin@apple.com | 877ce5b | 2010-05-28 15:38:58 +0000 | [diff] [blame] | 70 | { |
weinig@apple.com | 02f433a | 2015-01-06 22:32:48 +0000 | [diff] [blame] | 71 | return adoptRef(*new HTMLStyleElement(tagName, document, createdByParser)); |
darin@apple.com | 877ce5b | 2010-05-28 15:38:58 +0000 | [diff] [blame] | 72 | } |
| 73 | |
darin@apple.com | efecd25 | 2016-11-12 22:30:22 +0000 | [diff] [blame] | 74 | Ref<HTMLStyleElement> HTMLStyleElement::create(Document& document) |
| 75 | { |
| 76 | return adoptRef(*new HTMLStyleElement(styleTag, document, false)); |
| 77 | } |
| 78 | |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 79 | void HTMLStyleElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 80 | { |
antti@apple.com | 04c7fa3 | 2013-08-19 08:56:27 +0000 | [diff] [blame] | 81 | if (name == titleAttr && sheet()) |
| 82 | sheet()->setTitle(value); |
antti@apple.com | 7d5d853 | 2013-08-19 17:42:47 +0000 | [diff] [blame] | 83 | else if (name == mediaAttr) { |
antti@apple.com | 73ce3fe | 2013-08-19 10:36:11 +0000 | [diff] [blame] | 84 | m_styleSheetOwner.setMedia(value); |
antti@apple.com | 7d5d853 | 2013-08-19 17:42:47 +0000 | [diff] [blame] | 85 | if (sheet()) { |
megan_gardner@apple.com | 0616063 | 2018-03-16 01:12:39 +0000 | [diff] [blame] | 86 | sheet()->setMediaQueries(MediaQuerySet::create(value, MediaQueryParserContext(document()))); |
antti@apple.com | 215ce40 | 2016-10-07 16:55:11 +0000 | [diff] [blame] | 87 | if (auto* scope = m_styleSheetOwner.styleScope()) |
antti@apple.com | 4f9ac5b | 2016-10-21 13:36:45 +0000 | [diff] [blame] | 88 | scope->didChangeStyleSheetContents(); |
antti@apple.com | dff1072 | 2016-10-06 20:53:08 +0000 | [diff] [blame] | 89 | } else |
| 90 | m_styleSheetOwner.childrenChanged(*this); |
antti@apple.com | 04c7fa3 | 2013-08-19 08:56:27 +0000 | [diff] [blame] | 91 | } else if (name == typeAttr) |
antti@apple.com | 73ce3fe | 2013-08-19 10:36:11 +0000 | [diff] [blame] | 92 | m_styleSheetOwner.setContentType(value); |
antti@apple.com | 04c7fa3 | 2013-08-19 08:56:27 +0000 | [diff] [blame] | 93 | else |
akling@apple.com | 43e9d04 | 2012-11-18 16:55:06 +0000 | [diff] [blame] | 94 | HTMLElement::parseAttribute(name, value); |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 95 | } |
| 96 | |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 97 | void HTMLStyleElement::finishParsingChildren() |
| 98 | { |
weinig@apple.com | 6503182 | 2013-09-25 18:09:47 +0000 | [diff] [blame] | 99 | m_styleSheetOwner.finishParsingChildren(*this); |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 100 | HTMLElement::finishParsingChildren(); |
| 101 | } |
| 102 | |
rniwa@webkit.org | ddc3f39 | 2017-10-21 00:49:21 +0000 | [diff] [blame] | 103 | Node::InsertedIntoAncestorResult HTMLStyleElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 104 | { |
rniwa@webkit.org | ddc3f39 | 2017-10-21 00:49:21 +0000 | [diff] [blame] | 105 | auto result = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree); |
rniwa@webkit.org | a8490de | 2017-10-18 21:34:25 +0000 | [diff] [blame] | 106 | if (insertionType.connectedToDocument) |
antti@apple.com | dff1072 | 2016-10-06 20:53:08 +0000 | [diff] [blame] | 107 | m_styleSheetOwner.insertedIntoDocument(*this); |
rniwa@webkit.org | 31b42da | 2016-11-04 22:53:33 +0000 | [diff] [blame] | 108 | return result; |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 109 | } |
| 110 | |
rniwa@webkit.org | ddc3f39 | 2017-10-21 00:49:21 +0000 | [diff] [blame] | 111 | void HTMLStyleElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree) |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 112 | { |
rniwa@webkit.org | ddc3f39 | 2017-10-21 00:49:21 +0000 | [diff] [blame] | 113 | HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree); |
rniwa@webkit.org | f2fe9c4 | 2017-10-19 10:05:21 +0000 | [diff] [blame] | 114 | if (removalType.disconnectedFromDocument) |
antti@apple.com | dff1072 | 2016-10-06 20:53:08 +0000 | [diff] [blame] | 115 | m_styleSheetOwner.removedFromDocument(*this); |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void HTMLStyleElement::childrenChanged(const ChildChange& change) |
| 119 | { |
| 120 | HTMLElement::childrenChanged(change); |
weinig@apple.com | 6503182 | 2013-09-25 18:09:47 +0000 | [diff] [blame] | 121 | m_styleSheetOwner.childrenChanged(*this); |
akling@apple.com | b7b8be2 | 2013-09-17 08:13:32 +0000 | [diff] [blame] | 122 | } |
rolandsteiner@chromium.org | ab7f975 | 2011-12-05 11:48:28 +0000 | [diff] [blame] | 123 | |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 124 | void HTMLStyleElement::dispatchPendingLoadEvents() |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 125 | { |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 126 | styleLoadEventSender().dispatchPendingEvents(); |
| 127 | } |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 128 | |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 129 | void HTMLStyleElement::dispatchPendingEvent(StyleEventSender* eventSender) |
| 130 | { |
| 131 | ASSERT_UNUSED(eventSender, eventSender == &styleLoadEventSender()); |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 132 | if (m_loadedSheet) |
rniwa@webkit.org | 6c40374 | 2018-08-17 21:24:40 +0000 | [diff] [blame] | 133 | dispatchEvent(Event::create(eventNames().loadEvent, Event::CanBubble::No, Event::IsCancelable::No)); |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 134 | else |
rniwa@webkit.org | 6c40374 | 2018-08-17 21:24:40 +0000 | [diff] [blame] | 135 | dispatchEvent(Event::create(eventNames().errorEvent, Event::CanBubble::No, Event::IsCancelable::No)); |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void HTMLStyleElement::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred) |
| 139 | { |
| 140 | if (m_firedLoad) |
| 141 | return; |
| 142 | m_loadedSheet = !errorOccurred; |
bdakin@apple.com | 26cc033 | 2014-10-03 20:08:32 +0000 | [diff] [blame] | 143 | styleLoadEventSender().dispatchEventSoon(*this); |
dbates@webkit.org | 03278c1 | 2012-02-24 16:50:55 +0000 | [diff] [blame] | 144 | m_firedLoad = true; |
| 145 | } |
| 146 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 147 | void HTMLStyleElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const |
beidson@apple.com | 96bfd56fe | 2008-04-03 05:56:21 +0000 | [diff] [blame] | 148 | { |
commit-queue@webkit.org | bd9bc9a | 2012-05-30 20:50:31 +0000 | [diff] [blame] | 149 | HTMLElement::addSubresourceAttributeURLs(urls); |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 150 | |
jiewen_tan@apple.com | 667a08c | 2017-11-03 09:32:05 +0000 | [diff] [blame] | 151 | if (auto styleSheet = makeRefPtr(this->sheet())) { |
antti@apple.com | 804e815 | 2016-09-29 19:36:39 +0000 | [diff] [blame] | 152 | styleSheet->contents().traverseSubresources([&] (auto& resource) { |
| 153 | urls.add(resource.url()); |
| 154 | return false; |
| 155 | }); |
| 156 | } |
beidson@apple.com | 96bfd56fe | 2008-04-03 05:56:21 +0000 | [diff] [blame] | 157 | } |
| 158 | |
simon.fraser@apple.com | 5b0a15f | 2011-01-09 17:13:06 +0000 | [diff] [blame] | 159 | bool HTMLStyleElement::disabled() const |
| 160 | { |
antti@apple.com | 04c7fa3 | 2013-08-19 08:56:27 +0000 | [diff] [blame] | 161 | if (!sheet()) |
simon.fraser@apple.com | 5b0a15f | 2011-01-09 17:13:06 +0000 | [diff] [blame] | 162 | return false; |
| 163 | |
antti@apple.com | 04c7fa3 | 2013-08-19 08:56:27 +0000 | [diff] [blame] | 164 | return sheet()->disabled(); |
simon.fraser@apple.com | 5b0a15f | 2011-01-09 17:13:06 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void HTMLStyleElement::setDisabled(bool setDisabled) |
| 168 | { |
commit-queue@webkit.org | f4471b3 | 2011-10-24 01:49:50 +0000 | [diff] [blame] | 169 | if (CSSStyleSheet* styleSheet = sheet()) |
simon.fraser@apple.com | 5b0a15f | 2011-01-09 17:13:06 +0000 | [diff] [blame] | 170 | styleSheet->setDisabled(setDisabled); |
| 171 | } |
| 172 | |
eseidel | 1bdb6f5 | 2006-05-12 20:44:25 +0000 | [diff] [blame] | 173 | } |