Set quirk for WebView
Asked Answered
M

1

6

I have a app containing a WebView. When <!DOCTYPE html> is not defined in a HTML page, it produces unexpected errors as snippets which I run are only compatible with HTML5.

So instead of writing <!DOCTYPE html> everytime, can I simply set default quirk (quirk is a default HTML declaration which will be used if HTML version of a page is not specified).

So if page's HTML declaration is not specified, it will automatically render in HTML5.

Any ideas on how to do this? Major browsers use this technique (Including Chrome for Android).

EDIT

I have 500+ snippets like this using directly embedded SVG etc -

<svg width="300" height="200">
    <polygon points="100,10 40,198 190,78 10,78 160,198"
             style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg>

These snippets don't run well on other versions of HTML. I don't actually have any problem in writing <!DOCTYPE html> on every page but I am worried about my clients who can see, edit and modify my snippets or even create a new snippet.

I gave my app to my friends for testing and many of them complained the snippets that they created don't run properly and I think same sort of thing can happen with my clients as well.

Please help

Marivaux answered 3/10, 2014 at 5:9 Comment(4)
just for info, adding <DOCTYPE> everytime is a normal action. You should (and note have to) provide which HTML version you're using.Haviland
Have you every tried running a piece of HTML code on Google Chrome or Firefox? Did you notice that if you didn't add the <DOCTYLE ...>, the page will automatically render in the latest available HTML version? This is what I want. I want to set a default <DOCTYPE ...> thing that will be used if I didn't specify which HTML version in any of my HTML files.Marivaux
I wasn't being sarcastic bro. I though question wasn't clear to you. I was giving a bounty for good answer so have a great day too.Marivaux
I don't really get the requirements for you to include this. Are you pulling html from external resources or are you including snippets of your own? Please give us some code example so we can see why you would need this.Alvinia
R
1

EDIT: Chrome for Android does not use WebView [Source]:

Does this mean Chrome for Android is using the WebView?

No, Chrome for Android is separate from WebView. They're both based on the same code, including a common JavaScript engine and rendering engine.

So they have added this functionality on top of the normal WebView behavior. What you want to accomplish is not possible.


I think the only way this would be possible (based on my searches) is to override the WebView behavior and do something like this instead:

  1. Catch the intended url
  2. Use HttpClient to fetch the contents of the page
  3. Check for <!DOCTYPE> tag, and add if none exists
  4. Use WebView.loadDataWithBaseURL to render the HTML in the WebView and specify the base url caught in step 1

Obviously this is very hacky and I cannot confirm whether it works at all. So by far the best solution is to specify a <!DOCTYPE> tag on every page. If this requires validating snippets before loading / allowing clients to create them, then so be it.

Rockett answered 27/10, 2014 at 16:23 Comment(5)
Downvotes? I guess silence and no solution is preferable to someone trying to help...Rockett
I didn't downvote you. Thanks for the advice. But I still wonder how Chrome for Android does it.Marivaux
Yeah, me too. It's frustrating when Google seems to give themselves special privileges.Rockett
Check out my edit. It seems Chrome for Android is not based on WebView.Rockett
So I think I need to implement it in some other way. Thanks for the answer. I will surely get back to you if I find a way to do it.Marivaux

© 2022 - 2024 — McMap. All rights reserved.