1 | /* |
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) |
5 | * Copyright (C) 2003, 2010, 2013 Apple Inc. All rights reserved. |
6 | * (C) 2007 Rob Buis (buis@kde.org) |
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 |
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | * Boston, MA 02110-1301, USA. |
22 | */ |
23 | |
24 | #include "config.h" |
25 | #include "HTMLStyleElement.h" |
26 | |
27 | #include "CachedResource.h" |
28 | #include "Document.h" |
29 | #include "Event.h" |
30 | #include "EventNames.h" |
31 | #include "EventSender.h" |
32 | #include "HTMLNames.h" |
33 | #include "MediaList.h" |
34 | #include "MediaQueryParser.h" |
35 | #include "RuntimeEnabledFeatures.h" |
36 | #include "ScriptableDocumentParser.h" |
37 | #include "ShadowRoot.h" |
38 | #include "StyleScope.h" |
39 | #include "StyleSheetContents.h" |
40 | #include <wtf/IsoMallocInlines.h> |
41 | #include <wtf/NeverDestroyed.h> |
42 | |
43 | namespace WebCore { |
44 | |
45 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLStyleElement); |
46 | |
47 | using namespace HTMLNames; |
48 | |
49 | static StyleEventSender& styleLoadEventSender() |
50 | { |
51 | static NeverDestroyed<StyleEventSender> sharedLoadEventSender(eventNames().loadEvent); |
52 | return sharedLoadEventSender; |
53 | } |
54 | |
55 | inline HTMLStyleElement::HTMLStyleElement(const QualifiedName& tagName, Document& document, bool createdByParser) |
56 | : HTMLElement(tagName, document) |
57 | , m_styleSheetOwner(document, createdByParser) |
58 | { |
59 | ASSERT(hasTagName(styleTag)); |
60 | } |
61 | |
62 | HTMLStyleElement::~HTMLStyleElement() |
63 | { |
64 | m_styleSheetOwner.clearDocumentData(*this); |
65 | |
66 | styleLoadEventSender().cancelEvent(*this); |
67 | } |
68 | |
69 | Ref<HTMLStyleElement> HTMLStyleElement::create(const QualifiedName& tagName, Document& document, bool createdByParser) |
70 | { |
71 | return adoptRef(*new HTMLStyleElement(tagName, document, createdByParser)); |
72 | } |
73 | |
74 | Ref<HTMLStyleElement> HTMLStyleElement::create(Document& document) |
75 | { |
76 | return adoptRef(*new HTMLStyleElement(styleTag, document, false)); |
77 | } |
78 | |
79 | void HTMLStyleElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
80 | { |
81 | if (name == titleAttr && sheet() && !isInShadowTree()) |
82 | sheet()->setTitle(value); |
83 | else if (name == mediaAttr) { |
84 | m_styleSheetOwner.setMedia(value); |
85 | if (sheet()) { |
86 | sheet()->setMediaQueries(MediaQuerySet::create(value, MediaQueryParserContext(document()))); |
87 | if (auto* scope = m_styleSheetOwner.styleScope()) |
88 | scope->didChangeStyleSheetContents(); |
89 | } else |
90 | m_styleSheetOwner.childrenChanged(*this); |
91 | } else if (name == typeAttr) |
92 | m_styleSheetOwner.setContentType(value); |
93 | else |
94 | HTMLElement::parseAttribute(name, value); |
95 | } |
96 | |
97 | void HTMLStyleElement::finishParsingChildren() |
98 | { |
99 | m_styleSheetOwner.finishParsingChildren(*this); |
100 | HTMLElement::finishParsingChildren(); |
101 | } |
102 | |
103 | Node::InsertedIntoAncestorResult HTMLStyleElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) |
104 | { |
105 | auto result = HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree); |
106 | if (insertionType.connectedToDocument) |
107 | m_styleSheetOwner.insertedIntoDocument(*this); |
108 | return result; |
109 | } |
110 | |
111 | void HTMLStyleElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree) |
112 | { |
113 | HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree); |
114 | if (removalType.disconnectedFromDocument) |
115 | m_styleSheetOwner.removedFromDocument(*this); |
116 | } |
117 | |
118 | void HTMLStyleElement::childrenChanged(const ChildChange& change) |
119 | { |
120 | HTMLElement::childrenChanged(change); |
121 | m_styleSheetOwner.childrenChanged(*this); |
122 | } |
123 | |
124 | void HTMLStyleElement::dispatchPendingLoadEvents() |
125 | { |
126 | styleLoadEventSender().dispatchPendingEvents(); |
127 | } |
128 | |
129 | void HTMLStyleElement::dispatchPendingEvent(StyleEventSender* eventSender) |
130 | { |
131 | ASSERT_UNUSED(eventSender, eventSender == &styleLoadEventSender()); |
132 | if (m_loadedSheet) |
133 | dispatchEvent(Event::create(eventNames().loadEvent, Event::CanBubble::No, Event::IsCancelable::No)); |
134 | else |
135 | dispatchEvent(Event::create(eventNames().errorEvent, Event::CanBubble::No, Event::IsCancelable::No)); |
136 | } |
137 | |
138 | void HTMLStyleElement::notifyLoadedSheetAndAllCriticalSubresources(bool errorOccurred) |
139 | { |
140 | if (m_firedLoad) |
141 | return; |
142 | m_loadedSheet = !errorOccurred; |
143 | styleLoadEventSender().dispatchEventSoon(*this); |
144 | m_firedLoad = true; |
145 | } |
146 | |
147 | void HTMLStyleElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const |
148 | { |
149 | HTMLElement::addSubresourceAttributeURLs(urls); |
150 | |
151 | if (auto styleSheet = makeRefPtr(this->sheet())) { |
152 | styleSheet->contents().traverseSubresources([&] (auto& resource) { |
153 | urls.add(resource.url()); |
154 | return false; |
155 | }); |
156 | } |
157 | } |
158 | |
159 | bool HTMLStyleElement::disabled() const |
160 | { |
161 | if (!sheet()) |
162 | return false; |
163 | |
164 | return sheet()->disabled(); |
165 | } |
166 | |
167 | void HTMLStyleElement::setDisabled(bool setDisabled) |
168 | { |
169 | if (CSSStyleSheet* styleSheet = sheet()) |
170 | styleSheet->setDisabled(setDisabled); |
171 | } |
172 | |
173 | } |
174 | |