Ejemplo n.º 1
0
        void PrepareStylesAndContentOfChildNodes(
            HtmlElement parentElement,
            TopDownActiveCssTemplate activeCssTemplate)
        {
            //recursive
            CssBox principalCssBox = parentElement.CurrentPrincipalBox;
            bool   isblockContext  = true;//default

            if (principalCssBox != null)
            {
                isblockContext = principalCssBox.IsBlock;
            }

            foreach (WebDom.DomNode node in parentElement.GetChildNodeIterForward())
            {
                activeCssTemplate.EnterLevel();
                switch (node.NodeType)
                {
                case WebDom.HtmlNodeType.OpenElement:
                case WebDom.HtmlNodeType.ShortElement:
                {
                    HtmlElement htmlElement = (HtmlElement)node;
                    htmlElement.WellknownElementName = UserMapUtil.EvaluateTagName(htmlElement.LocalName);
                    switch (htmlElement.WellknownElementName)
                    {
                    case WellKnownDomNodeName.style:
                    {
                        //style element should have textnode child
                        int j = htmlElement.ChildrenCount;
                        for (int i = 0; i < j; ++i)
                        {
                            var ch = htmlElement.GetChildNode(i);
                            switch (ch.NodeType)
                            {
                            case HtmlNodeType.TextNode:
                            {
                                HtmlTextNode textNode = (HtmlTextNode)htmlElement.GetChildNode(0);
                                activeCssTemplate.LoadRawStyleElementContent(new string(textNode.GetOriginalBuffer()));
                                //break
                                i = j;
                            }
                            break;
                            }
                        }
                        activeCssTemplate.ExitLevel();
                        continue;
                    }

                    case WellKnownDomNodeName.link:
                    {
                        //<link rel="stylesheet"
                        DomAttribute relAttr;
                        if (htmlElement.TryGetAttribute(WellknownName.Rel, out relAttr))
                        {
                            //switch link type
                            switch (relAttr.Value.ToLower())
                            {
                            case "stylesheet":
                            {
                                //if found
                                DomAttribute hrefAttr;
                                if (htmlElement.TryGetAttribute(WellknownName.Href, out hrefAttr))
                                {
                                    string stylesheet = RaiseRequestStyleSheet(hrefAttr.Value);
                                    if (stylesheet != null)
                                    {
                                        activeCssTemplate.LoadRawStyleElementContent(stylesheet);
                                    }
                                }
                            }
                            break;

                            case "import":
                            {
                                //load data canbe sync or async
                                DomAttribute hrefAttr;
                                if (htmlElement.TryGetAttribute(WellknownName.Href, out hrefAttr))
                                {
                                    //import other html, reuseable html component
                                    //TODO: implement import request algo here
                                }
                            }
                            break;
                            }
                        }
                        activeCssTemplate.ExitLevel();
                        continue;
                    }
                    }
                    //-----------------------------
                    //apply style for this node
                    ApplyStyleSheetForSingleHtmlElement(htmlElement, parentElement.Spec, activeCssTemplate);
                    //-----------------------------

                    //recursive
                    PrepareStylesAndContentOfChildNodes(htmlElement, activeCssTemplate);
                    //-----------------------------
                }
                break;

                case WebDom.HtmlNodeType.TextNode:
                {
                    HtmlTextNode textnode = (HtmlTextNode)node;
                    //inner content is parsed here

                    var           parentSpec     = parentElement.Spec;
                    char[]        originalBuffer = textnode.GetOriginalBuffer();
                    List <CssRun> runlist        = new List <CssRun>();
                    bool          hasSomeCharacter;
                    contentTextSplitter.ParseWordContent(originalBuffer, parentSpec, isblockContext, runlist, out hasSomeCharacter);
                    textnode.SetSplitParts(runlist, hasSomeCharacter);
                }
                break;
                }

                activeCssTemplate.ExitLevel();
            }
        }