예제 #1
0
 /// <summary>
 /// Adds the root element (html) to the document.
 /// </summary>
 /// <param name="tag">The token which started this process.</param>
 void AddRoot(HtmlTagToken tag)
 {
     var element = new HtmlHtmlElement(_document);
     _document.AddNode(element);
     SetupElement(element, tag, false);
     _openElements.Add(element);
     _tokenizer.IsAcceptingCharacterData = false;
     _document.ApplyManifest();
 }
예제 #2
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);
 }
예제 #3
0
        /// <summary>
        /// Switches to the fragment algorithm with the specified context
        /// element. Then parses the given source and creates the document.
        /// </summary>
        /// <param name="options">The options to use for parsing.</param>
        /// <param name="context">
        /// The context element where the algorithm is applied to.
        /// </param>
        public HtmlDocument ParseFragment(HtmlParserOptions options, Element context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var tagName = context.LocalName;

            if (tagName.IsOneOf(TagNames.Title, TagNames.Textarea))
            {
                _tokenizer.State = HtmlParseMode.RCData;
            }
            else if (tagName.IsOneOf(TagNames.Style, TagNames.Xmp, TagNames.Iframe, TagNames.NoEmbed, TagNames.NoFrames))
            {
                _tokenizer.State = HtmlParseMode.Rawtext;
            }
            else if (tagName.Is(TagNames.Script))
            {
                _tokenizer.State = HtmlParseMode.Script;
            }
            else if (tagName.Is(TagNames.Plaintext))
            {
                _tokenizer.State = HtmlParseMode.Plaintext;
            }
            else if (tagName.Is(TagNames.NoScript) && options.IsScripting)
            {
                _tokenizer.State = HtmlParseMode.Rawtext;
            }

            var root = new HtmlHtmlElement(_document);
            _document.AddNode(root);
            _openElements.Add(root);

            if (context is HtmlTemplateElement)
            {
                _templateModes.Push(HtmlTreeMode.InTemplate);
            }

            Reset(context);

            _fragmentContext = context;
            _tokenizer.IsAcceptingCharacterData = !AdjustedCurrentNode.Flags.HasFlag(NodeFlags.HtmlMember);

            do
            {
                if (context is HtmlFormElement)
                {
                    _currentFormElement = (HtmlFormElement)context;
                    break;
                }

                context = context.ParentElement as Element;
            }
            while (context != null);

            return Parse(options);
        }
예제 #4
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);
 }