What is the proper way to decode UTF-8 encoded html and present the text in a UITextView while keeping the HTML formatting?
I have the following title:
<p><strong>Pasta and Pizza</strong><br />
After decoding:
<p><strong>Pasta and Pizza</strong><br />
Finally I would like to present the text in a UITextView that respects the HTML tags:
Pasta and Pizza
I am pulling this content directly from a database, so I simply can't pull the html page into a UIWebView. (I am not opposed to using a UIWebView. I simply don't know how to implement that either.)
Related
I am seeing a code in my project as
${'myproj.label' #i18n, format=[sighltyObj.field1], context='text'}
Intention is pass a variable into i18n text + encode the texts safely. Is this right to use display context along with i18n translations? When I tested with a field1 = "Hello%20World", it is NOT encoding the texts rather rendering as is.
How can I encode html strings while passing the arguments as variables into i18n?
HTL will not decode the text returned by format. I think the confusion comes from the documentation which states for the display context text the following:
Use this for simple HTML content - Encodes all HTML
(Source: HTL Specification Section 1.2.1 Display Context)
But this does not mean that this context decodes anything, it encodes HTML tags.
So if sighltyObj.field1 is Hello%20World it will not be rendered as Hello World but as Hello%20World as you already noticed.
The display context text will encode all HTML tags in the given text so that you can't "smuggle" them into a text (see code injection).
So for example:
${'<p>This is my text</p>' # context='text'}
will create the following HTML
<p>This is my text</p>
Note how the p tags were encoded:
<p> became <p> and </p> became </p>.
The getter for field1 in your sighltyObj will have to do the decoding so that Hello%20World becomes Hello World. There is already a answer on Stackoverflow that shows you how to do this: https://stackoverflow.com/a/6138183/190823
String result = java.net.URLDecoder.decode(url, "UTF-8");
I try to display an ascii85 encoded image, like:
<img src='data:image/jpg;base85,...'/>
or
<img src='data:image/jpg;ascii85,...' />
I found several examples on base64 files, but nothing about ascii85...
What do you think?
Thanks
You can't do it because, as far as I know, only PostScript and PDF are able to handle ascii85 (base85). Browsers are only handling base64 for now.
If you already have data in this encoding you will need convert it to base64 before putting it into the browser.
It can be done with some javascript/typescript.
Have the code crawl the DOM
find img/#src attributes that is base85 encoded
write the code to decode base85 and re-encode it in base64
update the IMG src attribute back
LinkedIn isn't encoding special chars when using their share API. Specifically having issues with quotations. Facebook Sharer seems to work fine but the LinkedIn one chokes when using ampersand encoding. The below renders as "We have a drone, and we’re prepared to shoot" and not as the intended "We have a drone, and we’re prepared to shoot".
<meta property="og:title" content="We have a drone, and we’re prepared to shoot" />
Site in question: http://nubix.ca/blog/we-have-a-drone-and-were-prepared-to-shoot/
’ is an HTML character reference. That is the correct thing to use inside of a <meta property> tag, as that is part of the HTML itself.
For the "Share this article" buttons at the button of the page, the Twitter and Linkedin share URLs are not encoded correctly. They both have the page title encoded as this:
We%20have%20a%20drone%2C%20and%20we%26%238217%3Bre%20prepared%20to%20shoot
Which is wrong, it needs to be encoded as this instead:
We%20have%20a%20drone%2C%20and%20we%E2%80%99re%20prepared%20to%20shoot
%26%238217%3B is the url-encoded form of the literal string ’, which gets displayed as-is because HTML character references have no meaning in URLs.
%E2%80%99 is the url-encoded form of the raw UTF-8 encoded octets of the Unicode ’ character.
So, whoever is generating the share URLs for Twitter and LinkedIn is not decoding the HTML character reference ’ to its actual Unicode codepoint U+2019 and then encoding it as UTF-8 octets E2 80 99 in the share URLs. Or more likely, is just outputting the same ’ text as-is in both HTML and URLs without taking into account that they encode Unicode characters in semantically different ways.
The Email and Facebook share URLs do not suffer from this problem because they are not specifying the page title anywhere. Facebook in particular is being passed just the page URL, and then it retrieves the page's HTML metadata, where ’ has meaning and gets properly decoded as ’.
I'm using JSF2.2, Tomcat 8 and MySQL DB to create a simple CMS. In my back pages I use Primefaces p:editor to create/edit my page content. I would like to insert an image somewhere into the text. It would be great if I could upload the image and insert it at the current cursor position. Ideally, the image should be saved as a blob, but if it's easier for the implementation it could instead be stored as a local file.
I can see that p:editor already has an option to insert a URL of an image, resulting in the <img> tag in the text. But I would really like the possibility to upload an image from my local drive. I haven't been able to Google anything useful so far.
this is how i did it:
.xhtml:
<p:editor id="editor"
widgetVar="editWidget"
value="#AnnouncementBean.text}" />
<p:fileUpload id="upload"
fileUploadListener="#{AnnouncementBean.uploadListener}"
label="Insert image"
mode="advanced"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
update="editor, growl">
</p:fileUpload>
An in my Backing Bean in the uploadListener after file upload:
String value = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap().get("editor_input");
setText(value + "<img src=\"/uploads/" + result.getName()+"\" />");
RequestContext.getCurrentInstance().update("editor_input");
where "editor_input" referes to the actual form field submitted for the editor (PF adds the_input to the id of your editor)
Notice how I update the editor_input not the actual editor. Only problem now is that the image is appended to the end of the text. sou you must move it manually within the editor
You can use a string to receive the editor (or textEditor) value, then use regex to find all img elements.
This is my code.
String regex="<img src="data:image/(gif|jpe?g|png);base64,([^"]*")[^<>]*>";
Matcher m = Pattern.compile(regex).matcher(your_textEditor_value);
while(m.find()){
String imageFileType=m.group(1);
String imageDataString=m.group(2);
byte[] imageData=Base64.decodeBase64(imageDataString);
}
The imageFileType is your file type, and imageData is data of the file. You can use it to do other thing.
Is there a way to send only an Image with a link and some alt text for each item in an RSS feed?
I looked at the enclosure tag but this is only for videos and music.
The enclosure element can be used to transmit pictures. The RSS 2.0 spec is quite clear about that, saying that the type is a MIME type. It does not say it is restricted to audio or video.
Here's an example: a set of photo feeds from Agence France Presse
One of solutions is to use CDATA in description
<![CDATA[
Image inside RSS
<img src="http://example.com/img/smiley.gif" alt="Smiley face">
]>
Note, that you may have a problem with hotlink prevented site.
This is possible in RRS2,
see
http://cyber.law.harvard.edu/rss/rss.html#ltenclosuregtSubelementOfLtitemgt
So you have to use the enclosure tag, to add media
You should use the enclosure tag within item to include the image. You can use it for images by setting the correct Mime Type (for example: image/jpeg) and including the image size as the "length" attribute. The length attribute doesn't need to be completely accurate but it's required for the RSS to be considered valid.
Here's a helpful article that discusses this and other options.
To work with the Mailchimp RSS to email feature, they expect the image to be specified in a <media:content> element inside <item>. This is their source for the feed item's image macro in their templates.
Thus, you need to add to the declarations
xmlns:media="http://search.yahoo.com/mrss/
Then inside the <item> element add
<media:content medium="image" url="http://whatever/foo.jpg" width="300" height="201" />
Without the extra declaration, the feed is invalid since media:content is not a known element.
Inside tag ITEM
<image:image xmlns:image="http://web.resource.org/rss/1.0/modules/image/">
http://domain. com/image.jpg
< /image:image>
Inside Description Tag
<![CDATA[
Some Text..
<br/><img src='http://domain. com/image.jpg' ><br/>
More Text
]]>
Regarding the <p> tag issue, You need to encode html within the xml.
Your code would look something like this:
<description><p> Text in the tag </p></description>
Since you are using php you can use htmlentities() to encode the html tags. They look horrible in the xml but RSS readers know what to do with it.
http://php.net/manual/en/function.htmlentities.php