Beispiel #1
0
        public override void ExitPairElement(HTMLParser.PairElementContext ctx)
        {
            var textContent = new StringBuilder();
            textContent.Append("<");

            var tagNameCtx = ctx.htmlTagName(0);
            var tagNameNode = tagNameCtx?.TAG_NAME();

            var tagNameText = tagNameNode?.GetText();

            if(string.IsNullOrEmpty(tagNameText)){
                return;
            }

            //add this tag name to the list if its not already present
            if (!_results.Tags2Attrs.ContainsKey(tagNameText))
            {
                _results.Tags2Attrs.Add(tagNameText, new List<string>());
            }

            var tagContents = new List<string> {tagNameText};

            foreach(var attrCtx in ctx.htmlAttribute()){
                var attrText = MyTreeProperty.Get(attrCtx);
                if(string.IsNullOrEmpty(attrText)){
                    continue;
                }
                //add this tag's attributes if its not already present as-is
                if (_results.Tags2Attrs[tagNameText].All(a => a != attrText))
                {
                    _results.Tags2Attrs[tagNameText].Add(attrText);
                }
                tagContents.Add(attrText);
            }

            textContent.Append(string.Join(" ", tagContents));

            textContent.Append(">");

            var contentCtx = ctx.htmlContent();
            if(contentCtx != null){
                var contentsText = MyTreeProperty.Get(contentCtx);
                if(!string.IsNullOrEmpty(contentsText)){
                    textContent.Append(contentsText);
                }
            }

            textContent.Append("</");
            textContent.Append(tagNameText);
            textContent.Append(">");
            textContent.Append("\n");

            FilteredPut(ctx, textContent.ToString());
        }