[gxml] XHtmlDocument: fix memory leaks



commit f12995fbf9dc05e65f26b0b2deb48b06fc4cf9ef
Author: Daniel Espinosa <esodan gmail com>
Date:   Sun Feb 6 14:07:36 2022 -0600

    XHtmlDocument: fix memory leaks

 gxml/XHtmlDocument.vala     | 27 ++++++++++++++++++++---
 test/XHtmlDocumentTest.vala | 54 ++++++++++++++++++++++++++++++++++++---------
 2 files changed, 67 insertions(+), 14 deletions(-)
---
diff --git a/gxml/XHtmlDocument.vala b/gxml/XHtmlDocument.vala
index 8d41b3e..884556f 100644
--- a/gxml/XHtmlDocument.vala
+++ b/gxml/XHtmlDocument.vala
@@ -53,9 +53,9 @@ namespace GXml {
                 * your document content or source.
                 */
                public XHtmlDocument.from_file (File file, int options = 0, Cancellable? cancel = null) 
throws GLib.Error {
-                       var ostream = new MemoryOutputStream.resizable ();
-                       ostream.splice (file.read (), GLib.OutputStreamSpliceFlags.CLOSE_SOURCE, cancel);
-                       this.from_string ((string) ostream.data, options);
+                       var istream = new GLib.DataInputStream (file.read ());
+                       string text = istream.read_upto ("\0", -1, null);
+                       this.from_string (text, options);
                }
                /**
                 * This method parse strings using {@link Html.Doc.read_memory} method.
@@ -82,11 +82,21 @@ namespace GXml {
                }
                // DomHtmlDocument implementation
                public void read_from_string (string str) {
+                       if (this.doc != null) {
+                               delete this.doc;
+                               this.doc = null;
+                       }
+
                        this.doc = Html.Doc.read_memory ((char[]) str, str.length, "", null, 0);
                }
 
                public void read_from_string_tolerant (string str) throws GLib.Error {
                        Html.ParserCtxt ctx = new Html.ParserCtxt ();
+                       if (this.doc != null) {
+                               delete this.doc;
+                               this.doc = null;
+                       }
+
                        this.doc = ctx.read_memory ((char[]) str, str.length, "", null, 0);
                }
                public string to_html () throws GLib.Error {
@@ -96,6 +106,17 @@ namespace GXml {
                        message (len.to_string ());
                        return buffer.dup ();
                }
+
+               public override string to_string () {
+                       string t = "";
+                       try {
+                               t = to_html ();
+                       } catch (GLib.Error e) {
+                               warning (_("Error while converting HTML document to string: %s"), e.message);
+                       }
+
+                       return t;
+               } 
                /**
                 * Search all {@link GXml.Element} with a property called "class" and with a
                 * value as a class apply to a node.
diff --git a/test/XHtmlDocumentTest.vala b/test/XHtmlDocumentTest.vala
index 58e284f..2832567 100644
--- a/test/XHtmlDocumentTest.vala
+++ b/test/XHtmlDocumentTest.vala
@@ -113,17 +113,49 @@ class XHtmlDocumentTest : GLib.Object {
                                assert_not_reached ();
                        }
                });
-               // Test.add_func ("/gxml/XHtmlDocument/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/XHtmlDocument/new", () => {
+                       try {
+                               DomDocument doc = new XHtmlDocument ();
+                var e = doc.create_element ("body");
+                doc.append_child (e);
+                               message (((XDocument) doc).to_string ());
+                       } catch (GLib.Error e){
+                               warning ("ERROR: "+e.message);
+                       }
+               });
+               Test.add_func ("/gxml/XHtmlDocument/read_from_string", () => {
+                       try {
+                               DomDocument doc = new XHtmlDocument ();
+                doc.read_from_string ("<html><body/></html>");
+                               message (((XDocument) doc).to_string ());
+                       } catch (GLib.Error e){
+                               warning ("ERROR: "+e.message);
+                       }
+               });
+               Test.add_func ("/gxml/XHtmlDocument/read_from_string_tolerant", () => {
+                       try {
+                               XHtmlDocument doc = new XHtmlDocument ();
+                doc.read_from_string_tolerant ("<html><body/></html>");
+                               message (((XDocument) doc).to_string ());
+                       } catch (GLib.Error e){
+                               warning ("ERROR: "+e.message);
+                       }
+               });
+               Test.add_func ("/gxml/XHtmlDocument/uri", () => {
+                       try {
+                NetworkMonitor monitor = NetworkMonitor.get_default ();
+                if (!monitor.network_available) {
+                    message ("Network not available, abortind test");
+                    return;
+                }
+                               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 (((XDocument) doc).to_string ());
+                       } catch (GLib.Error e){
+                               message ("ERROR: "+e.message);
+                               assert_not_reached ();
+                       }
+               });
                Test.add_func ("/gxml/XHtmlDocument/element-by-property", () => {
                        var src = """
 <!--[if lt IE 7]>      <html dir="ltr" lang="fr" data-locale="fr" data-locale-long="fr_FR" 
data-locale-name="French (France)" data-locale-facebook="fr_FR" data-locale-twitter="fr" 
data-locale-google="fr" data-locale-linkedin="fr_FR" class="no-js lt-ie9 lt-ie8 lt-ie7 "> <![endif]-->


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]