1/*
2 * Copyright (C) 2014 Frédéric Wang (fred.wang@free.fr). 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY 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.
24 */
25
26#include "config.h"
27#include "RenderMathMLToken.h"
28
29#if ENABLE(MATHML)
30
31#include "MathMLNames.h"
32#include "RenderElement.h"
33#include "RenderIterator.h"
34
35namespace WebCore {
36
37using namespace MathMLNames;
38
39RenderMathMLToken::RenderMathMLToken(Element& element, PassRef<RenderStyle> style)
40    : RenderMathMLBlock(element, WTF::move(style))
41    , m_containsElement(false)
42{
43}
44
45RenderMathMLToken::RenderMathMLToken(Document& document, PassRef<RenderStyle> style)
46    : RenderMathMLBlock(document, WTF::move(style))
47    , m_containsElement(false)
48{
49}
50
51void RenderMathMLToken::addChild(RenderObject* newChild, RenderObject* beforeChild)
52{
53    createWrapperIfNeeded();
54    toRenderElement(firstChild())->addChild(newChild, beforeChild);
55}
56
57void RenderMathMLToken::createWrapperIfNeeded()
58{
59    if (!firstChild()) {
60        RenderPtr<RenderMathMLBlock> wrapper = createAnonymousMathMLBlock();
61        RenderMathMLBlock::addChild(wrapper.leakPtr());
62    }
63}
64
65void RenderMathMLToken::updateTokenContent()
66{
67    m_containsElement = false;
68    if (!isEmpty()) {
69        // The renderers corresponding to the children of the token element are wrapped inside an anonymous RenderMathMLBlock.
70        // When one of these renderers is a RenderElement, we handle the RenderMathMLToken differently.
71        // For some reason, an additional anonymous RenderBlock is created as a child of the RenderMathMLToken and the renderers are actually inserted into that RenderBlock so we need to dig down one additional level here.
72        const auto& wrapper = toRenderElement(firstChild());
73        if (const auto& block = toRenderElement(wrapper->firstChild()))
74            m_containsElement = childrenOfType<RenderElement>(*block).first();
75        updateStyle();
76    }
77    setNeedsLayoutAndPrefWidthsRecalc();
78}
79
80void RenderMathMLToken::updateStyle()
81{
82    const auto& tokenElement = element();
83
84    const auto& wrapper = toRenderElement(firstChild());
85    auto newStyle = RenderStyle::createAnonymousStyleWithDisplay(&style(), FLEX);
86
87    if (tokenElement.hasTagName(MathMLNames::miTag)) {
88        // This tries to emulate the default mathvariant value on <mi> using the CSS font-style property.
89        // FIXME: This should be revised when mathvariant is implemented (http://wkbug/85735) and when fonts with Mathematical Alphanumeric Symbols characters are more popular.
90        FontDescription fontDescription(newStyle.get().fontDescription());
91        FontSelector* fontSelector = newStyle.get().font().fontSelector();
92        if (!m_containsElement && element().textContent().stripWhiteSpace().simplifyWhiteSpace().length() == 1 && !tokenElement.hasAttribute(mathvariantAttr))
93            fontDescription.setItalic(true);
94        if (newStyle.get().setFontDescription(fontDescription))
95            newStyle.get().font().update(fontSelector);
96    }
97
98    wrapper->setStyle(WTF::move(newStyle));
99    wrapper->setNeedsLayoutAndPrefWidthsRecalc();
100}
101
102void RenderMathMLToken::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
103{
104    RenderMathMLBlock::styleDidChange(diff, oldStyle);
105    if (!isEmpty())
106        updateStyle();
107}
108
109void RenderMathMLToken::updateFromElement()
110{
111    RenderMathMLBlock::updateFromElement();
112    if (!isEmpty())
113        updateStyle();
114}
115
116}
117
118#endif // ENABLE(MATHML)
119