From 0caf6a0f7d236a671c5c517422b55fa07ae9f243 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 29 Sep 2017 09:55:53 -0400 Subject: [PATCH] Support for HTML values in widget hbs templates --- .../discourse/components/discourse-topic.js.es6 | 1 - .../javascripts/discourse/widgets/raw-html.js.es6 | 7 ++++++- lib/javascripts/widget-hbs-compiler.js.es6 | 12 ++++++++++-- script/test_hbs_compiler.rb | 2 ++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/components/discourse-topic.js.es6 b/app/assets/javascripts/discourse/components/discourse-topic.js.es6 index ba81a63bf39..809d3b6f904 100644 --- a/app/assets/javascripts/discourse/components/discourse-topic.js.es6 +++ b/app/assets/javascripts/discourse/components/discourse-topic.js.es6 @@ -99,7 +99,6 @@ export default Ember.Component.extend(AddArchetypeClass, Scrolling, { // this happens after route exit, stuff could have trickled in this.appEvents.trigger('header:hide-topic'); this.appEvents.off('post:highlight'); - }, @observes('Discourse.hasFocus') diff --git a/app/assets/javascripts/discourse/widgets/raw-html.js.es6 b/app/assets/javascripts/discourse/widgets/raw-html.js.es6 index e009d3caf43..740af14d2a7 100644 --- a/app/assets/javascripts/discourse/widgets/raw-html.js.es6 +++ b/app/assets/javascripts/discourse/widgets/raw-html.js.es6 @@ -4,7 +4,7 @@ export default class RawHtml { } init() { - const $html = $(this.html); + const $html = $.parseHTML(this.html); this.decorate($html); return $html[0]; } @@ -20,3 +20,8 @@ export default class RawHtml { } RawHtml.prototype.type = 'Widget'; + +// TODO: Improve how helpers are registered for vdom compliation +if (typeof Discourse !== "undefined") { + Discourse.__widget_helpers.rawHtml = RawHtml; +} diff --git a/lib/javascripts/widget-hbs-compiler.js.es6 b/lib/javascripts/widget-hbs-compiler.js.es6 index 07d418ff2ba..0f84f1c03d6 100644 --- a/lib/javascripts/widget-hbs-compiler.js.es6 +++ b/lib/javascripts/widget-hbs-compiler.js.es6 @@ -64,7 +64,12 @@ function mustacheValue(node, state) { return `__iN("${icon}")`; break; default: - return `${resolve(path)}`; + if (node.escaped) { + return `${resolve(path)}`; + } else { + state.helpersUsed.rawHtml = true; + return `new __rH({ html: '' + ${resolve(path)} + ''})`; + } break; } } @@ -180,7 +185,10 @@ function compile(template) { let imports = ''; if (compiler.state.helpersUsed.iconNode) { - imports = "var __iN = Discourse.__widget_helpers.iconNode; "; + imports += "var __iN = Discourse.__widget_helpers.iconNode; "; + } + if (compiler.state.helpersUsed.rawHtml) { + imports += "var __rH = Discourse.__widget_helpers.rawHtml; "; } return `function(attrs, state) { ${imports}var _r = [];\n${code}\nreturn _r; }`; diff --git a/script/test_hbs_compiler.rb b/script/test_hbs_compiler.rb index a6ef4335923..d18a41fc176 100644 --- a/script/test_hbs_compiler.rb +++ b/script/test_hbs_compiler.rb @@ -1,5 +1,7 @@ template = <<~HBS {{attach widget="widget-name" attrs=attrs}} + {{a}} + {{{htmlValue}}} {{#if state.category}} {{attach widget="category-display" attrs=(hash category=state.category)}} {{/if}}