Avoid interpreting HTML code in a QTextBrowser - html

I have a QTextBrowser in my Qt application. I would like to append some text but, I need part of this text not to be interpreted in HTML. How can I achieve this? May I encode the QString?

If you want your browser not to interpret only parts of your text as HTML you will need to quote the part you want to omit (replace "<" with "&l t;" etc.). You can use convenient escape method:
textBrowser->insertHtml(
QString("<b>this will be bold</b>") +
Qt::escape(QString("<b>this will not</b>"))
);
If you would like not to interpret the whole thing you can insert it as plain text:
textBrowser->insertPlainText ( "<b>foobar</b>" );

Finally I solved my own question using:
QString codedHtml = Qt::escape(html);

Related

HTML: show > in plain text

In HTML files, if I want the browser to show a special symbol, such as >, I can use the special escape character sequence > and the browser will automatically transform it to >.
But what if I don't want it to be transferred into >? What if I happen to want the character sequence to be shown in plain text?
In order to have a character sequence not automatically rendered as a symbol, you can escape out the ampersand. This method is commonly used by instructional pages with lists of HTML symbols.
Source: >
Result: >
Source: &gt;
Result: >
Thanks to Ry-♦ for stating the obvious. I was so concerned about using raw string, I didn't realize what I was using is adequate already. Use .textContent property to render text as is. If you use something like .innerHTML, it will parse your text as HTML and apply escape sequences.
Demo
var str = ">"
document.querySelector('body').textContent = str;

Show the code (i.e "&entity_name;"), not the corresponding character

If I write in a HTML file the browser automatically translates it to its corresponding character, in this case a space.
How can I escape from this phase? I mean, if I would like to print just the code (i.e. ) how can I prevent the browser from doing that?
You can't stop the browser from treating HTML as HTML.
If you want to include a & as data instead of as the start of a character reference, then use the character reference for it:
&nbsp;
For this you will need JQuery.
Try .text() method. http://api.jquery.com/text/
Get the body content an then replace it with text.
Means: $('body').text($('body').html());.
This will give you an output of your html in plain Text. Actually you can do this with other Elements as well. Replace the Word body with the element, class or id you need.

Html Encoding Texts in Razor Views

Texts and/or markups are rendered to output as-is without any html-encoding as we already expect.
For the following, the plain text with markup must be html-encoded.(We don't care about the code output here.)
#{ var theVar = "xyz"; }
some text & other text >>#theVar
So, the html in the output;
some text & other text >>xx
So, when we want to write some static text that needs to be html-encoded we have to use constructs like;
#{ var theVar = "xyz"; }
#("some text & other text >>")#theVar
to get the following html in the output;
some text & other text >>xyz
and for clarity when viewed in browser;
some text & other text >>xyz
So, is there a simple way of doing this? Some shortcut to html encode texts instead of using #("...") for each text which will start to look nasty when there are multiples of them.
What would be the best practice? How do you do this?
So, it is not a big concern when we specify utf-8 encoding for the document. It is not required to html encode characters as entity references except special(<, >, &, ", ') characters when utf-8 encoding used for the document.
Even using & by itself is not wrong for lenient browsers but there would be ambigous cases to consider like volt&amp. So, it would be better to html encode all of these special characters.
Check the W3 Consortium articles "When to use escapes" section;
http://www.w3.org/International/questions/qa-escapes#use

How to parse links and escape html entities?

I have some user provided content that I want to render.
Obviously the content should be escaped, rails does this by default. However I also want to parse the text so that urls are presented as links.
There is an auto_link helper which does just that. However no matter what order I do this in I can't get the desired result.
Take content:
content
=> "<img src=\"foo\" />\\r\\n\\r\\nhttp://google.com"
If this is escaped, because the slashes in the url are escaped, auto_link will not work:
Rack::Utils.escape_html(content)
=> "<img src="foo" />\\r\\n\\r\\nhttp://google.com"
If I use auto_link first obviously the link will be escaped. Additionally auto_link strips unwanted content rather than escaping. If a script tag is present in the input I want it escaped not removed.
auto_link(content)
=> "<img src=\"foo\" />\\r\\n\\r\\nhttp://google.com"
Any idea how to do get the desired output?
Thanks for any help.
You could strip out all the escaped whitespace characters with content.gsub!(/\\./, ""). Then you'll be able to use auto_link.
The solution I ended up using was ditching auto_link, letting Rack escape my content server side and then parsed the links out of the text on the client side using https://github.com/gabrielizaias/urlToLink
$('p').urlToLink();
I've had success with:
auto_link(h(content))

Reg Exp To Remove 'onclick' from HTML elements (Notepad++)

I have a big html file (87000+ lines) and want to delete all occurrences of onclick from all elements. Two examples of what I want to catch are:
1. onclick="fnSelectNode('name2',2, true);"
2. onclick="fnExpandNode('img3','node3','/include/format_DB.asp?Index2=3');"
The problem is that both function names and parameters passed to them are not the same. So I need a Regular expression that matches onclick= + anything + );"
And I need one that works in Notepad++
Thanks for helping ;-)
Not familiar with notepad++, but what I use in vim is:
onclick="[^"]+"
Of course this depends on there being double quotes around the onclick in every case...
This regular expression will fail if you have a " or ' character included within quotes escaped by a \. Other than that, this should do it.
(onclick="[^"]+")|(onclick='[^"]+')
onclick="[^"]+" works for me, for that 2 strings.
If you want to go with a regex:
/onclick=".*?"/
You could also use something which is DOM-aware, such as a HTML/XML parser, or even just load up jQuery:
$("[onclick]").removeAttr("onclick");
... and then copy the body HTML into a new file.
Could
onclick=\".+;\"
Work?
onclick=\".*\);\"
This regex should do the trick.
(\s+onclick=(?:"[^"]+")|(?:'[^']+'))
Open your file on dreamweaver, choose edit from the toolbar, select find and replace,
put onclick="[^"]+" in find field and keep replace blank
this will do the whole thing.
Enjoy