     @copyNewHeadProvisionalElements()
 
   replaceBody: ->
-    @activateBodyScriptElements()
-    @importBodyPermanentElements()
+    permanentElements = @extractCurrentBodyPermanentElements()
+    @activateNewBodyScriptElements()
     @assignNewBody()
+    @replaceNewBodyPermanentElements(permanentElements)
+    @replacePlaceholdersForPermanentElements(permanentElements)
 
   shouldRender: ->
     @newSnapshot.isVisitable() and @trackedElementsAreIdentical()

  constructor: (@currentSnapshot, @newSnapshot, @isPreview) ->
    @currentHeadDetails = @currentSnapshot.headDetails
    @newHeadDetails = @newSnapshot.headDetails
    @newBody = @newSnapshot.bodyElement

  render: (callback) ->

  getNewHeadProvisionalElements: ->
    @newHeadDetails.getProvisionalElements()

  getCurrentBodyPermanentElements: ->
    @currentBody.querySelectorAll("[id][data-turbolinks-permanent]")

  findNewBodyPermanentElementById: (id) ->
    @newBody.querySelector("##{id}[data-turbolinks-permanent]")

  findPlaceholderById: (id) ->
    @currentBody.querySelector("meta[name=turbolinks-permanent-placeholder][content='#{id}']")

  getNewBodyScriptElements: ->
    @newBody.querySelectorAll("script")

  findFirstAutofocusableElement: ->
    @newBody.querySelector("[autofocus]")

createPlaceholderForPermanentElement = (permanentElement) ->
  placeholder = document.createElement("meta")
  placeholder.setAttribute("name", "turbolinks-permanent-placeholder")
  placeholder.setAttribute("content", permanentElement.id)
  placeholder

replaceElementWithElement = (fromElement, toElement) ->
  if parentElement = fromElement.parentNode
    parentElement.replaceChild(toElement, fromElement)
    toElement
