Use document.documentElement.cloneNode() when parsing new documents
Use document.documentElement.cloneNode() when parsing new documents

This lets browsers correctly choose which image to use when using
resolution independent html features such as:

    <img src="image.png" srcset="image@2x.png 2x, image@3x.png 3x">

Without this fix, in many browsers pages loaded through turbolinks
will never show the higher resolution alternatives even when viewed
on retina-like displays.

The reason for this is that browsers based on the latest versions
of both Blink and WebKit (and maybe other engines too) set the
device pixel ratio (DPR) of detached DocumentElements to 1.0. They
only gain a correct DPR when displayed in a browser window, by which
time the choice between multiple resolution images has already been
made.

This affects all existing document creation methods, as they all create
new DocumentElements. Using cloneNode() instead on the existing document
creates a new DocumentElement that inherits its DPR from the currently
displayed page, thereby allowing the correct images to be chosen.

gmalette/turbolinks,schneidmaster/rails-turbolinks,arigesher/turbolinks,saboyutaka/turbolinks,gmalette/turbolinks,imton/turbolinks,ngpestelos/turbolinks,mizchi/turbolinks,ngpestelos/turbolinks,DrewMartin/turbolinks,WojtekKruszewski/turbolinks,peterjm/turbolinks,jasnow/turbolinks,jho406/turbolinks,jho406/turbolinks,missive/turbolinks,elia/turbolinks,jasnow/turbolinks,rails/turbolinks,jvenezia/turbolinks,caulfield/turbolinks,activebridge/turbolinks,caulfield/turbolinks,monkbroc/turbolinks,jnillo/turbolinks,smd686s/turbolinks,maclover7/turbolinks,gmalette/turbolinks,monkbroc/turbolinks,fukayatsu/turbolinks,glittershark/turbolinks,rstacruz/turbolinks,glittershark/turbolinks,turbolinks/turbolinks-classic,philiptzou/turbolinks,arigesher/turbolinks,Novik/turbolinks,jasnow/turbolinks,imton/turbolinks,garbles/turbolinks,ngpestelos/turbolinks,arigesher/turbolinks,schneidmaster/rails-turbolinks,jvenezia/turbolinks,infinum/turbolinks,celsodantas/turbolinks,rails/turbolinks,turbolinks/turbolinks-classic,activebridge/turbolinks,savef/turbolinks,maclover7/turbolinks,maclover7/turbolinks,saboyutaka/turbolinks,savef/turbolinks,jnillo/turbolinks,elia/turbolinks,Ju2ender/turbolinks,garbles/turbolinks,mizchi/turbolinks,celsodantas/turbolinks,rstacruz/turbolinks,schneidmaster/rails-turbolinks,activebridge/turbolinks,LOTSIX/Turbolinks,mizchi/turbolinks,celsodantas/turbolinks,schneidmaster/rails-turbolinks,mizchi/turbolinks,Thibaut/turbolinks,jvenezia/turbolinks,caulfield/turbolinks,Novik/turbolinks,naveed-ahmad/turbolinks,imton/turbolinks,imton/turbolinks,WojtekKruszewski/turbolinks,caulfield/turbolinks,arigesher/turbolinks,Novik/turbolinks,smd686s/turbolinks,missive/turbolinks,christhekeele/turbolinks,rstacruz/turbolinks,turbolinks/turbolinks-classic,elia/turbolinks,hexacyanide/turbolinks,eileencodes/turbolinks,jnillo/turbolinks,rails/turbolinks,ngpestelos/turbolinks,Thibaut/turbolinks,gmalette/turbolinks,naveed-ahmad/turbolinks,Thibaut/turbolinks,smd686s/turbolinks,WojtekKruszewski/turbolinks,LOTSIX/Turbolinks,DrewMartin/turbolinks,monkbroc/turbolinks,Thibaut/turbolinks,WojtekKruszewski/turbolinks,missive/turbolinks,christhekeele/turbolinks,activebridge/turbolinks,elia/turbolinks,jnillo/turbolinks,celsodantas/turbolinks,savef/turbolinks,rails/turbolinks,monkbroc/turbolinks,Novik/turbolinks,saboyutaka/turbolinks,missive/turbolinks,jho406/turbolinks,jho406/turbolinks,naveed-ahmad/turbolinks,turbolinks/turbolinks-classic,naveed-ahmad/turbolinks,saboyutaka/turbolinks,rstacruz/turbolinks,jasnow/turbolinks,savef/turbolinks,jvenezia/turbolinks,maclover7/turbolinks,peterjm/turbolinks,smd686s/turbolinks