From 5018274b34cd23925904e38b4a1ece4af3da47a2 Mon Sep 17 00:00:00 2001 From: Daniel Espinosa Date: Wed, 24 Jun 2020 20:39:01 -0500 Subject: [PATCH] HTML: implement a GXml.Element basic HtmlDocument Fixes issue #10 --- gxml/HTMLCollection.vala | 4 +- gxml/HtmlDocument.vala | 45 ++ gxml/meson.build | 1 + test/HtmlDocumentTest.vala | 1529 ++++++++++++++++++++++++++++++++++++ test/meson.build | 14 + 5 files changed, 1591 insertions(+), 2 deletions(-) create mode 100644 gxml/HtmlDocument.vala create mode 100644 test/HtmlDocumentTest.vala diff --git a/gxml/HTMLCollection.vala b/gxml/HTMLCollection.vala index 3cb313c8..5004dba7 100644 --- a/gxml/HTMLCollection.vala +++ b/gxml/HTMLCollection.vala @@ -1,7 +1,7 @@ /* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */ /* GXmlDomCollections.vala * - * Copyright (C) 2016 Daniel Espinosa + * Copyright (C) 2016-2020 Daniel Espinosa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,7 +23,7 @@ using Gee; /** - * DOM4 HTML Collection, powered by libxml2 library. + * DOM4 HTML Collection */ public class GXml.HTMLCollection : Gee.ArrayList, GXml.DomHTMLCollection diff --git a/gxml/HtmlDocument.vala b/gxml/HtmlDocument.vala new file mode 100644 index 00000000..4ca7cbab --- /dev/null +++ b/gxml/HtmlDocument.vala @@ -0,0 +1,45 @@ +/* HtmlDocument.vala + * + * Copyright (C) 2020 Daniel Espinosa + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + * Authors: + * Daniel Espinosa + */ + +using Gee; + +/** + * DOM4 HTML Document + */ +public class GXml.HtmlDocument : GXml.Document +{ + [Description (nick="::ROOT")] + public GXml.DomElement html { get; set; } + construct { + html = (DomElement) GLib.Object.new (typeof (HtmlElement), "owner-document", this); + } +} + +public class GXml.HtmlElement : GXml.Element +{ + construct { + try { + initialize ("html"); + } catch (GLib.Error e) { + warning ("Error: %s", e.message); + } + } +} diff --git a/gxml/meson.build b/gxml/meson.build index b87d5886..c902cb7b 100644 --- a/gxml/meson.build +++ b/gxml/meson.build @@ -60,6 +60,7 @@ valasources = files ([ 'HashPairedMap.vala', 'HashThreeMap.vala', 'HTMLCollection.vala', + 'HtmlDocument.vala', 'IXsdSchema.vala', 'LXPathObject.vala', 'Node.vala', diff --git a/test/HtmlDocumentTest.vala b/test/HtmlDocumentTest.vala new file mode 100644 index 00000000..3eed57b0 --- /dev/null +++ b/test/HtmlDocumentTest.vala @@ -0,0 +1,1529 @@ +/* HtmlDocumentTest.vala + * + * Copyright (C) 2011-2013 Richard Schwarting + * Copyright (C) 2011-2015 Daniel Espinosa + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + * + * Authors: + * Richard Schwarting + * Daniel Espinosa + */ + +using GXml; + +public class XHtmlDocumentTest : GLib.Object { + public static int main (string[] args) + { + Test.init (ref args); + Test.add_func ("/gxml/HtmlDocument/api/element_id", () => { + try { + var doc = new XHtmlDocument.from_path (GXmlTestConfig.TEST_DIR+"/index.html"); + Test.message ("Checking root element..."); + assert (doc.document_element != null); + assert (doc.document_element.node_name.down () == "html".down ()); + Test.message ("Searching for elemento with id 'user'..."); + var n = doc.get_element_by_id ("user"); + assert (n != null); + assert (n.node_name == "p"); + assert (n is GXml.DomElement); + message (((GXml.DomElement) n).text_content); + assert (((GXml.DomElement) n).node_value == ""); + } catch (GLib.Error e){ + Test.message ("ERROR: "+e.message); + assert_not_reached (); + } + }); + Test.add_func ("/gxml/HtmlDocument/api/element_class", () => { + try { + var doc = new XHtmlDocument.from_path (GXmlTestConfig.TEST_DIR+"/index.html"); + Test.message ("Checking root element..."); + assert (doc.document_element != null); + assert (doc.document_element.node_name.down () == "html".down ()); + Test.message ("Searching for element with property class and value app..."); + var np = doc.document_element.get_elements_by_property_value ("class","app"); + assert (np != null); + assert (np.size == 2); + Test.message ("Searching for elemento with class 'app'..."); + var l = doc.get_elements_by_class_name ("app"); + assert (l != null); + assert (l.size == 2); + bool fdiv, fp; + fdiv = fp = false; + foreach (GXml.DomElement e in l) { + if (e.node_name == "div") fdiv = true; + if (e.node_name == "p") fp = true; + } + assert (fdiv); + assert (fp); + } catch (GLib.Error e){ + Test.message ("ERROR: "+e.message); + assert_not_reached (); + } + }); + Test.add_func ("/gxml/HtmlDocument/fom_string_doc", () => { + try { + var sdoc = " + + + + + + + + +"; + var doc = new XHtmlDocument.from_string_doc (sdoc); + assert (doc.document_element != null); + assert (doc.document_element.node_name.down () == "html".down ()); + var ln = doc.document_element.get_elements_by_property_value ("type","text/javascript"); + assert (ln != null); + assert (ln.size == 1); + var np = ln.item (0); + assert (np != null); + assert (np.node_name == "script"); + var l = doc.get_elements_by_tag_name ("style"); + assert (l != null); + assert (l.size == 1); + var sn = l.item (0); + assert (sn != null); + assert (sn.node_name == "style"); + message (sn.child_nodes.length.to_string ()); + assert (sn.child_nodes.length == 1); + message (doc.to_html ()); + var s = doc.to_html (); + message (s); + assert ("style>\n * { color: red; }\n " in s); + } catch (GLib.Error e){ + Test.message ("ERROR: "+e.message); + assert_not_reached (); + } + }); + // Test.add_func ("/gxml/HtmlDocument/uri", () => { + // try { + // var f = GLib.File.new_for_uri ("http://www.omgubuntu.co.uk/2017/05/kde-neon-5-10-available-download-comes-plasma-5-10"); + // DomDocument doc; + // doc = new XHtmlDocument.from_uri ("http://www.omgubuntu.co.uk/2017/05/kde-neon-5-10-available-download-comes-plasma-5-10"); + // message ((doc as GDocument).to_string ()); + // } catch (GLib.Error e){ + // message ("ERROR: "+e.message); + // assert_not_reached (); + // } + // }); + Test.add_func ("/gxml/HtmlDocument/element-by-property", () => { + var src = """ + + + + +Climat : la Chine est-elle le nouveau leader ? - France 24 + +
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+ + + + + + + +   + + +
+ +
+

Rendez-vous

+
+
+ +
+

Rejouer

+
    + +
    +
    + +
    +
    +
    +
    +
    +

    +

    + +
    +
    +

    LES DERNIÈRES ÉMISSIONS

    +
    +
    +
    +
    +

    MODE

    +

    La mode en 2017 : féminisme, business et tabous à briser

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    REVUE DE PRESSE

    +

    Trump pour "une expansion massive des forages pétroliers offshore"

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    REVUE DE PRESSE

    +

    Erdogan en visite à Paris : "Le déjeuner qui fait grincer"

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    LE JOURNAL DE L'ÉCO

    +

    Erdogan à Paris, une visite aux accents économiques

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    LE JOURNAL DE L’AFRIQUE

    +

    L'Égyptien Mohamed Salah sacré Joueur africain de l'année

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    UN ŒIL SUR LES MÉDIAS

    +

    Emmanuel Macron déclare la guerre aux "Fake News"

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    LE DÉBAT

    +

    Diplomatie française : Emmanuel Macron, changement de style ou changement de fond ? (partie 2)

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    LE DÉBAT

    +

    Diplomatie française : Emmanuel Macron, changement de style ou changement de fond ? (partie 1)

    +
    +

    + En savoir plus +

    +
    +
    +
    +
    +
    +

    FOCUS

    +

    Le Rwanda en plein "réveil" évangélique

    +
    +

    + En savoir plus +

    +
    + +
    +
    + + +
    + +
    +
    +
    +
    + +
    +
    + + +
    + +
    +
    + ÉLÉMENT TERRE
    +
    +

    Ouvrons les yeux sur le monde qui nous entoure! Aidés par une équipe de graphistes, nous dépoussiérons et décortiquons les grands thèmes qui font l’environnement aujourd’hui. Le samedi à 20h20. Et dès le vendredi en avant-première sur internet!

    +
    +
    +
    +
    + +

    Dernière modification : 20/10/2017

    +

    Climat : la Chine est-elle le nouveau leader ?

    + +
    + + +
      + + + + + + +