X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=editor.coffee;h=0f68f40e4f486b8d9ea72be1f76bfebc95bf4cbf;hb=a85e7976ce04ccbceb207ffe60ffbd212eb5a789;hp=32c1d4df02cacc585e77bde399986d7ef45a8008;hpb=c052fef9484e52df1ac860610ce1620c9a3420c2;p=peach-html5-editor.git
diff --git a/editor.coffee b/editor.coffee
index 32c1d4d..0f68f40 100644
--- a/editor.coffee
+++ b/editor.coffee
@@ -15,9 +15,12 @@
# along with this program. If not, see .
# encode text so it can be safely placed inside an html attribute
+enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
enc_attr = (txt) ->
- # FIXME implement
- return txt
+ return txt.replace enc_attr_regex, (match, amp, quote) ->
+ return '&' if (amp)
+ return '"' if (quote)
+ return ' '
void_elements = {
area: true
@@ -44,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}>"
@@ -63,16 +68,76 @@ 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
-make_wysiwyg = (el, options = {}) ->
+domify = (h) ->
+ for tag, attrs of h
+ if tag is 'text'
+ return document.createTextNode attrs
+ el = document.createElement tag
+ for k, v of attrs
+ if k is 'children'
+ for child in v
+ el.appendChild child
+ else
+ el.setAttribute k, v
+ return el
+
+css = ''
+css += 'span.peach_editor_cursor {'
+css += 'display: inline-block;'
+css += 'height: 1em;'
+css += 'width: 2px;'
+css += 'margin-left: -1px;'
+css += 'margin-right: -1px;'
+css += 'background: #000;'
+css += '-webkit-animation: 1s blink step-end infinite;'
+css += 'animation: 1s blink step-end infinite;'
+css += '}'
+css += '@-webkit-keyframes "blink" {'
+css += 'from, to { background: transparent; }'
+css += '50% { background: #000; }'
+css += '}'
+css += '@keyframes "blink" {'
+css += 'from, to { background: transparent; }'
+css += '50% { background: #000; }'
+css += '}'
+
+wysiwyg = (el, options = {}) ->
opt_fragment = options.fragment ? true
parser_opts = {}
if opt_fragment
parser_opts.fragment = 'body'
- dom = wheic_parser.parse(el.value, parser_opts)
- el.value = dom_to_html dom
+ editor_instance = {
+ dom: []
+ iframe: document.createElement('iframe')
+ load_html: (html) ->
+ @dom = wheic_parser.parse html, parser_opts
+ as_html = wheic.dom_to_html @dom
+ as_html = as_html.substr(0, 5) + '' + as_html.substr(5)
+ @iframe.contentDocument.body.innerHTML = as_html
+ }
+ el.parentNode.appendChild editor_instance.iframe
+ idoc = editor_instance.iframe.contentDocument
+ idoc.body.onkeypress = (e) ->
+ char = e.charCode ? e.keyCode ? e.which
+ el.value += String.fromCharCode char
+ editor_instance.load_html el.value
+ return false
+ if options.stylesheet # TODO test this
+ istyle = idoc.createElement 'style'
+ istyle.setAttribute 'src', options.stylesheet
+ idoc.head.appendChild istyle
+ icss = idoc.createElement 'style'
+ icss.appendChild idoc.createTextNode css
+ idoc.head.appendChild icss
+ editor_instance.load_html el.value
+ return editor_instance
-window.wheic = make_wysiwyg
+window.wheic = {
+ wysiwyg: wysiwyg
+ dom_to_html: dom_to_html
+}
# test in browser: wheic(document.getElementsByTagName('textarea')[0])