Exemple #1
0
        public override void ExitEmptyElement(HTMLParser.EmptyElementContext ctx)
        {
            var textContent = new StringBuilder();

            textContent.Append("<");

            var tagNameCtx = ctx.htmlTagName();

            if (tagNameCtx == null)
            {
                return;
            }
            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("/>");
            textContent.Append("\n");

            FilteredPut(ctx, textContent.ToString());
        }
 /// <summary>
 /// Exit a parse tree produced by the <c>emptyElement</c>
 /// labeled alternative in <see cref="HTMLParser.htmlElement"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitEmptyElement([NotNull] HTMLParser.EmptyElementContext context)
 {
 }