Search in sources :

Example 1 with HtmlElementDescriptorImpl

use of com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl 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 2 with HtmlElementDescriptorImpl

use of com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl in project intellij-community by JetBrains.

the class HtmlCompletionContributor method addSpecificCompletions.

@NotNull
@NonNls
protected static String[] addSpecificCompletions(final XmlAttribute attribute) {
    @NonNls String name = attribute.getName();
    final XmlTag tag = attribute.getParent();
    if (tag == null)
        return ArrayUtil.EMPTY_STRING_ARRAY;
    @NonNls String tagName = tag.getName();
    if (tag.getDescriptor() instanceof HtmlElementDescriptorImpl) {
        name = name.toLowerCase();
        tagName = tagName.toLowerCase();
    }
    final String namespace = tag.getNamespace();
    if (XmlUtil.XHTML_URI.equals(namespace) || XmlUtil.HTML_URI.equals(namespace)) {
        if ("target".equals(name)) {
            return new String[] { "_blank", "_top", "_self", "_parent" };
        } else if ("enctype".equals(name)) {
            return new String[] { "multipart/form-data", "application/x-www-form-urlencoded" };
        } else if ("rel".equals(name) || "rev".equals(name)) {
            return new String[] { "alternate", "author", "bookmark", "help", "icon", "license", "next", "nofollow", "noreferrer", "prefetch", "prev", "search", "stylesheet", "tag", "start", "contents", "index", "glossary", "copyright", "chapter", "section", "subsection", "appendix", "script", "import", "apple-touch-icon", "apple-touch-icon-precomposed", "apple-touch-startup-image" };
        } else if ("media".equals(name)) {
            return new String[] { "all", "braille", "embossed", "handheld", "print", "projection", "screen", "speech", "tty", "tv" };
        } else if ("language".equals(name)) {
            return new String[] { "JavaScript", "VBScript", "JScript", "JavaScript1.2", "JavaScript1.3", "JavaScript1.4", "JavaScript1.5" };
        } else if ("type".equals(name) && "link".equals(tagName)) {
            return new String[] { "text/css", "text/html", "text/plain", "text/xml" };
        } else if ("http-equiv".equals(name) && "meta".equals(tagName)) {
            return HtmlUtil.RFC2616_HEADERS;
        } else if ("content".equals(name) && "meta".equals(tagName) && tag.getAttribute("name") == null) {
            return HtmlUtil.CONTENT_TYPES;
        } else if ("accept".equals(name) && "input".equals(tagName)) {
            return HtmlUtil.CONTENT_TYPES;
        } else if ("accept-charset".equals(name) || "charset".equals(name)) {
            Charset[] charSets = CharsetToolkit.getAvailableCharsets();
            String[] names = new String[charSets.length];
            for (int i = 0; i < names.length; i++) {
                names[i] = charSets[i].toString();
            }
            return names;
        }
    }
    return ArrayUtil.EMPTY_STRING_ARRAY;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) XmlTag(com.intellij.psi.xml.XmlTag) NonNls(org.jetbrains.annotations.NonNls) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with HtmlElementDescriptorImpl

use of com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl in project intellij-community by JetBrains.

the class DefaultXmlTagNameProvider method addTagNameVariants.

@Override
public void addTagNameVariants(List<LookupElement> elements, @NotNull XmlTag tag, String prefix) {
    final List<String> namespaces;
    if (prefix.isEmpty()) {
        namespaces = new ArrayList<>(Arrays.asList(tag.knownNamespaces()));
        // empty namespace
        namespaces.add(XmlUtil.EMPTY_URI);
    } else {
        namespaces = new ArrayList<>(Collections.singletonList(tag.getNamespace()));
    }
    PsiFile psiFile = tag.getContainingFile();
    XmlExtension xmlExtension = XmlExtension.getExtension(psiFile);
    List<String> nsInfo = new ArrayList<>();
    List<XmlElementDescriptor> variants = TagNameVariantCollector.getTagDescriptors(tag, namespaces, nsInfo);
    if (variants.isEmpty() && psiFile instanceof XmlFile && ((XmlFile) psiFile).getRootTag() == tag) {
        getRootTagsVariants(tag, elements);
        return;
    }
    final Set<String> visited = new HashSet<>();
    for (int i = 0; i < variants.size(); i++) {
        XmlElementDescriptor descriptor = variants.get(i);
        String qname = descriptor.getName(tag);
        if (!visited.add(qname))
            continue;
        if (!prefix.isEmpty() && qname.startsWith(prefix + ":")) {
            qname = qname.substring(prefix.length() + 1);
        }
        PsiElement declaration = descriptor.getDeclaration();
        if (declaration != null && !declaration.isValid()) {
            LOG.error(descriptor + " contains invalid declaration: " + declaration);
        }
        LookupElementBuilder lookupElement = declaration == null ? LookupElementBuilder.create(qname) : LookupElementBuilder.create(declaration, qname);
        final int separator = qname.indexOf(':');
        if (separator > 0) {
            lookupElement = lookupElement.withLookupString(qname.substring(separator + 1));
        }
        String ns = nsInfo.get(i);
        if (StringUtil.isNotEmpty(ns)) {
            lookupElement = lookupElement.withTypeText(ns, true);
        }
        if (descriptor instanceof PsiPresentableMetaData) {
            lookupElement = lookupElement.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
        }
        if (xmlExtension.useXmlTagInsertHandler()) {
            lookupElement = lookupElement.withInsertHandler(XmlTagInsertHandler.INSTANCE);
        }
        lookupElement = lookupElement.withCaseSensitivity(!(descriptor instanceof HtmlElementDescriptorImpl));
        elements.add(PrioritizedLookupElement.withPriority(lookupElement, separator > 0 ? 0 : 1));
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) XmlExtension(com.intellij.xml.XmlExtension) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiFile(com.intellij.psi.PsiFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 4 with HtmlElementDescriptorImpl

use of com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl in project intellij-plugins by JetBrains.

the class AngularJSAttributeDescriptorsProvider method getAttributeDescriptors.

@Override
public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag xmlTag) {
    if (xmlTag != null) {
        final Map<String, XmlAttributeDescriptor> result = new LinkedHashMap<>();
        final Project project = xmlTag.getProject();
        final XmlElementDescriptor descriptor = xmlTag.getDescriptor();
        final Collection<String> directives = AngularIndexUtil.getAllKeys(AngularDirectivesIndex.KEY, project);
        if (AngularIndexUtil.hasAngularJS2(project)) {
            if (descriptor instanceof HtmlElementDescriptorImpl) {
                final XmlAttributeDescriptor[] descriptors = ((HtmlElementDescriptorImpl) descriptor).getDefaultAttributeDescriptors(xmlTag);
                for (XmlAttributeDescriptor attributeDescriptor : descriptors) {
                    final String name = attributeDescriptor.getName();
                    if (name.startsWith("on")) {
                        addAttributes(project, result, "(" + name.substring(2) + ")", null);
                    }
                }
            }
            for (XmlAttribute attribute : xmlTag.getAttributes()) {
                final String name = attribute.getName();
                if (isAngular2Attribute(name, project) || !directives.contains(name))
                    continue;
                final PsiElement declaration = applicableDirective(project, name, xmlTag, AngularDirectivesIndex.KEY);
                if (isApplicable(declaration)) {
                    for (XmlAttributeDescriptor binding : AngularAttributeDescriptor.getFieldBasedDescriptors((JSImplicitElement) declaration)) {
                        result.put(binding.getName(), binding);
                    }
                }
            }
        }
        final Collection<String> docDirectives = AngularIndexUtil.getAllKeys(AngularDirectivesDocIndex.KEY, project);
        for (String directiveName : docDirectives) {
            PsiElement declaration = applicableDirective(project, directiveName, xmlTag, AngularDirectivesDocIndex.KEY);
            if (isApplicable(declaration)) {
                addAttributes(project, result, directiveName, declaration);
            }
        }
        for (String directiveName : directives) {
            if (!docDirectives.contains(directiveName)) {
                PsiElement declaration = applicableDirective(project, directiveName, xmlTag, AngularDirectivesIndex.KEY);
                if (isApplicable(declaration)) {
                    addAttributes(project, result, directiveName, declaration);
                }
            }
        }
        return result.values().toArray(new XmlAttributeDescriptor[result.size()]);
    }
    return XmlAttributeDescriptor.EMPTY;
}
Also used : Project(com.intellij.openapi.project.Project) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

HtmlElementDescriptorImpl (com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl)4 PsiElement (com.intellij.psi.PsiElement)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 Project (com.intellij.openapi.project.Project)1 HtmlTag (com.intellij.psi.html.HtmlTag)1 PsiPresentableMetaData (com.intellij.psi.meta.PsiPresentableMetaData)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)1 XmlExtension (com.intellij.xml.XmlExtension)1 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 NonNls (org.jetbrains.annotations.NonNls)1 NotNull (org.jetbrains.annotations.NotNull)1