JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
work around bug in firefox (nested iframe onload)
[peach-html5-editor.git] / editor.coffee
index 98595f1..370f2a1 100644 (file)
@@ -277,12 +277,16 @@ control_key_codes = # we react to these, but they aren't typing
        '9':  KEY_TAB
 
 instantiate_tree = (tree, parent) ->
-       for c in tree
+       remove = []
+       for c, i in tree
                switch c.type
                        when TYPE_TEXT
                                c.el = parent.ownerDocument.createTextNode c.text
                                parent.appendChild c.el
                        when TYPE_TAG
+                               if c.name in ['script', 'object', 'iframe', 'link']
+                                       # TODO put placeholders instead
+                                       remove.unshift i
                                # TODO create in correct namespace
                                c.el = parent.ownerDocument.createElement c.name
                                for k, v of c.attrs
@@ -291,6 +295,8 @@ instantiate_tree = (tree, parent) ->
                                parent.appendChild c.el
                                if c.children.length
                                        instantiate_tree c.children, c.el
+       for i in remove
+               tree.splice i, 1
 
 traverse_tree = (tree, state, cb) ->
        for c in tree
@@ -558,6 +564,7 @@ class PeachHTML5Editor
                if @options.editor_id?
                        @outer_iframe.setAttribute 'id', @options.editor_id
                @outer_iframe.onload = =>
+                       console.log 'outer onload'
                        @outer_idoc = @outer_iframe.contentDocument
                        icss = domify @outer_idoc, style: children: [
                                domify @outer_idoc, text: css
@@ -566,6 +573,7 @@ class PeachHTML5Editor
                        @iframe = domify @outer_idoc, iframe: {}
                        @iframe.onload = =>
                                @init()
+                       setTimeout (=> @init()), 200 # firefox never fires this onload
                        @outer_idoc.body.appendChild(
                                domify @outer_idoc, div: id: 'wrap1', children: [
                                        domify @outer_idoc, div: id: 'wrap2', children: [
@@ -587,7 +595,8 @@ class PeachHTML5Editor
                @outer_iframe.setAttribute 'style', outer_iframe_style
                css = outer_css w: outer_bounds.w, h: outer_bounds.h
                outer_wrap.appendChild @outer_iframe
-       init: -> # called by @iframe's onload
+       init: -> # called by @iframe's onload (or timeout on firefox)
+               return if @initialized # ignore timeout for non-broken browsers
                @idoc = @iframe.contentDocument
                @overlay.onclick = (e) =>
                        return @onclick e