Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attribute.QuoteType does not output the correct quote type #560

Closed
rvishruth opened this issue Jul 9, 2024 · 4 comments
Closed

attribute.QuoteType does not output the correct quote type #560

rvishruth opened this issue Jul 9, 2024 · 4 comments
Assignees

Comments

@rvishruth
Copy link

rvishruth commented Jul 9, 2024

1. Description

attribute.QuoteType does not output the correct quote type

In the code below, despite setting the GlobalAttributeValueQuote property to AttributeValueQuote.Initial, the a.QuoteType outputs the quote types as DoubleQuote, SingleQuote, and DoubleQuote, instead of the expected None, SingleQuote, and DoubleQuote.

Follow up question: Is there any way currently to detect the quote type of an attribute's value (even if it's unquoted to begin with)?

2. Exception

N/A

3. Fiddle or Project

// @nuget: HtmlAgilityPack
using System;
using HtmlAgilityPack;
using System.Linq;

public class Program
{
	public static void Main()
	{
		// Load
		string htmlString = "<p><a href=https://example.com>Link</a><a href='https://example.com'>Link</a><a href=\"https://example.com\">Link</a></p>";
		HtmlDocument dom = new HtmlDocument()
		{
			GlobalAttributeValueQuote = AttributeValueQuote.Initial
		};
		dom.LoadHtml(htmlString);
		var documentNodeDescendants = dom.DocumentNode.Descendants().ToList();
		Console.WriteLine(String.Join(",", documentNodeDescendants.SelectMany(n => n.Attributes).Select(a => a.QuoteType)));
	}
}

Output:

DoubleQuote,SingleQuote,DoubleQuote

image

4. Any further technical details

  • HAP version: 1.11.59
  • NET version: .NET8
@JonathanMagnan JonathanMagnan self-assigned this Jul 10, 2024
@JonathanMagnan
Copy link
Member

Hello @rvishruth ,

Thank you for reporting.

On the good side, we output correctly without a quote when performing Console.WriteLine(dom.DocumentNode.InnerHtml);

We will look at this issue.

Follow up question: Is there any way currently to detect the quote type of an attribute's value (even if it's unquoted to begin with)?

I'm not sure I understand your follow-up question. Could you try again?

Best Regards,

Jon

@rvishruth
Copy link
Author

rvishruth commented Jul 10, 2024

Thanks @JonathanMagnan! Sorry let me rephrase -

What is the current best way to detect if an attribute's value is unquoted?

@JonathanMagnan
Copy link
Member

Thank @rvishruth , now it's 100% clear.

You can currently get it through reflection:

var internalQuoteTypeProperty = typeof(HtmlAttribute).GetProperty("InternalQuoteType", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

Console.WriteLine(String.Join(",", documentNodeDescendants.SelectMany(n => n.Attributes).Select(a => internalQuoteTypeProperty.GetValue(a))));

The InternalQuoteType should always be the one we parsed. We probably added it in the past for not impacting projects that were already using the QuoteType to be able to support the Initial flag.

Let me know if that solution could work for you. If yes, make sure to keep the BindingFlags.Public as we never know if we will make it public in the future.

Best Regards,

Jon

@rvishruth
Copy link
Author

Thank you @JonathanMagnan! This makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants