Warning, file /pim/kitinerary/src/lib/htmldocument.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kitinerary_export.h"
0010 
0011 #include <QObject>
0012 
0013 #include <memory>
0014 
0015 struct _xmlNode;
0016 
0017 namespace KItinerary {
0018 
0019 class HtmlDocument;
0020 class HtmlDocumentPrivate;
0021 
0022 /** HTML document element. */
0023 class KITINERARY_EXPORT HtmlElement
0024 {
0025     Q_GADGET
0026     Q_PROPERTY(bool isNull READ isNull)
0027     Q_PROPERTY(QString name READ name)
0028     Q_PROPERTY(KItinerary::HtmlElement parent READ parent)
0029     Q_PROPERTY(KItinerary::HtmlElement firstChild READ firstChild)
0030     Q_PROPERTY(KItinerary::HtmlElement nextSibling READ nextSibling)
0031     Q_PROPERTY(QString content READ content)
0032     Q_PROPERTY(QString recursiveContent READ recursiveContent)
0033 public:
0034     HtmlElement();
0035     ~HtmlElement();
0036 
0037     /** Check if the element is null. */
0038     bool isNull() const;
0039     /** The element name. */
0040     QString name() const;
0041     /** Value of the attribute @p attr. */
0042     Q_INVOKABLE QString attribute(const QString &attr) const;
0043     /** Returns the parent element of this node. */
0044     HtmlElement parent() const;
0045     /** Returns the first child element of this node. */
0046     HtmlElement firstChild() const;
0047     /** Returns the next sibling element of this node. */
0048     HtmlElement nextSibling() const;
0049     /** Returns the content of this element.
0050      *  That is, all text nodes that are immediate children of this element.
0051      *  The content is trimmed from leading or trailing whitespaces.
0052      */
0053     QString content() const;
0054     /** Returns the content of this element and all its children. */
0055     QString recursiveContent() const;
0056     /** Checks whether an attribute with name @p attr exists. */
0057     bool hasAttribute(const QString &attr) const;
0058     /** Returns the list of all attributes of this node. */
0059     QStringList attributes() const;
0060 
0061     /** Evaluate an XPath expression relative to this node. */
0062     Q_INVOKABLE QVariant eval(const QString &xpath) const;
0063 
0064     /** Checks if two HtmlElement instances refer to the same DOM node. */
0065     bool operator==(const HtmlElement &other) const;
0066 
0067 private:
0068     friend class HtmlDocument;
0069     HtmlElement(_xmlNode *dd);
0070     _xmlNode *d;
0071 };
0072 
0073 /** HTML document for extraction.
0074  *  This is used as input for ExtractorEngine and the JS extractor scripts.
0075  *  @note This class is only functional if libxml is available as a dependency,
0076  *  otherwise all methods return empty values.
0077  */
0078 class KITINERARY_EXPORT HtmlDocument : public QObject
0079 {
0080     Q_OBJECT
0081     Q_PROPERTY(KItinerary::HtmlElement root READ root)
0082     Q_PROPERTY(QString rawData READ rawData CONSTANT)
0083 public:
0084     ~HtmlDocument();
0085 
0086     /** Creates a HtmlDocument from the given raw data.
0087      *  @returns @c nullptr if loading fails or libxml was not found.
0088      */
0089     static HtmlDocument* fromData(const QByteArray &data, QObject *parent = nullptr);
0090     /** Creates a HtmlDocument from a given (unicode) string.
0091      *  @returns @c nullptr if loading fails or libxml was not found.
0092      */
0093     static HtmlDocument* fromString(const QString &data, QObject *parent = nullptr);
0094 
0095     /** Returns the root element of the document. */
0096     HtmlElement root() const;
0097 
0098     /** Returns the raw textual HTML data. */
0099     QString rawData() const;
0100 
0101     /** Evaluate an XPath expression relative to the document root. */
0102     Q_INVOKABLE QVariant eval(const QString &xpath) const;
0103 
0104 private:
0105     explicit HtmlDocument(QObject *parent = nullptr);
0106     std::unique_ptr<HtmlDocumentPrivate> d;
0107 };
0108 
0109 }
0110 
0111 Q_DECLARE_METATYPE(KItinerary::HtmlElement)
0112