Handle elements with contenteditable correctly for editor

This commit is contained in:
Florian Bruhin 2014-07-16 07:42:02 +02:00
parent 92e67b8018
commit bbd9d24334
2 changed files with 26 additions and 5 deletions

View File

@ -717,7 +717,10 @@ class CommandDispatcher:
raise CommandError("No element focused!")
if not webelem.is_editable(elem):
raise CommandError("Focused element is not editable!")
text = elem.evaluateJavaScript('this.value')
if webelem.is_content_editable(elem):
text = elem.toPlainText()
else:
text = elem.evaluateJavaScript('this.value')
self._editor = ExternalEditor(self._tabs)
self._editor.editing_finished.connect(
partial(self.on_editing_finished, elem))
@ -734,5 +737,8 @@ class CommandDispatcher:
"""
if elem.isNull():
raise CommandError("Element vanished while editing!")
text = webelem.javascript_escape(text)
elem.evaluateJavaScript("this.value='{}'".format(text))
if webelem.is_content_editable(elem):
elem.setPlainText(text)
else:
text = webelem.javascript_escape(text)
elem.evaluateJavaScript("this.value='{}'".format(text))

View File

@ -187,6 +187,21 @@ def _is_object_editable(elem):
return False
def is_content_editable(elem):
"""Check if an element hsa a contenteditable attribute.
FIXME: Add tests.
Args:
elem: The QWebElement to check.
Return:
True if the element has a contenteditable attribute, False otherwise.
"""
return (elem.hasAttribute('contenteditable') and
elem.attribute('contenteditable') not in ('false', 'inherit'))
def is_editable(elem):
"""Check whether we should switch to insert mode for this element.
@ -204,8 +219,8 @@ def is_editable(elem):
div_classes = ('CodeMirror', # Javascript editor over a textarea
'kix-') # Google Docs editor
roles = ('combobox', 'textbox')
if elem.hasAttribute('role') and elem.attribute('role') in roles:
return is_writable(elem)
if is_content_editable(elem) and is_writable(elem):
return True
if (elem.hasAttribute('contenteditable') and
elem.attribute('contenteditable') not in ('false', 'inherit')):
return is_writable(elem)