MathMLSelectElement.cpp [plain text]
#include "config.h"
#include "MathMLSelectElement.h"
#if ENABLE(MATHML)
#include "Event.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "MathMLNames.h"
#include "RenderMathMLRow.h"
#include "SVGElement.h"
#include "SVGNames.h"
namespace WebCore {
using namespace MathMLNames;
MathMLSelectElement::MathMLSelectElement(const QualifiedName& tagName, Document& document)
: MathMLInlineContainerElement(tagName, document)
, m_selectedChild(nullptr)
{
}
PassRefPtr<MathMLSelectElement> MathMLSelectElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(new MathMLSelectElement(tagName, document));
}
RenderPtr<RenderElement> MathMLSelectElement::createElementRenderer(PassRef<RenderStyle> style)
{
return createRenderer<RenderMathMLRow>(*this, WTF::move(style));
}
bool MathMLSelectElement::isMathMLEncoding(const AtomicString& value)
{
return value == "application/mathml-presentation+xml" || value == "MathML-Presentation";
}
bool MathMLSelectElement::isSVGEncoding(const AtomicString& value)
{
return value == "image/svg+xml" || value == "SVG1.1";
}
bool MathMLSelectElement::isHTMLEncoding(const AtomicString& value)
{
return value == "application/xhtml+xml" || value == "text/html";
}
bool MathMLSelectElement::childShouldCreateRenderer(const Node& child) const
{
return MathMLElement::childShouldCreateRenderer(child) && m_selectedChild == &child;
}
void MathMLSelectElement::finishParsingChildren()
{
updateSelectedChild();
MathMLInlineContainerElement::finishParsingChildren();
}
void MathMLSelectElement::childrenChanged(const ChildChange& change)
{
updateSelectedChild();
MathMLInlineContainerElement::childrenChanged(change);
}
void MathMLSelectElement::attributeChanged(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue, AttributeModificationReason reason)
{
if (hasTagName(mactionTag) && (name == MathMLNames::actiontypeAttr || name == MathMLNames::selectionAttr))
updateSelectedChild();
MathMLInlineContainerElement::attributeChanged(name, oldValue, newValue, reason);
}
int MathMLSelectElement::getSelectedActionChildAndIndex(Element*& selectedChild)
{
ASSERT(hasTagName(mactionTag));
selectedChild = firstElementChild();
if (!selectedChild)
return 1;
int selection = fastGetAttribute(MathMLNames::selectionAttr).toInt();
int i;
for (i = 1; i < selection; i++) {
Element* nextChild = selectedChild->nextElementSibling();
if (!nextChild)
break;
selectedChild = nextChild;
}
return i;
}
Element* MathMLSelectElement::getSelectedActionChild()
{
ASSERT(hasTagName(mactionTag));
Element* child = firstElementChild();
if (!child)
return child;
const AtomicString& actiontype = fastGetAttribute(MathMLNames::actiontypeAttr);
if (actiontype == "statusline")
{ }
else if (actiontype == "tooltip")
{ }
else {
getSelectedActionChildAndIndex(child);
}
return child;
}
Element* MathMLSelectElement::getSelectedSemanticsChild()
{
ASSERT(hasTagName(semanticsTag));
Element* child = firstElementChild();
if (!child)
return child;
if (!child->isMathMLElement() || !toMathMLElement(child)->isPresentationMathML()) {
child = child->nextElementSibling();
} else if (!toMathMLElement(child)->isSemanticAnnotation()) {
return child;
}
for ( ; child; child = child->nextElementSibling()) {
if (!child->isMathMLElement())
continue;
if (child->hasTagName(MathMLNames::annotationTag)) {
if (child->hasAttribute(MathMLNames::srcAttr))
continue;
return child;
}
if (child->hasTagName(MathMLNames::annotation_xmlTag)) {
if (child->hasAttribute(MathMLNames::srcAttr))
continue;
const AtomicString& value = child->fastGetAttribute(MathMLNames::encodingAttr);
if (isMathMLEncoding(value) || isSVGEncoding(value) || isHTMLEncoding(value))
return child;
}
}
return firstElementChild();
}
void MathMLSelectElement::updateSelectedChild()
{
Element* newSelectedChild = hasTagName(mactionTag) ? getSelectedActionChild() : getSelectedSemanticsChild();
if (m_selectedChild == newSelectedChild)
return;
if (m_selectedChild && m_selectedChild->renderer())
Style::detachRenderTree(*m_selectedChild);
m_selectedChild = newSelectedChild;
setNeedsStyleRecalc();
}
void MathMLSelectElement::defaultEventHandler(Event* event)
{
if (event->type() == eventNames().clickEvent) {
if (fastGetAttribute(MathMLNames::actiontypeAttr) == "toggle") {
toggle();
event->setDefaultHandled();
return;
}
}
MathMLInlineContainerElement::defaultEventHandler(event);
}
bool MathMLSelectElement::willRespondToMouseClickEvents()
{
return fastGetAttribute(MathMLNames::actiontypeAttr) == "toggle";
}
void MathMLSelectElement::toggle()
{
Element* selectedChild;
int newSelectedChildIndex = getSelectedActionChildAndIndex(selectedChild) + 1;
if (!selectedChild || !selectedChild->nextElementSibling())
newSelectedChildIndex = 1;
setAttribute(MathMLNames::selectionAttr, AtomicString::number(newSelectedChildIndex));
}
}
#endif // ENABLE(MATHML)