Issue #AR-4-delinter_fixes: change heuristic for space removal
This commit is contained in:
@@ -1,21 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Archetype.PropertyEditors;
|
||||
using Archetype.Serialization;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.Editors;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Web.PropertyEditors;
|
||||
|
||||
namespace Archetype.Tests.Serialization.Base
|
||||
{
|
||||
|
||||
@@ -56,7 +56,11 @@ namespace Archetype.Tests.Serialization.Delinter
|
||||
{
|
||||
const string _JSON_WITH_RESERVED_CHARS = @"{
|
||||
""alias"": ""captions"",
|
||||
""value"": ""{\""fieldsets\"":[{\""properties\"":[{\""alias\"":\""textstring\"",\""value\"":\""test1 \r\n test1\r\n\""}],\""alias\"":\""textstringArray\""},{\""properties\"":[{\""alias\"":\""textstring\"",\""value\"":\""test2\r\ntest2\""}],\""alias\"":\""textstringArray\""},{\""properties\"":[{\""alias\"":\""textstring\"",\""value\"":\""test3b \\r\\r\\rtest3\""}],\""alias\"":\""textstringArray\""}]}""
|
||||
""value"": ""{ \""fieldsets\"": [
|
||||
{
|
||||
\""properties\"":
|
||||
[
|
||||
{ \""alias\"": \""textstring\"" , \""value\"":\""test1 \r\n test1\r\n\""}], \""alias\"":\""textstringArray\"" },{\""properties\"": [ { \""alias\"":\""textstring\"",\""value\"":\""test2\r\ntest2\""}],\""alias\"":\""textstringArray\""},{\""properties\"":[{\""alias\"":\""textstring\"",\""value\"":\""test3b \""quote\"" \\r\\r\\rtest3\""}],\""alias\"":\""textstringArray\"" } ] }""
|
||||
}";
|
||||
|
||||
var referenceModel = new Captions()
|
||||
@@ -66,10 +70,13 @@ namespace Archetype.Tests.Serialization.Delinter
|
||||
"},
|
||||
new TextString() {Text = @"test2
|
||||
test2"},
|
||||
new TextString() {Text = @"test3b \r\r\rtest3"}
|
||||
new TextString() {Text = @"test3b ""quote"" \r\r\rtest3"}
|
||||
};
|
||||
|
||||
var actualModel = ConvertArchetypeJsonToModel<Captions>(_JSON_WITH_RESERVED_CHARS.DelintArchetypeJson());
|
||||
var delintedJson = _JSON_WITH_RESERVED_CHARS.DelintArchetypeJson();
|
||||
var actualModel = ConvertArchetypeJsonToModel<Captions>(delintedJson);
|
||||
|
||||
var t = JToken.Parse(delintedJson).ToString(Formatting.None);
|
||||
|
||||
Assert.IsInstanceOf<Captions>(actualModel);
|
||||
Assert.IsNotNull(actualModel);
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Archetype.Serialization
|
||||
{
|
||||
public enum DelinterStep
|
||||
{
|
||||
RemoveWhiteSpace = 0,
|
||||
RemoveNewLine,
|
||||
{
|
||||
RemoveNewLine = 0,
|
||||
RemoveWhiteSpace,
|
||||
UnescapeLabels,
|
||||
UnescapeAlias,
|
||||
UnescapeValues,
|
||||
@@ -25,9 +26,9 @@ namespace Archetype.Serialization
|
||||
|
||||
public class ArchetypeJsonDelinter
|
||||
{
|
||||
public IDictionary<DelinterStep, DelinterAction> Pipeline { get; set; }
|
||||
public IDictionary<DelinterStep, Regex> Tokens { get; set; }
|
||||
public IDictionary<DelinterAction, Func<string, Regex, string>> Actions { get; set; }
|
||||
public IDictionary<DelinterStep, DelinterAction> Pipeline { get; private set; }
|
||||
public IDictionary<DelinterStep, Regex> Tokens { get; private set; }
|
||||
public IDictionary<DelinterAction, Func<string, Regex, string>> Actions { get; private set; }
|
||||
|
||||
public ArchetypeJsonDelinter()
|
||||
{
|
||||
@@ -52,7 +53,7 @@ namespace Archetype.Serialization
|
||||
Tokens = new Dictionary<DelinterStep, Regex>
|
||||
{
|
||||
{DelinterStep.RemoveNewLine, new Regex(@"(?<=\,){0,1}(\\+r\\+n)(?=\\*?""(alias|value|properties|fieldsets)\\*?"":\{*?(\\*|""|\[))|(\r|\n)+|(\\+r\\+n)(?=\s*?(\{|\}|\]))|(\\+r\\n+)(?=\s+\\*?""(alias|value|properties|fieldsets))")},
|
||||
{DelinterStep.RemoveWhiteSpace, new Regex(@"(\s*?)\\+""(fieldsets|properties|alias|value)\\+"":(\s+)|""(\s*?)\},(\s+)\{|\[(\s+)\{|[\]\}](\s+)[\]\}]")},
|
||||
{DelinterStep.RemoveWhiteSpace, new Regex(@"(""\S+?"":)(\s+?)("")|([\{\[\}\],]|(?<!\\)"")(\s+)([\{\[\}\],]|(?<!\\)"")")},
|
||||
{DelinterStep.UnescapeLabels, new Regex(@"\\+""(fieldsets|properties|alias|value)\\+"":(\s*)")},
|
||||
{DelinterStep.UnescapeAlias, new Regex(@"""(alias)"":\\+""(.*?)\\+""")},
|
||||
{DelinterStep.UnescapeValues, new Regex(@"""(value)"":\\+""(.*?)\\+""(?=\s*?\})")},
|
||||
@@ -65,7 +66,13 @@ namespace Archetype.Serialization
|
||||
RecursiveReplace(input, pattern, match => String.Empty)
|
||||
},
|
||||
{DelinterAction.RemoveWhiteSpace, (input, pattern) =>
|
||||
RecursiveReplace(input, pattern, match => match.Groups[2].Success ? match.Groups[0].Value : String.Empty)
|
||||
RecursiveReplace(input.Trim(), pattern, match =>
|
||||
String.Join(String.Empty,
|
||||
match.Groups
|
||||
.OfType<Group>()
|
||||
.Select((g, i) => String.IsNullOrWhiteSpace(g.Value) ? String.Empty : g.Value)
|
||||
.Skip(1)
|
||||
.ToArray()))
|
||||
},
|
||||
{DelinterAction.UnescapeLabels, (input, pattern) =>
|
||||
RecursiveReplace(input, pattern, match => String.Format(@"""{0}"":", match.Groups[1].Value))
|
||||
@@ -85,7 +92,7 @@ namespace Archetype.Serialization
|
||||
{DelinterStep.UnescapeAlias, DelinterAction.UnescapeValues},
|
||||
{DelinterStep.UnescapeValues, DelinterAction.UnescapeValues},
|
||||
{DelinterStep.FixNestedFieldsets, DelinterAction.FixNestedFieldsets},
|
||||
//{DelinterStep.RemoveWhiteSpace, DelinterAction.RemoveWhiteSpace},
|
||||
{DelinterStep.RemoveWhiteSpace, DelinterAction.RemoveWhiteSpace},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -94,7 +101,7 @@ namespace Archetype.Serialization
|
||||
var buffer = input;
|
||||
while (pattern.IsMatch(buffer))
|
||||
{
|
||||
buffer = pattern.Replace(input, match => matchFunc(match));
|
||||
buffer = pattern.Replace(buffer, match => matchFunc(match));
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user