You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
currently, I'm facing an issue where some component tests fail after the bUnit update to version 1.12.6.
I'm serializing an object into a string and put the value into a custom attribute of an HTML tag, and then test the component to see if the serialized value is in that specific attribute.
Source code of the component:
<pdata-single-quotes='@UserAsJson'data-double-quotes="@UserAsJson">
I'm @User.Name and @User.Age years old.
</p>
@code{
[Parameter]
[EditorRequired]
publicrequiredUserUser{get; set; }privatestringUserAsJson=>JsonSerializer.Serialize(User);
}
The rendered result (in the browser and in bUnit) looks like this:
<pdata-single-quotes="{"Name":"Justin","Age":24}" data-double-quotes="{"Name":"Justin","Age":24}">
I'm Justin and 24 years old.
</p>
As you can see, the GitHub Syntax highlighting is also confused. This test works fine with version 1.11.7.
I created a simple project to showcase the problem. Simply switch the version of bUnit.
Maybe this issue is not bUnit related. Do you have any idea how to solve this problem, anyway?
after I created the issue AngleSharp/AngleSharp#1071 (comment) in the AngleSharp project, I dug deeper into the issue and the cause of it. I'll try to explain in more detail what I think the problem is.
Valid HTML
First, I want to explain what valid HTML is and the browser expects. The following simple HTML is valid and can be interpreted by the browser:
<pdata='{"foo":"bar"}'></p>
It uses the simple quotation marks to be able to use double quotation marks in the value of the attribute.
The following HTML is invalid and cannot be read by the browser correctly:
<pdata="{"foo":"bar"}"></p>
Important: The two examples are source code written in a simple index.html file (No Blazor or anything). This is not the code that is shown in the dev tools in browser. In the dev tools, the examples look like this:
As expected, the second example was not parsed correctly. However, the first example has now double quotation marks. So good so far.
How Blazor works
Blazor don't really care if I use single quotation marks or not. I'm having the following simple Blazor component:
// First see if we have special handling for this attributeif(!this.tryApplySpecialProperty(batch,toDomElement,attributeName,attributeFrame)){// If not, treat it as a regular string-valued attributetoDomElement.setAttribute(attributeName,frameReader.attributeValue(attributeFrame)!);}
Blazor uses the plain JavaScript setAttribute method to add the value to the DOM element. A code example that relates to the first example.
This method seems to automatically "escapes" the double quotation marks in the JSON string and add it correctly to the DOM element. This is the "magic" that I mentioned in previous comments.
Unfortunately, this is invalid HTML. It must either replace the double quotation marks with single quotation marks or escape the double quotation marks in the value to have a valid HTML. The ToMarkup method from AngleSharp returns the same broken attributes like the browser in the second example in screenshot above.
So in general, the content of the Markup property looks identical to the markup in the dev tools. However, this is invalid HTML. The browser simply prepared and beautified the HTML to look better, but this is not the exact same that we get from a rendered Blazor component.
I'm not sure if this is 100% correct what I wrote here, but hopefully it's now easier to get an idea of what the issue is.
I guess I do know what you want to say. The gist is bUnits output is the same as what you see in the dev tools. But the dev tools or better the browser output is already a "prettified" version. So the question is: What does Blazor produce before the browser does stuff with it? Correct?
A few things from my side. For v2 we try to be closer to your given example of the TypeScript code.
My current problem is if it makes sense to align bUnit to something when the input is invalid/unspecified.
I can't reproduce the setAttribute escaping mechanism:
Your JavaScript example is a good one. It showcases the escape mechanism, because when you now calling the getAttribute method, the response will be "hellobutton":
So the setAttribute method escapes the two double quotation marks in the value. However, if you would write the two attributes like in your example directly in HTML (without using the setAttribute method) it would be invalid HTML and therefore parsed incorrectly by the browser.
In my opinion, there is no real gap between bUnit and Blazor directly, because using bUnit without any AngleSharp tools works perfectly fine. However, I think that there is a gap between bUnit and AngleSharp. While Blazor puts attributes through JavaScript into the DOM to achieve a valid HTML in the browser, bUnit "just" creates a markup, which is unfortunately invalid for AngleSharp
For instance, if I use the setAttribute method in AngleSharp:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Hey,
currently, I'm facing an issue where some component tests fail after the bUnit update to version 1.12.6.
I'm serializing an object into a string and put the value into a custom attribute of an HTML tag, and then test the component to see if the serialized value is in that specific attribute.
Source code of the component:
Source code of the test:
The rendered result (in the browser and in bUnit) looks like this:
As you can see, the GitHub Syntax highlighting is also confused. This test works fine with version 1.11.7.
I created a simple project to showcase the problem. Simply switch the version of bUnit.
Maybe this issue is not bUnit related. Do you have any idea how to solve this problem, anyway?
Thanks
All reactions