/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.codeInsight.template;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlTag;
import com.intellij.xml.XmlBundle;
import org.jetbrains.annotations.NotNull;

public class XmlElementContextType extends TemplateContextType {

  public XmlElementContextType() {
    super("XML_TAG", XmlBundle.message("xml.tag"), XmlContextType.class);
  }

  @Override
  public boolean isInContext(@NotNull TemplateActionContext templateActionContext) {
    PsiFile file = templateActionContext.getFile();
    int startOffset = templateActionContext.getStartOffset();
    int endOffset = templateActionContext.getEndOffset();
    if (endOffset <= startOffset) return false;
    if (!XmlContextType.isInXml(file, startOffset)) return false;
    PsiElement start = file.findElementAt(startOffset);
    PsiElement end = file.findElementAt(endOffset - 1);
    if (start instanceof PsiWhiteSpace) {
      start = start.getNextSibling();
    }
    if (end instanceof PsiWhiteSpace) {
      end = end.getPrevSibling();
    }
    if (start == null || end == null) return false;
    PsiElement parent = PsiTreeUtil.findCommonParent(start, end);
    if (!(parent instanceof XmlTag)) return false;
    TextRange range = parent.getTextRange();
    return range.getStartOffset() >= startOffset && range.getEndOffset() <= endOffset;
  }
}

@@ -1333,24 +1333,373 @@
 rtOffset();%0A
+    if (!XmlContextType.isInXml(file, startOffset)) return false;%0A    %0A    return isInXmlElementContext(templateActionContext);%0A  %7D%0A%0A  public static boolean isInXmlElementContext(@NotNull TemplateActionContext templateActionContext) %7B%0A    PsiFile file = templateActionContext.getFile();%0A    int startOffset = templateActionContext.getStartOffset();%0A
     int endO
@@ -1800,69 +1800,8 @@
     
-if (!XmlContextType.isInXml(file, startOffset)) return false;
 %0A   

fix surround with tags WEB-48604