0

From an XML file displayed in a WebView, I would like to be able to select text with the mouse, and when a button is pressed, add a unique XML tag to that selection.

Sample XML file fragment:

<p>
    some text that is in this paragraph
</p>

If I select "that is" from the above, and press the button, it should result in:

<p>
    some text <x id="foo">that is</x> in this paragraph
</p>

where <x> is a special tag I will use for my own purposes. It might have attributes, like an id or something.

I see that I can use bits of JS code to gather info about the HTML tags and locate the selected text in a given tag, but I don't know how to get an "absolute" location. Nor do I know how to inject the XML tag into the XML file. Or maybe I should treat it as an HTMLEditor or something? I just don't want the user to be able to change the XML file except in this particular way.

Any ideas? Very appreciated.

buggaby
  • 359
  • 2
  • 13
  • I found that I can use this `webEngine.executeScript("window.getSelection().getRangeAt(0).insertNode(document.createElement(\"p\"))");`to add a paragraph, but it doesn't actually change the underlying file. – buggaby Jul 01 '16 at 01:16

1 Answers1

0

The executed script is only changing the DOM as it exists in memory. You would need to use webEngine.getDocument() and then write that out over the underlying file. This answer describes how that part of it is done.

Community
  • 1
  • 1
Geoff
  • 568
  • 4
  • 11