JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix loading css for editable content
authorJason Woofenden <jason@jasonwoof.com>
Tue, 15 Mar 2016 03:03:25 +0000 (23:03 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 15 Mar 2016 03:03:25 +0000 (23:03 -0400)
.git-ftp-include
.gitignore
Makefile
editor.coffee
editor_tests.coffee
editor_tests_compiled.html
page_dark.styl [new file with mode: 0644]

index cba16da..ac71a0a 100644 (file)
@@ -3,3 +3,4 @@ editor_tests.js:editor_tests.coffee
 parser.js:parser.coffee
 parser_no_browser_helper.js:parser_no_browser_helper.coffee
 parser_tests.js:parser_tests.coffee
+page_dark.css:page_dark.styl
index 21f7542..dd05b79 100644 (file)
@@ -3,3 +3,4 @@
 /parser_tests.js
 /editor.js
 /editor_tests.js
+/page_dark.css
index 257fda2..3c4cdd2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,12 @@
-OBJECTS= parser.js parser_tests.js parser_no_browser_helper.js editor.js editor_tests.js
+OBJECTS= parser.js parser_tests.js parser_no_browser_helper.js editor.js editor_tests.js page_dark.css
+
 all: $(OBJECTS)
 
 %.js: %.coffee
        coffee -c $< && sed -i -e 's/\(parser\|parser_no_browser_helper\)[.]coffee/\1.js/g' $@
 
+%.css: %.styl
+       stylus $<
+
 clean:
        rm -f $(OBJECTS)
index 66d1dfe..a5cd8d6 100644 (file)
@@ -24,6 +24,19 @@ next_frame = (cb) ->
        else
                timeout 16, cb
 
+this_url_sans_path = ->
+       ret = "#{window.location.href}"
+       clip = ret.lastIndexOf '#'
+       if clip > -1
+               ret = ret.substr 0, clip
+       clip = ret.lastIndexOf '?'
+       if clip > -1
+               ret = ret.substr 0, clip
+       clip = ret.lastIndexOf '/'
+       if clip > -1
+               ret = ret.substr 0, clip + 1
+       return ret
+
 # xml 1.0 spec, chromium and firefox accept these, plus lots of unicode chars
 valid_attr_regex = new RegExp '^[a-zA-Z_:][-a-zA-Z0-9_:.]*$'
 # html5 spec is much more lax, but chromium won't let me make at attribute with the name "4"
@@ -587,6 +600,7 @@ tree_dedup_space = (tree) ->
 class PeachHTML5Editor
        # Options: (all optional)
        #   editor_id: "id" attribute for outer-most element created by/for editor
+       #   css_file: filename of a css file to style editable content
        #   on_init: callback for when the editable content is in place
        constructor: (in_el, options) ->
                @options = options ? {}
@@ -619,7 +633,7 @@ class PeachHTML5Editor
                                domify @outer_idoc, text: css
                        ]
                        @outer_idoc.head.appendChild icss
-                       @iframe = domify @outer_idoc, iframe: {}
+                       @iframe = domify @outer_idoc, iframe: sandbox: 'allow-same-origin allow-scripts'
                        @iframe.onload = =>
                                @init()
                        setTimeout (=> @init() unless @inited), 200 # firefox never fires this onload
@@ -661,9 +675,12 @@ class PeachHTML5Editor
                @outer_idoc.body.onkeypress = (e) =>
                        @have_focus()
                        return event_return e, @onkeypress e
-               if @options.stylesheet
+               # chromium doesn't resolve relative urls as though they were at the same domain
+               # so add a <base> tag
+               @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
+               if @options.css_file
                        # TODO test this
-                       @idoc.head.appendChild domify @idoc, style: src: @options.stylesheet
+                       @idoc.head.appendChild domify @idoc, link: rel: 'stylesheet', type: 'text/css', href: @options.css_file
                @load_html @in_el.value
                @inited = true
                if @options.on_init?
index 7297567..c45895a 100644 (file)
@@ -1,4 +1,4 @@
 in_el = document.getElementById 'in'
 button = document.getElementById 'button'
 button.parentNode.removeChild button
-window.editor = peach_html5_editor in_el
+window.editor = peach_html5_editor in_el, css_file: 'page_dark.css'
index cff5d6f..f046002 100644 (file)
@@ -4,6 +4,7 @@
        <meta charset="UTF-8">
        <link rel="icon" href="data:null">
        <title>html editor tester</title>
+       <link rel="stylesheet" href="page_dark.css">
        <style>
                textarea {
                        box-sizing: border-box;
diff --git a/page_dark.styl b/page_dark.styl
new file mode 100644 (file)
index 0000000..21ac9db
--- /dev/null
@@ -0,0 +1,6 @@
+body
+       color: white
+       background: black
+
+a, a:link, a:hover, a:visited
+       color: #88f