Element factory to build a CsQuery DOM using HtmlParserSharp.
Ejemplo n.º 1
0
        /// <summary>
        /// Loads both CSQ and HAP documents
        /// </summary>
        /// <param name="doc"></param>
        public void LoadBoth(string doc)
        {
            var html = Support.GetFile(Program.ResourceDirectory+"\\"+doc+".htm");

            var factory = new ElementFactory(DomIndexProviders.Simple);

            using (var stream = html.ToStream())
            {
                var document = factory.Parse(stream, Encoding.UTF8);
                CsqueryDocument_Simple = CQ.Create(document);
            }

            factory = new ElementFactory(DomIndexProviders.Ranged);

            using (var stream = html.ToStream())
            {
                var document = factory.Parse(stream, Encoding.UTF8);
                CsqueryDocument_Ranged= CQ.Create(document);
            }

            factory = new ElementFactory(DomIndexProviders.None);

            using (var stream = html.ToStream())
            {
                var document = factory.Parse(stream, Encoding.UTF8);
                CsqueryDocument_NoIndex = CQ.Create(document);
            }

            HapDocument = new HtmlDocument();
            HapDocument.LoadHtml(html);

            AngleSharpDocument = AngleSharp.DocumentBuilder.Html(html);
        }
Ejemplo n.º 2
0
  private static ElementFactory GetNewParser(HtmlParsingMode parsingMode, HtmlParsingOptions parsingOptions, DocType docType)
  {
      var parser = new ElementFactory();
      parser.HtmlParsingMode = parsingMode;
      parser.DocType = GetDocType(docType);
      parser.HtmlParsingOptions = MergeOptions(parsingOptions);
      return parser;
 }
Ejemplo n.º 3
0
        public void Run(String source)
        {
            var factory = new ElementFactory(DomIndexProviders.Simple);

            using (var stream = source.ToStream())
            {
                var document = factory.Parse(stream, Encoding.UTF8);
            }
        }
Ejemplo n.º 4
0
        public void Run(String source)
        {
            var factory = new ElementFactory(DomIndexProviders.Simple);

            using (var stream = source.ToStream())
            {
                var document = factory.Parse(stream, Encoding.UTF8);
                // Even using two kinds of selectors -- measure perf.
                document.QuerySelectorAll("a[href]");
                document.QuerySelectorAll("div > p > a");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new fragment in a given context.
        /// </summary>
        ///
        /// <param name="html">
        /// The elements.
        /// </param>
        /// <param name="context">
        /// (optional) the context. If omitted, will be automatically determined.
        /// </param>
        /// <param name="docType">
        /// (optional) type of the document.
        /// </param>
        ///
        /// <returns>
        /// A new fragment.
        /// </returns>

        public static IDomDocument Create(string html,
           string context=null,
           DocType docType = DocType.Default)
        {
            var factory = new ElementFactory();
            factory.FragmentContext = context;
            factory.HtmlParsingMode = HtmlParsingMode.Fragment;
            factory.HtmlParsingOptions = HtmlParsingOptions.AllowSelfClosingTags;
            factory.DocType = docType;
            
            using (var reader = new StringReader(html))
            {
                return factory.Parse(new StringReader(html));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new fragment in a given context.
        /// </summary>
        ///
        /// <param name="html">
        /// The elements.
        /// </param>
        /// <param name="context">
        /// (optional) the context. If omitted, will be automatically determined.
        /// </param>
        /// <param name="docType">
        /// (optional) type of the document.
        /// </param>
        ///
        /// <returns>
        /// A new fragment.
        /// </returns>

        public static IDomDocument Create(string html,
           string context=null,
           DocType docType = DocType.Default)
        {
            var factory = new ElementFactory();
            factory.FragmentContext = context;
            factory.HtmlParsingMode = HtmlParsingMode.Fragment;
            factory.HtmlParsingOptions = HtmlParsingOptions.AllowSelfClosingTags;
            factory.DocType = docType;

            Encoding encoding = Encoding.UTF8;
            using (var stream = new MemoryStream(encoding.GetBytes(html)))
            {
                return factory.Parse(stream, encoding);
            }
        }
Ejemplo n.º 7
0
        public void LoadingDom()
        {

            var html = Support.GetFile(Program.ResourceDirectory+"\\"+DocName+".htm");
            
            Action csq1 = new Action(() =>
            {
                var factory = new ElementFactory(DomIndexProviders.None);
                
                using (var stream = html.ToStream()) {
                    var document = factory.Parse(stream,Encoding.UTF8);
                    var csqDoc = CQ.Create(document);
                }

            });

            Action csq2 = new Action(() =>
            {
                var factory = new ElementFactory(DomIndexProviders.Simple);

                using (var stream = html.ToStream())
                {
                    var document = factory.Parse(stream, Encoding.UTF8);
                    var csqDoc = CQ.Create(document);
                }

            });
            Action csq3 = new Action(() =>
            {
                var factory = new ElementFactory(DomIndexProviders.Ranged);

                using (var stream = html.ToStream())
                {
                    var document = factory.Parse(stream, Encoding.UTF8);
                    var csqDoc = CQ.Create(document);
                }

                //var test = csqDoc["*"].Count();
            });
            Action hap = new Action(() =>
            {
                var hapDoc = new HtmlDocument();
                hapDoc.LoadHtml(html);
            });

            IDictionary<string,Action> tests = new Dictionary<string,Action>();

            if (Program.IncludeTests.HasFlag(TestMethods.CsQuery_NoIndex)) {
                tests.Add("No Index (CsQuery)", csq1);
            }
            if (Program.IncludeTests.HasFlag(TestMethods.CsQuery_SimpleIndex)) {
                tests.Add("Simple Index (CsQuery)",csq2);
            }
            if (Program.IncludeTests.HasFlag(TestMethods.CsQuery_RangedIndex))
            {
                tests.Add("Ranged Index (CsQuery)", csq3);
            }
            if (Program.IncludeTests.HasFlag(TestMethods.HAP) || Program.IncludeTests.HasFlag(TestMethods.Fizzler))
            {
                tests.Add("HAP", hap);
            }
            Compare(tests, "Create DOM from html");

        }