1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "WebRenderObject.h"
28
29#include "APIArray.h"
30#include "APIString.h"
31#include "WebPage.h"
32#include <WebCore/Frame.h>
33#include <WebCore/FrameLoader.h>
34#include <WebCore/FrameLoaderClient.h>
35#include <WebCore/RenderInline.h>
36#include <WebCore/RenderText.h>
37#include <WebCore/RenderView.h>
38#include <WebCore/RenderWidget.h>
39
40namespace WebKit {
41using namespace WebCore;
42
43RefPtr<WebRenderObject> WebRenderObject::create(WebPage* page)
44{
45 Frame* mainFrame = page->mainFrame();
46 if (!mainFrame)
47 return nullptr;
48
49 if (!mainFrame->loader().client().hasHTMLView())
50 return nullptr;
51
52 RenderView* contentRenderer = mainFrame->contentRenderer();
53 if (!contentRenderer)
54 return nullptr;
55
56 return adoptRef(new WebRenderObject(contentRenderer, true));
57}
58
59Ref<WebRenderObject> WebRenderObject::create(const String& name, const String& elementTagName, const String& elementID, RefPtr<API::Array>&& elementClassNames, WebCore::IntPoint absolutePosition, WebCore::IntRect frameRect, const String& textSnippet, unsigned textLength, RefPtr<API::Array>&& children)
60{
61 return adoptRef(*new WebRenderObject(name, elementTagName, elementID, WTFMove(elementClassNames), absolutePosition, frameRect, textSnippet, textLength, WTFMove(children)));
62}
63
64WebRenderObject::WebRenderObject(RenderObject* renderer, bool shouldIncludeDescendants)
65{
66 m_name = renderer->renderName();
67 m_textLength = 0;
68
69 if (Node* node = renderer->node()) {
70 if (is<Element>(*node)) {
71 Element& element = downcast<Element>(*node);
72 m_elementTagName = element.tagName();
73 m_elementID = element.getIdAttribute();
74
75 if (element.isStyledElement() && element.hasClass()) {
76 Vector<RefPtr<API::Object>> classNames;
77 classNames.reserveInitialCapacity(element.classNames().size());
78
79 for (size_t i = 0, size = element.classNames().size(); i < size; ++i)
80 classNames.append(API::String::create(element.classNames()[i]));
81
82 m_elementClassNames = API::Array::create(WTFMove(classNames));
83 }
84 }
85
86 if (node->isTextNode()) {
87 String value = node->nodeValue();
88 m_textLength = value.length();
89
90 const int maxSnippetLength = 40;
91 if (value.length() > maxSnippetLength)
92 m_textSnippet = value.substring(0, maxSnippetLength);
93 else
94 m_textSnippet = value;
95 }
96 }
97
98 // FIXME: broken with transforms
99 m_absolutePosition = flooredIntPoint(renderer->localToAbsolute());
100
101 if (is<RenderBox>(*renderer))
102 m_frameRect = snappedIntRect(downcast<RenderBox>(*renderer).frameRect());
103 else if (is<RenderText>(*renderer)) {
104 m_frameRect = downcast<RenderText>(*renderer).linesBoundingBox();
105 m_frameRect.setLocation(downcast<RenderText>(*renderer).firstRunLocation());
106 } else if (is<RenderInline>(*renderer))
107 m_frameRect = IntRect(downcast<RenderInline>(*renderer).borderBoundingBox());
108
109 if (!shouldIncludeDescendants)
110 return;
111
112 Vector<RefPtr<API::Object>> children;
113
114 for (auto* coreChild = renderer->firstChildSlow(); coreChild; coreChild = coreChild->nextSibling())
115 children.append(adoptRef(*new WebRenderObject(coreChild, shouldIncludeDescendants)));
116
117 if (is<RenderWidget>(*renderer)) {
118 auto* widget = downcast<RenderWidget>(*renderer).widget();
119 if (is<FrameView>(widget)) {
120 if (auto* coreContentRenderer = downcast<FrameView>(*widget).frame().contentRenderer())
121 children.append(adoptRef(*new WebRenderObject(coreContentRenderer, shouldIncludeDescendants)));
122 }
123 }
124
125 m_children = API::Array::create(WTFMove(children));
126}
127
128WebRenderObject::WebRenderObject(const String& name, const String& elementTagName, const String& elementID, RefPtr<API::Array>&& elementClassNames, WebCore::IntPoint absolutePosition, WebCore::IntRect frameRect, const String& textSnippet, unsigned textLength, RefPtr<API::Array>&& children)
129 : m_children(WTFMove(children))
130 , m_name(name)
131 , m_elementTagName(elementTagName)
132 , m_elementID(elementID)
133 , m_textSnippet(textSnippet)
134 , m_elementClassNames(WTFMove(elementClassNames))
135 , m_absolutePosition(absolutePosition)
136 , m_frameRect(frameRect)
137 , m_textLength(textLength)
138{
139}
140
141WebRenderObject::~WebRenderObject()
142{
143}
144
145} // namespace WebKit
146