static SrcSet _AttributeKeyValuesToSrcSet(Dictionary <string, string> kvs)
        {
            if (kvs.IsNulle())
            {
                return(null);
            }

            string src    = kvs.V("src");
            string srcSet = kvs.V("srcset");

            if (src.IsNulle())
            {
                return(null);
            }

            var srcImg = new SrcSetImg()
            {
                Url      = src,
                Size     = kvs.V("width").ToInt(0),
                SizeType = SrcSizeType.Width
            };

            SrcSet srcset = new SrcSet()
            {
                Src             = srcImg,
                Sizes           = kvs.V("sizes"),
                SrcSets         = SrcSet.ParseSrcSets(srcSet, out SrcSizeType sizeType),
                SrcSetsSizeType = sizeType
            };

            srcset.InitValues();

            return(srcset);
        }
示例#2
0
        public static List <SrcSetImg> ParseSrcSets(string srcSetStr, out SrcSizeType sizeType)
        {
            sizeType = SrcSizeType.None;

            if (srcSetStr.IsNulle())
            {
                return(null);
            }

            if (srcSetStr.Last() != ',')
            {
                srcSetStr += ',';                 // makes regex much easier / better, and allows consistent
            }
            var matches = _rxSrcSets
                          .Matches(srcSetStr)
                          .OfType <Match>()
                          .Where(m => m.Groups.Count > 0)
                          .ToArray();

            List <SrcSetImg> srcs = new List <SrcSetImg>();

            if (matches.Length > 0)
            {
                for (int i = 0; i < matches.Length; i++)
                {
                    var m = matches[i];

                    var grps = m.Groups;
                    if ((grps?.Count ?? 0).InRange(5, 6))                      // final comma can be missing
                    {
                        string url        = grps[1].Value.NullIfEmptyTrimmed();
                        int    size       = grps[3].Value.ToInt(-1);
                        string sizeTypStr = grps[4].Value;

                        if (url == null || size < 1 || sizeTypStr == null || sizeTypStr.Length != 1)
                        {
                            continue;
                        }

                        char sType = sizeTypStr[0];
                        if (i == 0)
                        {
                            sizeType = SrcSetImg.CharForSrcSetType(sType);
                            if (sizeType == SrcSizeType.None)
                            {
                                return(null);
                            }
                        }

                        SrcSetImg sImg = new SrcSetImg()
                        {
                            Url      = url,
                            SizeType = sizeType,
                            Size     = size
                        };

                        if (!sImg.SetNumberVal(size, sizeType, sizeTypStr[0]))
                        {
                            continue;
                        }

                        srcs.Add(sImg);
                    }
                }
            }

            if (srcs.IsNulle())
            {
                return(null);
            }

            return(srcs);
        }