1/*
2 * Copyright (C) 2013 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 "InlineElementBox.h"
28
29#include "InlineFlowBox.h"
30#include "PaintInfo.h"
31#include "RenderBlock.h"
32#include "RenderBox.h"
33#include "RenderLineBreak.h"
34
35namespace WebCore {
36
37void InlineElementBox::deleteLine()
38{
39    if (!extracted()) {
40        if (renderer().isBox())
41            toRenderBox(renderer()).setInlineBoxWrapper(nullptr);
42        else if (renderer().isLineBreak())
43            toRenderLineBreak(renderer()).setInlineBoxWrapper(nullptr);
44    }
45    delete this;
46}
47
48void InlineElementBox::extractLine()
49{
50    setExtracted(true);
51    if (renderer().isBox())
52        toRenderBox(renderer()).setInlineBoxWrapper(nullptr);
53    else if (renderer().isLineBreak())
54        toRenderLineBreak(renderer()).setInlineBoxWrapper(nullptr);
55}
56
57void InlineElementBox::attachLine()
58{
59    setExtracted(false);
60    if (renderer().isBox())
61        toRenderBox(renderer()).setInlineBoxWrapper(this);
62    else if (renderer().isLineBreak())
63        toRenderLineBreak(renderer()).setInlineBoxWrapper(this);
64}
65
66void InlineElementBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
67{
68    if (!paintInfo.shouldPaintWithinRoot(renderer()) || (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection))
69        return;
70
71    LayoutPoint childPoint = paintOffset;
72    if (renderer().isBox() && parent()->renderer().style().isFlippedBlocksWritingMode()) // Faster than calling containingBlock().
73        childPoint = renderer().containingBlock()->flipForWritingModeForChild(&toRenderBox(renderer()), childPoint);
74
75    // Paint all phases of replaced elements atomically, as though the replaced element established its
76    // own stacking context.  (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
77    // specification.)
78    bool preservePhase = paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip;
79    PaintInfo info(paintInfo);
80    info.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
81    renderer().paint(info, childPoint);
82    if (!preservePhase) {
83        info.phase = PaintPhaseChildBlockBackgrounds;
84        renderer().paint(info, childPoint);
85        info.phase = PaintPhaseFloat;
86        renderer().paint(info, childPoint);
87        info.phase = PaintPhaseForeground;
88        renderer().paint(info, childPoint);
89        info.phase = PaintPhaseOutline;
90        renderer().paint(info, childPoint);
91    }
92}
93
94bool InlineElementBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
95{
96    // Hit test all phases of replaced elements atomically, as though the replaced element established its
97    // own stacking context.  (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
98    // specification.)
99    LayoutPoint childPoint = accumulatedOffset;
100    if (renderer().isBox() && parent()->renderer().style().isFlippedBlocksWritingMode()) // Faster than calling containingBlock().
101        childPoint = renderer().containingBlock()->flipForWritingModeForChild(&toRenderBox(renderer()), childPoint);
102
103    return renderer().hitTest(request, result, locationInContainer, childPoint);
104}
105
106}
107