2 * Copyright (C) 2000 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2006, 2008, 2009 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "core/dom/ProcessingInstruction.h"
24 #include "core/FetchInitiatorTypeNames.h"
25 #include "core/css/CSSStyleSheet.h"
26 #include "core/css/MediaList.h"
27 #include "core/css/StyleSheetContents.h"
28 #include "core/dom/Document.h"
29 #include "core/dom/StyleEngine.h"
30 #include "core/fetch/CSSStyleSheetResource.h"
31 #include "core/fetch/FetchRequest.h"
32 #include "core/fetch/ResourceFetcher.h"
33 #include "core/fetch/XSLStyleSheetResource.h"
34 #include "core/xml/XSLStyleSheet.h"
35 #include "core/xml/parser/XMLDocumentParser.h" // for parseAttributes()
39 inline ProcessingInstruction::ProcessingInstruction(Document& document, const String& target, const String& data)
40 : CharacterData(document, data, CreateOther)
44 , m_createdByParser(false)
48 ScriptWrappable::init(this);
51 PassRefPtrWillBeRawPtr<ProcessingInstruction> ProcessingInstruction::create(Document& document, const String& target, const String& data)
53 return adoptRefWillBeNoop(new ProcessingInstruction(document, target, data));
56 ProcessingInstruction::~ProcessingInstruction()
60 m_sheet->clearOwnerNode();
62 // FIXME: ProcessingInstruction should not be in document here.
63 // However, if we add ASSERT(!inDocument()), fast/xsl/xslt-entity.xml
64 // crashes. We need to investigate ProcessingInstruction lifetime.
67 document().styleEngine()->removeStyleSheetCandidateNode(this);
69 document().styleEngine()->removeXSLStyleSheet(this);
74 String ProcessingInstruction::nodeName() const
79 Node::NodeType ProcessingInstruction::nodeType() const
81 return PROCESSING_INSTRUCTION_NODE;
84 PassRefPtrWillBeRawPtr<Node> ProcessingInstruction::cloneNode(bool /*deep*/)
86 // FIXME: Is it a problem that this does not copy m_localHref?
87 // What about other data members?
88 return create(document(), m_target, m_data);
91 void ProcessingInstruction::didAttributeChanged()
97 if (!checkStyleSheet(href, charset))
99 process(href, charset);
102 bool ProcessingInstruction::checkStyleSheet(String& href, String& charset)
104 if (m_target != "xml-stylesheet" || !document().frame() || parentNode() != document())
107 // see http://www.w3.org/TR/xml-stylesheet/
108 // ### support stylesheet included in a fragment of this (or another) document
109 // ### make sure this gets called when adding from javascript
111 const HashMap<String, String> attrs = parseAttributes(m_data, attrsOk);
114 HashMap<String, String>::const_iterator i = attrs.find("type");
116 if (i != attrs.end())
119 m_isCSS = type.isEmpty() || type == "text/css";
120 m_isXSL = (type == "text/xml" || type == "text/xsl" || type == "application/xml" || type == "application/xhtml+xml" || type == "application/rss+xml" || type == "application/atom+xml");
121 if (!m_isCSS && !m_isXSL)
124 href = attrs.get("href");
125 charset = attrs.get("charset");
126 String alternate = attrs.get("alternate");
127 m_alternate = alternate == "yes";
128 m_title = attrs.get("title");
129 m_media = attrs.get("media");
131 return !m_alternate || !m_title.isEmpty();
134 void ProcessingInstruction::process(const String& href, const String& charset)
136 if (href.length() > 1 && href[0] == '#') {
137 m_localHref = href.substring(1);
138 // We need to make a synthetic XSLStyleSheet that is embedded.
139 // It needs to be able to kick off import/include loads that
140 // can hang off some parent sheet.
142 KURL finalURL(ParsedURLString, m_localHref);
143 m_sheet = XSLStyleSheet::createEmbedded(this, finalURL);
151 String url = document().completeURL(href).string();
153 ResourcePtr<StyleSheetResource> resource;
154 FetchRequest request(ResourceRequest(document().completeURL(href)), FetchInitiatorTypeNames::processinginstruction);
156 resource = document().fetcher()->fetchXSLStyleSheet(request);
158 request.setCharset(charset.isEmpty() ? document().charset() : charset);
159 resource = document().fetcher()->fetchCSSStyleSheet(request);
164 document().styleEngine()->addPendingSheet();
165 setResource(resource);
169 bool ProcessingInstruction::isLoading() const
175 return m_sheet->isLoading();
178 bool ProcessingInstruction::sheetLoaded()
181 document().styleEngine()->removePendingSheet(this);
187 void ProcessingInstruction::setCSSStyleSheet(const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource* sheet)
195 CSSParserContext parserContext(document(), 0, baseURL, charset);
197 RefPtrWillBeRawPtr<StyleSheetContents> newSheet = StyleSheetContents::create(href, parserContext);
199 RefPtrWillBeRawPtr<CSSStyleSheet> cssSheet = CSSStyleSheet::create(newSheet, this);
200 cssSheet->setDisabled(m_alternate);
201 cssSheet->setTitle(m_title);
202 cssSheet->setMediaQueries(MediaQuerySet::create(m_media));
204 m_sheet = cssSheet.release();
206 // We don't need the cross-origin security check here because we are
207 // getting the sheet text in "strict" mode. This enforces a valid CSS MIME
209 parseStyleSheet(sheet->sheetText(true));
212 void ProcessingInstruction::setXSLStyleSheet(const String& href, const KURL& baseURL, const String& sheet)
215 m_sheet = XSLStyleSheet::create(this, href, baseURL);
216 parseStyleSheet(sheet);
219 void ProcessingInstruction::parseStyleSheet(const String& sheet)
222 toCSSStyleSheet(m_sheet.get())->contents()->parseString(sheet);
224 toXSLStyleSheet(m_sheet.get())->parseString(sheet);
230 toCSSStyleSheet(m_sheet.get())->contents()->checkLoaded();
232 toXSLStyleSheet(m_sheet.get())->checkLoaded();
235 void ProcessingInstruction::setCSSStyleSheet(PassRefPtrWillBeRawPtr<CSSStyleSheet> sheet)
240 sheet->setTitle(m_title);
241 sheet->setDisabled(m_alternate);
244 Node::InsertionNotificationRequest ProcessingInstruction::insertedInto(ContainerNode* insertionPoint)
246 CharacterData::insertedInto(insertionPoint);
247 if (!insertionPoint->inDocument())
248 return InsertionDone;
252 bool isValid = checkStyleSheet(href, charset);
254 document().styleEngine()->addStyleSheetCandidateNode(this, m_createdByParser);
256 document().styleEngine()->addXSLStyleSheet(this, m_createdByParser);
258 process(href, charset);
259 return InsertionDone;
262 void ProcessingInstruction::removedFrom(ContainerNode* insertionPoint)
264 CharacterData::removedFrom(insertionPoint);
265 if (!insertionPoint->inDocument())
269 document().styleEngine()->removeStyleSheetCandidateNode(this);
271 document().styleEngine()->removeXSLStyleSheet(this);
273 RefPtrWillBeRawPtr<StyleSheet> removedSheet = m_sheet;
276 ASSERT(m_sheet->ownerNode() == this);
277 m_sheet->clearOwnerNode();
281 // If we're in document teardown, then we don't need to do any notification of our sheet's removal.
282 if (document().isActive())
283 document().removedStyleSheet(removedSheet.get());
286 void ProcessingInstruction::trace(Visitor* visitor)
288 visitor->trace(m_sheet);
289 CharacterData::trace(visitor);