2021-03-19 02:20:20 +00:00
|
|
|
global html
|
2021-03-20 17:39:54 +00:00
|
|
|
import html.parser
|
2021-03-19 02:20:20 +00:00
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
global browserDoc
|
|
|
|
class browserDoc(QTextBrowser):
|
|
|
|
def __init__(self,*args,**kwargs):
|
|
|
|
super().__init__(*args,**kwargs)
|
|
|
|
self.setOpenLinks(False)
|
2021-03-18 18:17:14 +00:00
|
|
|
style = ""
|
|
|
|
if (config["accessibility"]["colorBackground"] != "default"):
|
|
|
|
style += "background-color: " +config["accessibility"]["colorBackground"] + ";"
|
|
|
|
|
|
|
|
if (config["accessibility"]["colorText"] != "default"):
|
|
|
|
style += "color: " +config["accessibility"]["colorText"]+ ";"
|
|
|
|
|
|
|
|
self.setStyleSheet("QTextBrowser { " +style+ " }")
|
2021-03-18 15:22:56 +00:00
|
|
|
self.anchorClicked.connect(self.cAnchorClicked)
|
|
|
|
|
2021-03-20 17:39:54 +00:00
|
|
|
class cHtmlParser(html.parser.HTMLParser):
|
2021-03-19 02:20:20 +00:00
|
|
|
def __init__(self):
|
2021-03-20 17:39:54 +00:00
|
|
|
html.parser.HTMLParser.__init__(self)
|
2021-03-19 02:20:20 +00:00
|
|
|
self.output = ""
|
2021-03-19 18:14:02 +00:00
|
|
|
self.voidElements = ["area","base","br","col","hr","img","input","link","meta","param","command","keygen","source"]
|
|
|
|
self.blackList = ["img","script","style"]
|
|
|
|
self.tagDir = []
|
2021-03-19 02:20:20 +00:00
|
|
|
|
|
|
|
def handle_starttag(self,tag,attrs):
|
2021-03-19 18:14:02 +00:00
|
|
|
if not tag in self.voidElements:
|
|
|
|
self.tagDir.append(tag)
|
|
|
|
|
|
|
|
if tag in self.blackList: return
|
2021-03-19 02:20:20 +00:00
|
|
|
|
2021-03-19 18:14:02 +00:00
|
|
|
self.output += "<" +html.escape(tag)
|
2021-03-19 02:20:20 +00:00
|
|
|
for attr in attrs:
|
2021-03-19 18:14:02 +00:00
|
|
|
self.output += " " +html.escape(attr[0])
|
2021-03-19 02:24:36 +00:00
|
|
|
if attr[1] != None:
|
2021-03-19 18:14:02 +00:00
|
|
|
self.output += '="' +html.escape(attr[1])+ '"'
|
2021-03-19 02:20:20 +00:00
|
|
|
self.output += ">"
|
|
|
|
|
|
|
|
def handle_endtag(self,tag):
|
2021-03-19 18:14:02 +00:00
|
|
|
if not tag in self.voidElements:
|
|
|
|
index = len(self.tagDir) - 1
|
|
|
|
while index >= 0:
|
|
|
|
if self.tagDir[index] == tag:
|
|
|
|
self.tagDir = self.tagDir[index + 1:]
|
|
|
|
break
|
|
|
|
index -= 1
|
2021-03-19 02:20:20 +00:00
|
|
|
self.output += "</" +html.escape(tag)+ ">"
|
|
|
|
|
|
|
|
def handle_data(self,data):
|
2021-03-19 18:14:02 +00:00
|
|
|
curTag = ""
|
|
|
|
if len(self.tagDir) > 0:
|
|
|
|
curTag = self.tagDir[-1]
|
|
|
|
if curTag in self.blackList: return
|
2021-03-25 12:14:37 +00:00
|
|
|
self.output += html.escape(data)
|
2021-03-19 02:20:20 +00:00
|
|
|
|
2021-03-25 19:34:16 +00:00
|
|
|
def cRenderHtml(self,htm,contentType):
|
|
|
|
if contentType != "text/html":
|
|
|
|
self.clear()
|
|
|
|
self.insertHtml("<html><body><pre>" +html.escape(htm)+ "</pre></body></html>")
|
|
|
|
self.update()
|
|
|
|
return
|
|
|
|
|
2021-03-19 02:20:20 +00:00
|
|
|
parser = self.cHtmlParser()
|
|
|
|
parser.feed(htm)
|
|
|
|
|
2021-03-18 15:22:56 +00:00
|
|
|
self.clear()
|
2021-03-19 02:20:20 +00:00
|
|
|
self.insertHtml(parser.output)
|
2021-03-18 15:22:56 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def cAnchorClicked(self,url):
|
|
|
|
url = str(url.toEncoded(),"utf-8")
|
|
|
|
browserWindow = self.parent()
|
|
|
|
curUrl = browserWindow.cDocumentInfo["url"]
|
|
|
|
curUrlParsed = parseUrl(curUrl)
|
|
|
|
urlParsed = parseUrl(url)
|
2021-03-19 02:20:20 +00:00
|
|
|
print("---")
|
|
|
|
print("navigating from: " +curUrl)
|
|
|
|
print("to: " +url)
|
2021-03-18 15:22:56 +00:00
|
|
|
if urlParsed["protocol"] == "": # is relative
|
|
|
|
if url[0] == "#":
|
|
|
|
curUrlParsed["anchor"] = url[1:]
|
|
|
|
url = unparseUrl(curUrlParsed)
|
|
|
|
elif url[:2] == "//":
|
|
|
|
url = curUrlParsed["protocol"] + ":" + url
|
|
|
|
elif url[0] == "/":
|
|
|
|
urlParsed["protocol"] = curUrlParsed["protocol"]
|
|
|
|
urlParsed["domain"] = curUrlParsed["domain"]
|
|
|
|
urlParsed["path"] = urlParsed["path"][1:]
|
|
|
|
url = unparseUrl(urlParsed)
|
|
|
|
else:
|
|
|
|
urlParsed["protocol"] = curUrlParsed["protocol"]
|
|
|
|
urlParsed["domain"] = curUrlParsed["domain"]
|
|
|
|
while len(curUrlParsed["path"]) > 0 and curUrlParsed["path"][-1] == "/": curUrlParsed["path"] = curUrlParsed["path"][:-1]
|
|
|
|
urlParsed["path"] = curUrlParsed["path"] + "/" + url
|
|
|
|
url = unparseUrl(urlParsed)
|
|
|
|
browserWindow.cUrlBar.setText(url)
|
|
|
|
browserWindow.cNavigate()
|