]> git.phdru.name Git - bookmarks_db.git/blobdiff - parse_html/bkmk_ph_html5.py
Fix(parse_html): Do not parse empty strings
[bookmarks_db.git] / parse_html / bkmk_ph_html5.py
index 6d0d38094bea6bd8935b4ddb29455ca458cd7cbf..d973b729976e59f4225c09de66c1fdfdf689e9f8 100644 (file)
@@ -5,7 +5,7 @@ This file is a part of Bookmarks database and Internet robot.
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 2010-2014 PhiloSoft Design"
+__copyright__ = "Copyright (C) 2010-2023 PhiloSoft Design"
 __license__ = "GNU GPL"
 
 __all__ = ['parse_html']
@@ -16,8 +16,14 @@ from .bkmk_ph_util import HTMLParser
 
 
 def parse_html(html_text, charset=None, log=None):
+    if not html_text:
+        return None
     parser = HTML5Parser()
-    html_tree = parser.parse(html_text, encoding=charset, parseMeta=bool(charset))
+    if isinstance(html_text, bytes):
+        html_tree = parser.parse(
+            html_text, encoding=charset, parseMeta=bool(charset))
+    else:
+        html_tree = parser.parse(html_text)
 
     html = None
     if hasattr(html_tree, 'childNodes'):
@@ -59,7 +65,8 @@ def parse_html(html_text, charset=None, log=None):
                 if meta_content:
                     try:
                         meta_charset = \
-                            meta_content.lower().split('charset=')[1].split(';')[0]
+                            meta_content.lower().split('charset=')[1].\
+                            split(';')[0]
                     except IndexError:
                         meta_charset = False
                     else:
@@ -71,8 +78,8 @@ def parse_html(html_text, charset=None, log=None):
         if not charset:
             charset = parser.tokenizer.stream.charEncoding[0]
 
-        if title and (charset or meta_charset):
-            title = title.encode(charset or meta_charset)
+        #if title and (charset or meta_charset):
+        #    title = title.encode(charset or meta_charset)
 
         for node in head.childNodes:
             if node.name == 'meta' and \