2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "InjectedBundleNodeHandle.h"
30 #include "WebFrameLoaderClient.h"
31 #include <JavaScriptCore/APICast.h>
32 #include <WebCore/Document.h>
33 #include <WebCore/Frame.h>
34 #include <WebCore/HTMLFrameElement.h>
35 #include <WebCore/HTMLIFrameElement.h>
36 #include <WebCore/HTMLInputElement.h>
37 #include <WebCore/HTMLNames.h>
38 #include <WebCore/HTMLTableCellElement.h>
39 #include <WebCore/HTMLTextAreaElement.h>
40 #include <WebCore/IntRect.h>
41 #include <WebCore/JSNode.h>
42 #include <WebCore/Node.h>
43 #include <wtf/HashMap.h>
44 #include <wtf/text/WTFString.h>
46 using namespace WebCore;
47 using namespace HTMLNames;
51 typedef HashMap<Node*, InjectedBundleNodeHandle*> DOMHandleCache;
53 static DOMHandleCache& domHandleCache()
55 DEFINE_STATIC_LOCAL(DOMHandleCache, cache, ());
59 PassRefPtr<InjectedBundleNodeHandle> InjectedBundleNodeHandle::getOrCreate(JSContextRef, JSObjectRef object)
61 Node* node = toNode(toJS(object));
62 return getOrCreate(node);
65 PassRefPtr<InjectedBundleNodeHandle> InjectedBundleNodeHandle::getOrCreate(Node* node)
70 std::pair<DOMHandleCache::iterator, bool> result = domHandleCache().add(node, 0);
72 return PassRefPtr<InjectedBundleNodeHandle>(result.first->second);
74 RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::create(node);
75 result.first->second = nodeHandle.get();
76 return nodeHandle.release();
79 PassRefPtr<InjectedBundleNodeHandle> InjectedBundleNodeHandle::create(Node* node)
81 return adoptRef(new InjectedBundleNodeHandle(node));
84 InjectedBundleNodeHandle::InjectedBundleNodeHandle(Node* node)
89 InjectedBundleNodeHandle::~InjectedBundleNodeHandle()
91 domHandleCache().remove(m_node.get());
94 Node* InjectedBundleNodeHandle::coreNode() const
99 PassRefPtr<InjectedBundleNodeHandle> InjectedBundleNodeHandle::document()
101 return getOrCreate(m_node->document());
104 // Additional DOM Operations
105 // Note: These should only be operations that are not exposed to JavaScript.
107 IntRect InjectedBundleNodeHandle::elementBounds() const
109 if (!m_node->isElementNode())
112 return static_cast<Element*>(m_node.get())->boundsInWindowSpace();
115 IntRect InjectedBundleNodeHandle::renderRect(bool* isReplaced) const
117 return m_node.get()->renderRect(isReplaced);
120 void InjectedBundleNodeHandle::setHTMLInputElementValueForUser(const String& value)
122 if (!m_node->hasTagName(inputTag))
125 static_cast<HTMLInputElement*>(m_node.get())->setValueForUser(value);
128 bool InjectedBundleNodeHandle::isHTMLInputElementAutofilled() const
130 if (!m_node->hasTagName(inputTag))
133 return static_cast<HTMLInputElement*>(m_node.get())->isAutofilled();
136 void InjectedBundleNodeHandle::setHTMLInputElementAutofilled(bool filled)
138 if (!m_node->hasTagName(inputTag))
141 static_cast<HTMLInputElement*>(m_node.get())->setAutofilled(filled);
144 bool InjectedBundleNodeHandle::htmlInputElementLastChangeWasUserEdit()
146 if (!m_node->hasTagName(inputTag))
149 return static_cast<HTMLInputElement*>(m_node.get())->lastChangeWasUserEdit();
152 bool InjectedBundleNodeHandle::htmlTextAreaElementLastChangeWasUserEdit()
154 if (!m_node->hasTagName(textareaTag))
157 return static_cast<HTMLTextAreaElement*>(m_node.get())->lastChangeWasUserEdit();
160 PassRefPtr<InjectedBundleNodeHandle> InjectedBundleNodeHandle::htmlTableCellElementCellAbove()
162 if (!m_node->hasTagName(tdTag))
165 return getOrCreate(static_cast<HTMLTableCellElement*>(m_node.get())->cellAbove());
168 PassRefPtr<InjectedBundleNodeHandle> InjectedBundleNodeHandle::elementShadowRoot()
170 if (!m_node->isElementNode())
173 return getOrCreate(static_cast<Element*>(m_node.get())->shadowRoot());
176 PassRefPtr<WebFrame> InjectedBundleNodeHandle::documentFrame()
178 if (!m_node->isDocumentNode())
181 Frame* frame = static_cast<Document*>(m_node.get())->frame();
185 return static_cast<WebFrameLoaderClient*>(frame->loader()->client())->webFrame();
188 PassRefPtr<WebFrame> InjectedBundleNodeHandle::htmlFrameElementContentFrame()
190 if (!m_node->hasTagName(frameTag))
193 Frame* frame = static_cast<HTMLFrameElement*>(m_node.get())->contentFrame();
197 return static_cast<WebFrameLoaderClient*>(frame->loader()->client())->webFrame();
200 PassRefPtr<WebFrame> InjectedBundleNodeHandle::htmlIFrameElementContentFrame()
202 if (!m_node->hasTagName(iframeTag))
205 Frame* frame = static_cast<HTMLIFrameElement*>(m_node.get())->contentFrame();
209 return static_cast<WebFrameLoaderClient*>(frame->loader()->client())->webFrame();
212 } // namespace WebKit