From 4ec66bc8686ba62d584e6f85e3421b9379d0bfec Mon Sep 17 00:00:00 2001 From: Tom Fulton Date: Fri, 22 May 2015 20:40:30 -0600 Subject: [PATCH] Fix U4-6655: RTE Macro not rendering in backoffice when "macroAlias" is not first attribute Loosens UMBRACO_MACRO regex parsing to fix, add tests --- src/Umbraco.Core/Macros/MacroTagParser.cs | 2 +- src/Umbraco.Tests/Macros/MacroParserTests.cs | 23 +++++++++++++++++++ .../src/common/services/macro.service.js | 2 +- .../common/services/macro-service.spec.js | 12 ++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Macros/MacroTagParser.cs b/src/Umbraco.Core/Macros/MacroTagParser.cs index f731eda56e..0ed7c5bad2 100644 --- a/src/Umbraco.Core/Macros/MacroTagParser.cs +++ b/src/Umbraco.Core/Macros/MacroTagParser.cs @@ -12,7 +12,7 @@ namespace Umbraco.Core.Macros internal class MacroTagParser { private static readonly Regex MacroRteContent = new Regex(@"()", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline); - private static readonly Regex MacroPersistedFormat = new Regex(@"(<\?UMBRACO_MACRO macroAlias=[""']([^""\'\n\r]+?)[""'].+?)(?:/>|>.*?)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex MacroPersistedFormat = new Regex(@"(<\?UMBRACO_MACRO (?:.+)?macroAlias=[""']([^""\'\n\r]+?)[""'].+?)(?:/>|>.*?)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline); /// /// This formats the persisted string to something useful for the rte so that the macro renders properly since we diff --git a/src/Umbraco.Tests/Macros/MacroParserTests.cs b/src/Umbraco.Tests/Macros/MacroParserTests.cs index 27cd949c4a..08477a1649 100644 --- a/src/Umbraco.Tests/Macros/MacroParserTests.cs +++ b/src/Umbraco.Tests/Macros/MacroParserTests.cs @@ -109,6 +109,29 @@ namespace Umbraco.Tests.Macros

asdfasdf

".Replace(Environment.NewLine, string.Empty), result.Replace(Environment.NewLine, string.Empty)); } + [Test] + public void Format_RTE_Data_For_Editor_With_Params_When_MacroAlias_Not_First() + { + var content = @"

asdfasdf

+

asdfsadf

+ +

asdfasdf

"; + var result = MacroTagParser.FormatRichTextPersistedDataForEditor(content, new Dictionary() { { "test1", "value1" }, { "test2", "value2" } }); + +// Assert.AreEqual(@"

asdfasdf

+//

asdfsadf

+//
+// +//Macro alias: Map
+//

asdfasdf

".Replace(Environment.NewLine, string.Empty), result.Replace(Environment.NewLine, string.Empty)); + Assert.AreEqual(@"

asdfasdf

+

asdfsadf

+
+ +Macro alias: Map
+

asdfasdf

".Replace(Environment.NewLine, string.Empty), result.Replace(Environment.NewLine, string.Empty)); + } + [Test] public void Format_RTE_Data_For_Editor_With_Params_Closing_Tag() { diff --git a/src/Umbraco.Web.UI.Client/src/common/services/macro.service.js b/src/Umbraco.Web.UI.Client/src/common/services/macro.service.js index e60ee01ff0..172c0836e7 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/macro.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/macro.service.js @@ -15,7 +15,7 @@ function macroService() { //This regex will match an alias of anything except characters that are quotes or new lines (for legacy reasons, when new macros are created // their aliases are cleaned an invalid chars are stripped) - var expression = /(<\?UMBRACO_MACRO macroAlias=["']([^\"\'\n\r]+?)["'][\s\S]+?)(\/>|>.*?<\/\?UMBRACO_MACRO>)/i; + var expression = /(<\?UMBRACO_MACRO (?:.+)?macroAlias=["']([^\"\'\n\r]+?)["'][\s\S]+?)(\/>|>.*?<\/\?UMBRACO_MACRO>)/i; var match = expression.exec(syntax); if (!match || match.length < 3) { return null; diff --git a/src/Umbraco.Web.UI.Client/test/unit/common/services/macro-service.spec.js b/src/Umbraco.Web.UI.Client/test/unit/common/services/macro-service.spec.js index bdb9cfbd17..ff71516fb5 100644 --- a/src/Umbraco.Web.UI.Client/test/unit/common/services/macro-service.spec.js +++ b/src/Umbraco.Web.UI.Client/test/unit/common/services/macro-service.spec.js @@ -40,6 +40,18 @@ describe('macro service tests', function () { expect(result.macroParamsDictionary.test2).toBe("hello"); }); + it('can parse syntax for macros when macroAlias is not the first parameter', function () { + + var result = macroService.parseMacroSyntax(""); + + expect(result).not.toBeNull(); + expect(result.macroAlias).toBe("Map.Test"); + expect(result.macroParamsDictionary.test).not.toBeUndefined(); + expect(result.macroParamsDictionary.test).toBe("asdf"); + expect(result.macroParamsDictionary.test2).not.toBeUndefined(); + expect(result.macroParamsDictionary.test2).toBe("hello"); + }); + it('can parse syntax for macros with aliases containing whitespace and other chars', function () { var result = macroService.parseMacroSyntax("");