Cannot override default treatment of attributes with a custom IMarkupFormatter
See original GitHub issueI 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&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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
thanks for the confirmation @bmode . Fixed https://github.com/AngleSharp/anglesharp.github.io/blob/master/_docs/Questions.md
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 🍻