Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Components.Server;
Expand Down Expand Up @@ -61,7 +62,7 @@ private async Task RevalidationLoop(Task<AuthenticationState> authenticationStat
try
{
var authenticationState = await authenticationStateTask;
if (authenticationState.User.Identity?.IsAuthenticated == true)
if (SecurityHelper.IsAuthenticated(authenticationState.User))
{
while (!cancellationToken.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)PooledArrayBufferWriter.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Metrics\MetricsConstants.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)SecurityHelper\**\*.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Components\ComponentsActivityLinkStore.cs" LinkBase="Shared" />

<!-- Add a project dependency without reference output assemblies to enforce build order -->
Expand Down
54 changes: 48 additions & 6 deletions src/Components/Web.JS/src/Virtualize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const SpacerVisibilityReason = {
RenderedContentMeasurement: 3,
} as const;

const ViewportFillDirection = {
Covered: 0,
Before: 1,
After: 2,
} as const;

const ScrollSource = {
None: 0,
UserScroll: 1,
Expand Down Expand Up @@ -644,7 +650,7 @@ function init(dotNetHelper: DotNet.DotNetObject, spacerBefore: HTMLElement, spac
}

// Measures the target's viewport-relative top and aligns it to containerTop.
function alignToItemAt(localIndex: number): void {
function alignToItemAt(localIndex: number): number | null {
function beginAlign(): void {
scrollActivity.ignoreNextScroll();
scrollActivity.source = ScrollSource.AlignToItem;
Expand All @@ -656,10 +662,10 @@ function init(dotNetHelper: DotNet.DotNetObject, spacerBefore: HTMLElement, spac
flushPendingStyleMutations();
const delta = measureLocalChildOffset(localIndex);
if (Number.isNaN(delta)) {
// Target item isn't in DOM yet. Retry after the next render.
// Target item isn't in the committed window.
pendingAlignLocalIndex = localIndex;
beginAlign();
return;
return null;
}
pendingAlignLocalIndex = null;

Expand All @@ -671,6 +677,36 @@ function init(dotNetHelper: DotNet.DotNetObject, spacerBefore: HTMLElement, spac
pendingJumpToEnd = false;
scrollElement.scrollTo({ top: scrollElement.scrollTop + delta, behavior: 'instant' });
}

return getViewportFillDirection();
}

function getViewportBounds(scaleFactor: number): { top: number; bottom: number } {
let viewportTop = 0;
let viewportBottom = document.documentElement.clientHeight;
if (scrollContainer) {
const scrollContainerRect = scrollContainer.getBoundingClientRect();
viewportTop = scrollContainerRect.top + scrollContainer.clientTop * scaleFactor;
viewportBottom = viewportTop + scrollContainer.clientHeight * scaleFactor;
}
return { top: viewportTop, bottom: viewportBottom };
}

function occupiesViewport(spacer: HTMLElement, viewport: { top: number; bottom: number }): boolean {
const spacerRect = spacer.getBoundingClientRect();
return Math.min(spacerRect.bottom, viewport.bottom) > Math.max(spacerRect.top, viewport.top);
}

function getViewportFillDirection(): number {
const scaleFactor = getScaleFactor(spacerBefore, spacerAfter);
const viewport = getViewportBounds(scaleFactor);
if (occupiesViewport(spacerBefore, viewport)) {
return ViewportFillDirection.Before;
}
if (occupiesViewport(spacerAfter, viewport)) {
return ViewportFillDirection.After;
}
return ViewportFillDirection.Covered;
}

observersByDotNetObjectId[id] = {
Expand Down Expand Up @@ -855,6 +891,9 @@ function init(dotNetHelper: DotNet.DotNetObject, spacerBefore: HTMLElement, spac
});

if (intersectingEntries.length === 0) {
if (source === ScrollSource.AlignToItem) {
scrollActivity.clear();
}
return;
}

Expand All @@ -875,7 +914,6 @@ function init(dotNetHelper: DotNet.DotNetObject, spacerBefore: HTMLElement, spac
const isBefore = entry.target === spacerBefore;
const spacer = isBefore ? spacerBefore : spacerAfter;

// Skip an empty after spacer because it provides no useful measurement.
if (!isBefore && spacer.offsetHeight === 0) {
return;
}
Expand All @@ -895,6 +933,10 @@ function init(dotNetHelper: DotNet.DotNetObject, spacerBefore: HTMLElement, spac
const methodName = isBefore ? 'OnSpacerBeforeVisible' : 'OnSpacerAfterVisible';
dotNetHelper.invokeMethodAsync(methodName, spacerSize, spacerSeparation, containerSize, reason);
});

if (source === ScrollSource.AlignToItem) {
scrollActivity.clear();
}
}

function isValidTableElement(element: HTMLElement | null): boolean {
Expand Down Expand Up @@ -934,9 +976,9 @@ function restoreAnchor(dotNetHelper: DotNet.DotNetObject): void {
entry?.restoreAnchor?.();
}

function alignToItem(dotNetHelper: DotNet.DotNetObject, localIndex: number): void {
function alignToItem(dotNetHelper: DotNet.DotNetObject, localIndex: number): number | null {
const { observersByDotNetObjectId, id } = getObserversMapEntry(dotNetHelper);
observersByDotNetObjectId[id]?.alignToItem?.(localIndex);
return observersByDotNetObjectId[id]?.alignToItem?.(localIndex) ?? null;
}

function beginProgrammaticScroll(dotNetHelper: DotNet.DotNetObject): void {
Expand Down
51 changes: 40 additions & 11 deletions src/Components/Web/src/Forms/ExpressionMemberAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Concurrent;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -31,26 +32,37 @@ private static MemberInfo GetMemberInfo<TValue>(Expression<Func<TValue>> accesso
return _memberInfoCache.GetOrAdd(accessor, static expr =>
{
var lambdaExpression = (LambdaExpression)expr;
var accessorBody = lambdaExpression.Body;

if (accessorBody is UnaryExpression unaryExpression
&& unaryExpression.NodeType == ExpressionType.Convert
&& unaryExpression.Type == typeof(object))
{
accessorBody = unaryExpression.Operand;
}

if (accessorBody is not MemberExpression memberExpression)
var member = GetMemberInfo(lambdaExpression.Body, out var accessorBody);
if (member is null)
{
throw new ArgumentException(
$"The provided expression contains a {accessorBody.GetType().Name} which is not supported. " +
$"Only simple member accessors (fields, properties) of an object are supported.");
}

return memberExpression.Member;
return member;
});
}

private static MemberInfo? GetMemberInfo(Expression accessorBody, out Expression normalizedAccessorBody)
{
normalizedAccessorBody = accessorBody;

if (normalizedAccessorBody is UnaryExpression
{
NodeType: ExpressionType.Convert,
Type: var type
} unaryExpression &&
type == typeof(object))
{
normalizedAccessorBody = unaryExpression.Operand;
}

return normalizedAccessorBody is MemberExpression memberExpression
? memberExpression.Member
: null;
}

public static string GetDisplayName(MemberInfo member)
{
ArgumentNullException.ThrowIfNull(member);
Expand Down Expand Up @@ -84,6 +96,23 @@ public static string GetDisplayName<TValue>(Expression<Func<TValue>> accessor)
return GetDisplayName(member);
}

public static bool TryGetDisplayName<TValue>(
Expression<Func<TValue>> accessor,
[NotNullWhen(true)] out string? displayName)
{
ArgumentNullException.ThrowIfNull(accessor);

var member = GetMemberInfo(accessor.Body, out _);
if (member is null)
{
displayName = null;
return false;
}

displayName = GetDisplayName(member);
return true;
}

private static void ClearCache()
{
_memberInfoCache.Clear();
Expand Down
12 changes: 12 additions & 0 deletions src/Components/Web/src/Forms/InputBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public abstract class InputBase<TValue> : ComponentBase, IDisposable
/// </summary>
[Parameter] public string? DisplayName { get; set; }

internal string GetDisplayName()
{
if (DisplayName is not null)
{
return DisplayName;
}

return ExpressionMemberAccessor.TryGetDisplayName(ValueExpression!, out var displayName)
? displayName
: FieldIdentifier.FieldName;
}

/// <summary>
/// Gets the associated <see cref="Forms.EditContext"/>.
/// This property is uninitialized if the input does not have a parent <see cref="EditForm"/>.
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Web/src/Forms/InputDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(fa
}
else
{
validationErrorMessage = string.Format(CultureInfo.InvariantCulture, _parsingErrorMessage, DisplayName ?? FieldIdentifier.FieldName);
validationErrorMessage = string.Format(CultureInfo.InvariantCulture, _parsingErrorMessage, GetDisplayName());
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Web/src/Forms/InputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static class InputExtensions
}

result = default;
validationErrorMessage = $"The {input.DisplayName ?? input.FieldIdentifier.FieldName} field is not valid.";
validationErrorMessage = $"The {input.GetDisplayName()} field is not valid.";
return false;
}
catch (InvalidOperationException ex)
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Web/src/Forms/InputNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(fa
}
else
{
validationErrorMessage = string.Format(CultureInfo.InvariantCulture, ParsingErrorMessage, DisplayName ?? FieldIdentifier.FieldName);
validationErrorMessage = string.Format(CultureInfo.InvariantCulture, ParsingErrorMessage, GetDisplayName());
return false;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Components/Web/src/Virtualization/ViewportFillDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Components.Web.Virtualization;

/// <remarks>
/// The numeric values must stay in sync with the <c>ViewportFillDirection</c> constant in
/// <c>Virtualize.ts</c>.
/// </remarks>
internal enum ViewportFillDirection
{
Covered = 0,
Before = 1,
After = 2,
}
Loading
Loading