Allow html conditional comments and doctype
I'm hacking on an fully server and client compatible app using React. One obstactle I'm running into is it seems react doesn't like exclamation marks in your jsx-style code.
Here's an example of a pretty standard boilerplate for HTML:
<!doctype html>
<!--[if IE 7]> <html class="no-js ie ie7 ltie8 ltie9"><![endif]-->
<!--[if IE 8]> <html class="no-js ie ie8 ltie9"><![endif]-->
<!--[if gt IE 8]> <html class="no-js ie gtie8"><![endif]-->
<!--[if !IE]><!--><html class="no-js" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" ><!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/css/app.css" type="text/css" media="all" />
</head>
<body>
<!--[if lt IE 8]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
{this.props.page}
<!-- <script src="/js/vendor.js"></script> -->
<script src="/js/app.js"></script>
</body>
</html>
I think it's getting stuck on the conditional comments in this example.
I would prefer to keep this code in the HTML style, and to render it from the server without making major changes, it needs to be inside a react class. I'd be open to trying to hack this together, I may take a stab sometime this coming week.
After checking further, it did seem to break on the doctype (!) as well.
JSX is not HTML. You'll want to look at the HTML to JSX compiler. http://facebook.github.io/react/html-jsx.html
You won't be able to generate doctypes or conditional comments with React. This is technically impossible by design. If you want functionality like this, you might consider creating a script or addon to html-jsx to generate a javascript (JSX) equivalent.
While this is true it sucks for people who want to do full-page server rendering. I think if we claim to support full-page server rendering we should at least support doctype.
I can see a good reason for leaving out these portions of the HTML spec. I ended up adding on to this branch of react-app-middleware some changes to allow pulling in a static HTML template initially and then rendering components results into it.
See an example of it being used in this repo (see server.js).
@petehunt I agree with you but I also think it's not really that practical to render full pages inside of react.
I think a good reason against is that React is used by many as a client side library, and therefore should be kept as light as possible.
Perhaps there could be an extension made that implements the "rest" of the HTML5 spec, but could be mixed in only on the server side as client's shouldn't need to render stuff like doctype once the server has.
I guess you could use React.DOM.injection and modify the JSX transform to read <!doctype. Plain conditional comments could probably be done similarly.
@natew
How do you deal with void tags like <img> and <meta> right now? Do you self-close them before embedding them?
Would you use a tool like html-jsx to transform the html to syntax that the jsx transformer understands?
I have images working like this: <img src={this.imagePath + image.thumb} /> with no problems.
I have played with the html to jsx transformer thats on React's site currently, but haven't needed it yet personally. Is there a document that outlines all the supported tags? I haven't checked, but I also haven't gotten too deep into things yet.
Edit: As for the self-closing, yea I've always felt more accustomed to using them anyway.
On second thought, I think I may have missed the point above. Adding something like React.DOM._doctype would be useful, and would have very little impact on React's code.
Same could probably be done with conditional comments (React.DOM._ConditionalCommentHide ,React.DOM._ConditionalCommentShow). Inserting conditional comments using innerHTML might work? I'm not sure how updating them will work though.
I don't think there's a document for it (@Daniel15 )
A few issues I've seen with the html-jsx transformer:
strips script tags
strips html head body tags
<style> tag content isn't transformed
namespaced attributes (xmlns:fb or data:myattr) don't work
There's a few issues with css styles and attribute case-sensitivity in another github issue.
Since it uses the browser dom (you can use jsdom so you don't have to use a browser), all tags supported by browsers should work. Has worked pretty well for me.
html-jsx transformer does things like turn class -> className and style strings into objects and turn void tags (<img>) into self-closing tags (<img/>).
I'm creating tools that transform user-provided html/data (as-is) into components, so I've had to make use it.
I too would like to see JSX support HTML conditional comments and not do the things @jhiswin described. I was going to implement a JSX preprendering tool until I found out I couldn't even write HTML comments and doctype...
I'm not sure if html comments are a deal breaking use case -- are conditional comments the only solution to these problems? -- and doctype can be prepended by your web server request handler or static build system.
Prepending doctype is easy, conditional comments are harder to because that's the easiest way to conditionally load IE shims, short of this option I'll have to write a bunch of JS pre-shims for browser detection or fiddle with the webserver config, which I may not always have access to. IE8 users are still ~25% of the market. Is it difficult to elevate HTML comments to have same status as JS comments in your Esprima fork?
@jeffmo
@wyuenho As another take on it, you could have a preprocessor (like html-jsx-lib) turn <!x to <_x (or something like ConditionalComment) and then add React.DOM._x
My view on it is that you will need a preprocessor to deal with differences between HTML and JSX anyways.
@jhiswin Or better yet, preprocess to JSX. As a hack it'll work, just have to make sure to spit out the right string what renderComponentToString is called. The extra setup is a hassle tho. This also denies the claim that React is designed to be easily rendered on the server.
I still think it's best to have JSX support as many HTML5 constructs as possible. Just fewer surprises.
BTW: rendering on the server doesn't necessarily mean that we need to be able to render full pages, especially since browser support for updating <html> and <head> is so sketchy. We've gone back and forth as to whether we want to actually support this or not for this reason.
We could probably get comments into core with a little work (mostly just cloning ReactTextComponent to work with comments and figuring out how to look it up by ID), not sure about the jsx transform. Not sure if we can prioritize this relative to other things (especially since this can be worked around), but I think a PR would definitely be considered.
Will look into it this weekend. What's the release schedule for 0.9?
Sometime between Tuesday and Friday