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/RenderText.h>
36#include <WebCore/RenderView.h>
37#include <WebCore/RenderWidget.h>
38
39using namespace WebCore;
40
41namespace WebKit {
42
43PassRefPtr<WebRenderObject> WebRenderObject::create(WebPage* page)
44{
45    Frame* mainFrame = page->mainFrame();
46    if (!mainFrame)
47        return 0;
48
49    if (!mainFrame->loader().client().hasHTMLView())
50        return 0;
51
52    RenderView* contentRenderer = mainFrame->contentRenderer();
53    if (!contentRenderer)
54        return 0;
55
56    return adoptRef(new WebRenderObject(contentRenderer, true));
57}
58
59PassRefPtr<WebRenderObject> WebRenderObject::create(const String& name, const String& elementTagName, const String& elementID, PassRefPtr<API::Array> elementClassNames, WebCore::IntPoint absolutePosition, WebCore::IntRect frameRect, PassRefPtr<API::Array> children)
60{
61    return adoptRef(new WebRenderObject(name, elementTagName, elementID, elementClassNames, absolutePosition, frameRect, children));
62}
63
64WebRenderObject::WebRenderObject(RenderObject* renderer, bool shouldIncludeDescendants)
65{
66    m_name = renderer->renderName();
67
68    if (Node* node = renderer->node()) {
69        if (node->isElementNode()) {
70            Element* element = toElement(node);
71            m_elementTagName = element->tagName();
72            m_elementID = element->getIdAttribute();
73
74            if (element->isStyledElement() && element->hasClass()) {
75                Vector<RefPtr<API::Object>> classNames;
76                classNames.reserveInitialCapacity(element->classNames().size());
77
78                for (size_t i = 0, size = element->classNames().size(); i < size; ++i)
79                    classNames.append(API::String::create(element->classNames()[i]));
80
81                m_elementClassNames = API::Array::create(WTF::move(classNames));
82            }
83        }
84    }
85
86    // FIXME: broken with transforms
87    m_absolutePosition = flooredIntPoint(renderer->localToAbsolute());
88
89    if (renderer->isBox())
90        m_frameRect = toRenderBox(renderer)->pixelSnappedFrameRect();
91    else if (renderer->isText()) {
92        m_frameRect = toRenderText(renderer)->linesBoundingBox();
93        m_frameRect.setLocation(toRenderText(renderer)->firstRunLocation());
94    } else if (renderer->isRenderInline())
95        m_frameRect = toRenderBoxModelObject(renderer)->borderBoundingBox();
96
97    if (!shouldIncludeDescendants)
98        return;
99
100    Vector<RefPtr<API::Object>> children;
101
102    for (RenderObject* coreChild = renderer->firstChildSlow(); coreChild; coreChild = coreChild->nextSibling()) {
103        RefPtr<WebRenderObject> child = adoptRef(new WebRenderObject(coreChild, shouldIncludeDescendants));
104        children.append(WTF::move(child));
105    }
106
107    if (renderer->isWidget()) {
108        if (Widget* widget = toRenderWidget(renderer)->widget()) {
109            if (widget->isFrameView()) {
110                FrameView* frameView = toFrameView(widget);
111                if (RenderView* coreContentRenderer = frameView->frame().contentRenderer()) {
112                    RefPtr<WebRenderObject> contentRenderer = adoptRef(new WebRenderObject(coreContentRenderer, shouldIncludeDescendants));
113
114                    children.append(WTF::move(contentRenderer));
115                }
116            }
117        }
118    }
119
120    m_children = API::Array::create(WTF::move(children));
121}
122
123WebRenderObject::WebRenderObject(const String& name, const String& elementTagName, const String& elementID, PassRefPtr<API::Array> elementClassNames, WebCore::IntPoint absolutePosition, WebCore::IntRect frameRect, PassRefPtr<API::Array> children)
124    : m_children(children)
125    , m_name(name)
126    , m_elementTagName(elementTagName)
127    , m_elementID(elementID)
128    , m_elementClassNames(elementClassNames)
129    , m_absolutePosition(absolutePosition)
130    , m_frameRect(frameRect)
131{
132}
133
134WebRenderObject::~WebRenderObject()
135{
136}
137
138} // namespace WebKit
139