JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
preview in iframe, load_html method
[peach-html5-editor.git] / editor.coffee
index c01a2b1..4805550 100644 (file)
@@ -47,12 +47,14 @@ dom_to_html = (dom) ->
                                ret += '<' + el.name
                                attr_keys = []
                                for k of el.attrs
-                                       attr_keys.push k
-                               attr_keys.sort()
+                                       attr_keys.unshift k
+                               #attr_keys.sort()
                                for k in attr_keys
-                                       ret += " #{k}=\"#{enc_attr el.attrs[k]}\""
+                                       ret += " #{k}"
+                                       if el.attrs[k].length > 0
+                                               ret += "=\"#{enc_attr el.attrs[k]}\""
                                ret += '>'
-                               unless ret.name in void_elements
+                               unless void_elements[el.name]
                                        if el.children.length
                                                ret += dom_to_html el.children
                                        ret += "</#{el.name}>"
@@ -66,6 +68,7 @@ dom_to_html = (dom) ->
                                        ret += " \"#{el.public_identifier}\""
                                if el.system_identifier? and el.system_identifier.length > 0
                                        ret += " \"#{el.system_identifier}\""
+                               ret += ">\n"
        return ret
 
 wysiwyg = (el, options = {}) ->
@@ -73,8 +76,18 @@ wysiwyg = (el, options = {}) ->
        parser_opts = {}
        if opt_fragment
                parser_opts.fragment = 'body'
-       editor_instance = dom: wheic_parser.parse(el.value, parser_opts)
-       # el.value = dom_to_html dom
+       editor_instance = {
+               dom: wheic_parser.parse el.value, parser_opts
+               iframe: document.createElement('iframe')
+               load_html: (html) ->
+                       @dom = wheic_parser.parse el.value, parser_opts
+       }
+       idoc = editor_instance.iframe.contentDocument
+       if options.stylesheet # TODO test this
+               istyle = idoc.createElement 'style'
+               istyle.setAttribute 'src', options.stylesheet
+               idoc.head.appendChild istyle
+       el.parentNode.appendChild editor_instance.iframe
        return editor_instance
 
 window.wheic = {