2 * Copyright (C) 2009 Alex Milowski (alex@milowski.com). 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 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.
30 #include "RenderMathMLFenced.h"
32 #include "FontSelector.h"
33 #include "MathMLNames.h"
34 #include "RenderInline.h"
35 #include "RenderMathMLOperator.h"
36 #include "RenderText.h"
37 #include <wtf/text/StringBuilder.h>
41 using namespace MathMLNames;
43 enum Braces { OpeningBraceChar = 0x28, ClosingBraceChar = 0x29 };
45 static const float gOperatorPadding = 0.1f;
47 RenderMathMLFenced::RenderMathMLFenced(Node* fenced)
48 : RenderMathMLRow(fenced)
49 , m_open(OpeningBraceChar)
50 , m_close(ClosingBraceChar)
54 void RenderMathMLFenced::updateFromElement()
56 Element* fenced = static_cast<Element*>(node());
58 // FIXME: Handle open/close values with more than one character (they should be treated like text).
59 AtomicString openValue = fenced->getAttribute(MathMLNames::openAttr);
60 if (openValue.length() > 0)
61 m_open = openValue[0];
62 AtomicString closeValue = fenced->getAttribute(MathMLNames::closeAttr);
63 if (closeValue.length() > 0)
64 m_close = closeValue[0];
66 AtomicString separators = static_cast<Element*>(fenced)->getAttribute(MathMLNames::separatorsAttr);
67 if (!separators.isNull()) {
68 StringBuilder characters;
69 for (unsigned int i = 0; i < separators.length(); i++) {
70 if (!isSpaceOrNewline(separators[i]))
71 characters.append(separators[i]);
73 m_separators = !characters.length() ? 0 : characters.toString().impl();
75 // The separator defaults to a single comma.
76 m_separators = StringImpl::create(",");
83 RefPtr<RenderStyle> RenderMathMLFenced::makeOperatorStyle()
85 RefPtr<RenderStyle> newStyle = RenderStyle::create();
86 newStyle->inheritFrom(style());
87 newStyle->setDisplay(INLINE_BLOCK);
88 newStyle->setPaddingRight(Length(static_cast<int>(gOperatorPadding * style()->fontSize()), Fixed));
92 void RenderMathMLFenced::makeFences()
94 RenderObject* openFence = new (renderArena()) RenderMathMLOperator(node(), m_open);
95 openFence->setStyle(makeOperatorStyle().release());
96 RenderBlock::addChild(openFence, firstChild());
97 RenderObject* closeFence = new (renderArena()) RenderMathMLOperator(node(), m_close);
98 closeFence->setStyle(makeOperatorStyle().release());
99 RenderBlock::addChild(closeFence);
102 void RenderMathMLFenced::addChild(RenderObject* child, RenderObject*)
104 // make the fences if the render object is empty
108 if (m_separators.get()) {
109 unsigned int count = 0;
110 for (Node* position = child->node(); position; position = position->previousSibling()) {
111 if (position->nodeType() == Node::ELEMENT_NODE)
118 // Use the last separator if we've run out of specified separators.
119 if ((count - 1) >= m_separators.get()->length())
120 separator = (*m_separators.get())[m_separators.get()->length() - 1];
122 separator = (*m_separators.get())[count - 1];
124 RenderObject* separatorObj = new (renderArena()) RenderMathMLOperator(node(), separator);
125 separatorObj->setStyle(makeOperatorStyle().release());
126 RenderBlock::addChild(separatorObj, lastChild());
130 // If we have a block, we'll wrap it in an inline-block.
131 if (child->isBlockFlow() && child->style()->display() != INLINE_BLOCK) {
132 // Block objects wrapper.
134 RenderBlock* block = new (renderArena()) RenderBlock(node());
135 RefPtr<RenderStyle> newStyle = RenderStyle::create();
136 newStyle->inheritFrom(style());
137 newStyle->setDisplay(INLINE_BLOCK);
138 block->setStyle(newStyle.release());
140 RenderBlock::addChild(block, lastChild());
141 block->addChild(child);
143 RenderBlock::addChild(child, lastChild());