Ejemplo n.º 1
0
        public override ITemplate Build()
        {
            var        id                  = _id ?? 0;
            Guid       key                 = _key ?? Guid.NewGuid();
            var        name                = _name ?? Guid.NewGuid().ToString();
            var        alias               = _alias ?? name.ToCamelCase();
            DateTime   createDate          = _createDate ?? DateTime.Now;
            DateTime   updateDate          = _updateDate ?? DateTime.Now;
            var        path                = _path ?? $"-1,{id}";
            var        content             = _content;
            var        isMasterTemplate    = _isMasterTemplate ?? false;
            var        masterTemplateAlias = _masterTemplateAlias ?? string.Empty;
            Lazy <int> masterTemplateId    = _masterTemplateId ?? new Lazy <int>(() => - 1);

            var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());

            var template = new Template(shortStringHelper, name, alias)
            {
                Id                  = id,
                Key                 = key,
                CreateDate          = createDate,
                UpdateDate          = updateDate,
                Path                = path,
                Content             = content,
                IsMasterTemplate    = isMasterTemplate,
                MasterTemplateAlias = masterTemplateAlias,
                MasterTemplateId    = masterTemplateId,
            };

            return(template);
        }
Ejemplo n.º 2
0
    public void CleanStringLeadingChars()
    {
        var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                  .WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            // letters and digits are valid leading chars
            IsTerm     = (c, leading) => char.IsLetterOrDigit(c),
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
            Separator  = '*',
        }));

        Assert.AreEqual("0123foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                              .WithDefault(new RequestHandlerSettings())
                                              .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            // only letters are valid leading chars
            IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
            Separator  = '*',
        }));
        Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123foo_bar 543 nil 321", CleanStringType.Alias));
        Assert.AreEqual("foo*bar*543*nil*321", helper.CleanString("0123 foo_bar 543 nil 321", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(
            new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings()));
        Assert.AreEqual("child2", helper.CleanStringForSafeAlias("1child2"));
    }
Ejemplo n.º 3
0
        public void CleanStringSeparator()
        {
            var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
                                                      .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '*'
            }));

            Assert.AreEqual("foo*bar", helper.CleanString("foo bar", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = ' '
            }));
            Assert.AreEqual("foo bar", helper.CleanString("foo bar", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged
            }));
            Assert.AreEqual("foobar", helper.CleanString("foo bar", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '文'
            }));
            Assert.AreEqual("foo文bar", helper.CleanString("foo bar", CleanStringType.Alias));
        }
Ejemplo n.º 4
0
    public void CleanStringTermOnUpper()
    {
        var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                  .WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,

            // uppercase letter means new term
            BreakTermsOnUpper = true,
            Separator         = '*',
        }));

        Assert.AreEqual("foo*Bar", helper.CleanString("fooBar", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                              .WithDefault(new RequestHandlerSettings())
                                              .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,

            // uppercase letter is part of term
            BreakTermsOnUpper = false,
            Separator         = '*',
        }));
        Assert.AreEqual("fooBar", helper.CleanString("fooBar", CleanStringType.Alias));
    }
        public void CleanStringDefaultConfig()
        {
            var settings    = SettingsForTests.GenerateMockSettings();
            var contentMock = Mock.Get(settings.RequestHandler);

            contentMock.Setup(x => x.CharCollection).Returns(Enumerable.Empty <IChar>());
            contentMock.Setup(x => x.ConvertUrlsToAscii).Returns(false);
            SettingsForTests.ConfigureSettings(settings);

            var helper = new DefaultShortStringHelper(settings).WithDefaultConfig();

            const string input = "0123 中文测试 中文测试 léger ZÔRG (2) a?? *x";

            var alias    = helper.CleanStringForSafeAlias(input);
            var filename = helper.CleanStringForSafeFileName(input);
            var segment  = helper.CleanStringForUrlSegment(input);

            // umbraco-cased ascii alias, must begin with a proper letter
            Assert.AreEqual("legerZORG2AX", alias, "alias");

            // lower-cased, utf8 filename, removing illegal filename chars, using dash-separator
            Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", filename, "filename");

            // lower-cased, utf8 url segment, only letters and digits, using dash-separator
            Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", segment, "segment");
        }
Ejemplo n.º 6
0
    public void CleanStringAcronymOnNonUpper()
    {
        var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                  .WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,

            // non-uppercase letter means cut acronym
            CutAcronymOnNonUpper = true,
            Separator            = '*',
        }));

        Assert.AreEqual("foo*BAR*Rnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BA*Rnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
        Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                              .WithDefault(new RequestHandlerSettings())
                                              .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,

            // non-uppercase letter means word
            CutAcronymOnNonUpper = false,
            Separator            = '*',
        }));
        Assert.AreEqual("foo*BARRnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BARnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
        Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
    }
Ejemplo n.º 7
0
    public void CleanStringEncoding()
    {
        var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                  .WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(
                                                      CleanStringType.Alias,
                                                      new DefaultShortStringHelperConfig.Config
        {
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
            Separator  = '*',
        }));

        Assert.AreEqual("中文测试", helper.CleanString("中文测试", CleanStringType.Alias));
        Assert.AreEqual("léger*中文测试*ZÔRG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                              .WithDefault(new RequestHandlerSettings())
                                              .WithConfig(
                                                  CleanStringType.Alias,
                                                  new DefaultShortStringHelperConfig.Config
        {
            StringType = CleanStringType.Ascii | CleanStringType.Unchanged,
            Separator  = '*',
        }));
        Assert.AreEqual(string.Empty, helper.CleanString("中文测试", CleanStringType.Alias));
        Assert.AreEqual("leger*ZORG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
    }
Ejemplo n.º 8
0
    public void CleanStringGreedyAcronyms()
    {
        var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                  .WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(
                                                      CleanStringType.Alias,
                                                      new DefaultShortStringHelperConfig.Config
        {
            StringType           = CleanStringType.Utf8 | CleanStringType.Unchanged,
            CutAcronymOnNonUpper = true,
            GreedyAcronyms       = true,
            Separator            = '*',
        }));

        Assert.AreEqual("foo*BARR*nil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BAR*nil", helper.CleanString("foo BARnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BA*nil", helper.CleanString("foo BAnil", CleanStringType.Alias));
        Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                              .WithDefault(new RequestHandlerSettings())
                                              .WithConfig(
                                                  CleanStringType.Alias,
                                                  new DefaultShortStringHelperConfig.Config
        {
            StringType           = CleanStringType.Utf8 | CleanStringType.Unchanged,
            CutAcronymOnNonUpper = true,
            GreedyAcronyms       = false,
            Separator            = '*',
        }));
        Assert.AreEqual("foo*BAR*Rnil", helper.CleanString("foo BARRnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BA*Rnil", helper.CleanString("foo BARnil", CleanStringType.Alias));
        Assert.AreEqual("foo*BAnil", helper.CleanString("foo BAnil", CleanStringType.Alias));
        Assert.AreEqual("foo*Bnil", helper.CleanString("foo Bnil", CleanStringType.Alias));
    }
Ejemplo n.º 9
0
    public void CleanStringDefaultConfig()
    {
        var requestHandlerSettings = new RequestHandlerSettings
        {
            CharCollection = Array.Empty <CharItem>(),
            EnableDefaultCharReplacements = false,
            ConvertUrlsToAscii            = "false",
        };

        var helper =
            new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings));

        const string input = "0123 中文测试 中文测试 léger ZÔRG (2) a?? *x";

        var alias    = helper.CleanStringForSafeAlias(input);
        var filename = helper.CleanStringForSafeFileName(input);
        var segment  = helper.CleanStringForUrlSegment(input);

        // umbraco-cased ascii alias, must begin with a proper letter
        Assert.AreEqual("legerZORG2AX", alias, "alias");

        // lower-cased, utf8 filename, removing illegal filename chars, using dash-separator
        Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", filename, "filename");

        // lower-cased, utf8 URL segment, only letters and digits, using dash-separator
        Assert.AreEqual("0123-中文测试-中文测试-léger-zôrg-2-a-x", segment, "segment");
    }
        public void CleanStringSeparator()
        {
            var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                         .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '*'
            });

            Assert.AreEqual("foo*bar", helper.CleanString("foo bar", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                     .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = ' '
            });
            Assert.AreEqual("foo bar", helper.CleanString("foo bar", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                     .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged
            });
            Assert.AreEqual("foobar", helper.CleanString("foo bar", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                     .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '文'
            });
            Assert.AreEqual("foo文bar", helper.CleanString("foo bar", CleanStringType.Alias));
        }
        public void U4_4056_TryAscii()
        {
            var settings    = SettingsForTests.GenerateMockSettings();
            var contentMock = Mock.Get(settings.RequestHandler);

            contentMock.Setup(x => x.CharCollection).Returns(Enumerable.Empty <IChar>());
            contentMock.Setup(x => x.ConvertUrlsToAscii).Returns(false);
            SettingsForTests.ConfigureSettings(settings);

            const string input1 = "ÆØÅ and æøå and 中文测试 and  אודות האתר and größer БбДдЖж page";
            const string input2 = "ÆØÅ and æøå and größer БбДдЖж page";

            var helper = new DefaultShortStringHelper(settings).WithDefaultConfig(); // unicode

            Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
            Assert.AreEqual("æøå-and-æøå-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input2));

            helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                     .WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelper.Config
            {
                IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
                StringType = CleanStringType.LowerCase | CleanStringType.TryAscii,     // try ascii
                Separator  = '-'
            });
            Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
            Assert.AreEqual("aeoa-and-aeoa-and-grosser-bbddzhzh-page", helper.CleanStringForUrlSegment(input2));
        }
Ejemplo n.º 12
0
    public void U4_4056_TryAscii()
    {
        var requestHandlerSettings = new RequestHandlerSettings
        {
            CharCollection = Array.Empty <CharItem>(),
            EnableDefaultCharReplacements = false,
            ConvertUrlsToAscii            = "false",
        };

        const string input1 = "ÆØÅ and æøå and 中文测试 and  אודות האתר and größer БбДдЖж page";
        const string input2 = "ÆØÅ and æøå and größer БбДдЖж page";

        var helper =
            new DefaultShortStringHelper(
                new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)); // unicode

        Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
        Assert.AreEqual("æøå-and-æøå-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input2));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)
                                              .WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
        {
            IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
            StringType = CleanStringType.LowerCase | CleanStringType.TryAscii,     // try ascii
            Separator  = '-',
        }));
        Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", helper.CleanStringForUrlSegment(input1));
        Assert.AreEqual("aeoa-and-aeoa-and-grosser-bbddzhzh-page", helper.CleanStringForUrlSegment(input2));
    }
Ejemplo n.º 13
0
    public void CleanStringUnderscoreInTerm()
    {
        var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                  .WithDefault(new RequestHandlerSettings())
                                                  .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            // underscore is accepted within terms
            IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
            Separator  = '*',
        }));

        Assert.AreEqual("foo_bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));

        helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                              .WithDefault(new RequestHandlerSettings())
                                              .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
        {
            // underscore is not accepted within terms
            IsTerm     = (c, leading) => char.IsLetterOrDigit(c),
            StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
            Separator  = '*',
        }));
        Assert.AreEqual("foo*bar*nil", helper.CleanString("foo_bar nil", CleanStringType.Alias));
    }
Ejemplo n.º 14
0
        [TestCase("WhoIsNumber6InTheVillage", "Who Is Number6 In The Village")] // issue is fixed
        public void CompatibleDefaultReplacement(string input, string expected)
        {
            var helper = new DefaultShortStringHelper(SettingsForTests.GetDefaultUmbracoSettings());
            var output = input.Length < 2 ? input : helper.SplitPascalCasing(input, ' ').ToFirstUpperInvariant();

            Assert.AreEqual(expected, output);
        }
        public void U4_4056()
        {
            var requestHandlerSettings = new RequestHandlerSettings()
            {
                CharCollection     = Enumerable.Empty <IChar>(),
                ConvertUrlsToAscii = "false"
            };

            const string input = "ÆØÅ and æøå and 中文测试 and  אודות האתר and größer БбДдЖж page";

            var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)); // unicode
            var output = helper.CleanStringForUrlSegment(input);

            Assert.AreEqual("æøå-and-æøå-and-中文测试-and-אודות-האתר-and-größer-ббдджж-page", output);

            helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(requestHandlerSettings)
                                                  .WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
            {
                IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,     // ascii
                Separator  = '-'
            }));
            output = helper.CleanStringForUrlSegment(input);
            Assert.AreEqual("aeoa-and-aeoa-and-and-and-grosser-bbddzhzh-page", output);
        }
        public void CleanStringWithUnderscore(string input, string expected, bool allowUnderscoreInTerm)
        {
            var helper = new DefaultShortStringHelper()
                         .WithConfig(allowUnderscoreInTerm: allowUnderscoreInTerm);
            var output = helper.CleanString(input, CleanStringType.Alias | CleanStringType.Ascii | CleanStringType.CamelCase);

            Assert.AreEqual(expected, output);
        }
Ejemplo n.º 17
0
    public void SetUp() =>

    // NOTE pre-filters runs _before_ Recode takes place
    // so there still may be utf8 chars even though you want ascii
    ShortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig()
                                                     .WithDefault(new RequestHandlerSettings())
                                                     .WithConfig(CleanStringType.FileName, new DefaultShortStringHelperConfig.Config
    {
        // PreFilter = ClearFileChars, // done in IsTerm
        IsTerm = (c, leading) =>
                 (char.IsLetterOrDigit(c) || c == '_') && DefaultShortStringHelper.IsValidFileNameChar(c),
        StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
        Separator  = '-',
    })
                                                     .WithConfig(
                                                         CleanStringType.UrlSegment,
                                                         new DefaultShortStringHelperConfig.Config
    {
        PreFilter  = StripQuotes,
        IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
        StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
        Separator  = '-',
    })
                                                     .WithConfig(
                                                         "fr-FR",
                                                         CleanStringType.UrlSegment,
                                                         new DefaultShortStringHelperConfig.Config
    {
        PreFilter  = FilterFrenchElisions,
        IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c) || c == '_',
        StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
        Separator  = '-',
    })
                                                     .WithConfig(
                                                         CleanStringType.Alias,
                                                         new DefaultShortStringHelperConfig.Config
    {
        PreFilter  = StripQuotes,
        IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
        StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii,
    })
                                                     .WithConfig(
                                                         "fr-FR",
                                                         CleanStringType.Alias,
                                                         new DefaultShortStringHelperConfig.Config
    {
        PreFilter  = WhiteQuotes,
        IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
        StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii,
    })
                                                     .WithConfig(CleanStringType.ConvertCase, new DefaultShortStringHelperConfig.Config
    {
        PreFilter         = null,
        IsTerm            = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore
        StringType        = CleanStringType.Ascii,
        BreakTermsOnUpper = true,
    }));
        public void Initialize()
        {
            // NOTE: it is not possible to configure the helper once it has been assigned
            // to the resolver and resolution has frozen. but, obviously, it is possible
            // to configure filters and then to alter these filters after resolution has
            // frozen. beware, nothing is thread-safe in-there!

            // NOTE pre-filters runs _before_ Recode takes place
            // so there still may be utf8 chars even though you want ascii

            _helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                      .WithConfig(CleanStringType.FileName, new DefaultShortStringHelper.Config
            {
                //PreFilter = ClearFileChars, // done in IsTerm
                IsTerm     = (c, leading) => (char.IsLetterOrDigit(c) || c == '_') && DefaultShortStringHelper.IsValidFileNameChar(c),
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
                Separator  = '-'
            })
                      .WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelper.Config
            {
                PreFilter  = StripQuotes,
                IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
                Separator  = '-'
            })
                      .WithConfig(new CultureInfo("fr-FR"), CleanStringType.UrlSegment, new DefaultShortStringHelper.Config
            {
                PreFilter  = FilterFrenchElisions,
                IsTerm     = (c, leading) => leading ? char.IsLetter(c) : (char.IsLetterOrDigit(c) || c == '_'),
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
                Separator  = '-'
            })
                      .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                PreFilter  = StripQuotes,
                IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
                StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii
            })
                      .WithConfig(new CultureInfo("fr-FR"), CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                PreFilter  = WhiteQuotes,
                IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
                StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii
            })
                      .WithConfig(CleanStringType.ConvertCase, new DefaultShortStringHelper.Config
            {
                PreFilter         = null,
                IsTerm            = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore
                StringType        = CleanStringType.Ascii,
                BreakTermsOnUpper = true
            });

            ShortStringHelperResolver.Reset();
            ShortStringHelperResolver.Current = new ShortStringHelperResolver(_helper);
            Resolution.Freeze();
        }
        public void CleanStringWhiteSpace()
        {
            var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(SettingsForTests.GetDefaultUmbracoSettings())
                                                      .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '*'
            }));

            Assert.AreEqual("foo", helper.CleanString("   foo   ", CleanStringType.Alias));
            Assert.AreEqual("foo*bar", helper.CleanString("   foo   bar   ", CleanStringType.Alias));
        }
        public override void SetUp()
        {
            base.SetUp();

            // NOTE pre-filters runs _before_ Recode takes place
            // so there still may be utf8 chars even though you want ascii

            _helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(SettingsForTests.GetDefaultUmbracoSettings())
                                                   .WithConfig(CleanStringType.FileName, new DefaultShortStringHelperConfig.Config
            {
                //PreFilter = ClearFileChars, // done in IsTerm
                IsTerm     = (c, leading) => (char.IsLetterOrDigit(c) || c == '_') && DefaultShortStringHelper.IsValidFileNameChar(c),
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
                Separator  = '-'
            })
                                                   .WithConfig(CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
            {
                PreFilter  = StripQuotes,
                IsTerm     = (c, leading) => char.IsLetterOrDigit(c) || c == '_',
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
                Separator  = '-'
            })
                                                   .WithConfig("fr-FR", CleanStringType.UrlSegment, new DefaultShortStringHelperConfig.Config
            {
                PreFilter  = FilterFrenchElisions,
                IsTerm     = (c, leading) => leading ? char.IsLetter(c) : (char.IsLetterOrDigit(c) || c == '_'),
                StringType = CleanStringType.LowerCase | CleanStringType.Ascii,
                Separator  = '-'
            })
                                                   .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                PreFilter  = StripQuotes,
                IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
                StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii
            })
                                                   .WithConfig("fr-FR", CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                PreFilter  = WhiteQuotes,
                IsTerm     = (c, leading) => leading ? char.IsLetter(c) : char.IsLetterOrDigit(c),
                StringType = CleanStringType.UmbracoCase | CleanStringType.Ascii
            })
                                                   .WithConfig(CleanStringType.ConvertCase, new DefaultShortStringHelperConfig.Config
            {
                PreFilter         = null,
                IsTerm            = (c, leading) => char.IsLetterOrDigit(c) || c == '_', // letter, digit or underscore
                StringType        = CleanStringType.Ascii,
                BreakTermsOnUpper = true
            }));

            // fixme - move to a "compose" thing?
            Composition.RegisterUnique <IShortStringHelper>(f => _helper);
        }
        public void CleanStringSymbols()
        {
            var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                         .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '*'
            });

            Assert.AreEqual("house*2", helper.CleanString("house (2)", CleanStringType.Alias));

            // FIXME but for a filename we want to keep them!
            // FIXME and what about a url?
        }
Ejemplo n.º 22
0
        public void CleanStringSymbols()
        {
            var helper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig().WithDefault(new RequestHandlerSettings())
                                                      .WithConfig(CleanStringType.Alias, new DefaultShortStringHelperConfig.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '*'
            }));

            Assert.AreEqual("house*2", helper.CleanString("house (2)", CleanStringType.Alias));

            // FIXME: but for a filename we want to keep them!
            // FIXME: and what about a URL?
        }
        public void CleanStringCasing()
        {
            var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                         .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = ' '
            });

            // BBB is an acronym
            // E is a word (too short to be an acronym)
            // FF is an acronym

            // FIXME "C" can't be an acronym
            // FIXME "DBXreview" = acronym?!

            Assert.AreEqual("aaa BBB CCc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias)); // unchanged
            Assert.AreEqual("aaa Bbb Ccc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("Aaa Bbb Ccc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("aaa bbb ccc ddd e ff", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.LowerCase));
            Assert.AreEqual("AAA BBB CCC DDD E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.UpperCase));
            Assert.AreEqual("aaa BBB CCc Ddd E FF", helper.CleanString("aaa BBB CCc Ddd E FF", CleanStringType.Alias | CleanStringType.UmbracoCase));

            // MS rules & guidelines:
            // - Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.
            //     eg "DBRate" (pascal) or "ioHelper" (camel) - "SpecialDBRate" (pascal) or "specialIOHelper" (camel)
            // - Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.
            //     eg "XmlWriter (pascal) or "htmlReader" (camel) - "SpecialXmlWriter" (pascal) or "specialHtmlReader" (camel)
            // - Do not capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.
            //     eg "xmlWriter" or "dbWriter" (camel)

            Assert.AreEqual("aaa BB Ccc", helper.CleanString("aaa BB ccc", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("aa Bb Ccc", helper.CleanString("AA bb ccc", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("aaa Bb Ccc", helper.CleanString("AAA bb ccc", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("db Rate", helper.CleanString("DB rate", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("special DB Rate", helper.CleanString("special DB rate", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("xml Writer", helper.CleanString("XML writer", CleanStringType.Alias | CleanStringType.CamelCase));
            Assert.AreEqual("special Xml Writer", helper.CleanString("special XML writer", CleanStringType.Alias | CleanStringType.CamelCase));

            Assert.AreEqual("Aaa BB Ccc", helper.CleanString("aaa BB ccc", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("AA Bb Ccc", helper.CleanString("AA bb ccc", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("Aaa Bb Ccc", helper.CleanString("AAA bb ccc", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("DB Rate", helper.CleanString("DB rate", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("Special DB Rate", helper.CleanString("special DB rate", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("Xml Writer", helper.CleanString("XML writer", CleanStringType.Alias | CleanStringType.PascalCase));
            Assert.AreEqual("Special Xml Writer", helper.CleanString("special XML writer", CleanStringType.Alias | CleanStringType.PascalCase));
        }
        public void Setup()
        {
            // NOTE: it is not possible to configure the helper once it has been assigned
            // to the resolver and resolution has frozen. but, obviously, it is possible
            // to configure filters and then to alter these filters after resolution has
            // frozen. beware, nothing is thread-safe in-there!

            // NOTE pre-filters runs _before_ Recode takes place
            // so there still may be utf8 chars even though you want ascii

            _helper = new DefaultShortStringHelper()
                      .WithConfig(CleanStringType.Url, StripQuotes, allowLeadingDigits: true)
                      .WithConfig(new CultureInfo("fr-FR"), CleanStringType.Url, FilterFrenchElisions, allowLeadingDigits: true)
                      .WithConfig(CleanStringType.Alias, StripQuotes)
                      .WithConfig(new CultureInfo("fr-FR"), CleanStringType.Alias, WhiteQuotes);

            ShortStringHelperResolver.Reset();
            ShortStringHelperResolver.Current = new ShortStringHelperResolver(_helper);
            Resolution.Freeze();
        }
        public void CleanStringEncoding()
        {
            var helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                         .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Utf8 | CleanStringType.Unchanged,
                Separator  = '*'
            });

            Assert.AreEqual("中文测试", helper.CleanString("中文测试", CleanStringType.Alias));
            Assert.AreEqual("léger*中文测试*ZÔRG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));

            helper = new DefaultShortStringHelper(SettingsForTests.GetDefault())
                     .WithConfig(CleanStringType.Alias, new DefaultShortStringHelper.Config
            {
                StringType = CleanStringType.Ascii | CleanStringType.Unchanged,
                Separator  = '*'
            });
            Assert.AreEqual("", helper.CleanString("中文测试", CleanStringType.Alias));
            Assert.AreEqual("leger*ZORG", helper.CleanString("léger 中文测试 ZÔRG", CleanStringType.Alias));
        }
Ejemplo n.º 26
0
        public override Macro Build()
        {
            var  id            = _id ?? 1;
            var  name          = _name ?? Guid.NewGuid().ToString();
            var  alias         = _alias ?? name.ToCamelCase();
            Guid key           = _key ?? Guid.NewGuid();
            var  useInEditor   = _useInEditor ?? false;
            var  cacheDuration = _cacheDuration ?? 0;
            var  cacheByPage   = _cacheByPage ?? false;
            var  cacheByMember = _cacheByMember ?? false;
            var  dontRender    = _dontRender ?? false;
            var  macroSource   = _macroSource ?? string.Empty;

            var shortStringHelper = new DefaultShortStringHelper(new DefaultShortStringHelperConfig());

            var macro = new Macro(shortStringHelper, id, key, useInEditor, cacheDuration, alias, name, cacheByPage, cacheByMember, dontRender, macroSource);

            foreach (IMacroProperty property in _propertyBuilders.Select(x => x.Build()))
            {
                macro.Properties.Add(property);
            }

            return(macro);
        }