2 * Copyright (C) 2003 Apple Computer, 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "KWQRenderTreeDebug.h"
28 #include "dom_docimpl.h"
29 #include "dom_position.h"
31 #include "jsediting.h"
32 #include "khtmlview.h"
33 #include "render_canvas.h"
34 #include "render_replaced.h"
35 #include "render_table.h"
36 #include "render_text.h"
37 #include "render_br.h"
38 #include "selection.h"
40 #include "KWQKHTMLPart.h"
41 #include "KWQTextStream.h"
43 using DOM::DocumentImpl;
48 using khtml::BorderValue;
49 using khtml::EBorderStyle;
50 using khtml::InlineTextBox;
51 using khtml::RenderLayer;
52 using khtml::RenderObject;
53 using khtml::RenderTableCell;
54 using khtml::RenderWidget;
55 using khtml::RenderText;
56 using khtml::RenderCanvas;
57 using khtml::RenderBR;
58 using khtml::Selection;
59 using khtml::transparentColor;
61 static void writeLayers(QTextStream &ts, const RenderLayer* rootLayer, RenderLayer* l,
62 const QRect& paintDirtyRect, int indent=0);
64 static QTextStream &operator<<(QTextStream &ts, const QRect &r)
66 return ts << "at (" << r.x() << "," << r.y() << ") size " << r.width() << "x" << r.height();
69 static void writeIndent(QTextStream &ts, int indent)
71 for (int i = 0; i != indent; ++i) {
76 static void printBorderStyle(QTextStream &ts, const RenderObject &o, const EBorderStyle borderStyle)
78 switch (borderStyle) {
114 static QTextStream &operator<<(QTextStream &ts, const RenderObject &o)
116 ts << o.renderName();
118 if (o.style() && o.style()->zIndex()) {
119 ts << " zI: " << o.style()->zIndex();
123 QString tagName(getTagName(o.element()->id()).string());
124 if (!tagName.isEmpty()) {
125 ts << " {" << tagName << "}";
129 // FIXME: Will remove this <br> code once all layout tests pass. Until then, we can't really change
130 // all the results easily.
131 bool usePositions = true;
133 const RenderBR* br = static_cast<const RenderBR*>(&o);
134 usePositions = (br->firstTextBox() && br->firstTextBox()->isText());
136 QRect r(usePositions ? o.xPos() : 0, usePositions ? o.yPos() : 0, o.width(), o.height());
140 if (o.parent() && (o.parent()->style()->color() != o.style()->color()))
141 ts << " [color=" << o.style()->color().name() << "]";
142 if (o.parent() && (o.parent()->style()->backgroundColor() != o.style()->backgroundColor()) &&
143 o.style()->backgroundColor().isValid() &&
144 o.style()->backgroundColor().rgb() != khtml::transparentColor)
145 // Do not dump invalid or transparent backgrounds, since that is the default.
146 ts << " [bgcolor=" << o.style()->backgroundColor().name() << "]";
148 if (o.borderTop() || o.borderRight() || o.borderBottom() || o.borderLeft()) {
151 BorderValue prevBorder;
152 if (o.style()->borderTop() != prevBorder) {
153 prevBorder = o.style()->borderTop();
157 ts << " (" << o.borderTop() << "px ";
158 printBorderStyle(ts, o, o.style()->borderTopStyle());
159 QColor col = o.style()->borderTopColor();
160 if (!col.isValid()) col = o.style()->color();
161 ts << col.name() << ")";
165 if (o.style()->borderRight() != prevBorder) {
166 prevBorder = o.style()->borderRight();
167 if (!o.borderRight())
170 ts << " (" << o.borderRight() << "px ";
171 printBorderStyle(ts, o, o.style()->borderRightStyle());
172 QColor col = o.style()->borderRightColor();
173 if (!col.isValid()) col = o.style()->color();
174 ts << col.name() << ")";
178 if (o.style()->borderBottom() != prevBorder) {
179 prevBorder = o.style()->borderBottom();
180 if (!o.borderBottom())
183 ts << " (" << o.borderBottom() << "px ";
184 printBorderStyle(ts, o, o.style()->borderBottomStyle());
185 QColor col = o.style()->borderBottomColor();
186 if (!col.isValid()) col = o.style()->color();
187 ts << col.name() << ")";
191 if (o.style()->borderLeft() != prevBorder) {
192 prevBorder = o.style()->borderLeft();
196 ts << " (" << o.borderLeft() << "px ";
197 printBorderStyle(ts, o, o.style()->borderLeftStyle());
198 QColor col = o.style()->borderLeftColor();
199 if (!col.isValid()) col = o.style()->color();
200 ts << col.name() << ")";
208 if (o.isTableCell()) {
209 const RenderTableCell &c = static_cast<const RenderTableCell &>(o);
210 ts << " [r=" << c.row() << " c=" << c.col() << " rs=" << c.rowSpan() << " cs=" << c.colSpan() << "]";
216 static QString quoteAndEscapeNonPrintables(const QString &s)
220 for (uint i = 0; i != s.length(); ++i) {
224 } else if (c == '"') {
226 } else if (c == '\n' || c.unicode() == 0x00A0) {
229 ushort u = c.unicode();
230 if (u >= 0x20 && u < 0x7F) {
234 hex.sprintf("\\x{%X}", u);
243 static void writeTextRun(QTextStream &ts, const RenderText &o, const InlineTextBox &run)
245 ts << "text run at (" << run.m_x << "," << run.m_y << ") width " << run.m_width << ": "
246 << quoteAndEscapeNonPrintables(o.data().string().mid(run.m_start, run.m_len))
250 static void write(QTextStream &ts, const RenderObject &o, int indent = 0)
252 writeIndent(ts, indent);
256 if (o.isText() && !o.isBR()) {
257 const RenderText &text = static_cast<const RenderText &>(o);
258 for (InlineTextBox* box = text.firstTextBox(); box; box = box->nextTextBox()) {
259 writeIndent(ts, indent+1);
260 writeTextRun(ts, text, *box);
264 for (RenderObject *child = o.firstChild(); child; child = child->nextSibling()) {
265 if (child->layer()) {
268 write(ts, *child, indent + 1);
272 QWidget *widget = static_cast<const RenderWidget &>(o).widget();
273 if (widget && widget->inherits("KHTMLView")) {
274 KHTMLView *view = static_cast<KHTMLView *>(widget);
275 RenderObject *root = KWQ(view->part())->renderer();
278 RenderLayer* l = root->layer();
280 writeLayers(ts, l, l, QRect(l->xPos(), l->yPos(), l->width(), l->height()), indent+1);
286 static void write(QTextStream &ts, RenderLayer &l,
287 const QRect& layerBounds, const QRect& backgroundClipRect, const QRect& clipRect,
288 int layerType = 0, int indent = 0)
290 writeIndent(ts, indent);
293 ts << " " << layerBounds;
295 if (layerBounds != layerBounds.intersect(backgroundClipRect))
296 ts << " backgroundClip " << backgroundClipRect;
297 if (layerBounds != layerBounds.intersect(clipRect))
298 ts << " clip " << clipRect;
300 if (l.renderer()->hasOverflowClip()) {
301 if (l.scrollXOffset())
302 ts << " scrollX " << l.scrollXOffset();
303 if (l.scrollYOffset())
304 ts << " scrollY " << l.scrollYOffset();
305 if (l.renderer()->clientWidth() != l.scrollWidth())
306 ts << " scrollWidth " << l.scrollWidth();
307 if (l.renderer()->clientHeight() != l.scrollHeight())
308 ts << " scrollHeight " << l.scrollHeight();
312 ts << " layerType: background only";
313 else if (layerType == 1)
314 ts << " layerType: foreground only";
319 write(ts, *l.renderer(), indent + 1);
322 static void writeLayers(QTextStream &ts, const RenderLayer* rootLayer, RenderLayer* l,
323 const QRect& paintDirtyRect, int indent)
325 // Calculate the clip rects we should use.
326 QRect layerBounds, damageRect, clipRectToApply;
327 l->calculateRects(rootLayer, paintDirtyRect, layerBounds, damageRect, clipRectToApply);
329 // Ensure our z-order lists are up-to-date.
330 l->updateZOrderLists();
332 bool shouldPaint = l->intersectsDamageRect(layerBounds, damageRect);
333 QPtrVector<RenderLayer>* negList = l->negZOrderList();
334 if (shouldPaint && negList && negList->count() > 0)
335 write(ts, *l, layerBounds, damageRect, clipRectToApply, -1, indent);
338 for (unsigned i = 0; i != negList->count(); ++i)
339 writeLayers(ts, rootLayer, negList->at(i), paintDirtyRect, indent);
343 write(ts, *l, layerBounds, damageRect, clipRectToApply, negList && negList->count() > 0, indent);
345 QPtrVector<RenderLayer>* posList = l->posZOrderList();
347 for (unsigned i = 0; i != posList->count(); ++i)
348 writeLayers(ts, rootLayer, posList->at(i), paintDirtyRect, indent);
352 static QString nodePositionRelativeToRoot(NodeImpl *node, NodeImpl *root)
358 NodeImpl *p = n->parentNode();
359 if (!p || n == root) {
360 result += " of root {" + getTagName(n->id()).string() + "}";
366 for (NodeImpl *search = p->firstChild(); search != n; search = search->nextSibling())
368 result += "child " + QString::number(count) + " {" + getTagName(n->id()).string() + "}";
375 static void writeSelection(QTextStream &ts, const RenderObject *o)
377 NodeImpl *n = o->element();
378 if (!n || !n->isDocumentNode())
381 DocumentImpl *doc = static_cast<DocumentImpl *>(n);
385 Selection selection = doc->part()->selection();
386 if (selection.isNone())
389 if (!selection.start().node()->isContentEditable() || !selection.end().node()->isContentEditable())
392 Position startPosition = selection.start();
393 Position endPosition = selection.end();
395 QString startNodeTagName(getTagName(startPosition.node()->id()).string());
396 QString endNodeTagName(getTagName(endPosition.node()->id()).string());
398 NodeImpl *rootNode = doc->getElementById("root");
400 if (selection.isCaret()) {
401 Position upstream = startPosition.upstream(DOM::StayInBlock);
402 Position downstream = startPosition.downstream(DOM::StayInBlock);
403 QString positionString = nodePositionRelativeToRoot(startPosition.node(), rootNode);
404 QString upstreamString = nodePositionRelativeToRoot(upstream.node(), rootNode);
405 QString downstreamString = nodePositionRelativeToRoot(downstream.node(), rootNode);
406 ts << "selection is CARET:\n" <<
407 "start: position " << startPosition.offset() << " of " << positionString << "\n"
408 "upstream: position " << upstream.offset() << " of " << upstreamString << "\n"
409 "downstream: position " << downstream.offset() << " of " << downstreamString << "\n";
411 else if (selection.isRange()) {
412 QString startString = nodePositionRelativeToRoot(startPosition.node(), rootNode);
413 Position upstreamStart = startPosition.upstream(DOM::StayInBlock);
414 QString upstreamStartString = nodePositionRelativeToRoot(upstreamStart.node(), rootNode);
415 Position downstreamStart = startPosition.downstream(DOM::StayInBlock);
416 QString downstreamStartString = nodePositionRelativeToRoot(downstreamStart.node(), rootNode);
417 QString endString = nodePositionRelativeToRoot(endPosition.node(), rootNode);
418 Position upstreamEnd = endPosition.upstream(DOM::StayInBlock);
419 QString upstreamEndString = nodePositionRelativeToRoot(upstreamEnd.node(), rootNode);
420 Position downstreamEnd = endPosition.downstream(DOM::StayInBlock);
421 QString downstreamEndString = nodePositionRelativeToRoot(downstreamEnd.node(), rootNode);
422 ts << "selection is RANGE:\n" <<
423 "start: position " << startPosition.offset() << " of " << startString << "\n" <<
424 "upstream: position " << upstreamStart.offset() << " of " << upstreamStartString << "\n"
425 "downstream: position " << downstreamStart.offset() << " of " << downstreamStartString << "\n"
426 "end: position " << endPosition.offset() << " of " << endString << "\n"
427 "upstream: position " << upstreamEnd.offset() << " of " << upstreamEndString << "\n"
428 "downstream: position " << downstreamEnd.offset() << " of " << downstreamEndString << "\n";
432 static bool debuggingRenderTreeFlag = false;
434 bool debuggingRenderTree()
436 return debuggingRenderTreeFlag;
439 QString externalRepresentation(RenderObject *o)
441 debuggingRenderTreeFlag = true;
442 JSEditor::setSupportsPasteCommand(true);
448 // FIXME: Hiding the vertical scrollbar is a total hack to preserve the
449 // layout test results until I can figure out what the heck is going on. -dwh
450 o->canvas()->view()->setVScrollBarMode(QScrollView::AlwaysOff);
451 o->canvas()->view()->layout();
452 RenderLayer* l = o->layer();
454 writeLayers(ts, l, l, QRect(l->xPos(), l->yPos(), l->width(), l->height()));
455 writeSelection(ts, o);