Пример #1
0
        public LinkValue AddLinkParam(
            LinkParam param)
        {
            if (param == null)
            {
                return(this);
            }

            switch (param.Kind)
            {
            case LinkParamKind.Rel:
                RelationType ??= LinkRelationType.Parse(param.Value);
                break;

            case LinkParamKind.Anchor:
                Anchor ??= param.Value;
                break;

            case LinkParamKind.HrefLang:
            case LinkParamKind.Media:
            case LinkParamKind.Title:
            case LinkParamKind.Type:
            case LinkParamKind.Extension:
                _targetAttributes.Add(param);
                break;

            default:
                throw new InvalidOperationException(
                          "Link param kind is not supported");
            }

            return(this);
        }
Пример #2
0
        public static LinkParam Parse(
            string parameter)
        {
            if (string.IsNullOrWhiteSpace(parameter))
            {
                return(null);
            }

            var match = ParamRegex.Match(parameter);

            if (!match.Success ||
                !match.Groups[GroupNameKey]
                .Success)
            {
                return(null);
            }

            var key = match.Groups[GroupNameKey]
                      .Value
                      .Trim()
                      .ToLowerInvariant();

            var result = new LinkParam
            {
                Key = key.Replace(
                    "*",
                    string.Empty),
                IsExtendedParameter = key.EndsWith("*"),
            };

            if (match.Groups[GroupNameValue]
                .Success)
            {
                result.Value = match.Groups[GroupNameValue]
                               .Value.Trim();
            }
            else if (match.Groups[GroupNameValueUrlEncoded]
                     .Success)
            {
                result.Value = WebUtility.UrlDecode(
                    match.Groups[GroupNameValueUrlEncoded]
                    .Value.Trim());
            }

            return(result);
        }
Пример #3
0
        public static LinkValue Parse(
            string linkValue)
        {
            if (string.IsNullOrWhiteSpace(linkValue))
            {
                return(null);
            }

            var parts = linkValue.Split(
                new[] { ';' },
                StringSplitOptions.RemoveEmptyEntries);

            var targetUri = ParseTargetUri(
                parts[0]
                .Trim());

            if (targetUri == null)
            {
                return(null);
            }

            var result = new LinkValue
            {
                TargetUri = targetUri,
            };

            for (var i = 1;
                 i < parts.Length;
                 i++)
            {
                var param = LinkParam.Parse(
                    parts[i]
                    .Trim());
                result.AddLinkParam(param);
            }

            return(result);
        }