Exemplo n.º 1
0
        public static UIBase[] ParseAttributesForUI(string html)
        {
            var options = Configuration.Default.WithCss();
            var parser  = new HtmlParser(options);

            AngleSharp.Dom.Html.IHtmlDocument doc = null;

            try
            {
                doc = parser.Parse(html);
            }
            catch (Exception)
            {
                return(null);
            }

            List <UIBase> attributes      = new List <UIBase>();
            List <UIBase> attributesNotIn = new List <UIBase>();

            int[] layout = { 0, 0, 0, 0 };

            // Get all the elements as a big flat list in list attributes.
            foreach (var item in doc.Body.Children)
            {
                ParseAttributesForUI(attributes, attributesNotIn, item, layout);
            }

            // try to find again
            foreach (var spec in attributesNotIn)
            {
                foreach (var item in attributes)
                {
                    var frame = item as UIFrame;
                    if (frame != null)
                    {
                        if (spec.IsIn(frame))
                        {
                            frame.AddChild(spec);
                        }
                    }
                }
            }

            // Group now all the root franes,
            List <UIFrame> rootframes = new List <UIFrame>();

            foreach (var candidate in attributes)
            {
                UIFrame frame_candidate = candidate as UIFrame;
                if (frame_candidate == null)
                {
                    continue;
                }
                foreach (var attribute in attributes)
                {
                    UIFrame frame = attribute as UIFrame;
                    if (frame == null)
                    {
                        continue; // not a frame so can not be root.
                    }
                    if (attribute == frame_candidate)
                    {
                        continue;                    // Is same object no need to test
                    }
                    if (frame_candidate.IsIn(frame)) // Test if the frame_candidate is part of
                    {                                //   this frame. If it is it can not be a root frame.
                        frame_candidate = null;
                        break;
                    }
                }
                if (frame_candidate != null)
                {
                    // Is a root frame
                    rootframes.Add(frame_candidate);
                }
            }

            // Now we have all the root frames.
            // remove thses frames from the attributes list
            foreach (var frame in rootframes)
            {
                attributes.Remove(frame);
            }

            // Now group all the remaining elements in the rootframes.
            UIBase elm = attributes.FirstOrDefault();

            while (elm != null)
            {
                foreach (var attribute in rootframes)
                {
                    UIFrame frame = attribute as UIFrame; // Could also be an none frame element as root.
                    if (frame == null)
                    {
                        continue;
                    }
                    if (elm.IsIn(frame))
                    {
                        frame.AddChild(elm);
                        attributes.Remove(elm);
                        elm = null;
                        break;
                    }
                }
                if (elm != null) // is a none frame that is a root.
                {
                    rootframes.Add((UIFrame)elm);
                    attributes.Remove(elm);
                }
                elm = attributes.FirstOrDefault();
            }

            UIFrame        noframe  = null;
            List <UIFrame> toremove = new List <UIFrame>();

            foreach (var item in rootframes)
            {
                UIFrame frame = item as UIFrame;
                if (frame.Children == null || frame.Children.Count() == 0)
                {
                    toremove.Add(item);
                }
                //rootframes.Remove(item);

                if (frame.NestedName == "NO_FRAME")
                {
                    noframe = frame;
                }
            }

            foreach (var item in toremove)
            {
                rootframes.Remove(item);
            }

            //rootframes.Remove(noframe);
            //rootframes.Add(noframe);

            //rootframes = rootframes.OrderBy(x => x.y).ToList();

            return(rootframes.OrderBy(x => x.y).ToArray());

            //return rootframes.ToArray();
        }