diff --git a/app/assets/javascripts/discourse/helpers/application_helpers.js b/app/assets/javascripts/discourse/helpers/application_helpers.js index 919688461f0..8cf2d37f5b4 100644 --- a/app/assets/javascripts/discourse/helpers/application_helpers.js +++ b/app/assets/javascripts/discourse/helpers/application_helpers.js @@ -341,18 +341,13 @@ Ember.Handlebars.registerBoundHelper('date', function(dt) { }); /** - Look for custom html content in the preload store. + Look for custom html content using `Discourse.HTML` @method customHTML @for Handlebars **/ Handlebars.registerHelper('customHTML', function(property) { - var html = PreloadStore.get("customHTML"); - - if (html && html[property] && html[property].length) { - return new Handlebars.SafeString(html[property]); - } - + return Discourse.HTML.getCustomHTML(property); }); Ember.Handlebars.registerBoundHelper('humanSize', function(size) { diff --git a/app/assets/javascripts/discourse/lib/html.js b/app/assets/javascripts/discourse/lib/html.js index f3b21f0ef48..a47dd63ed2f 100644 --- a/app/assets/javascripts/discourse/lib/html.js +++ b/app/assets/javascripts/discourse/lib/html.js @@ -1,12 +1,47 @@ /** - Helpers to build HTML strings such as rich links to categories and topics. + Helpers to build HTML strings as well as custom fragments. @class HTML @namespace Discourse @module Discourse **/ + +var customizations = {}; + Discourse.HTML = { + /** + Return a custom fragment of HTML by key. It can be registered via a plugin + using `setCustomHTML(key, html)`. This is used by a handlebars helper to find + the HTML content it wants. It will also check the `PreloadStore` for any server + side preloaded HTML. + + @method getCustomHTML + @param {String} key to lookup + **/ + getCustomHTML: function(key) { + var c = customizations[key]; + if (c) { + return new Handlebars.SafeString(c); + } + + var html = PreloadStore.get("customHTML"); + if (html && html[key] && html[key].length) { + return new Handlebars.SafeString(html[key]); + } + }, + + /** + Set a fragment of HTML by key. It can then be looked up with `getCustomHTML(key)`. + + @method setCustomHTML + @param {String} key to store the html + @param {String} html fragment to store + **/ + setCustomHTML: function(key, html) { + customizations[key] = html; + }, + /** Returns the CSS styles for a category diff --git a/test/javascripts/lib/html_test.js b/test/javascripts/lib/html_test.js index c0d2879d77b..f339e3dca7b 100644 --- a/test/javascripts/lib/html_test.js +++ b/test/javascripts/lib/html_test.js @@ -1,7 +1,9 @@ module("Discourse.HTML"); +var html = Discourse.HTML; + test("categoryLink without a category", function() { - blank(Discourse.HTML.categoryLink(), "it returns no HTML"); + blank(html.categoryLink(), "it returns no HTML"); }); test("Regular categoryLink", function() { @@ -12,7 +14,7 @@ test("Regular categoryLink", function() { color: 'ff0', text_color: 'f00' }), - tag = parseHTML(Discourse.HTML.categoryLink(category))[0]; + tag = parseHTML(html.categoryLink(category))[0]; equal(tag.name, 'a', 'it creates an `a` tag'); equal(tag.attributes['class'], 'badge-category', 'it has the correct class'); @@ -26,7 +28,7 @@ test("Regular categoryLink", function() { test("undefined color", function() { var noColor = Discourse.Category.create({ name: 'hello', id: 123 }), - tag = parseHTML(Discourse.HTML.categoryLink(noColor))[0]; + tag = parseHTML(html.categoryLink(noColor))[0]; blank(tag.attributes.style, "it has no color style because there are no colors"); }); @@ -35,7 +37,18 @@ test("allowUncategorized", function() { var uncategorized = Discourse.Category.create({name: 'uncategorized', id: 345}); this.stub(Discourse.Site, 'currentProp').withArgs('uncategorized_category_id').returns(345); - blank(Discourse.HTML.categoryLink(uncategorized), "it doesn't return HTML for uncategorized by default"); - present(Discourse.HTML.categoryLink(uncategorized, {allowUncategorized: true}), "it returns HTML"); + blank(html.categoryLink(uncategorized), "it doesn't return HTML for uncategorized by default"); + present(html.categoryLink(uncategorized, {allowUncategorized: true}), "it returns HTML"); }); + +test("customHTML", function() { + blank(html.getCustomHTML('evil'), "there is no custom HTML for a key by default"); + + html.setCustomHTML('evil', 'trout'); + equal(html.getCustomHTML('evil'), 'trout', 'it retrieves the custom html'); + + PreloadStore.store('customHTML', {cookie: 'monster'}); + equal(html.getCustomHTML('cookie'), 'monster', 'it returns HTML fragments from the PreloadStore'); + +});