-
Notifications
You must be signed in to change notification settings - Fork 17
WComponents ID property
Any WComponent which outputs an identifiable UI artefact may have a custom ID applied to it. It is not mandatory to set IDs on components. If a WComponent needs an ID then the framework will generate a unique identifier if one is not set. It may, under certain circumstances, be advantageous to set specific (custom) IDs for some components. If relying on older or lower-quality automated testing tools for example it may be difficult to access particular components unless their ID is known.
The ID of a component is output as the id
attribute of the outermost element in the component's rendered HTML.
If a custom ID is required it can be set on a component using setIdName(String id)
. The format for IDs is slightly restricted compared to the HTML id attribute and XML Schema id Type. In WComponents an ID:
- must be able to be included in an XML document as an attribute of type ID;
- must not cause potential clashes with generated IDs; and
- must not be in a form which would require extensive escaping to use in CSS or a JavaScript query selector.
To make it easier for application specifiers and developers to meet these conditions setIdName(String id)
restricts the format of the String arg which is acceptable to a regular expression. The default value of this is: [a-zA-Z][0-9a-zA-Z_]*
. In a form fit for humans this is:
At least one Latin 1 letter followed by ZERO or more Latin 1 letters or numbers or UNDERSCORE (_) characters.
// Given a WComponent (let us assume a WTextField)
WTextField givenNameField = new WTextField();
// set an ID which makes sense to me.
givenNameField.setIdName("Given_Name_Field");
IDs must be unique, therefore it is important that a custom ID does not appear more than once in any given view. To overcome this WComponents uses naming contexts, including WNamingContext, to provide a region of a view which has a particular name. This may appear to change what should have been a "static" custom ID but the rules for ID generation are not complex.
Using naming contexts, preventing ID duplication, determining IDs and methods for ensuring immutable IDs are outlined in detail in WNamingContext.
WComponents containers may implement the concept of a Naming context. A Naming context allows reuse of components without the problem of clashing IDs. All WRepeater and WTable components contain an inbuilt naming context. For further information see WNamingContext.