示例#1
0
        void SetCustomCss()
        {
            var mainDocument = Widget.MainFrameDocument ?? Widget.MainFrame.DomDocument;
            var head         = mainDocument?.DocumentElement?.GetElementsByTagName("head")? [0];

            if (head == null)
            {
                customCssNode = null;
                return;
            }

            // reuse node reference only if the document did not change and still contains the injected node
            if (customCssNode != null && !head.ChildNodes.Contains(customCssNode))
            {
                customCssNode = null;
            }

            if (!string.IsNullOrEmpty(CustomCss))
            {
                if (customCssNode == null)
                {
                    customCssNode = mainDocument.CreateElement("style");
                    customCssNode.SetAttribute("type", "text/css");
                    if (head.ChildNodes.Count > 0)
                    {
                        head.InsertBefore(customCssNode, head.FirstChild);
                    }
                    else
                    {
                        head.AppendChild(customCssNode);
                    }
                }
                if (customCssNode.FirstChild != null)
                {
                    customCssNode.ReplaceChild(mainDocument.CreateTextNode(customCss), customCssNode.FirstChild);
                }
                else
                {
                    customCssNode.AppendChild(mainDocument.CreateTextNode(customCss));
                }
            }
            else if (customCssNode != null)
            {
                if (head.ChildNodes.Contains(customCssNode) == true)
                {
                    head.RemoveChild(customCssNode);
                }
                customCssNode.Dispose();
                customCssNode = null;
            }
        }