Try our conversational search powered by Generative AI!

XhtmlString does not parse empty HTML span element

Vote:
 

Hello,

I parse the following HTML string with XhtmlString class:

var incorrect = new XhtmlString(@"<span class=""myclassname"" aria-hidden=""true""></span>");

After parsing, the object incorrect does not containt any fragments but it should contain a fragment containing the span element. If I test parsing

var correct = new XhtmlString(@"<span class=""myclassname"" aria-hidden=""true""> </span>");

the result is correct and I get the elements. (Note the whitespace character in span element.)

Is there any way to fix this other than adding a whitespace to all empty elements? I think the default behavior of XhtmlString parser should be to parse the element without checking whether there is content inside the element.

Note: I did not test it with other HTML elements but it shouldn't really matter which element we are using.

#217539
Edited, Feb 25, 2020 12:57
Vote:
 

This happens when you use the constructor with the unparsedString parameter. As you can see, it's parsing your string into fragments.

I think the simplest thing would be to do something like this:

var correct = new XhtmlString();
correct.Fragments.Add(new StaticFragment(@"<span class=""myclassname"" aria-hidden=""true""></span>"));

which should work.

Alternatively, it you wanted to replace the default functionality you'd have to go ahead and implement your own IFragmentParser, that would be bit of an effort and you'd essentially have to duplicate the existing default implementation (albeit it removing the ParserOptions.TrimEmptyElements when initializing the HtmlStreamReader, which I believe is causing the behavior you're seeing). Anyway, I wouldn't recommend itbut it's an option.

#217558
Feb 25, 2020 19:07
Vote:
 

Thank you Jake, it seems overkill to implement a custom IFragmentParser and keep ip updated. Having a whitespace in the element is much easier.

#217589
Feb 26, 2020 7:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.