From 2c684978b6657acd19ac5a1a6016ed5761a260d2 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Mon, 14 Mar 2016 23:03:25 -0400 Subject: [PATCH] fix loading css for editable content --- .git-ftp-include | 1 + .gitignore | 1 + Makefile | 6 +++++- editor.coffee | 23 ++++++++++++++++++++--- editor_tests.coffee | 2 +- editor_tests_compiled.html | 1 + page_dark.styl | 6 ++++++ 7 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 page_dark.styl diff --git a/.git-ftp-include b/.git-ftp-include index cba16da..ac71a0a 100644 --- a/.git-ftp-include +++ b/.git-ftp-include @@ -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 diff --git a/.gitignore b/.gitignore index 21f7542..dd05b79 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /parser_tests.js /editor.js /editor_tests.js +/page_dark.css diff --git a/Makefile b/Makefile index 257fda2..3c4cdd2 100644 --- 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) diff --git a/editor.coffee b/editor.coffee index 66d1dfe..a5cd8d6 100644 --- a/editor.coffee +++ b/editor.coffee @@ -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 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? diff --git a/editor_tests.coffee b/editor_tests.coffee index 7297567..c45895a 100644 --- a/editor_tests.coffee +++ b/editor_tests.coffee @@ -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' diff --git a/editor_tests_compiled.html b/editor_tests_compiled.html index cff5d6f..f046002 100644 --- a/editor_tests_compiled.html +++ b/editor_tests_compiled.html @@ -4,6 +4,7 @@ html editor tester +