question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot override default treatment of attributes with a custom IMarkupFormatter

See original GitHub issue

I am using AngleSharp v0.13.0

I am trying to override the default formatting behavior for HTML attributes by using a custom IMarkupFormatter and passing in an override for Attribute(IAttr attribute). However, no matter what I do I can’t seem to get AngleSharp to use the override. For example:

Let’s say I give my IMarkupFormatter the following Attribute method:

public string Attribute(IAttr attribute) => "hello";

I would expect this method to be hit when ParseDocument is called, but instead it hits the default Attribute method and ignores my custom rule. Here is my complete implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PipelineTest
{
    using AngleSharp;
    using AngleSharp.Dom;
    using AngleSharp.Html;
    using AngleSharp.Html.Dom;
    using AngleSharp.Html.Parser;
    using AngleSharp.Xhtml;
    using PreMailer.Net;
    class Program
    {
        static void Main(string[] args)
        {
            RunAnglesharp();
            Console.WriteLine();
        }

        static async Task RunAnglesharp()
        {
            var parser = new HtmlParser(new HtmlParserOptions
            {
                IsNotConsumingCharacterReferences = true,
            });

            var formatter = new SupressEncodingFormatter();
            var document = parser.ParseDocument("<html><head></head><body><a href=\"https://www.foo.com/?h=4&w=8\">&foo</a></body></html>");
            var output = document.ToHtml(formatter);
            Console.WriteLine(output);
            // Expected Output: "<html><head></head><body><a href=\"hello">&foo</a></body></html>"
            // Actual Output: "<html><head></head><body><a href=\"https://www.foo.com/?h=4&amp;w=8\">&foo</a></body></html>"
        }

        class SupressEncodingFormatter : IMarkupFormatter
        {
            public string Attribute(IAttr attribute) => "hello";
            public string CloseTag(IElement element, bool selfClosing) => HtmlMarkupFormatter.Instance.CloseTag(element, selfClosing);
            public string Comment(IComment comment) => HtmlMarkupFormatter.Instance.Comment(comment);
            public string Doctype(IDocumentType doctype) => HtmlMarkupFormatter.Instance.Doctype(doctype);
            public string OpenTag(IElement element, bool selfClosing) => HtmlMarkupFormatter.Instance.OpenTag(element, selfClosing);
            public string Processing(IProcessingInstruction processing) => HtmlMarkupFormatter.Instance.Processing(processing);
            public string Text(ICharacterData text) => text.Data;
        }
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorianRapplcommented, Jan 14, 2021
1reaction
CaptainStackcommented, Mar 26, 2020

So as a non-expert on this, I see I kind of misunderstood how it worked. Your comment helped me, I now have a working implementation, I just needed to overload both OpenTag and Attribute, even though Attribute was the only one I really wanted to change.

I think 2 makes a lot of sense to me, when first getting into this I didn’t see why I needed to include the interfaces I wasn’t changing - I was assuming a more traditional inheritance model.

My only hesitation is making this a breaking change as I am now working on an implementation that assumes the current system! Cheers 🍻

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to override default attribute error message created as ...
I want to override the error message for every AssertThatAttribute, in the same way: to use jsonPropertyName. But the problem that ...
Read more >
Add custom attributes in UI for ASP.NET AJAX
I have a problem with adding custom attributes to menuitems. They can be added in code behind (I have tested the attribute count...
Read more >
JET Custom Components IV - Attributes, Properties and Data
In this article I'm going to drill down on all aspects of attributes and data in Custom JET Components. In the initial tutorial...
Read more >
Custom Elements v1 - Reusable Web Components
Custom elements allow web developers to define new HTML tags, extend existing ones, and create reusable web components.
Read more >
Element: <oj-dialog>
JET custom elements treat boolean attributes differently than HTML5. ... Optional markup. ... By default, the role will be set to dialog.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found