1 /*
2 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21 #include "config.h"
22
23 #if ENABLE(WML)
24 #include "WMLTemplateElement.h"
25
26 #include "MappedAttribute.h"
27 #include "NodeList.h"
28 #include "WMLCardElement.h"
29 #include "WMLDocument.h"
30 #include "WMLIntrinsicEventHandler.h"
31 #include "WMLNames.h"
32
33 namespace WebCore {
34
35 using namespace WMLNames;
36
WMLTemplateElement(const QualifiedName & tagName,Document * doc)37 WMLTemplateElement::WMLTemplateElement(const QualifiedName& tagName, Document* doc)
38 : WMLElement(tagName, doc)
39 {
40 }
41
~WMLTemplateElement()42 WMLTemplateElement::~WMLTemplateElement()
43 {
44 }
45
parseMappedAttribute(MappedAttribute * attr)46 void WMLTemplateElement::parseMappedAttribute(MappedAttribute* attr)
47 {
48 WMLIntrinsicEventType eventType = WMLIntrinsicEventUnknown;
49
50 if (attr->name() == onenterforwardAttr)
51 eventType = WMLIntrinsicEventOnEnterForward;
52 else if (attr->name() == onenterbackwardAttr)
53 eventType = WMLIntrinsicEventOnEnterBackward;
54 else if (attr->name() == ontimerAttr)
55 eventType = WMLIntrinsicEventOnTimer;
56 else {
57 WMLElement::parseMappedAttribute(attr);
58 return;
59 }
60
61 if (eventType == WMLIntrinsicEventUnknown)
62 return;
63
64 // Register intrinsic event in card
65 RefPtr<WMLIntrinsicEvent> event = WMLIntrinsicEvent::create(document(), attr->value());
66
67 createEventHandlerIfNeeded();
68 eventHandler()->registerIntrinsicEvent(eventType, event);
69 }
70
registerTemplatesInDocument(Document * doc)71 void WMLTemplateElement::registerTemplatesInDocument(Document* doc)
72 {
73 ASSERT(doc);
74
75 // Build list of cards in document
76 RefPtr<NodeList> nodeList = doc->getElementsByTagName("card");
77 if (!nodeList)
78 return;
79
80 unsigned length = nodeList->length();
81 if (length < 1)
82 return;
83
84 HashSet<WMLCardElement*> cards;
85 for (unsigned i = 0; i < length; ++i)
86 cards.add(static_cast<WMLCardElement*>(nodeList->item(i)));
87
88 if (cards.isEmpty())
89 return;
90
91 // Register template element to all cards
92 nodeList = doc->getElementsByTagName("template");
93 if (!nodeList)
94 return;
95
96 length = nodeList->length();
97 if (length < 1)
98 return;
99
100 // Only one template element should be allowed in a document
101 // Calling setTemplateElement() twice on a WMLCardElement, will result in a tokenizer error.
102 for (unsigned i = 0; i < length; ++i) {
103 WMLTemplateElement* temp = static_cast<WMLTemplateElement*>(nodeList->item(i));
104
105 HashSet<WMLCardElement*>::iterator it = cards.begin();
106 HashSet<WMLCardElement*>::iterator end = cards.end();
107
108 for (; it != end; ++it)
109 (*it)->setTemplateElement(temp);
110 }
111 }
112
113 }
114
115 #endif
116