Commit 1cff4b46 authored by antti@apple.com's avatar antti@apple.com

Add first()/last() to ElementIteratorAdapters

https://bugs.webkit.org/show_bug.cgi?id=122067

Reviewed by Darin Adler.

Add a convenient way for getting the first and last element if it exists.
        
Use it in some places.

* accessibility/AccessibilityNodeObject.cpp:
(WebCore::AccessibilityNodeObject::canvasHasFallbackContent):
* css/CSSFontFaceSource.cpp:
(WebCore::CSSFontFaceSource::getFontData):
* dom/Document.cpp:
(WebCore::Document::childrenChanged):
(WebCore::Document::removeTitle):
* dom/ElementChildIterator.h:
(WebCore::::first):
(WebCore::::last):
* dom/ElementDescendantIterator.h:
(WebCore::::first):
(WebCore::::last):
* html/HTMLFieldSetElement.cpp:
(WebCore::HTMLFieldSetElement::legend):
* html/HTMLLegendElement.cpp:
(WebCore::HTMLLegendElement::associatedControl):
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::finishParsingChildren):
(WebCore::HTMLMediaElement::selectMediaResource):
* svg/SVGElement.cpp:
(WebCore::SVGElement::title):
* svg/SVGFontFaceElement.cpp:
(WebCore::SVGFontFaceElement::rebuildFontFace):
* svg/graphics/SVGImage.cpp:
(WebCore::SVGImage::hasSingleSecurityOrigin):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@156612 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent a75e9229
2013-09-28 Antti Koivisto <antti@apple.com>
Add first()/last() to ElementIteratorAdapters
https://bugs.webkit.org/show_bug.cgi?id=122067
Reviewed by Darin Adler.
Add a convenient way for getting the first and last element if it exists.
Use it in some places.
* accessibility/AccessibilityNodeObject.cpp:
(WebCore::AccessibilityNodeObject::canvasHasFallbackContent):
* css/CSSFontFaceSource.cpp:
(WebCore::CSSFontFaceSource::getFontData):
* dom/Document.cpp:
(WebCore::Document::childrenChanged):
(WebCore::Document::removeTitle):
* dom/ElementChildIterator.h:
(WebCore::::first):
(WebCore::::last):
* dom/ElementDescendantIterator.h:
(WebCore::::first):
(WebCore::::last):
* html/HTMLFieldSetElement.cpp:
(WebCore::HTMLFieldSetElement::legend):
* html/HTMLLegendElement.cpp:
(WebCore::HTMLLegendElement::associatedControl):
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::finishParsingChildren):
(WebCore::HTMLMediaElement::selectMediaResource):
* svg/SVGElement.cpp:
(WebCore::SVGElement::title):
* svg/SVGFontFaceElement.cpp:
(WebCore::SVGFontFaceElement::rebuildFontFace):
* svg/graphics/SVGImage.cpp:
(WebCore::SVGImage::hasSingleSecurityOrigin):
2013-09-28 Mark Rowe <mrowe@apple.com>
Fix some failures with newer versions of clang.
......@@ -423,7 +423,7 @@ bool AccessibilityNodeObject::canvasHasFallbackContent() const
// If it has any children that are elements, we'll assume it might be fallback
// content. If it has no children or its only children are not elements
// (e.g. just text nodes), it doesn't have fallback content.
return elementChildren(canvasElement).begin() != elementChildren(canvasElement).end();
return elementChildren(canvasElement).first();
}
bool AccessibilityNodeObject::isImageButton() const
......
......@@ -139,16 +139,14 @@ PassRefPtr<SimpleFontData> CSSFontFaceSource::getFontData(const FontDescription&
if (!m_externalSVGFontElement)
return 0;
auto fontFaceChildren = childrenOfType<SVGFontFaceElement>(m_externalSVGFontElement.get());
auto firstFontFace = fontFaceChildren.begin();
if (firstFontFace != fontFaceChildren.end()) {
if (auto firstFontFace = childrenOfType<SVGFontFaceElement>(m_externalSVGFontElement.get()).first()) {
if (!m_svgFontFaceElement) {
// We're created using a CSS @font-face rule, that means we're not associated with a SVGFontFaceElement.
// Use the imported <font-face> tag as referencing font-face element for these cases.
m_svgFontFaceElement = &*firstFontFace;
m_svgFontFaceElement = firstFontFace;
}
fontData = SimpleFontData::create(SVGFontData::create(&*firstFontFace), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic);
fontData = SimpleFontData::create(SVGFontData::create(firstFontFace), fontDescription.computedPixelSize(), syntheticBold, syntheticItalic);
}
} else
#endif
......
......@@ -794,10 +794,7 @@ void Document::childrenChanged(const ChildChange& change)
}
#endif
Element* newDocumentElement = 0;
auto firstElementChild = elementChildren(this).begin();
if (firstElementChild != elementChildren(this).end())
newDocumentElement = &*firstElementChild;
Element* newDocumentElement = elementChildren(this).first();
if (newDocumentElement == m_documentElement)
return;
......@@ -1586,9 +1583,8 @@ void Document::removeTitle(Element* titleElement)
// Update title based on first title element in the head, if one exists.
if (HTMLElement* headElement = head()) {
auto firstTitle = childrenOfType<HTMLTitleElement>(headElement).begin();
if (firstTitle != childrenOfType<HTMLTitleElement>(headElement).end())
setTitleElement(firstTitle->textWithDirection(), &*firstTitle);
if (auto firstTitle = childrenOfType<HTMLTitleElement>(headElement).first())
setTitleElement(firstTitle->textWithDirection(), firstTitle);
}
if (!m_titleElement)
......
......@@ -52,6 +52,8 @@ public:
ElementChildIteratorAdapter(ContainerNode* root);
ElementChildIterator<ElementType> begin();
ElementChildIterator<ElementType> end();
ElementType* first();
ElementType* last();
private:
const ContainerNode* m_root;
......@@ -63,6 +65,8 @@ public:
ElementChildConstIteratorAdapter(const ContainerNode* root);
ElementChildConstIterator<ElementType> begin() const;
ElementChildConstIterator<ElementType> end() const;
const ElementType* first() const;
const ElementType* last() const;
private:
const ContainerNode* m_root;
......@@ -133,6 +137,18 @@ inline ElementChildIterator<ElementType> ElementChildIteratorAdapter<ElementType
return ElementChildIterator<ElementType>(m_root);
}
template <typename ElementType>
inline ElementType* ElementChildIteratorAdapter<ElementType>::first()
{
return Traversal<ElementType>::firstChild(m_root);
}
template <typename ElementType>
inline ElementType* ElementChildIteratorAdapter<ElementType>::last()
{
return Traversal<ElementType>::lastChild(m_root);
}
// ElementChildConstIteratorAdapter
template <typename ElementType>
......@@ -153,6 +169,18 @@ inline ElementChildConstIterator<ElementType> ElementChildConstIteratorAdapter<E
return ElementChildConstIterator<ElementType>(m_root);
}
template <typename ElementType>
inline const ElementType* ElementChildConstIteratorAdapter<ElementType>::first() const
{
return Traversal<ElementType>::firstChild(m_root);
}
template <typename ElementType>
inline const ElementType* ElementChildConstIteratorAdapter<ElementType>::last() const
{
return Traversal<ElementType>::lastChild(m_root);
}
// Standalone functions
inline ElementChildIteratorAdapter<Element> elementChildren(ContainerNode* root)
......
......@@ -52,6 +52,8 @@ public:
ElementDescendantIteratorAdapter(ContainerNode* root);
ElementDescendantIterator<ElementType> begin();
ElementDescendantIterator<ElementType> end();
ElementType* first();
ElementType* last();
private:
ContainerNode* m_root;
......@@ -63,6 +65,8 @@ public:
ElementDescendantConstIteratorAdapter(const ContainerNode* root);
ElementDescendantConstIterator<ElementType> begin() const;
ElementDescendantConstIterator<ElementType> end() const;
const ElementType* first() const;
const ElementType* last() const;
private:
const ContainerNode* m_root;
......@@ -134,6 +138,18 @@ inline ElementDescendantIterator<ElementType> ElementDescendantIteratorAdapter<E
return ElementDescendantIterator<ElementType>(m_root);
}
template <typename ElementType>
inline ElementType* ElementDescendantIteratorAdapter<ElementType>::first()
{
return Traversal<ElementType>::firstWithin(m_root);
}
template <typename ElementType>
inline ElementType* ElementDescendantIteratorAdapter<ElementType>::last()
{
return Traversal<ElementType>::lastWithin(m_root);
}
// ElementDescendantConstIteratorAdapter
template <typename ElementType>
......@@ -154,6 +170,18 @@ inline ElementDescendantConstIterator<ElementType> ElementDescendantConstIterato
return ElementDescendantConstIterator<ElementType>(m_root);
}
template <typename ElementType>
inline const ElementType* ElementDescendantConstIteratorAdapter<ElementType>::first() const
{
return Traversal<ElementType>::firstWithin(m_root);
}
template <typename ElementType>
inline const ElementType* ElementDescendantConstIteratorAdapter<ElementType>::last() const
{
return Traversal<ElementType>::lastWithin(m_root);
}
// Standalone functions
inline ElementDescendantIteratorAdapter<Element> elementDescendants(ContainerNode* root)
......
......@@ -90,11 +90,7 @@ RenderElement* HTMLFieldSetElement::createRenderer(RenderArena& arena, RenderSty
const HTMLLegendElement* HTMLFieldSetElement::legend() const
{
auto legendDescendants = descendantsOfType<HTMLLegendElement>(this);
auto firstLegend = legendDescendants.begin();
if (firstLegend != legendDescendants.end())
return &*firstLegend;
return nullptr;
return descendantsOfType<HTMLLegendElement>(this).first();
}
PassRefPtr<HTMLCollection> HTMLFieldSetElement::elements()
......
......@@ -56,9 +56,7 @@ HTMLFormControlElement* HTMLLegendElement::associatedControl()
// Find first form element inside the fieldset that is not a legend element.
// FIXME: Should we consider tabindex?
auto fieldsetFormControlDescendants = descendantsOfType<HTMLFormControlElement>(&*enclosingFieldset);
auto firstFormControl = fieldsetFormControlDescendants.begin();
return firstFormControl != fieldsetFormControlDescendants.end() ? &*firstFormControl : nullptr;
return descendantsOfType<HTMLFormControlElement>(&*enclosingFieldset).first();
}
void HTMLLegendElement::focus(bool, FocusDirection direction)
......
......@@ -548,8 +548,7 @@ void HTMLMediaElement::finishParsingChildren()
if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
return;
auto trackDescendants = descendantsOfType<HTMLTrackElement>(this);
if (trackDescendants.begin() != trackDescendants.end())
if (descendantsOfType<HTMLTrackElement>(this).first())
scheduleDelayedAction(ConfigureTextTracks);
#endif
}
......@@ -939,10 +938,9 @@ void HTMLMediaElement::selectMediaResource()
// Otherwise, if the media element does not have a src attribute but has a source
// element child, then let mode be children and let candidate be the first such
// source element child in tree order.
auto source = childrenOfType<HTMLSourceElement>(this).begin();
if (source != childrenOfType<HTMLSourceElement>(this).end()) {
if (auto firstSource = childrenOfType<HTMLSourceElement>(this).first()) {
mode = children;
m_nextChildNodeToConsider = &*source;
m_nextChildNodeToConsider = firstSource;
m_currentSourceNode = 0;
} else {
// Otherwise the media element has neither a src attribute nor a source element
......
......@@ -953,14 +953,8 @@ String SVGElement::title() const
// If we aren't an instance in a <use> or the <use> title was not found, then find the first
// <title> child of this element.
// If a title child was found, return the text contents.
auto titleDescendants = descendantsOfType<SVGTitleElement>(this);
auto firstTitle = titleDescendants.begin();
if (firstTitle != titleDescendants.end())
return const_cast<SVGTitleElement&>(*firstTitle).innerText();
// Otherwise return a null/empty string.
return String();
auto firstTitle = descendantsOfType<SVGTitleElement>(this).first();
return firstTitle ? const_cast<SVGTitleElement*>(firstTitle)->innerText() : String();
}
bool SVGElement::rendererIsNeeded(const RenderStyle& style)
......
......@@ -228,10 +228,7 @@ void SVGFontFaceElement::rebuildFontFace()
}
// we currently ignore all but the first src element, alternatively we could concat them
SVGFontFaceSrcElement* srcElement = 0;
auto firstFontFaceSrcElementChild = childrenOfType<SVGFontFaceSrcElement>(this).begin();
if (firstFontFaceSrcElementChild != childrenOfType<SVGFontFaceSrcElement>(this).end())
srcElement = &*firstFontFaceSrcElementChild;
auto srcElement = childrenOfType<SVGFontFaceSrcElement>(this).first();
bool describesParentFont = isSVGFontElement(parentNode());
RefPtr<CSSValueList> list;
......
......@@ -75,8 +75,7 @@ bool SVGImage::hasSingleSecurityOrigin() const
return true;
// Don't allow foreignObject elements since they can leak information with arbitrary HTML (like spellcheck or control theme).
auto foreignObjectDescendants = descendantsOfType<SVGForeignObjectElement>(rootElement);
if (foreignObjectDescendants.begin() != foreignObjectDescendants.end())
if (descendantsOfType<SVGForeignObjectElement>(rootElement).first())
return false;
// Because SVG image rendering disallows external resources and links,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment