The IMarkupFilter approach only detects attributes coded into the HTML.

Is there any way to get it to work for attributes created using the 
SimpleAttributeModifier or AttributeAppender? See code.  

FWIW I also tried the onComponentTag approach documented on the wiki 
https://cwiki.apache.org/WICKET/how-to-modify-an-attribute-on-a-html-tag.html

Craig

public class MyTextField<T> extends TextField<T> {
        ....
        public MyTextField<T> setAttribute(String name, String value){
                this.add(new SimpleAttributeModifier(name, value));
                return this;
        }
        ....
}

public class MarkupRuleFilter extends AbstractMarkupFilter {
        ....
        @Override
        public MarkupElement nextTag() throws ParseException {
                ComponentTag tag = nextComponentTag();
                String attrVal = tag.getAttribute("maxlength");
                if( StringUtils.isBlank(attrVal)){
                        throw new IllegalStateException("No maxlength defined 
for " + tag.getId());             
                }
                return tag;
        }
        ....
}

Craig

-----Original Message-----
From: Igor Vaynberg [mailto:[email protected]] 
Sent: May-13-11 5:02 PM
To: [email protected]
Subject: Re: How to check markup attributes?

if you are doing validation you can use imarkupfilter to check the attrs.

-igor


On Fri, May 13, 2011 at 1:57 PM, Craig Pardey
<[email protected]> wrote:
> I'd like to check that particular markup attributes have been set on a 
> component.
> My first instinct was to use component.getMarkupAttributes(), but the JavaDoc 
> quite clearly suggests that it shouldn't be used.
>
> For example, all TextFields should have a 'maxlength' defined:
>
> public class MyTextField<T> extends TextField<T> {
>      @Override
>      public void onAfterRender(){
>            super.onAfterRender();
>            ValueMap attrs = getMarkupAttributes();
>            if( !attrs.containsKey("maxlength")){
>                  throw new IllegalStateException("No maxlength defined for " 
> + getId());
>            }
>      }
> }
>
> Is there a better way to achieve this?
> Ideally I'd like to do it in a unit test rather than at runtime.
>
> Craig
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to