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.internal.services.javascript;
014
015import org.apache.tapestry5.MarkupWriter;
016import org.apache.tapestry5.dom.Element;
017import org.apache.tapestry5.services.MarkupRenderer;
018import org.apache.tapestry5.services.MarkupRendererFilter;
019
020/**
021 * Responsible for adding additional style tags that contain directives for non-standards compatible browsers
022 *
023 * @since 5.4
024 */
025public class AddBrowserCompatibilityStyles implements MarkupRendererFilter
026{
027    private final String ie9, ie8;
028
029    public AddBrowserCompatibilityStyles()
030    {
031        this(.25);
032    }
033
034    public AddBrowserCompatibilityStyles(double opacity)
035    {
036        // IE9 does not support CSS animations, so we make the loading mask translucent
037        ie9 = String.format("<!--[if IE 9]><style type=\"text/css\">.pageloading-mask{opacity:%.2f;}</style><![endif]-->", opacity);
038        // Older IE versions do not even support opacity, we'll have to resort to a filter
039        ie8 = String.format("<!--[if lt IE 9]><style type=\"text/css\">.pageloading-mask{filter:alpha(opacity=%d);}</style><![endif]-->",
040                (int) (100. * opacity));
041    }
042
043    public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer)
044    {
045        renderer.renderMarkup(writer);
046
047        Element head = writer.getDocument().find("html/head");
048
049        // Only add the respective style documents if we've rendered an HTML document
050        if (head != null)
051        {
052            head.raw(ie9);
053            head.raw(ie8);
054        }
055    }
056}