// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #nullable enable using System.Collections.Generic; using System.Collections.ObjectModel; using System.Management.Automation; namespace Microsoft.PowerShell.Commands { /// /// WebCmdletElementCollection for elements in html web responses. /// public class WebCmdletElementCollection : ReadOnlyCollection { internal WebCmdletElementCollection(IList list) : base(list) { } /// /// Finds the element with name or id. /// /// /// Found element as PSObject. public PSObject? Find(string nameOrId) => FindById(nameOrId) ?? FindByName(nameOrId); /// /// Finds the element by id. /// /// /// Found element as PSObject. public PSObject? FindById(string id) => Find(id, findById: true); /// /// Finds the element by name. /// /// /// Found element as PSObject. public PSObject? FindByName(string name) => Find(name, findById: false); private PSObject? Find(string nameOrId, bool findById) { foreach (PSObject candidate in this) { var namePropInfo = candidate.Properties[(findById ? "id" : "name")]; if (namePropInfo != null && (string)namePropInfo.Value == nameOrId) { return candidate; } } return null; } } }