From 6613522097a841922b80c0efd1cdc3da6cf85633 Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Wed, 8 Aug 2012 00:20:21 +0200 Subject: [PATCH] implement "special symbols" (switch certain strings for utf-8 symbols), fixes #3140 --- .../javascripts/app/helpers/text_formatter.js | 24 +++++++++++++++ .../app/helpers/text_formatter_spec.js | 29 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/app/assets/javascripts/app/helpers/text_formatter.js b/app/assets/javascripts/app/helpers/text_formatter.js index 5bd45b932..12d140c33 100644 --- a/app/assets/javascripts/app/helpers/text_formatter.js +++ b/app/assets/javascripts/app/helpers/text_formatter.js @@ -53,6 +53,30 @@ return text; }); + // make nice little utf-8 symbols + converter.hooks.chain("preConversion", function(text) { + var input_strings = [ + "<->", "->", "<-", + "(c)", "(r)", "(tm)", + "<3" + ]; + var output_symbols = [ + "↔", "→", "←", + "©", "®", "™", + "♥" + ]; + // quote function from: http://stackoverflow.com/a/494122 + var quote = function(str) { + return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1"); + }; + + _.each(input_strings, function(str, idx) { + var r = new RegExp(quote(str), "gi"); + text = text.replace(r, output_symbols[idx]); + }); + return text; + }); + converter.hooks.chain("postConversion", function (text) { return text.replace(/(\"(?:(?:http|https):\/\/)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:\/\S*)?\")(\>)/g, '$1 target="_blank">') }); diff --git a/spec/javascripts/app/helpers/text_formatter_spec.js b/spec/javascripts/app/helpers/text_formatter_spec.js index d5f9b1f18..1d41d3aa9 100644 --- a/spec/javascripts/app/helpers/text_formatter_spec.js +++ b/spec/javascripts/app/helpers/text_formatter_spec.js @@ -46,6 +46,35 @@ describe("app.helpers.textFormatter", function(){ }) }); + context("symbol conversion", function() { + beforeEach(function() { + this.input_strings = [ + "->", "<-", "<->", + "(c)", "(r)", "(tm)", + "<3" + ]; + this.output_symbols = [ + "→", "←", "↔", + "©", "®", "™", + "♥" + ]; + }); + + it("correctly converts the input strings to their corresponding output symbol", function() { + _.each(this.input_strings, function(str, idx) { + var text = this.formatter.markdownify(str); + expect(text).toContain(this.output_symbols[idx]); + }, this); + }); + + it("converts all symbols at once", function() { + var text = this.formatter.markdownify(this.input_strings.join(" ")); + _.each(this.output_symbols, function(sym) { + expect(text).toContain(sym); + }); + }); + }); + context("non-ascii url", function() { beforeEach(function() { this.evilUrls = [