2 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "WMLCardElement.h"
26 #include "Attribute.h"
28 #include "FrameLoader.h"
29 #include "HTMLNames.h"
32 #include "RenderStyle.h"
33 #include "WMLDoElement.h"
34 #include "WMLDocument.h"
35 #include "WMLInputElement.h"
36 #include "WMLIntrinsicEventHandler.h"
38 #include "WMLSelectElement.h"
39 #include "WMLTemplateElement.h"
40 #include "WMLTimerElement.h"
41 #include "WMLVariables.h"
45 using namespace WMLNames;
47 WMLCardElement::WMLCardElement(const QualifiedName& tagName, Document* doc)
48 : WMLElement(tagName, doc)
49 , m_isNewContext(false)
55 ASSERT(hasTagName(cardTag));
58 PassRefPtr<WMLCardElement> WMLCardElement::create(const QualifiedName& tagName, Document* document)
60 return adoptRef(new WMLCardElement(tagName, document));
63 WMLCardElement::~WMLCardElement()
67 void WMLCardElement::showCard()
86 void WMLCardElement::hideCard()
105 void WMLCardElement::setTemplateElement(WMLTemplateElement* temp)
107 // Only one template is allowed to be attached to a card
109 reportWMLError(document(), WMLErrorMultipleTemplateElements);
116 void WMLCardElement::setIntrinsicEventTimer(WMLTimerElement* timer)
118 // Only one timer is allowed in a card
120 reportWMLError(document(), WMLErrorMultipleTimerElements);
124 m_eventTimer = timer;
127 void WMLCardElement::handleIntrinsicEventIfNeeded()
129 WMLPageState* pageState = wmlPageStateForDocument(document());
133 Frame* frame = document()->frame();
137 FrameLoader* loader = frame->loader();
141 // Calculate the entry method of current card
142 WMLIntrinsicEventType eventType = WMLIntrinsicEventUnknown;
144 switch (loader->policyChecker()->loadType()) {
145 case FrameLoadTypeReload:
147 case FrameLoadTypeBack:
148 eventType = WMLIntrinsicEventOnEnterBackward;
150 case FrameLoadTypeBackWMLDeckNotAccessible:
151 reportWMLError(document(), WMLErrorDeckNotAccessible);
154 eventType = WMLIntrinsicEventOnEnterForward;
158 // Figure out target event handler
159 WMLIntrinsicEventHandler* eventHandler = this->eventHandler();
160 bool hasIntrinsicEvent = false;
162 if (eventType != WMLIntrinsicEventUnknown) {
163 if (eventHandler && eventHandler->hasIntrinsicEvent(eventType))
164 hasIntrinsicEvent = true;
165 else if (m_template) {
166 eventHandler = m_template->eventHandler();
167 if (eventHandler && eventHandler->hasIntrinsicEvent(eventType))
168 hasIntrinsicEvent = true;
172 if (hasIntrinsicEvent)
173 eventHandler->triggerIntrinsicEvent(eventType);
175 // Start the timer if it exists in current card
177 m_eventTimer->start();
179 for (Node* node = traverseNextNode(); node != 0; node = node->traverseNextNode()) {
180 if (!node->isElementNode())
183 if (node->hasTagName(inputTag))
184 static_cast<WMLInputElement*>(node)->initialize();
185 else if (node->hasTagName(selectTag))
186 static_cast<WMLSelectElement*>(node)->selectInitialOptions();
190 void WMLCardElement::handleDeckLevelTaskOverridesIfNeeded()
192 // Spec: The event-handling element may appear inside a template element and specify
193 // event-processing behaviour for all cards in the deck. A deck-level event-handling
194 // element is equivalent to specifying the event-handling element in each card.
198 Vector<WMLDoElement*>& templateDoElements = m_template->doElements();
199 if (templateDoElements.isEmpty())
202 Vector<WMLDoElement*>& cardDoElements = doElements();
203 Vector<WMLDoElement*>::iterator it = cardDoElements.begin();
204 Vector<WMLDoElement*>::iterator end = cardDoElements.end();
206 HashSet<String> cardDoElementNames;
207 for (; it != end; ++it)
208 cardDoElementNames.add((*it)->name());
210 it = templateDoElements.begin();
211 end = templateDoElements.end();
213 for (; it != end; ++it)
214 (*it)->setActive(!cardDoElementNames.contains((*it)->name()));
217 void WMLCardElement::parseMappedAttribute(Attribute* attr)
219 WMLIntrinsicEventType eventType = WMLIntrinsicEventUnknown;
221 if (attr->name() == onenterforwardAttr)
222 eventType = WMLIntrinsicEventOnEnterForward;
223 else if (attr->name() == onenterbackwardAttr)
224 eventType = WMLIntrinsicEventOnEnterBackward;
225 else if (attr->name() == ontimerAttr)
226 eventType = WMLIntrinsicEventOnTimer;
227 else if (attr->name() == newcontextAttr)
228 m_isNewContext = (attr->value() == "true");
229 else if (attr->name() == orderedAttr)
230 m_isOrdered = (attr->value() == "true");
232 WMLElement::parseMappedAttribute(attr);
236 if (eventType == WMLIntrinsicEventUnknown)
239 // Register intrinsic event in card
240 RefPtr<WMLIntrinsicEvent> event = WMLIntrinsicEvent::create(document(), attr->value());
242 createEventHandlerIfNeeded();
243 eventHandler()->registerIntrinsicEvent(eventType, event);
246 void WMLCardElement::insertedIntoDocument()
248 WMLElement::insertedIntoDocument();
249 Document* document = this->document();
251 // The first card inserted into a document, is visible by default.
253 RefPtr<NodeList> nodeList = document->getElementsByTagName("card");
254 if (nodeList && nodeList->length() == 1 && nodeList->item(0) == this)
258 // For the WML layout tests we embed WML content in a XHTML document. Navigating to different cards
259 // within the same deck has a different behaviour in HTML than in WML. HTML wants to "scroll to anchor"
260 // (see FrameLoader) but WML wants a reload. Notify the root document of the layout test that we want
261 // to mimic WML behaviour. This is rather tricky, but has been tested extensively. Usually it's not possible
262 // at all to embed WML in HTML, it's not designed that way, we're just "abusing" it for dynamically created layout tests.
263 if (document->page() && document->page()->mainFrame()) {
264 Document* rootDocument = document->page()->mainFrame()->document();
265 if (rootDocument && rootDocument != document)
266 rootDocument->setContainsWMLContent(true);
270 RenderObject* WMLCardElement::createRenderer(RenderArena* arena, RenderStyle* style)
274 style->setDisplay(NONE);
277 return WMLElement::createRenderer(arena, style);
280 WMLCardElement* WMLCardElement::findNamedCardInDocument(Document* doc, const String& cardName)
282 if (cardName.isEmpty())
285 RefPtr<NodeList> nodeList = doc->getElementsByTagName("card");
289 unsigned length = nodeList->length();
293 for (unsigned i = 0; i < length; ++i) {
294 WMLCardElement* card = static_cast<WMLCardElement*>(nodeList->item(i));
295 if (card->getIdAttribute() != cardName)
304 WMLCardElement* WMLCardElement::determineActiveCard(Document* doc)
306 WMLPageState* pageState = wmlPageStateForDocument(doc);
310 RefPtr<NodeList> nodeList = doc->getElementsByTagName("card");
314 unsigned length = nodeList->length();
318 // Figure out the new target card
319 String cardName = doc->url().fragmentIdentifier();
321 WMLCardElement* activeCard = findNamedCardInDocument(doc, cardName);
323 // Hide all cards - except the destination card - in document
324 for (unsigned i = 0; i < length; ++i) {
325 WMLCardElement* card = static_cast<WMLCardElement*>(nodeList->item(i));
327 if (card == activeCard)
333 // If the target URL didn't contain a fragment identifier, activeCard
334 // is 0, and has to be set to the first card element in the deck.
335 activeCard = static_cast<WMLCardElement*>(nodeList->item(0));
336 activeCard->showCard();
339 // Assure destination card is visible
340 ASSERT(activeCard->isVisible());
341 ASSERT(activeCard->attached());
342 ASSERT(activeCard->renderer());
344 // Update the document title
345 doc->setTitle(activeCard->title());