Search in sources :

Example 1 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class HtmlUnknownBooleanAttributeInspectionBase method checkAttribute.

@Override
protected void checkAttribute(@NotNull final XmlAttribute attribute, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (attribute.getValueElement() == null) {
        final XmlTag tag = attribute.getParent();
        if (tag instanceof HtmlTag) {
            XmlElementDescriptor elementDescriptor = tag.getDescriptor();
            if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
                return;
            }
            XmlAttributeDescriptor attributeDescriptor = elementDescriptor.getAttributeDescriptor(attribute);
            if (attributeDescriptor != null && !(attributeDescriptor instanceof AnyXmlAttributeDescriptor)) {
                String name = attribute.getName();
                if (!HtmlUtil.isBooleanAttribute(attributeDescriptor, null) && (!isCustomValuesEnabled() || !isCustomValue(name))) {
                    final boolean html5 = HtmlUtil.isHtml5Context(tag);
                    LocalQuickFix[] quickFixes = !html5 ? new LocalQuickFix[] { new AddCustomHtmlElementIntentionAction(BOOLEAN_ATTRIBUTE_KEY, name, XmlBundle.message("add.custom.html.boolean.attribute", name)), XmlQuickFixFactory.getInstance().addAttributeValueFix(attribute), new RemoveAttributeIntentionAction(name) } : new LocalQuickFix[] { XmlQuickFixFactory.getInstance().addAttributeValueFix(attribute) };
                    String error = null;
                    if (html5) {
                        if (attributeDescriptor instanceof XmlEnumerationDescriptor && ((XmlEnumerationDescriptor) attributeDescriptor).getValueDeclaration(attribute, "") == null) {
                            error = XmlErrorMessages.message("wrong.value", "attribute");
                        }
                    } else {
                        error = XmlErrorMessages.message("attribute.is.not.boolean", attribute.getName());
                    }
                    if (error != null) {
                        registerProblemOnAttributeName(attribute, error, holder, quickFixes);
                    }
                }
            }
        }
    }
}
Also used : AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) XmlEnumerationDescriptor(com.intellij.xml.impl.XmlEnumerationDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) AnyXmlAttributeDescriptor(com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class HtmlUnknownTagInspectionBase method checkTag.

@Override
protected void checkTag(@NotNull final XmlTag tag, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
        return;
    }
    XmlElementDescriptor descriptorFromContext = XmlUtil.getDescriptorFromContext(tag);
    PsiElement parent = tag.getParent();
    XmlElementDescriptor parentDescriptor = parent instanceof XmlTag ? ((XmlTag) parent).getDescriptor() : null;
    XmlElementDescriptor ownDescriptor = isAbstractDescriptor(descriptorFromContext) ? tag.getDescriptor() : descriptorFromContext;
    if (isAbstractDescriptor(ownDescriptor) || (parentDescriptor instanceof HtmlElementDescriptorImpl && ownDescriptor instanceof HtmlElementDescriptorImpl && isAbstractDescriptor(descriptorFromContext))) {
        final String name = tag.getName();
        if (!isCustomValuesEnabled() || !isCustomValue(name)) {
            final AddCustomHtmlElementIntentionAction action = new AddCustomHtmlElementIntentionAction(TAG_KEY, name, XmlBundle.message("add.custom.html.tag", name));
            // todo: support "element is not allowed" message for html5
            // some tags in html5 cannot be found in xhtml5.xsd if they are located in incorrect context, so they get any-element descriptor (ex. "canvas: tag)
            final String message = isAbstractDescriptor(ownDescriptor) ? XmlErrorMessages.message("unknown.html.tag", name) : XmlErrorMessages.message("element.is.not.allowed.here", name);
            final PsiElement startTagName = XmlTagUtil.getStartTagNameElement(tag);
            assert startTagName != null;
            final PsiElement endTagName = XmlTagUtil.getEndTagNameElement(tag);
            List<LocalQuickFix> quickfixes = new ArrayList<>();
            quickfixes.add(action);
            if (isOnTheFly) {
                PsiFile file = startTagName.getContainingFile();
                if (file instanceof XmlFile) {
                    quickfixes.add(XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(startTagName, "", null));
                }
                // People using non-HTML as their template data language (but having not changed this in the IDE)
                // will most likely see 'unknown html tag' error, because HTML is usually the default.
                // So if they check quick fixes for this error they'll discover Change Template Data Language feature.
                ContainerUtil.addIfNotNull(quickfixes, createChangeTemplateDataFix(file));
            }
            if (HtmlUtil.isHtml5Tag(name) && !HtmlUtil.hasNonHtml5Doctype(tag)) {
                quickfixes.add(new SwitchToHtml5WithHighPriorityAction());
            }
            ProblemHighlightType highlightType = tag.getContainingFile().getContext() == null ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING : ProblemHighlightType.INFORMATION;
            if (startTagName.getTextLength() > 0) {
                holder.registerProblem(startTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
            if (endTagName != null) {
                holder.registerProblem(endTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) PsiFile(com.intellij.psi.PsiFile) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class XmlHighlightVisitor method checkTagByDescriptor.

private void checkTagByDescriptor(final XmlTag tag) {
    String name = tag.getName();
    XmlElementDescriptor elementDescriptor;
    final PsiElement parent = tag.getParent();
    if (parent instanceof XmlTag) {
        XmlTag parentTag = (XmlTag) parent;
        elementDescriptor = XmlUtil.getDescriptorFromContext(tag);
        final XmlElementDescriptor parentDescriptor = parentTag.getDescriptor();
        if (parentDescriptor != null && elementDescriptor == null && shouldBeValidated(tag)) {
            if (tag instanceof HtmlTag) {
                //if (inspection != null /*&& isAdditionallyDeclared(inspection.getAdditionalEntries(XmlEntitiesInspection.UNKNOWN_TAG), name)*/) {
                return;
            //}
            }
            addElementsForTag(tag, XmlErrorMessages.message("element.is.not.allowed.here", name), getTagProblemInfoType(tag));
            return;
        }
        if (elementDescriptor instanceof AnyXmlElementDescriptor || elementDescriptor == null) {
            elementDescriptor = tag.getDescriptor();
        }
        if (elementDescriptor == null)
            return;
    } else {
        //root tag
        elementDescriptor = tag.getDescriptor();
        if (elementDescriptor == null) {
            addElementsForTag(tag, XmlErrorMessages.message("element.must.be.declared", name), HighlightInfoType.WRONG_REF);
            return;
        }
    }
    if (elementDescriptor instanceof Validator) {
        //noinspection unchecked
        ((Validator<XmlTag>) elementDescriptor).validate(tag, this);
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) HtmlTag(com.intellij.psi.html.HtmlTag) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) Validator(com.intellij.codeInsight.daemon.Validator)

Example 4 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class XmlHighlightVisitor method checkTag.

private void checkTag(XmlTag tag) {
    if (ourDoJaxpTesting)
        return;
    if (!myHolder.hasErrorResults()) {
        checkTagByDescriptor(tag);
    }
    if (!myHolder.hasErrorResults()) {
        if (!skipValidation(tag)) {
            final XmlElementDescriptor descriptor = tag.getDescriptor();
            if (tag instanceof HtmlTag && (descriptor instanceof AnyXmlElementDescriptor || descriptor == null)) {
                return;
            }
            checkReferences(tag);
        }
    }
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) HtmlTag(com.intellij.psi.html.HtmlTag) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor)

Example 5 with HtmlTag

use of com.intellij.psi.html.HtmlTag in project intellij-community by JetBrains.

the class XmlHighlightVisitor method reportAttributeProblem.

@Nullable
private HighlightInfo reportAttributeProblem(final XmlTag tag, final String localName, final XmlAttribute attribute, @NotNull String localizedMessage) {
    final RemoveAttributeIntentionFix removeAttributeIntention = new RemoveAttributeIntentionFix(localName, attribute);
    if (!(tag instanceof HtmlTag)) {
        final HighlightInfoType tagProblemInfoType = HighlightInfoType.WRONG_REF;
        final ASTNode node = SourceTreeToPsiMap.psiElementToTree(attribute);
        assert node != null;
        final ASTNode child = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(node);
        assert child != null;
        final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(tagProblemInfoType).range(child).descriptionAndTooltip(localizedMessage).create();
        addToResults(highlightInfo);
        QuickFixAction.registerQuickFixAction(highlightInfo, removeAttributeIntention);
        return highlightInfo;
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) HtmlTag(com.intellij.psi.html.HtmlTag) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

HtmlTag (com.intellij.psi.html.HtmlTag)25 XmlTag (com.intellij.psi.xml.XmlTag)9 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)8 PsiElement (com.intellij.psi.PsiElement)7 PsiFile (com.intellij.psi.PsiFile)7 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)5 ASTNode (com.intellij.lang.ASTNode)4 Language (com.intellij.lang.Language)4 TextRange (com.intellij.openapi.util.TextRange)4 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)3 XHTMLLanguage (com.intellij.lang.xhtml.XHTMLLanguage)3 XMLLanguage (com.intellij.lang.xml.XMLLanguage)3 Project (com.intellij.openapi.project.Project)3 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)3 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)2 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)2