コード例 #1
0
        public IList <Node> ParseFragment(string inputFragment, Element context, string baseUri, ParseErrorList errors)
        {
            // context may be null
            _state = HtmlTreeBuilderState.Initial;
            InitialiseParse(inputFragment, baseUri, errors);
            _contextElement  = context;
            _fragmentParsing = true;
            Element root = null;

            if (context != null)
            {
                if (context.OwnerDocument != null) // quirks setup:
                {
                    _doc.QuirksMode(context.OwnerDocument.QuirksMode());
                }

                // initialise the tokeniser state:
                string contextTag = context.TagName();
                if (StringUtil.In(contextTag, "title", "textarea"))
                {
                    _tokeniser.Transition(TokeniserState.RcData);
                }
                else if (StringUtil.In(contextTag, "iframe", "noembed", "noframes", "style", "xmp"))
                {
                    _tokeniser.Transition(TokeniserState.RawText);
                }
                else if (contextTag.Equals("script"))
                {
                    _tokeniser.Transition(TokeniserState.ScriptData);
                }
                else if (contextTag.Equals(("noscript")))
                {
                    _tokeniser.Transition(TokeniserState.Data); // if scripting enabled, rawtext
                }
                else if (contextTag.Equals("plaintext"))
                {
                    _tokeniser.Transition(TokeniserState.Data);
                }
                else
                {
                    _tokeniser.Transition(TokeniserState.Data); // default
                }

                root = new Element(Tag.ValueOf("html"), baseUri);
                _doc.AppendChild(root);
                _stack.AddFirst(root);
                ResetInsertionMode();
                // todo: setup form element to nearest form on context (up ancestor chain)
            }

            RunParser();
            if (context != null)
            {
                return(root.ChildNodes);
            }
            else
            {
                return(_doc.ChildNodes);
            }
        }
コード例 #2
0
ファイル: HtmlTreeBuilder.cs プロジェクト: wushian/dcsoup
 internal void Error(HtmlTreeBuilderState state)
 {
     if (errors.CanAddError)
     {
         errors.Add(new ParseError(reader.Pos(), "Unexpected token [{0}] when in state [{1}]", currentToken.Type(), state.Name()));
     }
 }
コード例 #3
0
 public void Error(HtmlTreeBuilderState state)
 {
     if (_errors == null)
     {
         throw new Exception();
     }
     if (_errors.CanAddError)
     {
         _errors.Add(new ParseError(_reader.Position, "Unexpected token [{0}] when in state [{1}]", _currentToken.Type, state));
     }
 }
コード例 #4
0
 public override Document Parse(string input, string baseUri, ParseErrorList errors)
 {
     _state = HtmlTreeBuilderState.Initial;
     return(base.Parse(input, baseUri, errors));
 }
コード例 #5
0
 public void MarkInsertionMode()
 {
     _originalState = _state;
 }
コード例 #6
0
 public void Transition(HtmlTreeBuilderState state)
 {
     this._state = state;
 }
コード例 #7
0
 public bool Process(Token token, HtmlTreeBuilderState state)
 {
     _currentToken = token;
     return(state.Process(token, this));
 }
コード例 #8
0
ファイル: HtmlTreeBuilder.cs プロジェクト: wushian/dcsoup
        internal IReadOnlyList <Node> ParseFragment(string inputFragment, Element context, string baseUri, ParseErrorList errors)
        {
            // context may be null
            state = HtmlTreeBuilderState.Initial;
            InitialiseParse(inputFragment, baseUri, errors);
            contextElement  = context;
            fragmentParsing = true;
            Element root = null;

            if (context != null)
            {
                if (context.OwnerDocument != null)
                {
                    // quirks setup:
                    doc.QuirksMode = context.OwnerDocument.QuirksMode;
                }
                // initialise the tokeniser state:
                string contextTag = context.TagName;
                if (StringUtil.In(contextTag, "title", "textarea"))
                {
                    tokeniser.Transition(TokeniserState.Rcdata);
                }
                else if (StringUtil.In(contextTag, "iframe", "noembed", "noframes", "style", "xmp"))
                {
                    tokeniser.Transition(TokeniserState.Rawtext);
                }
                else if (contextTag.Equals("script"))
                {
                    tokeniser.Transition(TokeniserState.ScriptData);
                }
                else if (contextTag.Equals(("noscript")))
                {
                    tokeniser.Transition(TokeniserState.Data); // if scripting enabled, rawtext
                }
                else if (contextTag.Equals("plaintext"))
                {
                    tokeniser.Transition(TokeniserState.Data);
                }
                else
                {
                    tokeniser.Transition(TokeniserState.Data);
                }

                // default
                root = new Element(Tag.ValueOf("html"), baseUri);
                doc.AppendChild(root);
                stack.Push(root);
                ResetInsertionMode();
                // setup form element to nearest form on context (up ancestor chain). ensures form controls are associated
                // with form correctly
                Elements contextChain = context.Parents;
                contextChain.Insert(0, context);
                foreach (Element parent in contextChain)
                {
                    if (parent is FormElement)
                    {
                        formElement = (FormElement)parent;
                        break;
                    }
                }
            }
            RunParser();
            if (context != null)
            {
                return(root.ChildNodes);
            }
            else
            {
                return(doc.ChildNodes);
            }
        }
コード例 #9
0
ファイル: HtmlTreeBuilder.cs プロジェクト: wushian/dcsoup
 // tag searches
 //private static final String[] TagsScriptStyle = new String[]{"script", "style"};
 // the current state
 // original / marked state
 // the current head element
 // the current form element
 // fragment parse context -- could be null even if fragment parsing
 // active (open) formatting elements
 // chars in table to be shifted out
 // if ok to go into frameset
 // if next inserts should be fostered
 // if parsing a fragment of html
 internal override Document Parse(string input, string baseUri, ParseErrorList errors)
 {
     state             = HtmlTreeBuilderState.Initial;
     baseUriSetFromDoc = false;
     return(base.Parse(input, baseUri, errors));
 }
コード例 #10
0
ファイル: HtmlTreeBuilder.cs プロジェクト: wushian/dcsoup
 internal void MarkInsertionMode()
 {
     originalState = state;
 }
コード例 #11
0
ファイル: HtmlTreeBuilder.cs プロジェクト: wushian/dcsoup
 internal void Transition(HtmlTreeBuilderState state)
 {
     this.state = state;
 }
コード例 #12
0
 internal virtual bool Process(Token token, HtmlTreeBuilderState state)
 {
     currentToken = token;
     return(state.Process(token, this));
 }