Search code examples
coldfusioncoldfusion-11custom-tag

Making sure custom tag does not have subtags


I am building custom tag to wrap around glyphicons.

 <b:icon binding="i" />  

Part of the Glyphicon spec includes:

Only for use on empty elements

Icon classes should only be used on elements that contain no text content and have no child elements.

I want to make sure no one does something like

 <b:icon binding="i">
    <cfset myVariable++>
 </b:icon>  

Is there a way to make sure a custom tag does not have any inner tags?


Solution

  • Well you have two options that I can see.

    First, throw an exception if thisTag.executionMode is anything other than "start". Or one could likewise throw an exception if thisTag.hasEndTag is true. However this will restrict tag usage to:

    <b:icon binding="i">
    

    And not:

    <b:icon binding="i" />
    

    Because /> is shorthand for an end-tag. This is less than ideal, and you perhaps won't accept that as an approach.

    Secondly you can check if there's any generatedContent but this is a big haphazard because it's entirely possible to have something between starting and closing tags, but is careful to not generate content:

    <b:icon binding="i"><cfset foo="bar"></b:icon>
    

    (note: even the new lines and indentation would count as generatedContent if there were any).

    Bottom line: whilst JSP custom tags allow for control of this sort of thing, I cannot see how it can be controlled by the CFML implementation. The closest you can get is to prohibit closing tags entirely.