Skip to content

Instantly share code, notes, and snippets.

@lemonmade
Created May 21, 2015 14:34
Show Gist options
  • Select an option

  • Save lemonmade/c24f2e0691d04878f4e0 to your computer and use it in GitHub Desktop.

Select an option

Save lemonmade/c24f2e0691d04878f4e0 to your computer and use it in GitHub Desktop.
#*
# Decodes HTML entities into their regular string values.
#
# @private
# @param {String} str - The encoded string.
# @returns {String} The decoded string.
decode_html_entities = (str) ->
element = document.createElement('div')
element.innerHTML = str.trim()
if element.childNodes.length == 0 then "" else (element.childNodes[0].nodeValue || element.innerHTML)
#*
# Moves the markup for for an iframe into the actual iframe. This looks for the
# `iframe__content` sibling node of the iframe, takes its inner HTML, decodes
# the escaped entities, and writes the entirety of the resulting string (which
# includes the HTML element and all children) to the iframe's window.
#
# @private
# @param {DOMElement} iframe - The iframe to inject content into.
# @returns nothing
move_markup_to_iframe = (iframe) ->
iframe_content = iframe.parentNode.querySelector(".#{CLASSES.CONTENT}")
iframeWindow = iframe.contentWindow
return unless iframe_content && iframeWindow
iframeWindow.document.open()
iframeWindow.document.write(decode_html_entities(iframe_content.innerHTML))
iframeWindow.document.close()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment