MathMLSelectElement.cpp [plain text]
#include "config.h"
#include "MathMLSelectElement.h"
#if ENABLE(MATHML)
#include "Event.h"
#include "EventNames.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "MathMLNames.h"
#include "RenderMathMLRow.h"
#include "RenderTreeUpdater.h"
#include "SVGElement.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(MathMLSelectElement);
using namespace MathMLNames;
MathMLSelectElement::MathMLSelectElement(const QualifiedName& tagName, Document& document)
: MathMLRowElement(tagName, document)
, m_selectedChild(nullptr)
{
}
Ref<MathMLSelectElement> MathMLSelectElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new MathMLSelectElement(tagName, document));
}
RenderPtr<RenderElement> MathMLSelectElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
return createRenderer<RenderMathMLRow>(*this, WTFMove(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();
MathMLRowElement::finishParsingChildren();
}
void MathMLSelectElement::childrenChanged(const ChildChange& change)
{
updateSelectedChild();
MathMLRowElement::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();
MathMLRowElement::attributeChanged(name, oldValue, newValue, reason);
}
int MathMLSelectElement::getSelectedActionChildAndIndex(Element*& selectedChild)
{
ASSERT(hasTagName(mactionTag));
selectedChild = firstElementChild();
if (!selectedChild)
return 1;
int selection = attributeWithoutSynchronization(MathMLNames::selectionAttr).toInt();
int i;
for (i = 1; i < selection; i++) {
auto* nextChild = selectedChild->nextElementSibling();
if (!nextChild)
break;
selectedChild = nextChild;
}
return i;
}
Element* MathMLSelectElement::getSelectedActionChild()
{
ASSERT(hasTagName(mactionTag));
auto* child = firstElementChild();
if (!child)
return child;
auto& actiontype = attributeWithoutSynchronization(MathMLNames::actiontypeAttr);
if (actiontype == "statusline")
{ }
else if (actiontype == "tooltip")
{ }
else {
getSelectedActionChildAndIndex(child);
}
return child;
}
Element* MathMLSelectElement::getSelectedSemanticsChild()
{
ASSERT(hasTagName(semanticsTag));
auto* child = firstElementChild();
if (!child)
return nullptr;
if (!is<MathMLElement>(*child) || !downcast<MathMLElement>(*child).isPresentationMathML()) {
child = child->nextElementSibling();
} else if (!downcast<MathMLElement>(*child).isSemanticAnnotation()) {
return child;
}
for ( ; child; child = child->nextElementSibling()) {
if (!is<MathMLElement>(*child))
continue;
if (child->hasTagName(MathMLNames::annotationTag)) {
if (child->hasAttributeWithoutSynchronization(MathMLNames::srcAttr))
continue;
return child;
}
if (child->hasTagName(MathMLNames::annotation_xmlTag)) {
if (child->hasAttributeWithoutSynchronization(MathMLNames::srcAttr))
continue;
auto& value = child->attributeWithoutSynchronization(MathMLNames::encodingAttr);
if (isMathMLEncoding(value) || isSVGEncoding(value) || isHTMLEncoding(value))
return child;
}
}
return firstElementChild();
}
void MathMLSelectElement::updateSelectedChild()
{
auto* newSelectedChild = hasTagName(mactionTag) ? getSelectedActionChild() : getSelectedSemanticsChild();
if (m_selectedChild == newSelectedChild)
return;
if (m_selectedChild && m_selectedChild->renderer())
RenderTreeUpdater::tearDownRenderers(*m_selectedChild);
m_selectedChild = newSelectedChild;
invalidateStyleForSubtree();
}
void MathMLSelectElement::defaultEventHandler(Event& event)
{
if (event.type() == eventNames().clickEvent) {
if (attributeWithoutSynchronization(MathMLNames::actiontypeAttr) == "toggle") {
toggle();
event.setDefaultHandled();
return;
}
}
MathMLRowElement::defaultEventHandler(event);
}
bool MathMLSelectElement::willRespondToMouseClickEvents()
{
return attributeWithoutSynchronization(MathMLNames::actiontypeAttr) == "toggle" || MathMLRowElement::willRespondToMouseClickEvents();
}
void MathMLSelectElement::toggle()
{
Element* selectedChild;
int newSelectedChildIndex = getSelectedActionChildAndIndex(selectedChild) + 1;
if (!selectedChild || !selectedChild->nextElementSibling())
newSelectedChildIndex = 1;
setAttributeWithoutSynchronization(MathMLNames::selectionAttr, AtomicString::number(newSelectedChildIndex));
}
}
#endif // ENABLE(MATHML)