示例#1
0
        public void HtmlParser_OnCommentStateTest() {
            string text = "<!-- abcde -->";
            var target = new HtmlParser();
            target._cs = new HtmlCharStream(text);

            target._tokenizer = new HtmlTokenizer(target._cs);

            target.CommentFound +=
                delegate (object sender, HtmlParserCommentEventArgs args) {
                    Assert.True(args.CommentToken is CommentToken);
                    CommentToken ct = args.CommentToken;

                    Assert.Equal(1, ct.Count);

                    Assert.Equal(0, ct.Start);
                    Assert.Equal(14, ct.End);

                    Assert.True(ct[0] is HtmlToken);
                    Assert.True(ct[0] is IExpandableTextRange);

                    Assert.Equal(0, ct[0].Start);
                    Assert.Equal(14, ct[0].End);
                };

            target.OnCommentState();
        }
示例#2
0
        public void HtmlParser_OnEndTagStateTest() {
            var target = new HtmlParser();
            target._cs = new HtmlCharStream("</span \r\n foo='bar' nowrap>");

            target._tokenizer = new HtmlTokenizer(target._cs);
            target.EndTagOpen +=
                delegate (object sender, HtmlParserOpenTagEventArgs args) {
                    Assert.True(args.NameToken is NameToken);
                    Assert.Equal(2, args.NameToken.Start);
                    Assert.Equal(6, args.NameToken.End);
                    endTagOpenCalls++;
                };

            target.EndTagClose +=
                delegate (object sender, HtmlParserCloseTagEventArgs args) {
                    Assert.False(args.IsShorthand);
                    Assert.Equal(26, args.CloseAngleBracket.Start);
                    endTagCloseCalls++;
                };

            target.AttributeFound +=
                delegate (object sender, HtmlParserAttributeEventArgs args) {
                    attributeCalls++;
                };

            target.OnEndTagState();

            Assert.Equal(1, endTagOpenCalls);
            Assert.Equal(1, endTagCloseCalls);
            Assert.Equal(0, attributeCalls);
        }
示例#3
0
        public void HtmlParser_FindEndOfBlockTest() {
            var target = new HtmlParser();
            string script = "<script>var x = \"boo\"; if(x < y) { }</script>";
            target._cs = new HtmlCharStream(script);
            target._tokenizer = new HtmlTokenizer(target._cs);

            var r = target.FindEndOfBlock("</script>");
            Assert.Equal(script.Length - 9, r.Length);
        }
示例#4
0
        public void OnStartTagState_WellFormedTest() {
            var target = new HtmlParser();
            target._cs = new HtmlCharStream("<input\r\n type='button' nowrap/>");

            int startTagOpenCalls = 0;
            int startTagCloseCalls = 0;
            int attributeCalls = 0;

            target._tokenizer = new HtmlTokenizer(target._cs);
            target.StartTagOpen +=
                delegate (object sender, HtmlParserOpenTagEventArgs args) {
                    Assert.True(args.NameToken is NameToken);
                    Assert.Equal(1, args.NameToken.Start);
                    Assert.Equal(6, args.NameToken.End);
                    startTagOpenCalls++;
                };

            target.StartTagClose +=
                delegate (object sender, HtmlParserCloseTagEventArgs args) {
                    Assert.Equal(29, args.CloseAngleBracket.Start);
                    Assert.Equal(31, args.CloseAngleBracket.End);
                    Assert.True(args.IsShorthand);
                    Assert.True(args.IsClosed);
                    startTagCloseCalls++;
                };

            target.AttributeFound +=
                delegate (object sender, HtmlParserAttributeEventArgs args) {
                    Assert.True(args.AttributeToken is AttributeToken);
                    var at = args.AttributeToken;

                    switch (attributeCalls) {
                        case 0:
                            Assert.True(at.HasName());
                            Assert.True(at.HasValue());
                            Assert.Equal(9, at.NameToken.Start);
                            Assert.Equal(13, at.NameToken.End);
                            Assert.Equal(14, at.ValueToken.Start);
                            Assert.Equal(22, at.ValueToken.End);
                            break;

                        case 1:
                            Assert.True(at.HasName());
                            Assert.False(at.HasValue());
                            Assert.Equal(23, at.NameToken.Start);
                            Assert.Equal(29, at.NameToken.End);
                            break;
                    }
                    attributeCalls++;
                };

            target.OnStartTagState();

            Assert.Equal(1, startTagOpenCalls);
            Assert.Equal(1, startTagCloseCalls);
            Assert.Equal(2, attributeCalls);
        }
示例#5
0
        public void OnEntity_WellFormedTest1() {
            HtmlParser parser = new HtmlParser();

            parser.EntityFound += delegate (object sender, HtmlParserRangeEventArgs args) {
                Assert.Equal(3, args.Range.Start);
                Assert.Equal(9, args.Range.End);
            };

            parser.Parse("foo&nbsp;");
        }
示例#6
0
        public void HtmlParser_UnknownDocType() {
            var parser = new HtmlParser();

            parser.Parse("<!doctype randomname1><html></html>");
            Assert.Equal(DocType.Unrecognized, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse("<html></html>");
            Assert.Equal(DocType.Undefined, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);
        }
示例#7
0
        public void HtmlParser_DuplicateDocType() {
            var parser = new HtmlParser();

            parser.Parse("<!doctype randomname1>" + DocTypeSignatures.Html5 + "<html></html>");
            Assert.Equal(DocType.Unrecognized, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Html5 + "<!doctype randomname1><html></html>");
            Assert.Equal(DocType.Html5, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);
        }
示例#8
0
        public void OnEntity_BrokenTest() {
            HtmlParser parser = new HtmlParser();
            int count = 0;

            parser.EntityFound += delegate (object sender, HtmlParserRangeEventArgs args) {
                count++;
            };

            parser.Parse("&gt");

            Assert.Equal(0, count);
        }
示例#9
0
        public void OnEntity_WellFormedTest3() {
            HtmlParser parser = new HtmlParser();
            int count = 0;

            parser.EntityFound += delegate (object sender, HtmlParserRangeEventArgs args) {
                count++;
            };

            parser.Parse(new TextStream("foo&#173;bar &#xabc;&abc;<html &lt; dir=\"&gt;"));

            Assert.Equal(2, count);
        }
示例#10
0
        public void OnEntity_WellFormedTest2() {
            HtmlParser parser = new HtmlParser();
            int count = 0;

            parser.EntityFound += delegate (object sender, HtmlParserRangeEventArgs args) {
                count++;
            };

            parser.Parse("foo&nbsp;bar &lt;<html &lt; dir=\"&gt;\"> &gt; &quot");

            Assert.Equal(3, count);
        }
示例#11
0
        public EventLogger(HtmlParser parser) {
            parser.AttributeFound += OnAttribute;
            parser.CommentFound += OnComment;
            parser.EndTagClose += OnEndTagClose;
            parser.EndTagOpen += OnEndTagOpen;
            parser.EntityFound += OnEntity;
            parser.ScriptBlockFound += OnScriptBlock;
            parser.StartTagClose += OnStartTagClose;
            parser.StartTagOpen += OnStartTagOpen;
            parser.StyleBlockFound += OnStyleBlock;

            Log = new StringBuilder();
            _parser = parser;
        }
示例#12
0
        public void HtmlParser_OnStyleStateTest() {
            var target = new HtmlParser();
            target._cs = new HtmlCharStream(" body { background-color: green }</style>");
            target._tokenizer = new HtmlTokenizer(target._cs);
            int count = 0;

            target.StyleBlockFound +=
                delegate (object sender, HtmlParserBlockRangeEventArgs args) {
                    Assert.Equal(0, args.Range.Start);
                    Assert.Equal(33, args.Range.End);
                    count++;
                };

            target.OnStyleState(NameToken.Create(0, 0));
            Assert.Equal(1, count);
        }
示例#13
0
        public HtmlTreeBuilder(HtmlParser parser, HtmlTree tree) {
            Parser = parser;
            _tree = tree;

            parser.ParsingStarting += OnParseBegin;
            parser.ParsingComplete += OnParseEnd;
            parser.StartTagOpen += OnStartTagOpen;
            parser.StartTagClose += OnStartTagClose;
            parser.AttributeFound += OnAttribute;
            parser.EndTagOpen += OnEndTagOpen;
            parser.EndTagClose += OnEndTagClose;

            parser.CommentFound += OnComment;
            // For tree building purposes we don't care about comments or entities.
            // Colorable item collection cares so it should be listening to those events.
        }
示例#14
0
        public void HtmlParser_OnScriptStateTest() {
            string text = " var x = 1; while(x < 5 && x > 0) { x++; }</script>";
            var target = new HtmlParser();
            target._cs = new HtmlCharStream(text);
            target._tokenizer = new HtmlTokenizer(target._cs);

            int count = 0;
            target.ScriptBlockFound +=
                    delegate (object sender, HtmlParserBlockRangeEventArgs args) {
                        Assert.Equal(0, args.Range.Start);
                        Assert.Equal(text.Length - 9, args.Range.End);
                        count++;
                    };

            target.OnScriptState(String.Empty, NameToken.Create(0, 0));
            Assert.Equal(1, count);
        }
示例#15
0
        static public void ParseText(HtmlTestFilesFixture fixture, string name) {
            string testFile = fixture.GetDestinationPath(name);
            string baselineFile = testFile + ".log";
            string text = fixture.LoadDestinationFile(name);
            EventLogger logger = null;

            HtmlParser p = new HtmlParser();
            logger = new EventLogger(p);
            p.Parse(text);
            var actual = logger.ToString();

            if (_regenerateBaselineFiles) {
                // Update this to your actual enlistment if you need to update baseline
                baselineFile = Path.Combine(fixture.SourcePath, name) + ".log";
                TestFiles.UpdateBaseline(baselineFile, actual);
            } else {
                TestFiles.CompareToBaseLine(baselineFile, actual);
            }
        }
示例#16
0
        public void HtmlParser_KnownDocTypes() {
            var parser = new HtmlParser();

            parser.Parse(DocTypeSignatures.Html32 + "<html></html>");
            Assert.Equal(DocType.Html32, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Html401Frameset + "<html></html>");
            Assert.Equal(DocType.Html401Frameset, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Html401Strict + "<html></html>");
            Assert.Equal(DocType.Html401Strict, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Html401Transitional + "<html></html>");
            Assert.Equal(DocType.Html401Transitional, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Html5 + "<html></html>");
            Assert.Equal(DocType.Html5, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Xhtml10Frameset + "<html></html>");
            Assert.Equal(DocType.Xhtml10Frameset, parser.DocType);
            Assert.Equal(ParsingMode.Xhtml, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Xhtml10Strict + "<html></html>");
            Assert.Equal(DocType.Xhtml10Strict, parser.DocType);
            Assert.Equal(ParsingMode.Xhtml, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Xhtml10Transitional + "<html></html>");
            Assert.Equal(DocType.Xhtml10Transitional, parser.DocType);
            Assert.Equal(ParsingMode.Xhtml, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Xhtml11 + "<html></html>");
            Assert.Equal(DocType.Xhtml11, parser.DocType);
            Assert.Equal(ParsingMode.Xhtml, parser.ParsingMode);

            parser.Parse(DocTypeSignatures.Xhtml20 + "<html></html>");
            Assert.Equal(DocType.Xhtml20, parser.DocType);
            Assert.Equal(ParsingMode.Xhtml, parser.ParsingMode);
        }
示例#17
0
        /// <summary>
        /// Main method to build a subtree from a text range.
        /// </summary>
        public virtual void Build(ITextRange range) {
            CommentCollection = new CommentCollection();

            HtmlParser parser = new HtmlParser(ParsingMode, ScriptTypeResolution, ScriptOrStyleTagNameService);
            HtmlTreeBuilder builder = new HtmlTreeBuilder(parser, this);
            parser.Parse(Text, range);

            DocType = parser.DocType;
            ParsingMode = parser.ParsingMode;
        }
示例#18
0
 private HtmlTree ParseHtml(string html) {
     HtmlParser parser = new HtmlParser();
     HtmlTree tree = new HtmlTree(new TextStream(html));
     tree.Build();
     return tree;
 }
示例#19
0
        public void AttributeParse_InlineStyle_Typo() {
            var target = new HtmlParser();

            target.AttributeFound +=
                delegate (object sender, HtmlParserAttributeEventArgs args) {
                    Assert.True(args.AttributeToken is AttributeToken);
                    AttributeToken at = args.AttributeToken;

                    switch (attributeCalls) {
                        case 0:
                            Assert.True(at.HasName());
                            Assert.Equal(7, at.NameToken.Start);
                            Assert.Equal(5, at.NameToken.Length);

                            Assert.True(at.HasValue());
                            Assert.Equal(13, at.ValueToken.Start);
                            Assert.Equal(14, at.ValueToken.Length);
                            break;

                        case 1:
                            Assert.True(at.HasName());
                            Assert.Equal(27, at.NameToken.Start);
                            Assert.Equal(1, at.NameToken.Length);

                            Assert.False(at.HasValue());
                            break;
                    }
                    attributeCalls++;
                };

            attributeCalls = 0;
            target.Parse("<input style=\"display:none\";>");
            Assert.Equal(2, attributeCalls);
        }
示例#20
0
        public void AttributeParse_RandomCharacters() {
            var target = new HtmlParser();

            target.AttributeFound +=
                delegate (object sender, HtmlParserAttributeEventArgs args) {
                    Assert.True(args.AttributeToken is AttributeToken);
                    AttributeToken at = args.AttributeToken;

                    switch (attributeCalls) {
                        case 0:
                            Assert.True(at.HasName());
                            Assert.Equal(3, at.NameToken.Start);
                            Assert.Equal(2, at.NameToken.Length);

                            Assert.True(at.HasValue());
                            Assert.Equal(6, at.ValueToken.Start);
                            Assert.Equal(1, at.ValueToken.Length);
                            break;

                        case 1:
                            Assert.True(at.HasName());
                            Assert.Equal(8, at.NameToken.Start);
                            Assert.Equal(1, at.NameToken.Length);

                            Assert.True(at.HasValue());
                            Assert.Equal(10, at.ValueToken.Start);
                            Assert.Equal(1, at.ValueToken.Length);
                            break;

                        case 2:
                            Assert.True(at.HasName());
                            Assert.Equal(12, at.NameToken.Start);
                            Assert.Equal(2, at.NameToken.Length);

                            Assert.False(at.HasValue());
                            break;
                    }
                    attributeCalls++;
                };

            attributeCalls = 0;
            target.Parse("<a id=# $=% ^^");

            Assert.Equal(3, attributeCalls);
        }
示例#21
0
 public static HtmlParser CreateParser(string text) {
     var target = new HtmlParser();
     target._cs = new HtmlCharStream(text);
     target._tokenizer = new HtmlTokenizer(target._cs);
     return target;
 }
示例#22
0
        public void HtmlParser_XmlPi() {
            var parser = new HtmlParser();

            parser.Parse("<?xml charset='utf-8' ?><html></html>");
            Assert.Equal(DocType.Undefined, parser.DocType);
            Assert.Equal(ParsingMode.Xml, parser.ParsingMode);
        }
示例#23
0
        public void HtmlParser_MalformedDocType() {
            var parser = new HtmlParser();

            parser.Parse("<!doctype randomname1<html></html>");
            Assert.Equal(DocType.Undefined, parser.DocType);
            Assert.Equal(ParsingMode.Html, parser.ParsingMode);
        }