Exemplo n.º 1
0
 public override INode Clone(Boolean deep = true)
 {
     var node = new HtmlDocument(Context, new TextSource(Source.Text));
     CopyProperties(this, node, deep);
     CopyDocumentProperties(this, node, deep);
     return node;
 }
Exemplo n.º 2
0
 public override INode Clone(Boolean deep = true)
 {
     var source = new TextSource(Source.Text);
     var node = new HtmlDocument(Context, source);
     CloneDocument(node, deep);
     return node;
 }
Exemplo n.º 3
0
 public void CreateMap()
 {
     var document = new HtmlDocument();
     a = new HtmlElement(document, "a");
     a.SetAttribute("data-test1", "test");
     a.SetAttribute("data-b", "b");
     stringMap = new StringMap("data-", a);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new instance of the HTML parser with the specified
 /// document based on the given source manager.
 /// </summary>
 /// <param name="document">
 /// The document instance to be constructed.
 /// </param>
 internal HtmlDomBuilder(HtmlDocument document)
 {
     _tokenizer = new HtmlTokenizer(document.Source, document.Options.Events);
     _document = document;
     _openElements = new List<Element>();
     _templateModes = new Stack<HtmlTreeMode>();
     _formattingElements = new List<Element>();
     _frameset = true;
     _currentMode = HtmlTreeMode.Initial;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Creates a new instance of the HTML parser with the specified
 /// document based on the given source manager.
 /// </summary>
 /// <param name="document">
 /// The document instance to be constructed.
 /// </param>
 internal HtmlDomBuilder(HtmlDocument document)
 {
     var resolver = document.Options.GetService<IEntityService>() ?? HtmlEntityService.Resolver;
     _tokenizer = new HtmlTokenizer(document.Source, document.Options.Events, resolver);
     _document = document;
     _openElements = new List<Element>();
     _templateModes = new Stack<HtmlTreeMode>();
     _formattingElements = new List<Element>();
     _frameset = true;
     _currentMode = HtmlTreeMode.Initial;
 }
Exemplo n.º 6
0
 internal async static Task<IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
 {
     var scripting = context.Configuration.IsScripting();
     var parserOptions = new HtmlParserOptions { IsScripting = scripting };
     var document = new HtmlDocument(context, options.Source);
     var parser = new HtmlDomBuilder(document);
     document.Setup(options);
     context.NavigateTo(document);
     context.Fire(new HtmlParseEvent(document, completed: false));
     await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false);
     context.Fire(new HtmlParseEvent(document, completed: true));
     return document;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a new instance of the HTML parser with the specified
 /// document based on the given source manager.
 /// </summary>
 /// <param name="document">
 /// The document instance to be constructed.
 /// </param>
 public HtmlDomBuilder(HtmlDocument document)
 {
     var options = document.Options;
     var context = document.Context;
     var resolver = options.GetProvider<IEntityProvider>() ?? HtmlEntityService.Resolver;
     _tokenizer = new HtmlTokenizer(document.Source, resolver);
     _tokenizer.Error += (_, error) => context.Fire(error);
     _document = document;
     _openElements = new List<Element>();
     _templateModes = new Stack<HtmlTreeMode>();
     _formattingElements = new List<Element>();
     _frameset = true;
     _currentMode = HtmlTreeMode.Initial;
     _htmlFactory = options.GetFactory<IElementFactory<HtmlElement>>();
     _mathFactory = options.GetFactory<IElementFactory<MathElement>>();
     _svgFactory = options.GetFactory<IElementFactory<SvgElement>>();
 }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
 internal async static Task<IDocument> LoadTextAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
 {
     var scripting = context.Configuration.IsScripting();
     var parserOptions = new HtmlParserOptions { IsScripting = scripting };
     var document = new HtmlDocument(context, options.Source);
     document.Setup(options);
     context.NavigateTo(document);
     var root = document.CreateElement(TagNames.Html);
     var head = document.CreateElement(TagNames.Head);
     var body = document.CreateElement(TagNames.Body);
     var pre = document.CreateElement(TagNames.Pre);
     document.AppendChild(root);
     root.AppendChild(head);
     root.AppendChild(body);
     body.AppendChild(pre);
     pre.SetAttribute(AttributeNames.Style, "word-wrap: break-word; white-space: pre-wrap;");
     await options.Source.PrefetchAllAsync(cancelToken).ConfigureAwait(false);
     pre.TextContent = options.Source.Text;
     return document;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Loads the document in the provided context from the given response.
        /// </summary>
        /// <param name="context">The browsing context.</param>
        /// <param name="response">The response to consider.</param>
        /// <param name="source">The source to use.</param>
        /// <param name="cancelToken">Token for cancellation.</param>
        /// <returns>The task that builds the document.</returns>
        internal async static Task<HtmlDocument> LoadAsync(IBrowsingContext context, IResponse response, TextSource source, CancellationToken cancelToken)
        {
            var document = new HtmlDocument(context, source);
            var evt = new HtmlParseStartEvent(document);
            var config = context.Configuration;
            var events = config.Events;
            var parser = new HtmlDomBuilder(document);
            document.ContentType = response.Headers.GetOrDefault(HeaderNames.ContentType, MimeTypes.Html);
            document.Referrer = response.Headers.GetOrDefault(HeaderNames.Referer, String.Empty);
            document.DocumentUri = response.Address.Href;
            document.Cookie = response.Headers.GetOrDefault(HeaderNames.SetCookie, String.Empty);
            document.ReadyState = DocumentReadyState.Loading;

            if (events != null)
                events.Publish(evt);

            var options = new HtmlParserOptions { IsScripting = config.IsScripting() };
            await parser.ParseAsync(options, cancelToken).ConfigureAwait(false);
            evt.FireEnd();
            return document;
        }
Exemplo n.º 11
0
        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;
            }
        }
Exemplo n.º 12
0
 public void CSSStyleDeclarationBoundInboundDirection()
 {
     var context = BrowsingContext.New(new Configuration().WithCss());
     var document = new HtmlDocument(context);
     var element = new HtmlSpanElement(document);
     var text = "background-color: rgb(255, 0, 0); color: rgb(0, 0, 0)";
     element.Style.CssText = text;
     Assert.AreEqual(text, element.GetAttribute("style"));
     Assert.AreEqual(2, element.Style.Length);
 }
Exemplo n.º 13
0
 public void DOMTokenListWritesBack()
 {
     var document = new HtmlDocument();
     var testClass = "myclass";
     var div = new HtmlDivElement(document);
     div.ClassName = "";
     div.ClassList.Add(testClass);
     Assert.AreEqual(testClass, div.ClassName);
 }
Exemplo n.º 14
0
        public void HtmlButtonToggleProperties()
        {
            var document = new HtmlDocument();
            var form = document.CreateElement<IHtmlFormElement>();
            var element = document.CreateElement<IHtmlButtonElement>();
            document.AppendChild(form);
            form.AppendChild(element);

            Assert.IsFalse(element.IsDisabled);
            Assert.IsFalse(element.IsDraggable);
            Assert.AreEqual(String.Empty, element.FormMethod);

            element.IsDisabled = true;
            element.IsDraggable = true;
            element.FormMethod = "get";

            Assert.IsTrue(element.IsDisabled);
            Assert.IsTrue(element.IsDraggable);
            Assert.AreEqual("get", element.FormMethod);
        }
Exemplo n.º 15
0
 public void CSSStyleDeclarationBoundOutboundDirectionDirect()
 {
     var context = BrowsingContext.New(new Configuration().WithCss());
     var document = new HtmlDocument(context);
     var element = document.CreateElement<IHtmlSpanElement>();
     var text = "background-color: rgb(255, 0, 0); color: rgb(0, 0, 0)";
     element.SetAttribute("style", String.Empty);
     Assert.AreEqual(String.Empty, element.Style.CssText);
     element.SetAttribute("style", text);
     Assert.AreEqual(text, element.Style.CssText);
     Assert.AreEqual(2, element.Style.Length);
 }
Exemplo n.º 16
0
 public void DOMStringMapBindingSet()
 {
     var document = new HtmlDocument();
     var value = "SomeUser";
     var div = new HtmlDivElement(document);
     div.Dataset["user"] = value;
     Assert.AreEqual(div.GetAttribute("data-user"), value);
 }
Exemplo n.º 17
0
        public void HtmlAnchorToggleProperties()
        {
            var document = new HtmlDocument();
            var element = document.CreateElement<IHtmlAnchorElement>();

            Assert.IsTrue(element.IsTranslated);
            Assert.IsFalse(element.IsSpellChecked);
            Assert.IsFalse(element.IsContentEditable);
            Assert.IsFalse(element.IsDraggable);
            Assert.IsFalse(element.IsHidden);

            element.IsTranslated = false;
            element.IsSpellChecked = true;
            element.IsDraggable = true;
            element.IsHidden = true;

            Assert.IsFalse(element.IsTranslated);
            Assert.IsTrue(element.IsSpellChecked);
            Assert.IsTrue(element.IsDraggable);
            Assert.IsTrue(element.IsHidden);
        }
Exemplo n.º 18
0
 public void HtmlHasRightBodyElement()
 {
     var document = new HtmlDocument();
     var root = new HtmlHtmlElement(document);
     document.AppendChild(root);
     var body = new HtmlBodyElement(document);
     root.AppendChild(body);
     Assert.AreEqual(body, document.Body);
 }
Exemplo n.º 19
0
 IWindow IWindow.Open(String url, String name, String features, String replace)
 {
     //TODO Context ?
     var document = new HtmlDocument();
     document.Location.Href = url;
     return new Window(document) { Name = name };
 }
Exemplo n.º 20
0
 public void HtmlCustomTitleGeneration()
 {
     var document = new HtmlDocument();
     var title = "My Title";
     document.Title = title;
     Assert.AreEqual("", document.Title);
     var html = document.CreateElement(TagNames.Html);
     var head = document.CreateElement(TagNames.Head);
     document.AppendChild(html);
     html.AppendChild(head);
     document.Title = title;
     Assert.AreEqual(title, document.Title);
 }
Exemplo n.º 21
0
 public void HtmlHasRightHeadElement()
 {
     var document = new HtmlDocument();
     var root = new HtmlHtmlElement(document);
     document.AppendChild(root);
     var head = new HtmlHeadElement(document);
     root.AppendChild(head);
     Assert.AreEqual(head, document.Head);
 }
Exemplo n.º 22
0
 public void DOMTokenListCorrectlyInitializedNoClass()
 {
     var document = new HtmlDocument();
     var testClass = "myclass1";
     var div = new HtmlDivElement(document);
     div.ClassName = "myclass2 whatever anotherclass";
     Assert.IsFalse(div.ClassList.Contains(testClass));
 }
Exemplo n.º 23
0
 public void DOMStringMapHasAttributesButRequestedMissing()
 {
     var document = new HtmlDocument();
     var div = new HtmlDivElement(document);
     div.SetAttribute("data-some", "test");
     div.SetAttribute("data-another", "");
     div.SetAttribute("data-test", "third attribute");
     Assert.IsTrue(div.Dataset["user"] == null);
 }
Exemplo n.º 24
0
 public void DOMStringMapHasNoAttribute()
 {
     var document = new HtmlDocument();
     var div = new HtmlDivElement(document);
     Assert.IsTrue(div.Dataset["user"] == null);
 }
Exemplo n.º 25
0
        /// <summary>
        /// Loads the document in the provided context from the given response.
        /// </summary>
        /// <param name="context">The browsing context.</param>
        /// <param name="options">The creation options to consider.</param>
        /// <param name="cancelToken">Token for cancellation.</param>
        /// <returns>The task that builds the document.</returns>
        internal async static Task<IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken)
        {
            var document = new HtmlDocument(context, options.Source);
            var evt = new HtmlParseStartEvent(document);
            var config = context.Configuration;
            var events = config.Events;
            var parser = new HtmlDomBuilder(document);
            var parserOptions = new HtmlParserOptions
            {
                IsScripting = config.IsScripting()
            };
            document.Setup(options);
            context.NavigateTo(document);

            if (events != null)
            {
                events.Publish(evt);
            }

            await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false);
            evt.FireEnd();
            return document;
        }
Exemplo n.º 26
0
 public void NormalizeRemovesEmptyTextNodes()
 {
     var document = new HtmlDocument();
     var div = document.CreateElement("div");
     div.AppendChild(document.CreateElement("a"));
     div.AppendChild(document.CreateTextNode(String.Empty));
     div.AppendChild(document.CreateElement("div"));
     div.AppendChild(document.CreateTextNode("Hi there!"));
     div.AppendChild(document.CreateElement("img"));
     div.Normalize();
     Assert.AreEqual(div.ChildNodes.Length, 4);
 }
Exemplo n.º 27
0
 HtmlDocument CreateDocument(TextSource textSource)
 {
     var document = new HtmlDocument(_context, textSource);
     return document;
 }
Exemplo n.º 28
0
 public void NormalizeMergeTextNodes()
 {
     var document = new HtmlDocument();
     var div = document.CreateElement("div");
     var a = document.CreateElement("a");
     a.AppendChild(document.CreateTextNode(""));
     a.AppendChild(document.CreateTextNode("Not empty."));
     div.AppendChild(a);
     div.AppendChild(document.CreateTextNode(""));
     div.AppendChild(document.CreateElement("div"));
     div.AppendChild(document.CreateTextNode("Certainly not empty!"));
     div.AppendChild(document.CreateTextNode("Certainly not empty!"));
     div.AppendChild(document.CreateTextNode("Certainly not empty!"));
     div.AppendChild(document.CreateTextNode("Certainly not empty!"));
     div.AppendChild(document.CreateElement("img"));
     div.Normalize();
     Assert.AreEqual(div.ChildNodes.Length, 4);
 }
Exemplo n.º 29
0
 public void DOMStringMapIEnumerableWorking()
 {
     var document = new HtmlDocument();
     var div = new HtmlDivElement(document);
     div.SetAttribute("data-some", "test");
     div.SetAttribute("data-another", "");
     div.SetAttribute("data-test", "third attribute");
     Assert.AreEqual(3, div.Dataset.Count());
     Assert.AreEqual("some", div.Dataset.First().Key);
     Assert.AreEqual("test", div.Dataset.First().Value);
     Assert.AreEqual("test", div.Dataset.Last().Key);
     Assert.AreEqual("third attribute", div.Dataset.Last().Value);
 }
Exemplo n.º 30
0
 public void DOMTokenListToggleWorksTurnOn()
 {
     var document = new HtmlDocument();
     var testClass = "myclass";
     var otherClasses = "otherClass someOther more";
     var div = new HtmlDivElement(document);
     div.ClassName = otherClasses;
     div.ClassList.Toggle(testClass);
     Assert.AreEqual(div.ClassName, otherClasses + " " + testClass);
 }