I'm trying to display a XML file with WKWebView. Has anyone got it managed to display it? I've set up a complete sample app (see below). The results were obtained by testing on iOS 9.3 iPhone 4S simulator.
LoadFileUrl()
string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");
NSUrl url = new NSUrl("file:///" + targetFilepath, false);
NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
webview.LoadFileUrl(url, readAccessUrl);
Result: white page
LoadHtmlString()
string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");
NSData documentData = NSData.FromFile(targetFilepath);
if(documentData != null && documentData.Length > 0)
{
NSString htmlString = NSString.FromData(documentData, NSStringEncoding.UTF8);
webview.LoadHtmlString(htmlString, readAccessUrl);
}
Result: content of xml is shown (without tags from xml), but styling is not applied.
NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
NSString htmlString = new NSString(File.ReadAllText(targetFilepath));
webview.LoadHtmlString(htmlString, readAccessUrl);
Result: content of xml is shown (without tags from xml), but styling is not applied.
LoadData()
string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");
NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
NSData documentData = NSData.FromFile(targetFilepath);
if(documentData != null && documentData.Length > 0)
{
webview.LoadData(documentData, "text/xml", "utf-8", readAccessUrl);
}
Result: white page
LoadRequest()
string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");
NSUrl url = new NSUrl("file:///" + targetFilepath, false);
webview.LoadRequest(new NSUrlRequest(url));
Result: white page
Different paths
var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User) [0].Path;
var caches = Path.Combine (documents, "Caches");
Result: white page
var caches = Path.Combine(System.IO.Path.GetTempPath(), "www");
Directory.CreateDirectory(caches);
Result: white page
Bundle Resource
string folder = NSBundle.MainBundle.BundlePath;
string source = Path.Combine(folder, "simplexsl.xml");
webview.LoadFileUrl(new NSUrl(source,false), new NSUrl(folder, true))
Result: white page
Link
Now I loaded a html with a link in it
XML Link
Result: html page is loaded, clicking the link leads to a white page, if xsl is commented out in xml then the xml is shown without styling
Online
webview.LoadRequest(new NSUrlRequest(new NSUrl("http://www.w3schools.com/xml/simplexsl.xml")));
Result: WKWebView can show the XML file if you allow it load non https resources.
Without XSL
Comment out the following line in simplexsl.xml
<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
and using
webview.LoadFileUrl(url, readAccessUrl);
Result: content of xml is shown (without tags from xml), but styling is not applied. So it seems that there is a basepath issue, but only with XSL files?
Displaying a html file, which references a picture relatively, does work, when I use LoadFileUrl(). But with xml files there seems to be a problem. For a short test UIWebView seems to handle that better.
So how is it done correctly?
Basic setup
public override void ViewDidLoad()
{
base.ViewDidLoad();
var webview = new WKWebView(View.Bounds, new WKWebViewConfiguration());
View.AddSubview(webview);
// code here
}
private string Init(string filename)
{
string sourceFilepath = Path.Combine(NSBundle.MainBundle.BundlePath, filename);
var library = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).Replace("Documents", "Library");
var caches = Path.Combine(library, "Caches");
var targetFilepath = Path.Combine(caches, filename);
if (CopyFile(sourceFilepath, targetFilepath))
return targetFilepath;
return string.Empty;
}
private bool CopyFile(string sourceFilepath, string targetFilepath)
{
if (File.Exists(sourceFilepath))
{
File.Copy(sourceFilepath, targetFilepath, true);
return true;
}
return false;
}
targetFilepath is something like /Users/some-user/Library/Developer/CoreSimulator/Devices/8808B80D-D232-4599-B776-139409C9DDB8/data/Containers/Data/Application/BC84CFD0-A805-417C-912F-C2B07834C822/Library/Caches/simplexsl.xml
simplexsl.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
simple.xsl
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold">
<xsl:value-of select="name"/> -
</span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic">
(<xsl:value-of select="calories"/> calories per serving)
</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
It would be no problem if you post your solutions in Objective-C or Swift.
I've the bad feeling, that the XSL transform is not allowed for local files (despite in the same directory) because of security concerns. If I open the file in Chrome I get
Unsafe attempt to load URL file:///C:/some/path/Projects/TestWKWebView/TestWKWebView/Resources/simple.xsl from frame with URL file:///C:/some/path/Projects/TestWKWebView/TestWKWebView/Resources/simplexsl.xml. 'file:' URLs are treated as unique security origins.
Firefox works fine. In Edge I get
XSLT8690: The system cannot locate the object specified.
Very interesting comment from here:
Interesting fact: Safari on Mac (8.0) works just fine with local XSLT, but Safari on iOS (8) simulator (and I suppose on iOS as well) I got the same error like in Chrome "Domains, protocols and ports must match.". But if I'm to implement UIWebView in the app and load XML with XSLT everything works OK.
After some research it seems that there are the following solutions:
Use UIWebView
Use a local webserver (e.g. look here)
Perhaps do the XSLT yourself with the help of libxslt (see here or here, also available as NuGet package)
Related
I am unpacking a Zip File and load the index.html like that
MyWebViewControl.NavigateToLocalStreamUri(MyUrl, new MyStreamUriResolver());
But it only shows Plain Html. The
<link href="css/custom.css" rel="stylesheet">
for example doesn't get invoked. I don't have any idea why it doesn't work.
I don't get it. Why can i load a WebPage like stackoverflow with css and javascript, but when i try to load my own Html File it doesn't work.
Any suggestions how i can fix the problem?
EDIT:
ShowCase for my Problem
Read.me First!
JQuery link is not necessary. You can try it with only a css link too.
This is how you get the whole content:
private async Task<IInputStream> GetContent(string path)
{
try
{
var arr = path.Split('/');
var localFolder = KnownFolders.MusicLibrary;
var folder = await localFolder.GetFolderAsync("myhtmlunzip");
var file = await folder.GetFileAsync(arr[arr.Length-1]);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read).AsTask().ConfigureAwait(false);
if (true)
{
Windows.Storage.Streams.DataReader reader = new DataReader(stream);
await reader.LoadAsync((uint)stream.Size);
var fileContent = reader.ReadString((uint)stream.Size);
stream.Seek(0);
}
return stream;
}
catch
{
throw new Exception("Path is invalid");
}
}
Informations (MSDN LINK)
Here is the problematic part of the template:
<ul id="list">
<template iterate='file in convertedfiles.files'>
<li>{{file.filename}}
<template if='file.isImage'>
<img src="{{file.src}}" alt="{{file.filename}}"><br/>
Source: {{file.src}}
</template>
</li>
</template>
</ul>
convertedfiles is a list of AndroidFile:
class AndroidFile {
File _file;
String filename;
String src;
bool isImage;
AndroidFile(this._file) : isImage = false {
filename = htmlEscape(_file.name);
// If the file is an image, read and display its thumbnail.
if (_file.type.startsWith('image')) {
FileReader reader = new FileReader();
reader.on.load.add((e) {
src = reader.result.toString().trim();
// prints the correct URL (data:image/png;base64,...)
print(src);
isImage = true;
watcher.dispatch();
});
reader.readAsDataUrl(_file);
}
}
}
The template gets displayed. It shows the filename, it shows the source but the imagetag looks like
<img alt="screenshot-1179.png" src="#">
The hash is underlined (in Chromium source view) and if I click on it it says "File not found: /web/out/"
Converted to JS is says in Chrome:
"Resource interpreted as Image but transferred with MIME type text/html"
Sample source is on GitHub
Any hints?
Note that if you know that you are handling a safe URI that is not vulnerable to XSS, you can work around this problem by using a SafeUri wrapper (imported from web_ui/web_ui.dart). For instance, change your template from:
<img src="{{file.src}}" alt="{{file.filename}}">
to:
<img src="{{new SafeUri.unsafe(file.src)}}" alt="{{file.filename}}">
Or change file.src internally to store a SafeUri.
I found the problem.
It's because the URI gets sanitized for security reasons. The sanitizer turns invalid URIs into a hash #.
From web_ui/templating.dart:
/**
* Ensure that [usiString] is a safe URI. Otherwise, return a '#' URL.
*
* The logic in this method was based on the GWT implementation located at:
* http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/safehtml/shared/UriUtils.java
*/
String sanitizeUri(uri) {
if (uri is SafeUri) return uri.toString();
uri = uri.toString();
return _isSafeUri(uri) ? uri : '#';
}
const _SAFE_SCHEMES = const ["http", "https", "ftp", "mailto"];
bool _isSafeUri(String uri) {
var scheme = new Uri(uri).scheme;
if (scheme == '') return true;
// There are two checks for mailto to correctly handle the Turkish locale.
// i -> to upper in Turkish locale -> İ
// I -> to lower in Turkish locale -> ı
// For details, see: http://www.i18nguy.com/unicode/turkish-i18n.html
return _SAFE_SCHEMES.contains(scheme.toLowerCase()) ||
"MAILTO" == scheme.toUpperCase();
}
So the sanitizer turns your data: scheme URI into a #. Data URIs can be used for XSS, but as far as I know the check could be improved by allowing data URIs when the data URI content type is image/*.
Perhaps file a bug report?
I am new in createjs and jquery mobile. This may be a simple question, but I do not know how to do it and have not found any answer online.
I created a canvas object using the Flash toolkit for CreateJS. I want to control it with a jQuery Mobile slider.
This is my html code:
<canvas id="canvas" width="200" height="200" style="background-color:#ffffff"></canvas>
<input type="range" name="slider-1" id="slider-1" value="1" min="1" max="6" data-highlight="true" />
The name of the instance that I want to control is squareB1, its timeline has 6 frames, see the fragments whole of code below. Notice that the slider has 6 values, the same number of frames.
(function (lib, img, cjs) {
var p; // shortcut to reference prototypes
// stage content:
(lib.squareB = function() {
this.initialize();
// Layer 1
this.instance = new lib.squareB1();
this.instance.setTransform(100,100,1,1,0,0,0,19.4,60.5);
this.addChild(this.instance);
}).prototype = p = new cjs.Container();
p.nominalBounds = new cjs.Rectangle(80.6,39.5,38.9,121);
// symbols:
(lib.squareB1 = function(mode,startPosition,loop) {
this.initialize(mode,startPosition,loop,{thiner:0,thin:1,mean:2,thick:3,thicker:4},true);
// timeline functions:
this.frame_0 = function() {
this.stop();
}
// actions tween:
this.timeline.addTween(cjs.Tween.get(this).call(this.frame_0).wait(4));
// Layer 1
this.shape = new cjs.Shape();
this.shape.graphics.f("rgba(71,31,7,0.2)").s("#1A1A1A").ss(1,1,1).p("ADCpcIAAS5ImDAAIAAy5IGDAA").cp();
this.shape.setTransform(19.5,60.5);
this.shape_1 = new cjs.Shape();
this.shape_1.graphics.f("rgba(71,31,7,0.2)").s("#1A1A1A").ss(1,1,1).p("Ak3pcIJvAAIAAS5IpvAAIAAy5").cp();
this.shape_1.setTransform(19.4,60.5);
this.shape_2 = new cjs.Shape();
this.shape_2.graphics.f("rgba(71,31,7,0.2)").s("#1A1A1A").ss(1,1,1).p("AmtpcINbAAIAAS5ItbAAIAAy5").cp();
this.shape_2.setTransform(19.4,60.5);
this.shape_3 = new cjs.Shape();
this.shape_3.graphics.f("rgba(71,31,7,0.2)").s("#1A1A1A").ss(1,1,1).p("AojpcIRHAAIAAS5IxHAAIAAy5").cp();
this.shape_3.setTransform(19.4,60.5);
this.shape_4 = new cjs.Shape();
this.shape_4.graphics.f("rgba(71,31,7,0.2)").s("#1A1A1A").ss(1,1,1).p("AKaJdI0zAAIAAy5IUzAAIAAS5").cp();
this.shape_4.setTransform(19.4,60.5);
this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape}]}).to({state:[{t:this.shape_1}]},1).to({state:[{t:this.shape_2}]},1).to({state:[{t:this.shape_3}]},1).to({state:[{t:this.shape_4}]},1).wait(1));
}).prototype = p = new cjs.MovieClip();
p.nominalBounds = new cjs.Rectangle(-35.3,0,109.7,121);
})(lib = lib||{}, images = images||{}, createjs = createjs||{});
var lib, images, createjs;
Then, I am doing something like this in jQuery, this is part of another JS file:
var canvas, stage, exportRoot;
function init() {
canvas = document.getElementById("canvas");
exportRoot = new lib.squareB();
stage = new createjs.Stage(canvas);
stage.addChild(exportRoot);
stage.update();
createjs.Ticker.setFPS(24);
createjs.Ticker.addListener(stage);
}
$('#slider-1').live('change', function(){
var slider_value = $(this).slider().val();
if(slider_value==1){
}
else if(slider_value==2){
//here is the issue, squareB1 is the symbol instance
exportRoot.squareB1.gotoAndStop(1);
}
else if...
}
My question is how to go to an specific frame of instance in the canvas object using the slider.
I appreciate any answer!
Feel free to post CreateJS questions on the support forum http://community.createjs.com
This looks like Toolkit for CreateJS output. The possible reason this isn't working is because unless you have created a variable named "canvas", which is the export root from Flash, then squareB1 is not defined. Elements that live on the stage in Flash, are exported as children of the exportRoot, which you can see in the generated HTML bootstrap file that gets created. The squareB1 child likely lives in that scope.
Can you post more code that shows where the export root is created, or describe your setup a little more?
The flash stage of toolkit is usually the name of your FLA, which is the first library definition in the generated JavaScript file. Usually it is created for you as "exportRoot" in the HTML that Toolkit creates. In your case, it is probably an instance of "SquareB" (note the "stage content" comment).
var exportRoot = new lib.SquareB();
exportRoot.instance.gotoAndStop(1);
Issue #1
When i'm uploading a file to google docs i receive status code "201" created, but when i try to open the file it seems that i'm doing something wrong, because i can't open it, and when i'm trying to download and open it on my PC i see the binary data instead of text or image. Current language is APEX, but i think it's pretty understandable.
First of all i'm getting Upload URL and then putting data to this URL;
public void getUploadURL()
{
Httprequest req = new Httprequest();
req.setEndpoint('https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false');
req.setMethod('POST');
req.setHeader('GData-Version', '3.0');
req.setHeader('Authorization', 'OAuth '+accessToken);
req.setHeader('Content-Length', '359');
req.setHeader('X-Upload-Content-Type', fileType);
req.setHeader('X-Upload-Content-Length', fileSize);
Dom.Document requestDoc = new Dom.Document();
String xml =
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
+'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">'
+'<title>'+fileName+'</title></entry>';
requestDoc.load(xml);
req.setBodyDocument(requestDoc);
Http h = new Http();
Httpresponse res = h.send(req);
System.debug('response=\n'+res.getHeader('Location'));
uploadFIle(res.getHeader('Location'));
}
public void uploadFIle(String uploadUrl)
{
Httprequest req = new Httprequest();
req.setEndpoint(uploadUrl);
req.setMethod('PUT');
req.setHeader('GData-Version', '3.0');
req.setHeader('Authorization', 'OAuth '+accessToken);
req.setHeader('Host', 'docs.google.com');
req.setHeader('Content-Length', fileSize);
req.setHeader('Content-Type', fileType);
req.setBody(''+binaryData);
Http h = new Http();
Httpresponse res = h.send(req);
System.debug('response=\n'+res.getBody());
}
As for "binaryData" property - i receive it from the page using javascript like this:
<input type="file" id="myuploadfield" onchange="getBinary()"/>
<script>
function getBinary()
{
var file = document.getElementById('myuploadfield').files[0];
fileSizeToController.val(file.size.toString());
fileNameToController.val(file.name.toString());
fileTypeToController.val(file.type.toString());
var r = new FileReader();
r.onload = function(){ binaryToController.val(r.result); };
r.readAsBinaryString(file);
}
</script>
r.onload = function(){ binaryToController.val(r.result); }; - this is the string that sends file binary data to my controller.
Issue #2
I'm trying to move one collection(folder) to another, and using this article (protocol tab instead of .NET). The issue is that i need to move collection instead of copying it and when i add my collection to another using this article, i'm currently adding reference to my collection instead of moving the whole collection from one place to another.
Please tell me what am i doing wrong.
Thank you for consideration.
Your "binary" data is being corrupted, when you are performing '' + binaryData.
In general, I have had more success using slicing of files, here is an example for webkit:
var chunk = this.file.webkitSlice(startByte, startByte + chunkSize, file_type);
// Upload the chunk
uploadChunk(startByte, chunk, callback);
Look this is my problem: I have updated to Grails 2.0.1 and now I have to make a QR Code. I have installed the qrcode plugin 0.1 but it is not working. I'm usign the tag:
<qrcode:image text="${createPromoInstance.id}" />
but it seems it doesn't do anything. I debugged with google chrome and I realized that in the "Elements" tab, the tag is being changed for <call></call>
I was asking yesterday about this, and someone said me that the plugin has some bugs that doesn't work with Grails 2.0.1, and he gave me some advices about what can I do.
For example, I editted the QRController like this:
class QrcodeController{
QRCodeRenderer qrcodeRenderer = new QRCodeRenderer()
def index = {
qrcodeRenderer.renderPng(response, request.getHeader("REFERER"), 300i)
} //It doesn't have any change
def url = {
String uri = params.u
String size = getSize(params)
qrcodeRenderer.renderPng(response, uri, size.toInteger().intValue())
} //it doesn't have any change
protected String getSize(Map params){
String size = params.s
if(!size || size.matches(/\D\)) {size = "128"}
return size
} //I have added the "protected word"
def text = {
String content = params.t //it used to be params.text
String size = getSize(params)
qrcodeRenderer.renderPng(response, content, size.toInteger().intValue())
}
}
and he said if I make those changes it will work, but no, it doesn't! I'm trying to render the code in an empty gsp just to try it out like so:
<%page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title><title>
<head>
<body>
<div>
<qrcode:image text="${createPromoInstance.id} />
</div>
</body>
</html>
As I understand it should work, but it doesn't. Does anyone know what am I doing wrong? I have to do something else to get the rendered QR Code?
Thanks for the help!
Jonatan!
I found the answer... I will just leave it here if anyone need it.
to get this working you need to do some changes as I wrote before.
protected String getSize(Map params{ //it used to be String getSize(Map params)
String size = params.s
if (!size || size.matches(\/D/) {size = "128"}
return size
}
and
def text = {
String content = params.t //it used to be params.text
String size = getSize(params)
qrcodeRenderer.renderPng(response, content, size.toInteger().intValue())
}
but this is not everything, you have to change something else in the BuildConfig like so:
//find the plugin call
plugins{
//this is how my plugins call looks like, here you might see the calls of the plugins you have installed
runtime: ":hibernate:$grailsVersion"
runtime: ":jquery:1.7.1"
runtime: ":resources:1.1.6"
//and you gotta add this
compile: ":qrcode:0.1"
/*.
.
.*/
build: ":tomcat:$grailsVersion"
}
Then you must go to the plugin taglib "QRCodeTagLib" and replace this:
def image = {attrs->
def size = attrs.height?:attrs.width
String text = attrs.text
String src = createLink(controller:'qrcode',action:'text',params:[t:text,s:size])
//it used to be like this
/*def mkp = new groovy.xml.MarkupBuilder(out)
mkp{
img(alt:url, src:src)
}
*/
//and now it looks like this
out <<"<img alt=\"${text}\" src=\"${src}\"/>"
}
and that's it, your QR Code will be rendered!
Hope it will be useful for anyone!
Jonatan!!
PD: The credit is not mine for this code, somebody helped me on the grails facebook page! Lots of thanks for Ingo. :)
ah! Other thing... He added something in the bootstrap but it didn't work for me, in the bootstrap he put:
QRCode m = new QRCode()
m.save()
try it and let me know if that works for you! :)