-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Creating empty value for attributte #552
Comments
It's not so obvious, but to get the desired behavior you have to configure var doc = new HtmlDocument()
{
GlobalAttributeValueQuote = AttributeValueQuote.Initial
}; (This could also be done after loading an HTML document.) EDIT: I just noticed that the newly created The problem is the internal field html-agility-pack/src/HtmlAgilityPack.Shared/HtmlNode.cs Lines 2352 to 2355 in 8efd5da
|
Thank you @elgonzo , Indeed to keep attribute, your proposed solution is perfect: As for the SingleQuote problem, I guess the only way at this moment is to use reflection to set the value to DoubleQuote. Such as: var html = "<a download href=\"/downloads/arquivo.zip\">Download do Arquivo</a>";
var doc = new HtmlDocument();
doc.GlobalAttributeValueQuote = AttributeValueQuote.Initial;
doc.LoadHtml(html);
var anchorNodes = doc.DocumentNode.SelectNodes("//a[@href]");
if (anchorNodes != null)
{
foreach (var node in anchorNodes)
{
if (node.GetAttributeValue("target", "") != "_blank")
{
node.SetAttributeValue("target", "_blank");
var targetAttribute = node.GetAttributes("target").Single();
var internalQuoteTypeProperty = typeof(HtmlAgilityPack.HtmlAttribute).GetProperty("InternalQuoteType", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
internalQuoteTypeProperty.SetValue(targetAttribute, AttributeValueQuote.DoubleQuote);
}
}
}
var outputHtml = doc.DocumentNode.OuterHtml; Best Regards, Jon |
Hi @JonathanMagnan , I just commited a PR to propose a correction for this issue, can you please take a look, I am facing the same issue and need this to be fixed in my system :). Thanks in advance and best regards |
Thank you @POFerro for your PR. I will try to look at it very soon. Best Regards, Jon |
Hi @JonathanMagnan , Any news? :) |
Hello @POFerro , Sorry for the delay. I didn't say it, but I have been on vacation since June 25 (a few days after your PR). I'm returning tomorrow, so I will look at it and merge it if accepted next week. Best Regards, Jon |
Hello @POFerro , Thank you again for your pull request. It has been merged and released in the version v1.11.62 Honestly, I'm always afraid of side impacts that will cause other developers as now the @joffremota , could you confirm it indeed fixed your issue as well? It seems to work flawlessly on my side. Best Regards, Jon |
Thanks for accepting the PR. Thanks and best regards ;) |
1. Description
I've got the following value
<a download href=\"/downloads/arquivo.zip\">Download do Arquivo</a>
When I pass this into the following method in order to add the
target="_blank"
attributte, I'm getting this:<a download="" href=\"/downloads/arquivo.zip\" target=\"_blank\">Download do Arquivo</a>
How can I prevent the lib to add the empty value on
download
?Here is the method.
2. Exception
Not applicable
3. Fiddle or Project
Not applicable
4. Any further technical details
The text was updated successfully, but these errors were encountered: