Patchwork [5,of,6,V2] hgweb: add docFromHTML javascript function

login
register
mail settings
Submitter Alexander Plavin
Date Aug. 17, 2013, 10:29 p.m.
Message ID <4b97b9c7034ef5d56102.1376778540@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2205/
State Superseded
Commit 2a9a21e1e1dbfd92c391f4a617556d0da5786bd5
Headers show

Comments

Alexander Plavin - Aug. 17, 2013, 10:29 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@plav.in>
# Date 1376739340 -14400
#      Sat Aug 17 15:35:40 2013 +0400
# Node ID 4b97b9c7034ef5d561021ce6f1f34a2f89764e7c
# Parent  0258e9b301d3d93c820ea640fdc3d19874d01656
hgweb: add docFromHTML javascript function

It takes a string with HTML markup and creates DOM document from it.
Laurens Holst - Aug. 20, 2013, 4:41 p.m.
Op 18-08-13 00:29, Alexander Plavin schreef:
> # HG changeset patch
> # User Alexander Plavin <alexander@plav.in>
> # Date 1376739340 -14400
> #      Sat Aug 17 15:35:40 2013 +0400
> # Node ID 4b97b9c7034ef5d561021ce6f1f34a2f89764e7c
> # Parent  0258e9b301d3d93c820ea640fdc3d19874d01656
> hgweb: add docFromHTML javascript function
>
> It takes a string with HTML markup and creates DOM document from it.
>
> diff -r 0258e9b301d3 -r 4b97b9c7034e mercurial/templates/static/mercurial.js
> --- a/mercurial/templates/static/mercurial.js	Sat Aug 17 15:59:45 2013 +0400
> +++ b/mercurial/templates/static/mercurial.js	Sat Aug 17 15:35:40 2013 +0400
> @@ -329,6 +329,12 @@
>       return xfr;
>   }
>   
> +function docFromHTML(html) {
> +    var doc = document.implementation.createHTMLDocument('');
> +    doc.documentElement.innerHTML = html;
> +    return doc;
> +}

https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation.createHTMLDocument

...says createHTMLDocument() is not supported in IE8.

Which Matt said earlier was important to support.

What about using document.createDocumentFragment() instead? Then you 
also don’t have the issue of needing to import the nodes afterwards, 
cause document fragments are owned by the original document.

~Laurens
Alexander Plavin - Aug. 22, 2013, 7:01 p.m.
20.08.2013, 20:41, "Laurens Holst" <laurens.nospam@grauw.nl>:
> Op 18-08-13 00:29, Alexander Plavin schreef:
>
>>  # HG changeset patch
>>  # User Alexander Plavin <alexander@plav.in>
>>  # Date 1376739340 -14400
>>  #      Sat Aug 17 15:35:40 2013 +0400
>>  # Node ID 4b97b9c7034ef5d561021ce6f1f34a2f89764e7c
>>  # Parent  0258e9b301d3d93c820ea640fdc3d19874d01656
>>  hgweb: add docFromHTML javascript function
>>
>>  It takes a string with HTML markup and creates DOM document from it.
>>
>>  diff -r 0258e9b301d3 -r 4b97b9c7034e mercurial/templates/static/mercurial.js
>>  --- a/mercurial/templates/static/mercurial.js Sat Aug 17 15:59:45 2013 +0400
>>  +++ b/mercurial/templates/static/mercurial.js Sat Aug 17 15:35:40 2013 +0400
>>  @@ -329,6 +329,12 @@
>>        return xfr;
>>    }
>>
>>  +function docFromHTML(html) {
>>  +    var doc = document.implementation.createHTMLDocument('');
>>  +    doc.documentElement.innerHTML = html;
>>  +    return doc;
>>  +}
>
> https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation.createHTMLDocument
>
> ...says createHTMLDocument() is not supported in IE8.
>
> Which Matt said earlier was important to support.

As I understand, it's important to support vital aspects in old browsers (e.g. IE8), but some niceties, not required for the use, can rely on newer features. For example, my (accepted) patch adding a line wrap toggle doesn't support IE8, and I see no problem here too. Here we have same case: ajax scrolling adds another (nicer :) ) way to achieve the same, thus it's possible not to consider old browsers.

Here I meant 'old' not by age mostly, but by amount of well-supported features.

>
> What about using document.createDocumentFragment() instead? Then you
> also don’t have the issue of needing to import the nodes afterwards,
> cause document fragments are owned by the original document.

DocumentFragment was the first solution I tried, but as I understand it's impossible to create it from a string (and here we have a string with a full HTML document). Or, rather, it's possible but only by creating a temporary object first (like createElement or createHTMLDocument) - if so, why use DocumentFragment at all? Through I'm not a JS expert (just learning :) ) so I can be wrong here - if so, please correct me.

>
> ~Laurens

P.S.: sorry for not replying for long, I've read both your messages soon after you sent them, and thought I answered (but actually didn't).
Laurens Holst - Aug. 23, 2013, 9:27 a.m.
Op 22-08-13 21:01, Alexander Plavin schreef:
>
> 20.08.2013, 20:41, "Laurens Holst" <laurens.nospam@grauw.nl>:
>> Op 18-08-13 00:29, Alexander Plavin schreef:
>>
>>>   # HG changeset patch
>>>   # User Alexander Plavin <alexander@plav.in>
>>>   # Date 1376739340 -14400
>>>   #      Sat Aug 17 15:35:40 2013 +0400
>>>   # Node ID 4b97b9c7034ef5d561021ce6f1f34a2f89764e7c
>>>   # Parent  0258e9b301d3d93c820ea640fdc3d19874d01656
>>>   hgweb: add docFromHTML javascript function
>>>
>>>   It takes a string with HTML markup and creates DOM document from it.
>>>
>>>   diff -r 0258e9b301d3 -r 4b97b9c7034e mercurial/templates/static/mercurial.js
>>>   --- a/mercurial/templates/static/mercurial.js Sat Aug 17 15:59:45 2013 +0400
>>>   +++ b/mercurial/templates/static/mercurial.js Sat Aug 17 15:35:40 2013 +0400
>>>   @@ -329,6 +329,12 @@
>>>         return xfr;
>>>     }
>>>
>>>   +function docFromHTML(html) {
>>>   +    var doc = document.implementation.createHTMLDocument('');
>>>   +    doc.documentElement.innerHTML = html;
>>>   +    return doc;
>>>   +}
>> https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation.createHTMLDocument
>>
>> ...says createHTMLDocument() is not supported in IE8.
>>
>> Which Matt said earlier was important to support.
> As I understand, it's important to support vital aspects in old browsers (e.g. IE8), but some niceties, not required for the use, can rely on newer features. For example, my (accepted) patch adding a line wrap toggle doesn't support IE8, and I see no problem here too. Here we have same case: ajax scrolling adds another (nicer :) ) way to achieve the same, thus it's possible not to consider old browsers.
>
> Here I meant 'old' not by age mostly, but by amount of well-supported features.
>
>> What about using document.createDocumentFragment() instead? Then you
>> also don’t have the issue of needing to import the nodes afterwards,
>> cause document fragments are owned by the original document.
> DocumentFragment was the first solution I tried, but as I understand it's impossible to create it from a string (and here we have a string with a full HTML document). Or, rather, it's possible but only by creating a temporary object first (like createElement or createHTMLDocument) - if so, why use DocumentFragment at all? Through I'm not a JS expert (just learning :) ) so I can be wrong here - if so, please correct me.

I guess it mostly makes sense if you intend to replace an existing 
<html> element with outerHTML. If you only have a direct reference to 
the element, outerHTML does nothing because it creates a whole new 
element. And if you create a container element, you’d be putting an 
<html> element inside another again. By using a document fragment as 
container you wouldn’t be doing that.

But I’m also fine with the direction you said you’d take your other mail.

>> ~Laurens
> P.S.: sorry for not replying for long, I've read both your messages soon after you sent them, and thought I answered (but actually didn't).

That’s fine :).

~Laurens

Patch

diff -r 0258e9b301d3 -r 4b97b9c7034e mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js	Sat Aug 17 15:59:45 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Sat Aug 17 15:35:40 2013 +0400
@@ -329,6 +329,12 @@ 
     return xfr;
 }
 
+function docFromHTML(html) {
+    var doc = document.implementation.createHTMLDocument('');
+    doc.documentElement.innerHTML = html;
+    return doc;
+}
+
 function ajaxScrollInit(urlFormat,
                         nextHash,
                         nextHashRegex,