I am trying to process HTML data held in a QString. The data has encoded HTML tags, e.g. "<" etc. I want to convert these to the appropriate symbols.
I have been trying a number of approaches but none seem to work, which suggest I am missing something really simple.
Here is the code (amended to fix typos reported by earlier comments):
QString theData = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Arial'; font-size:20pt; font-weight:400; font-style:normal;">
<table border="0" style="-qt-table-type: root; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;">
<tr>
<td style="border: none;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:14pt; color:#4cb8ff;">This is text on the second page. This page contains a embedded image,</span></p>
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:14pt; color:#4cb8ff;">and audio.</span></p></td></tr></table></body></html>";
QString t2 = theData.replace("&", "&").replace("<", "<").replace(">", ">").replace(""", "'");
The value of t2 however is the same as theData after the replaces.
There is no definition of t1 in your code, I suppose you mean theData (and no double dot). The QString::replace functions alter the value of the string and return a reference of this.
QString s = "abc";
s.replace("a", "z").replace("b", "z");
// s = "zzc";
// if you don't want to alter s
QString s = "abc";
QString t = s;
t.replace("a", "z").replace("b", "z");
But there is better way to escape/unescape html strings:
// html -> plain text
QTextDocument doc;
doc.setHtml(theData);
QString t2 = doc.toPlainText();
// plain text -> html
QString plainText = "#include <QtCore>"
QString htmlText = plainText.toHtmlEscaped();
// htmlText == "#include <QtCore>"
If you only want to convert html entities, I use the following function, complementary to QString::toHtmlEscaped():
QString fromHtmlEscaped(QString html) {
html.replace(""", "\"", Qt::CaseInsensitive);
html.replace(">", ">", Qt::CaseInsensitive);
html.replace("<", "<", Qt::CaseInsensitive);
html.replace("&", "&", Qt::CaseInsensitive);
return html;
}
In all cases, it should hold that str == fromHtmlEscaped(str.toHtmlEscaped()).
Related
I am using SwiftSoup library to parse a dynamically created HTML string in swift...
let doc = try SwiftSoup.parse(htmlString)
And let's say I have this result
<html>
<head>
<style>
myClass {
font-size: 14px;
color: #000;
}
</style>
</head>
<body>
<span class="myClass">Hello World</span>
</body>
</html>
Now I can get the class value of my span like this
let span = try doc.select("body span")
let myClass = try span.attr("class")
Please is there a way I can iterate through the CSS attributes of myClass and get the attributes and their values.
Something like this:
var cssStyle = ""
let myClassAttrs = // a dictionary containing all myClass attributes(as dictionary keys) and values
for attr, value in myClassAttrs {
cssStyle += "\(attr): \(value);"
}
I want to make demo with sap.m.FormattedText nested in the sap.m.CustomTile in the jsbin. I do not know why I got this insted of the text SYSTEM SIZE:
The string I used is:
var sString = "<p style="font-size:20px; color:#808080; padding-left:40px; " > SYSTEM SIZE </p>" ;
var oFtext = new sap.m.FormattedText();
oFtext.setHtmlText(sString);
All code is in the jsbin example.
When I use the same string in the view it works:
<CustomTile>
<Vbox>
<FormattedText htmlText='
<p style="font-size:20px; color:#808080; padding-left:40px; margin-bottom:0px; "> SYSTEM SIZE </p>
'/>
</Vbox>
</CustomTile>
Thanks for any advice.
You are escaping the <, > and ' characters. This is necessary on the xml file (it is actually just part of the specification of xml, as you can see in this question on SO) but not in the js file.
Write the html normally and it will work: <p style='font-size:20px; color:#808080; padding-left:40px; ' >; SYSTEM SIZE </p>"
Having enough people writing in upper case, I inserted the syntax text-transform: lowercase; or the text to be written in lower case and syntax ::first-letter for a capital is created after the beginning of each sentence after the point.
text-transform: lowercase; works fine but for ::first-letter he created me a capital letter at the beginning of the sentence but not after!
Is it possible to create CSS capitalized after a point?
Keep all data into a variable and split it with the point you want. Then display all array inside paragraph. This might be working.
var str = "What ever you want to do. Please do it here.";
var res = str.split(".");
then use for loop and getElementbyId to replace the content
Try this:
str = 'ABC. DEF. XYZ';
str2 = str.toLowerCase();
str3 = str2.replace(/\. /g, '.</span> <span class = caps>')
$('#output').html('<span class = caps>' + str3)
.caps {
display: inline-block;
}
.caps::first-letter {
text-transform: uppercase;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
Convert the entire string to lower case; then replace the . with span elements; apply CSS rules to the span elements so that they are block level, inline and first letter capitalised); and just to tidy up, add an opening <span> before the replacement string to match the closing tag at the end of the first sentence.
<html>
<head>
<style>
p::first-letter {
font-weight: bold;
color: red;
}
</style>
<script>
function myFunction() {
var str = document.getElementById('data').innerHTML;
var res = str.split(".");
var data = "";
for(i=0; i<(res.length-1); i++){
var data = data + "<p>"+res[i]+".</p>";
}
document.getElementById("data").innerHTML = data;
}
</script>
</head>
<body onload="myFunction()">
<div id="data">What ever you want to do. Please do it here.</div>
</body>
</html>
This will automatically change the data onload.
Have more questions leave me a message in grandamour
Is not the first time that I have this "problem". Other times, I did solve it avoiding the problem, but now I want to face it.
The idea is to load some file "myBeautifulStyles.css" with a simple css code like this:
#charset "UTF-8";
/* CSS Document */
h1, h2, h3, p, a {
font-family:Arial, Helvetica, sans-serif;
}
h1 {
font-size:16px;
font-weight:bold;
}
h2 {
font-size:14px;
font-weight:bold;
}
h3 {
font-size:12px;
font-weight:bold;
}
p {
font-size:12px;
font-weight:normal;
}
If I just loaded it as a text and try a StyleSheet.parseCSS() the result is null, the .styleNames returns an empty Array. I'm wondering if is about the text (break-line marks, initial codes...) or something else is missing. The examples around the web are always single line code. It's easy to clean this text and put it in a single line, but will work?
And most important, what's the best approach of this task?
Edit: as required, this code loads de css file trough a php (for avoiding caches):
var ur:URLRequest = new URLRequest(httpBase+"admin_arqs.php");
ur.method = URLRequestMethod.POST;
var Vars:URLVariables = new URLVariables();
Vars.op = "ler";
Vars.url = httpBase+"conteudos_estilos.css";
ur.data = Vars;
var ul:URLLoader = new URLLoader();
ul.dataFormat = URLLoaderDataFormat.TEXT;
ul.addEventListener(Event.COMPLETE, estilos);
ul.addEventListener(IOErrorEvent.IO_ERROR, ioerror);
ul.load(ur);
function estilos(e){
e.currentTarget.removeEventListener(e.type, arguments.callee);
e.target.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
var Res = JSON.decode(clearRes(String(e.target.data)));
if(Res.erro!="OK"){
msg("Erro: "+Res.msg);
} else {
ini_edit(Res.dados);
}
}
function ioerror(e){
e.currentTarget.removeEventListener(e.type, arguments.callee);
e.target.removeEventListener(Event.COMPLETE, estilos);
msg("Erro de IO!");
}
This part set the stylesheet:
trace("Estilos em string: "+css);
var estilos:StyleSheet = new StyleSheet();
estilos.parseCSS(css);
texto.styleSheet = estilos;
trace("Estilos definidos: "+estilos.styleNames);
The first trace generate the css text is displayed above. The second should display an Array with names. I also try tracing estilos.styleName.length and is zero.
texto is the TextField and css came from the Res.dados.
Seems like this line #charset "UTF-8"; at the head of css file causes the problem. Try to remove it and see if it solving the issue.
I have an html page for a preview functionality.
I pass this as html into a stringbuilder type object and replace content through another page.
Now I want a certain section to be hidden under a specific circumstnace.
Currently that section is a row.
So how can I do so?
The following code in the section I want to hide:
<tr id="rowcontent" bgcolor="E96F00">
<td style="font-family: Arial; font-size: 14px; font-weight: Bold; color:white;">Course Content Link</td>
</tr>
<tr>
<td style="font-family: calibri; font-size: 14px;">#CourseContent#<BR> </td>
</tr>
This is how I am using the above html :
file = Server.MapPath(".") + "\\Template\\Level100_Template.htm";
string input = string.Empty;
if (File.Exists(file))
{
sr = File.OpenText(file);
//input += Server.HtmlEncode(sr.ReadToEnd());
input += sr.ReadToEnd();
x.Append(input);
sr.Close();
}
This is how I am replacing the content section:
if (dt.Rows[0]["CourseContentPath"].ToString() != string.Empty)
{x.Replace ("#CourseContent#", "<A href='" +CourseContentLink + "' target=_blank onclick='window.open(this.href, this.target,'height=1000px,width=1000px'); return false>Click here</A> to find the course content");
}
How can I hide the entire section in a particular case..
A simplest way is to use this code
string StartStr= "<tr id=\"rowcontent\"", EndStr= "</tr>", String= x.ToString();
int Start= String.IndexOf(StartStr);
int End= String.IndexOf(EndStr, String.IndexOf(EndStr, Start)+ EndStr.Length);
x.Remove(Start,End- Start+ EndStr.Length);