Example #1
0
        /// <summary>
        /// Creates a new document fragment with the given nodelist as
        /// children.
        /// </summary>
        /// <param name="context">The context for the fragment mode.</param>
        /// <param name="html">The HTML source code to use.</param>
        internal DocumentFragment(Element context, String html)
            : this(context.Owner)
        {
            var source = new TextSource(html);
            var document = new HtmlDocument(Owner.Context, source);
            var parser = new HtmlDomBuilder(document);
            var options = new HtmlParserOptions
            {
                IsEmbedded = false,
                IsScripting = Owner.Options.IsScripting()
            };
            var root = parser.ParseFragment(options, context).DocumentElement;

            while (root.HasChildNodes)
            {
                var child = root.FirstChild;
                root.RemoveChild(child);
                this.PreInsert(child, null);
            }
        }
        public static INodeList ToHtmlFragment(this String sourceCode, IElement context = null, IConfiguration configuration = null)
        {
            var ctx = BrowsingContext.New(configuration);
            var source = new TextSource(sourceCode);
            var document = new HtmlDocument(ctx, source);
            var parser = new HtmlDomBuilder(sourceCode, configuration);
            var element = context as Element;

            if (element != null)
            {
                var options = new HtmlParserOptions
                {
                    IsEmbedded = false,
                    IsScripting = configuration.IsScripting()
                };
                return parser.ParseFragment(options, element).DocumentElement.ChildNodes;
            }
            else
            {
                return parser.Parse(default(HtmlParserOptions)).ChildNodes;
            }
        }
Example #3
0
        /// <summary>
        /// Parses the string and returns the result.
        /// </summary>
        public INodeList ParseFragment(String source, IElement context)
        {
            var document = CreateDocument(source);
            var parser = new HtmlDomBuilder(document);

            if (context != null)
            {
                var element = context as Element;

                if (element == null)
                {
                    var configuration = document.Options;
                    var factory = configuration.GetFactory<IElementFactory<HtmlElement>>();
                    element = factory.Create(document, context.LocalName, context.Prefix);
                }

                return parser.ParseFragment(_options, element).DocumentElement.ChildNodes;
            }

            return parser.Parse(_options).ChildNodes;
        }
Example #4
0
        /// <summary>
        /// Parses the string and returns the result.
        /// </summary>
        public INodeList ParseFragment(String source, IElement context)
        {
            var document = CreateDocument(source);
            var parser = new HtmlDomBuilder(document);

            if (context == null)
                return parser.Parse(_options).ChildNodes;

            var element = context as Element ?? 
                Factory.HtmlElements.Create(document, context.LocalName, context.Prefix);
            return parser.ParseFragment(_options, element).DocumentElement.ChildNodes;
        }