    001// Licensed under the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005//     http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.corelib.mixins;
    014
    015import org.apache.tapestry5.MarkupWriter;
    016import org.apache.tapestry5.annotations.MixinAfter;
    017import org.apache.tapestry5.dom.Element;
    018
    019/**
    020 * A mixin that attaches to an element that renders an element.  At the end of the render, if the element is empty, then
    021 * a non-breaking space (&amp;nbsp;) is injected into the element. This is often necessary for proper rendering on the
    022 * client.
    023 *
    024 * Often used in conjunction with the {@link org.apache.tapestry5.corelib.components.Any} component.
    025 *
    026 * @since 5.1.0.0
    027 * @tapestrydoc
    028 */
    029@MixinAfter
    030public class NotEmpty
    031{
    032    private Element element;
    033
    034    void beginRender(MarkupWriter writer)
    035    {
    036        element = writer.getElement();
    037    }
    038
    039    void afterRender()
    040    {
    041        if (element.isEmpty())
    042        {
    043            element.removeChildren();
    044            element.raw("&nbsp;");
    045        }
    046    }
    047}