PDA

View Full Version : Get HTML selection from QWebPage



vfm_blankpage
15th April 2011, 15:33
QWebPage::selectedText() returns the selected text from a page. However, I would like to get the HTML. How can this be done?

For example, if the user selects this HTML:


The <b>quick brown fox</b> <img src="fox.jpg"/> jumps over the <em>lazy dog</em>.

QWebPage::selectedText() returns:


The quick brown fox jumps over the lazy dog.

That is, no HTML tags. I would like to get the HTML code too.

Thanks.

SixDegrees
15th April 2011, 15:57
This seems quite difficult. Consider the HTML <tag>quick brown fox</tag>.

The user selects the text "brown" in their browser. The copy routine might notice that <tag> has been applied to this text, but what should it do? Insert open and close tags around the selected text? That doesn't match the original document's HTML. How far ahead/behind should it search for the enclosing tags? What if those tags are nested within a larger enclosing block that applies some other property to the text?

Going further: what about stylesheets? Should the copy routine reach outside the document itself and pull in this external information?

Either way, you'll quickly run into cases where the copy is no longer a duplicate of the original source, or doesn't carry the information you're (probably) looking for. As such, there doesn't appear to be a unique solution to this problem; although you could still implement such a feature yourself, it would be unique to your particular requirements, and someone else might not be able to use it at all.

vfm_blankpage
15th April 2011, 16:14
Actually, I'm not planning to copy the selected text anywhere outside of its own document. I just want to modify it. Think of the typical bar with buttons for bold, italic, underlined and add link. When you select some text and click the Add Link button, after asking for the link target, the application takes the selection and wraps it between <a href="foo"> and </a>. That's exactly what I want.

Thanks.

SixDegrees
15th April 2011, 17:15
Yes, but you explicitly tell it you're looking for a link, so it has some information about what tags to look for and how to apply them. That's not what you originally asked - which was to select text from an HTML page and have the HTML tags it contains come along. As discussed above, this is a hard problem without a unique solution.

If all you want to do is wrap selected text within a user-provided <a...></a> block, that's something you can trivially do on your end.