So I'm creating a small program using Oython / GTK+, at it's core is a WebView which displays some HTML-page. I now want to allow the user to manipulate the view by highlighting text, underline text etc. Later I want my program to store this markup in a different file, so it can dynamically be loaded at a later time.
I could simply set the WebView as editable, but then I have to save a changed HTML-document; I rather want to leave the original document untouched and save the user-markup seperately.
My idea was to use the DOM, esp. the DOMDOMSelection and DOMRange functions. The WebKit3.0 API (here) states that
WebKitViewer.get_dom_document()
should return a WebKit.DOMDocument. However, as running the attached MWE (below) shows, this call returns a WebKit.DOMHTMLDocument. Using this object I don't find a way to access the DOMDocument or DOMDOMSelection I would need to proceed further.
My question:
Q: Is it a known bug or am I using the wrong calls?
EDIT1:
While I am not sure if the return value mentioned above is correct, I found a way to circumwent my problem:
# The still strange return document
DOMHTMLdoc = editor.get_dom_document()
# The view that contains the doc
def_view = DOMHTMLdoc.get_default_view()
# On this view one can finally create a selection
sel = def_view.get_selection()
END_EDIT1
If there is another (easier) way to achive this I'm open for suggestions ;) Thanks in advance.
Setup:
Linux Antergos 4.9.11-1-ARCH x86_64
Geany
Python3.6.0
gir1.0
MWE
import gi
import os
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit', '3.0')
from gi.repository import Gtk, WebKit
w = Gtk.Window()
w.set_title("Example Editor")
w.set_default_size(800,400)
w.connect("destroy", Gtk.main_quit)
message = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><title>ERSTE SCHRITTE MIT BRACKETS</title><meta name=\"description\" content=\"Ein interaktiver Wegweiser für die ersten Schritte mit Brackets.\"><link rel=\"stylesheet\" href=\"main.css\"></head><body><h1>ERSTE SCHRITTE MIT BRACKETS</h1><h2>Dies ist Ihre Anleitung!</h2><p> Willkommen zu Brackets, einem modernen, quelloffenen Code-Editor, der Webdesign versteht. Es ist ein einfacher, aber dennoch leistungsfähiger Editor, der Ihnen immer die richtigen Tools einblendet, sodass Sie die genau richtige Menge an Hilfestellung haben, wann immer Sie diese brauchen.</p></body></html>"
editor = WebKit.WebView()
status = editor.get_load_status()
DOMHTMLdoc = editor.get_dom_document()
def load_up(self):
editor.load_string(message,"text/html","utf-8","file://")
def show_status(self):
# Give the WebView some time to load everything
status = editor.get_load_status()
if status == 2:
# Here I would expect a DOMDocument to be returned
# but instead I get DOMHTMLDocument
DOMHTMLdoc = editor.get_dom_document()
el = DOMHTMLdoc.get_active_element()
coll = el.get_children()
length = coll.get_length()
print("DOMHTMLdoc: ")
print(DOMHTMLdoc)
# Here I can loop over the elements, but always DOMHTMLxx
print("DOMHTMLElement: ")
print(el)
for i in range(0, length):
print(coll.item(i))
hb = Gtk.HBox()
vb = Gtk.VBox()
b = Gtk.Button("Load")
b.connect("clicked", load_up)
b2 = Gtk.Button("DOM status")
b2.connect("clicked", show_status)
swindow = Gtk.ScrolledWindow()
swindow.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
swindow.add(editor)
hb.add(b)
hb.add(b2)
vb.pack_start(hb, False, False, 0)
vb.add(swindow)
w.add(vb)
w.show_all()
Gtk.main()