예제 #1
0
        /// <summary>
        /// Returns an IDisposable LocalWrapper that will render the open tag upon opening, and a close tag upon closing
        /// </summary>
        /// <returns>returns a LocalWrapper containing the rendered tag</returns>
        public LocalWrapper Open()
        {
            if (!(IsDisplayed || DebugMode))
            {
                return(new LocalWrapper(null, _context));
            }

            var builder = new TagBuilder(TagName);

            foreach (var attribute in LocalizedAttributes)
            {
                var value = Repository.GetQualified(attribute.Value.Key, attribute.Value.Default);
                builder.MergeAttribute(attribute.Key, Processors.Aggregate(
                                           value.Value.DecodeWithReplacements(Replacements),
                                           (val, processor) => processor(val)
                                           ));

                if (!DebugMode)
                {
                    continue;
                }
                builder.MergeAttribute("data-loc-debug", "true");
                var key = "data-loc-attribute-" + attribute.Key + "-";
                builder.MergeAttribute(key + "part", value.Qualifier.Part.ToString());
                builder.MergeAttribute(key + "content", value.Value.Content);
            }

            if (DebugMode)
            {
                if (LocalizedAttributes.Any())
                {
                    var localizations = String.Join(((char)31).ToString(CultureInfo.InvariantCulture), LocalizedAttributes.Select(localization => localization.Key + (char)30 + localization.Value));
                    builder.MergeAttribute("data-loc-localizations", localizations);
                }

                if (Replacements.Any())
                {
                    var replacements = String.Join(((char)31).ToString(CultureInfo.InvariantCulture), Replacements.Select(replacement => replacement.Key + (char)30 + replacement.Value));
                    builder.MergeAttribute("data-loc-replacements", replacements);
                }
            }

            foreach (var attribute in OtherAttributes)
            {
                builder.MergeAttribute(attribute.Key, attribute.Value);
            }

            return(new LocalWrapper(builder, _context));
        }
        public string ToHtmlString()
        {
            if (!(IsDisplayed || DebugMode))
            {
                return(String.Empty);
            }

            var builder = new TagBuilder(TagName);

            var            innerHtml      = String.Empty;
            QualifiedValue innerHtmlValue = null;

            if (!String.IsNullOrEmpty(LocalizedHtmlKey.Key))
            {
                innerHtmlValue = Repository.GetQualified(LocalizedHtmlKey.Key, LocalizedHtmlKey.Default);
                var decodedValue = Processors.Aggregate(
                    innerHtmlValue.Value.DecodeWithReplacements(Replacements),
                    (value, processor) => processor(value)
                    );
                builder.InnerHtml = decodedValue;
            }

            foreach (var attribute in LocalizedAttributes)
            {
                var value = Repository.GetQualified(attribute.Value.Key, attribute.Value.Default);
                builder.MergeAttribute(attribute.Key, Processors.Aggregate(
                                           value.Value.DecodeWithReplacements(Replacements),
                                           (val, processor) => processor(val)
                                           ));

                if (!DebugMode)
                {
                    continue;
                }
                var key = "data-loc-attribute-" + attribute.Key + "-";
                builder.MergeAttribute(key + "part", value.Qualifier.Part.ToString());
                builder.MergeAttribute(key + "content", value.Value.Content);
            }

            if (DebugMode)
            {
                builder.MergeAttribute("data-loc-debug", "true");

                if (innerHtmlValue != null)
                {
                    builder.MergeAttribute("data-loc-inner-part", innerHtmlValue.Qualifier.Part.ToString());
                    builder.MergeAttribute("data-loc-inner-key", innerHtmlValue.Qualifier.Key);
                    builder.MergeAttribute("data-loc-inner-value", innerHtmlValue.Value.Content);
                }

                if (LocalizedAttributes.Any())
                {
                    var localizations = String.Join(((char)31).ToString(CultureInfo.InvariantCulture), LocalizedAttributes.Select(localization => localization.Key + (char)30 + localization.Value));
                    builder.MergeAttribute("data-loc-localizations", localizations);
                }

                if (Replacements.Any())
                {
                    var replacements = String.Join(((char)31).ToString(CultureInfo.InvariantCulture), Replacements.Select(replacement => replacement.Key + (char)30 + replacement.Value));
                    builder.MergeAttribute("data-loc-replacements", replacements);
                }
            }

            if (!String.IsNullOrEmpty(OtherHtml))
            {
                innerHtml += OtherHtml;
            }
            if (!String.IsNullOrEmpty(innerHtml))
            {
                builder.InnerHtml = innerHtml;
            }

            foreach (var attribute in OtherAttributes)
            {
                builder.MergeAttribute(attribute.Key, attribute.Value);
            }

            return(builder.ToString());
        }