-
Notifications
You must be signed in to change notification settings - Fork 2
use template.content #2
Comments
Good point, thanks! Didn't actually think about that. |
This actually raises another issue: The I'm sure how to approach that. I'd probably
That solution is not particularly pretty, but I don't think there's a better one for browsers not supporting Also that means that this way, users can decide on their own if they need to do this for compatibility reasons (i.e. "if they do support IE11") or not. |
There is actually a polyfill for the template element, to be released soon-ish i believe. |
Right, but the polyfill does not (and probably cannot) fix the problem of out-of-context elements. It will fail having I suppose this is just not fixable. |
Though it is to add that jQuery manages to do that correctly, no matter the browser. I suppose they use some way more sophisticated algortihm though. |
This is the sophisticated algo :) |
Also, we could just generally use a However, before doing that we should figure out
|
Template engines just grep for and wrap them, then unwrap them.
|
Simple example: https://github.com/kapouer/domt/blob/master/domt.js#L209-L226 |
Also... this problem is unrelated to using |
(on IE11 i mean) |
Okay, so wrapping in Well, this is kinda related to If we'd introduce a working way with |
I'm pretty sure browsers supporting natively (EDIT: fixing bad english style) |
Yes, I agree that fixing templates is not in the primary scope of this project. However, since we cannot point to a working polyfill instead, I'd still insist on having an IE11-compatible solution—probably the way jQuery did it. I'd be happy to drop that fallback as soon as a working template polyfill is available. Maybe we should open a pull request over there at the polyfill. |
on it :) |
Granted, webcomponents/template is not well maintained at the moment. |
Cool. I'm curious how responsive the folks over there are. Some issues didn't seem to get attention in quite a while. |
They're not. At some point someone will carry on the duties and things will go faster. Hopefully... |
We'll see. However, you might have to revisit your contribution as it currently cannot handle some edge cases with multiple nodes. Consider this example: wrapMap('<!--Comment--><tr><td>Content</td></tr>') This will return the default wrap map. Thus the Maybe have another look at the jQuery source, they correctly handle all quirky edge cases I came up with: // Correctly returns the comment and the row.
$('<!--Comment--><tr><td>Content</td></tr>')
// Correctly returns the comment and the row.
// Does not accidentally wrap `<tr>` in comment content with `<table>` tags.
$('<!--<tr><td>Comment</td></tr>--><tr><td>Content</td></tr>') // You may also want to add those two (or similar) cases to your tests there. |
True, i think i just "fixed" one case, and not sure about the other. Added tests too. |
function createFragment(html){
var tmpl = document.createElement('template');
tmpl.innerHTML = html;
if (tmpl.content == void 0){ // ie11
var fragment = document.createDocumentFragment();
var isTableEl = /^[^\S]*?<(t(?:head|body|foot|r|d|h))/i.test(html);
tmpl.innerHTML = isTableEl ? '<table>'+html : html;
var els = isTableEl ? tmpl.querySelector(RegExp.$1).parentNode.childNodes : tmpl.childNodes;
while(els[0]) fragment.appendChild(els[0]);
return fragment;
}
return tmpl.content;
} It creates a document fragment from arbitrary HTML code with
EDIT: No wonder that function magically worked if I'm only testing it in modern browsers... |
The "more sophisticated algorithm" of jQuery seems to be this function, by the way. That one really goes deep into the HTML string. |
Also, the Dojo Toolkit has such a method. Maybe we should get inspiration from their approach, since their |
The one you point on jquery is actually going deep for <script> processing. |
Agreed to the last, IE11 is all I'd target for. It's pretty natural that the dojo method is similar to the jQuery one, they should basically do the same thing. I just mentioned it here because you seemed to have copied only parts of the jQuery method (which is the reason that more complex test cases like the mentioned ones with comments do fail). Just in case that it's easier to extract from there. |
Not exactly, i'm now testing on IE11 and the problems with comments come from serialization (the innerHTML getter of the template polyfill), not from parsing :) |
I might have taken a too short look on your implementation. Isn't your |
Yes it is, but jquery and dojo do the same. jQuery is misleading: it always accept an array of elements to process, so you always find some iterator before actual stuff is being done. |
It's cool, I guess we're pretty good to go already if the above tests do pass. 👍 |
I'm having a look at Edge, IE10, IE9, and some more, just in case. |
IE10 is bad :( but i have a clue at what's going on. |
IE10 pass tests now :) this template polyfill was made in a hurry... |
If you got this all running, maybe consider publishing the solution as a standalone package. 👍 I haven't seen a good one so far and we could require it as a dependency here. Would definitely give us a better separation of concerns. :) |
(What I mean: not the Template polyfill itself but a generic "convert-html-to-DOM" package.) EDIT: Recovered my initial comment and removed all the followup gibberish that did not make any sense. :) |
I won't have time for that, but i released "@kapouer/template" version 1.0.1 with IE9-10-11 compatibility. |
Okay. I probably won't have too much time during the week either, but let's see what we can accomplish. Thanks for all your contributions so far! 👍 |
FYI i forked because i actually need to concentrate on a project with particular needs. I kept you in the authors/LICENSE. Also BTW you're missing a LICENSE file. |
Because
https://github.com/straker/html-tagged-template/blob/c06f23ac/index.js#L150-L157
The text was updated successfully, but these errors were encountered: