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

Add attribute scrubbing to documentation for blazor:elementreference #373

Open
GeertvanHorrik opened this issue May 2, 2023 · 3 comments

Comments

@GeertvanHorrik
Copy link

I am trying to render Blazor components, but Blazor adds the "blazor:elementreference" attribute which changes every time we run the tests using bUnit.

Generated code:

<span style="font-size: 12px; text-align: center; margin: 0 auto; display: table" blazor:onclick="1" blazor:elementreference="e35f1ea4-22ae-4c45-8d3b-4f6f07877f10">Test value</span>

In this case, this result is rendered by the following code:

<Blazorise.Text Style="@TextStyle">@Text</Blazorise.Text>

Before submitting a potential PR to update the docs, would you be interested in this?

HtmlPrettyPrint.All(nodes => nodes.ScrubAttributes("blazor:elementreference"));
@SimonCropp
Copy link
Member

shouldnt this be scrubbed by default?

@GeertvanHorrik
Copy link
Author

I would vote yes

@biohazard999
Copy link

biohazard999 commented Jul 30, 2023

Given i faced the same problem there are 2 scrubbers i found helpful:

VerifierSettings.ScrubInlineGuids();

will replace the blazor:elementreference value. Good enough for most stuff.

second one is remove noise when you don't care about event's or those special blazor attributes at all (but just passing down some parameters to a child component and make sure they got propagated:

Thanks ChatGPT for the solution:

        // Add custom scrubber for blazor:xxx attributes
        VerifierSettings.ScrubLinesWithReplace(s =>
        {
            if(!s.Contains("blazor:"))
            {
                return s;
            }

            // Use regex to match and remove any blazor:oninput attribute
            // For example, it will match 'blazor:oninput="some value"' and remove it.
            // Remove all blazor event attributes (e.g., blazor:onclick, blazor:oninput, etc.)
            var scrubbed = Regex.Replace(s, @"(\s*)blazor:[a-zA-Z]+\s*=\s*""[^""]*""", "");

            return scrubbed;
        });

which would be equivalent to

        HtmlPrettyPrint.All(nodes => nodes.ScrubAttributes(a => a.Name.StartsWith("blazor:")));

Because it depends on your testing scenario i would vote for several extension methods:

  • ScrubBlazorElementReference
  • ScrubBlazorEvent("blazor:oninput") or ScrubBlazorEvent("oninput") (second just prepends the blazor:)
  • ScrubBlazorEvents()

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

3 participants