Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / layout / TracedLayoutObject.cpp
blobee60df3dcdedfef88bb996b50d8a887137531a8c
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "config.h"
6 #include "core/layout/TracedLayoutObject.h"
8 #include "core/layout/LayoutInline.h"
9 #include "core/layout/LayoutTableCell.h"
10 #include "core/layout/LayoutText.h"
11 #include "core/layout/LayoutView.h"
12 #include "platform/JSONValues.h"
14 namespace blink {
16 PassRefPtr<TraceEvent::ConvertableToTraceFormat> TracedLayoutObject::create(const LayoutView& view, bool traceGeometry)
18 return adoptRef(new TracedLayoutObject(view, traceGeometry));
21 String TracedLayoutObject::asTraceFormat() const
23 StringBuilder builder;
24 RefPtr<JSONObject> json(toJSON());
25 json->writeJSON(&builder);
26 return builder.toString();
29 TracedLayoutObject::TracedLayoutObject(const LayoutObject& object, bool traceGeometry)
30 : m_address((unsigned long) &object)
31 , m_isAnonymous(object.isAnonymous())
32 , m_isPositioned(object.isOutOfFlowPositioned())
33 , m_isRelPositioned(object.isRelPositioned())
34 , m_isStickyPositioned(object.isStickyPositioned())
35 , m_isFloating(object.isFloating())
36 , m_selfNeeds(object.selfNeedsLayout())
37 , m_positionedMovement(object.needsPositionedMovementLayout())
38 , m_childNeeds(object.normalChildNeedsLayout())
39 , m_posChildNeeds(object.posChildNeedsLayout())
40 , m_isTableCell(object.isTableCell())
41 , m_name(String(object.name()).isolatedCopy())
42 , m_absRect(traceGeometry ? object.absoluteBoundingBoxRect() : IntRect())
44 if (Node* node = object.node()) {
45 m_tag = String(node->nodeName()).isolatedCopy();
46 if (node->isElementNode()) {
47 Element& element = toElement(*node);
48 if (element.hasID())
49 m_id = String(element.getIdAttribute()).isolatedCopy();
50 if (element.hasClass()) {
51 for (size_t i = 0; i < element.classNames().size(); ++i) {
52 m_classNames.append(
53 String(element.classNames()[i]).isolatedCopy());
59 // FIXME: When the fixmes in LayoutTreeAsText::writeLayoutObject() are
60 // fixed, deduplicate it with this.
61 if (traceGeometry) {
62 if (object.isText()) {
63 m_rect = LayoutRect(toLayoutText(object).linesBoundingBox());
64 } else if (object.isLayoutInline()) {
65 m_rect = LayoutRect(toLayoutInline(object).linesBoundingBox());
66 } else if (object.isBox()) {
67 m_rect = toLayoutBox(&object)->frameRect();
71 if (m_isTableCell) {
72 const LayoutTableCell& c = toLayoutTableCell(object);
73 if (c.row() && c.row()->rowIndexWasSet() && c.hasCol()
74 && (!c.row()->section() || !c.row()->section()->needsCellRecalc())) {
75 m_row = c.rowIndex();
76 m_col = c.col();
77 m_rowSpan = c.rowSpan();
78 m_colSpan = c.colSpan();
82 for (LayoutObject* child = object.slowFirstChild(); child; child = child->nextSibling()) {
83 m_children.append(adoptRef(new TracedLayoutObject(*child, traceGeometry)));
87 PassRefPtr<JSONObject> TracedLayoutObject::toJSON() const
89 RefPtr<JSONObject> json(JSONObject::create());
90 json->setNumber("address", m_address);
91 json->setString("name", m_name);
92 if (!m_tag.isEmpty())
93 json->setString("tag", m_tag);
94 if (!m_id.isEmpty())
95 json->setString("htmlId", m_id);
96 if (m_classNames.size()) {
97 RefPtr<JSONArray> classNames(JSONArray::create());
98 for (const auto& className : m_classNames) {
99 classNames->pushString(className);
101 json->setArray("classNames", classNames);
103 json->setNumber("absX", m_absRect.x());
104 json->setNumber("absY", m_absRect.y());
105 json->setNumber("relX", m_rect.x());
106 json->setNumber("relY", m_rect.y());
107 json->setNumber("width", m_rect.width());
108 json->setNumber("height", m_rect.height());
109 if (m_isPositioned)
110 json->setBoolean("positioned", m_isPositioned);
111 if (m_selfNeeds)
112 json->setBoolean("selfNeeds", m_selfNeeds);
113 if (m_positionedMovement)
114 json->setBoolean("positionedMovement", m_positionedMovement);
115 if (m_childNeeds)
116 json->setBoolean("childNeeds", m_childNeeds);
117 if (m_posChildNeeds)
118 json->setBoolean("posChildNeeds", m_posChildNeeds);
119 if (m_isTableCell) {
120 json->setNumber("row", m_row);
121 json->setNumber("col", m_col);
122 if (m_rowSpan != 1)
123 json->setNumber("rowSpan", m_rowSpan);
124 if (m_colSpan != 1)
125 json->setNumber("colSpan", m_colSpan);
127 if (m_isAnonymous)
128 json->setBoolean("anonymous", m_isAnonymous);
129 if (m_isRelPositioned)
130 json->setBoolean("relativePositioned", m_isRelPositioned);
131 if (m_isStickyPositioned)
132 json->setBoolean("stickyPositioned", m_isStickyPositioned);
133 if (m_isFloating)
134 json->setBoolean("float", m_isFloating);
135 if (m_children.size()) {
136 RefPtr<JSONArray> children(JSONArray::create());
137 for (const auto& child : m_children) {
138 children->pushObject(child->toJSON());
140 json->setArray("children", children);
142 return json.release();
145 } // namespace blink