A New Challenger to CSS Selectors: WebForms Place Criteria (WPC)

A New Challenger to CSS Selectors: WebForms Place Criteria (WPC)

One of the major improvements in WebForms Core 2.1 is the expansion of WebForms Place Criteria (WPC). WPC is a DSL built into WebForms Core for selecting, filtering, and manipulating HTML elements and groups of DOM elements. At its basic level, WPC provides selectors for finding elements by ID, Name, Tag, Class, and Attribute, as well as navigating through children, parents, the current element, the target element, Document, Window, HTML, Head, Root, and other locations within the DOM. Elements can also be selected by index, all matching elements can be retrieved, and selection paths can be combined using methods such as Child() and Parent(). WPC selectors are used to find (or select) HTML elements (and beyond) that you want to apply changes to. WPC is a competitor to CSS Selectors, yet it also covers the syntax of CSS Selectors. WPC is only available in WebForms Core and is not available outside of this technology. However, the main focus of this article is WebForms Place Criteria, or Criteria. Criteria extends the basic WPC selection mechanism by allowing developers to filter and evaluate already-selected elements based on their text content, attributes, tag names, visibility, enabled or disabled state, checked state, DOM relationships, position, ranges, and element sets. Criteria supports different operators and allows multiple conditions to be chained together, making it possible to create complex selections that cannot be expressed using only an ID, Class, Tag, or other basic selector. The examples in this article therefore focus specifically on the Criteria layer of WPC and demonstrate how it can be used to refine element selections, evaluate their properties and state, navigate their relationships, and combine multiple conditions to produce precise DOM selections. These examples demonstrate how WPC can go far beyond selecting an element by ID or class. 1. Filtering by Text and Numeric Value First, all elements are selected and filtered according to their text content: form.SetBackgroundColor("Criteria|*?t>:10", "lightgreen"); form.SetBackgroundColor("Criteria|*?t*?t>:10 Enter fullscreen mode Exit fullscreen mode It starts with the tag with the ID "Criteria" (id=Criteria). * selects all elements. ?t checks the direct text of the element. > performs a greater-than comparison. :10 includes 10 in the comparison, resulting in >= 10. Therefore, table cells whose values are greater than or equal to 10 become green. For example: 17 → green 6 → red 5 → red 11 → green 9 → red 15 → green 20 → green Enter fullscreen mode Exit fullscreen mode The second condition: form.SetBackgroundColor("Criteria|*?t*?!t*?t:20", "border", "2px solid"); Enter fullscreen mode Exit fullscreen mode This selects only elements whose text is exactly 20. In the resulting HTML, the two cells containing 20 receive a border. 3. Searching for Text Inside Elements WPC can also search directly for text: form.AddText("Criteria|*?t*Adriano", "Find: Adriano"); Enter fullscreen mode Exit fullscreen mode The following part: t*Adriano Enter fullscreen mode Exit fullscreen mode means that the element's text must contain Adriano. The original HTML contains: underline Adriano Leite Ribeiro is a Brazilian former professional footballer. Enter fullscreen mode Exit fullscreen mode This is found, and the following text is added to it: Find: Adriano Enter fullscreen mode Exit fullscreen mode Therefore, WPC can be used not only for numeric comparisons, but also for searching textual content. 4. Selecting Elements by Attribute WPC can also select elements based on an attribute and its value: form.SetValue("Criteria|*?a\"att1\"$9", "att1 attribute End With 9"); Enter fullscreen mode Exit fullscreen mode Here: a"att1"$9 Enter fullscreen mode Exit fullscreen mode means: a → attribute value "att1" → attribute name $ → the attribute value ends with 9 → the value being searched for The original HTML contains: Enter fullscreen mode Exit fullscreen mode Therefore, these values: 123456789 119 59 Enter fullscreen mode Exit fullscreen mode match because they end with 9. This allows WPC to filter elements based on attribute names and attribute values. 5. Selecting Elements by Tag Name Another example is: form.SetStyle("Criteria|*?g^inp", "border", "2px solid yellow"); Enter fullscreen mode Exit fullscreen mode Here: g^inp Enter fullscreen mode Exit fullscreen mode means that the tag name starts with inp. Therefore, all: Enter fullscreen mode Exit fullscreen mode elements are selected. As a result, all input elements in the form receive a yellow border. 6. Checking Visibility This example demonstrates visibility filtering: form.Message(Fetch.GetText("Criteria|*?!v")); Enter fullscreen mode Exit fullscreen mode v is the visibility criterion. The ! operator reverses the condition, so: ?!v Enter fullscreen mode Exit fullscreen mode selects elements that are not visible. The original HTML contains: underlineIs Visible Tag! Enter fullscreen mode Exit fullscreen mode Because this element is hidden, WPC can find it using the visibility criterion. 7. Finding Disabled Elements This example finds disabled input elements: form.AddValue("Criteria|*?!e", "Is Disabled"); Enter fullscreen mode Exit fullscreen mode e represents whether an element is enabled. Therefore: !e Enter fullscreen mode Exit fullscreen mode means that the element is not enabled — in other words, it is disabled. The original HTML contains: Enter fullscreen mode Exit fullscreen mode This input is selected, and the following value is added: Is Disabled Enter fullscreen mode Exit fullscreen mode 8. Finding Checked Checkboxes WPC can also work with the current state of checkboxes: form.SetChecked("Criteria|\"type'checkbox\"*?!c", true); Enter fullscreen mode Exit fullscreen mode Here: "type'checkbox" Enter fullscreen mode Exit fullscreen mode selects elements where: type = checkbox Enter fullscreen mode Exit fullscreen mode Then: !c Enter fullscreen mode Exit fullscreen mode means that the checkbox is not checked. Therefore, the checkboxes that were initially unchecked are selected. The original HTML contains: Enter fullscreen mode Exit fullscreen mode The checkboxes without checked are found by the criteria. 9. Using c for Checked Checkboxes The opposite operation is also possible: form.AddLabel("Criteria|\"type'checkbox\"*?c", "Add New Label"); Enter fullscreen mode Exit fullscreen mode ?c means that only checkboxes that are checked are selected. In the original HTML, vehicle3 and vehicle6 are checked. Therefore, a new label is added for those elements. This demonstrates that WPC can work with the current state of the DOM, not only with static HTML attributes. 10. Checking for Children Consider this HTML: underlineABC Enter fullscreen mode Exit fullscreen mode We can check whether an element has a child: form.AddText("Criteria|{child-check}*?h", "Is Has Child!"); Enter fullscreen mode Exit fullscreen mode {child-check}* selects all elements with the child-check class. Then: ?h Enter fullscreen mode Exit fullscreen mode keeps only elements that have a child. In this example, is a child of the , so the text is added to the elements. It is also possible to check for a specific child: form.AddText("Criteria|{child-check}*?h", "Has u Child!"); Enter fullscreen mode Exit fullscreen mode This means: Find elements with the child-check class that have a direct child. 11. Checking Descendants with H The difference between h and H can also be demonstrated: form.SetFontSize("Criteria|*?H", 26); Enter fullscreen mode Exit fullscreen mode H checks whether an element contains a descendant. Therefore, if an element contains a at any depth, it can be selected. In contrast, h checks for a direct child. In simple terms: h → Direct Child H → Descendant Enter fullscreen mode Exit fullscreen mode 12. Range and Index Selection WPC can also operate on a specific range of elements: form.SetWidth("Criteria|\"type'checkbox\"*?[4:]", 30); Enter fullscreen mode Exit fullscreen mode [4:] means from index 4 to the end. Therefore, the fifth and sixth checkboxes receive a width of 30px. Then: form.SetWidth("Criteria|\"type'checkbox\"*?![3:]", 20); Enter fullscreen mode Exit fullscreen mode The ! reverses the range condition, so elements outside that range are selected. This is useful when you need to operate on a specific portion of a collection. 13. Selecting Siblings WPC can also move relative to previous and next sibling elements. For example: form.SetBackgroundColor("BTag|1?+", "lightblue"); Enter fullscreen mode Exit fullscreen mode First: 1 Enter fullscreen mode Exit fullscreen mode selects the element at index 1. Then: ?+ Enter fullscreen mode Exit fullscreen mode moves to the next sibling element. Another example: form.SetBackgroundColor("BTag|2?++", "lightgreen"); Enter fullscreen mode Exit fullscreen mode ++ means to select all following siblings. As a result, several elements from a specific point onward receive a green background. Movement in the opposite direction is also possible: form.SetTextColor("BTag|-1?--", "red"); Enter fullscreen mode Exit fullscreen mode -- selects all previous siblings. And: form.SetFontSize("BTag|-1?-5", 26); Enter fullscreen mode Exit fullscreen mode can move through a specified number of previous siblings. 14. Multiple Criteria in One Expression One of the important features is the ability to combine multiple criteria in a single expression: form.AddStyle("BTag|1?-?+2", "border", "2px solid"); Enter fullscreen mode Exit fullscreen mode The selection is performed in multiple stages: 1 ↓ - ↓ + ↓ 2 Enter fullscreen mode Exit fullscreen mode The result of one selection becomes the input for the next operation. Therefore, WPC is not simply a basic selector. Multiple selection operations can be chained together. 15. Intersection and Union WPC can also combine multiple sets of elements. For example: form.SetBackgroundColor("UTag|*?&UTag$[vb];0?.UTag$[vb];2", "violet"); Enter fullscreen mode Exit fullscreen mode & is used for Intersection. This means that only elements existing in both sets are selected. In contrast: . Enter fullscreen mode Exit fullscreen mode is used for Union. For example: form.SetBackgroundColor("UTag|1?.UTag$[vb];3?.UTag$[vb];5", "lightblue"); Enter fullscreen mode Exit fullscreen mode combines multiple element sets using .. This makes it possible to combine different groups of DOM elements into a single selection. 16. Difference In this example: form.SetBackgroundColor("ITag|*?\\ITag$[vb];3?", "pink"); Enter fullscreen mode Exit fullscreen mode \ is used for Difference. In other words: Select all elements, then remove the elements belonging to the second set. That is why most of the elements become pink while the excluded element remains unchanged. 17. Adding a New Tag to an Existing Element This example: form.AddTagToUp("ITag|//", "h3", "H3Tag"); form.SetText("H3Tag", "H3 Tag Added!"); Enter fullscreen mode Exit fullscreen mode adds a new element above the specified location. Then: form.SetText("H3Tag", "H3 Tag Added!"); Enter fullscreen mode Exit fullscreen mode sets its text. As a result, the final HTML contains: H3 Tag Added! Enter fullscreen mode Exit fullscreen mode before the button. 18. Adding a Tag to Multiple Elements In this example: form.AddTag("SpanTags|*|//", "marquee"); form.SetText("*", "Marquee Tag Added!"); Enter fullscreen mode Exit fullscreen mode the existing elements inside SpanTags are selected, and a new marquee structure is added. Then: form.SetText("*", "Marquee Tag Added!"); Enter fullscreen mode Exit fullscreen mode sets the text of all generated marquee elements. The same pattern is used for elements: form.AddText("SpanTags|*|//|", "Add in b Tag Successful!"); Enter fullscreen mode Exit fullscreen mode As a result, new elements are inserted into the existing DOM structure. 19. Selecting Direct Children The final example demonstrates navigation through a specific DOM path: form.SetBackgroundColor("DirectChild|.-1|.-1", "#50689b"); Enter fullscreen mode Exit fullscreen mode The original HTML contains: A B C D... E F Enter fullscreen mode Exit fullscreen mode The expression: .-1|.-1 Enter fullscreen mode Exit fullscreen mode selects the last at each step of the path. As a result, the F element is selected and its background color is changed: F Enter fullscreen mode Exit fullscreen mode Conclusion These examples demonstrate that WebForms Place Criteria (WPC) in WebForms Core 2.1 is much more than a simple ID or class selector. It can: Search by text using t and T. Perform numeric comparisons. Filter by attributes. Select elements by tag name. Find visible, enabled, disabled, and checked elements. Check for children or descendants. Work with ranges and indexes. Navigate to previous and next siblings. Chain multiple criteria together. Perform Intersection, Union, and Difference operations. Navigate through the DOM structure. Dynamically create and insert new tags based on the selection. These examples show how ElementPlace evolves from simple element addressing into a query and filtering system for the DOM in WebForms Core 2.1.

Original Source

Read the full article at Dev →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.