LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
domchildrenrange.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "domchildrenrange.h"
10 #include <QVector>
11 
12 namespace LC::Util
13 {
14  namespace
15  {
16  void DomDescendants (const QDomElement& parent, const QString& tag, QVector<QDomElement>& result)
17  {
18  for (const auto& elem : DomChildren (parent, {}))
19  {
20  if (elem.tagName () == tag)
21  result << elem;
22 
23  DomDescendants (elem, tag, result);
24  }
25  }
26  }
27 
28  QVector<QDomElement> DomDescendants (const QDomElement& parent, const QString& tag)
29  {
30  const auto& allElems = parent.elementsByTagName (tag);
31  const auto elemsCount = allElems.size ();
32 
33  QVector<QDomElement> result;
34  result.reserve (elemsCount);
35 
36  // QDomNodeList operations are slower than explicit recursion for bigger lists,
37  // and this is a somewhat empirical threshold.
38  constexpr auto countThreshold = 200;
39 
40  if (elemsCount < countThreshold)
41  for (int i = 0; i < allElems.size (); ++i)
42  result << allElems.at (i).toElement ();
43  else
44  DomDescendants (parent, tag, result);
45 
46  return result;
47  }
48 }
auto DomChildren(const QDomNode &parent, const QString &tag)
Creates a range iterating over direct children named tag.
QVector< QDomElement > DomDescendants(const QDomElement &parent, const QString &tag)
Creates a vector with all descendants of parent named tag.