2 * Copyright (C) 2012 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 "WebRenderObject.h"
30 #include "APIString.h"
32 #include <WebCore/Frame.h>
33 #include <WebCore/FrameLoader.h>
34 #include <WebCore/FrameLoaderClient.h>
35 #include <WebCore/MainFrame.h>
36 #include <WebCore/RenderInline.h>
37 #include <WebCore/RenderText.h>
38 #include <WebCore/RenderView.h>
39 #include <WebCore/RenderWidget.h>
41 using namespace WebCore;
45 RefPtr<WebRenderObject> WebRenderObject::create(WebPage* page)
47 Frame* mainFrame = page->mainFrame();
51 if (!mainFrame->loader().client().hasHTMLView())
54 RenderView* contentRenderer = mainFrame->contentRenderer();
58 return adoptRef(new WebRenderObject(contentRenderer, true));
61 PassRefPtr<WebRenderObject> WebRenderObject::create(const String& name, const String& elementTagName, const String& elementID, PassRefPtr<API::Array> elementClassNames, WebCore::IntPoint absolutePosition, WebCore::IntRect frameRect, const String& textSnippet, unsigned textLength, PassRefPtr<API::Array> children)
63 return adoptRef(new WebRenderObject(name, elementTagName, elementID, elementClassNames, absolutePosition, frameRect, textSnippet, textLength, children));
66 WebRenderObject::WebRenderObject(RenderObject* renderer, bool shouldIncludeDescendants)
68 m_name = renderer->renderName();
71 if (Node* node = renderer->node()) {
72 if (is<Element>(*node)) {
73 Element& element = downcast<Element>(*node);
74 m_elementTagName = element.tagName();
75 m_elementID = element.getIdAttribute();
77 if (element.isStyledElement() && element.hasClass()) {
78 Vector<RefPtr<API::Object>> classNames;
79 classNames.reserveInitialCapacity(element.classNames().size());
81 for (size_t i = 0, size = element.classNames().size(); i < size; ++i)
82 classNames.append(API::String::create(element.classNames()[i]));
84 m_elementClassNames = API::Array::create(WTF::move(classNames));
88 if (node->isTextNode()) {
89 String value = node->nodeValue();
90 m_textLength = value.length();
92 const int maxSnippetLength = 40;
93 if (value.length() > maxSnippetLength)
94 m_textSnippet = value.substring(0, maxSnippetLength);
96 m_textSnippet = value;
100 // FIXME: broken with transforms
101 m_absolutePosition = flooredIntPoint(renderer->localToAbsolute());
103 if (is<RenderBox>(*renderer))
104 m_frameRect = snappedIntRect(downcast<RenderBox>(*renderer).frameRect());
105 else if (is<RenderText>(*renderer)) {
106 m_frameRect = downcast<RenderText>(*renderer).linesBoundingBox();
107 m_frameRect.setLocation(downcast<RenderText>(*renderer).firstRunLocation());
108 } else if (is<RenderInline>(*renderer))
109 m_frameRect = downcast<RenderInline>(*renderer).borderBoundingBox();
111 if (!shouldIncludeDescendants)
114 Vector<RefPtr<API::Object>> children;
116 for (RenderObject* coreChild = renderer->firstChildSlow(); coreChild; coreChild = coreChild->nextSibling()) {
117 RefPtr<WebRenderObject> child = adoptRef(new WebRenderObject(coreChild, shouldIncludeDescendants));
118 children.append(WTF::move(child));
121 if (is<RenderWidget>(*renderer)) {
122 if (Widget* widget = downcast<RenderWidget>(*renderer).widget()) {
123 if (is<FrameView>(*widget)) {
124 FrameView& frameView = downcast<FrameView>(*widget);
125 if (RenderView* coreContentRenderer = frameView.frame().contentRenderer()) {
126 RefPtr<WebRenderObject> contentRenderer = adoptRef(new WebRenderObject(coreContentRenderer, shouldIncludeDescendants));
128 children.append(WTF::move(contentRenderer));
134 m_children = API::Array::create(WTF::move(children));
137 WebRenderObject::WebRenderObject(const String& name, const String& elementTagName, const String& elementID, PassRefPtr<API::Array> elementClassNames, WebCore::IntPoint absolutePosition, WebCore::IntRect frameRect, const String& textSnippet, unsigned textLength, PassRefPtr<API::Array> children)
138 : m_children(children)
140 , m_elementTagName(elementTagName)
141 , m_elementID(elementID)
142 , m_textSnippet(textSnippet)
143 , m_elementClassNames(elementClassNames)
144 , m_absolutePosition(absolutePosition)
145 , m_frameRect(frameRect)
146 , m_textLength(textLength)
150 WebRenderObject::~WebRenderObject()
154 } // namespace WebKit