Search in sources :

Example 1 with HTMLElementDeclaration

use of org.eclipse.wst.html.core.internal.contentmodel.HTMLElementDeclaration in project webtools.sourceediting by eclipse.

the class HTMLModelParserAdapter method canContain.

/**
 */
public boolean canContain(Element element, Node child) {
    if (element == null || child == null)
        return false;
    IDOMElement impl = (IDOMElement) element;
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        if (!impl.isGlobalTag())
            // non HTML tag
            return true;
        IDOMElement childElement = (IDOMElement) child;
        CMElementDeclaration myDec = CMNodeUtil.getElementDeclaration(element);
        if (myDec == null)
            return true;
        // if (!(myDec instanceof HTMLElementDeclaration)) return true;
        if (myDec.getContentType() == CMElementDeclaration.EMPTY)
            return false;
        if (!childElement.isGlobalTag())
            // non HTML tag
            return true;
        CMElementDeclaration childDec = CMNodeUtil.getElementDeclaration(childElement);
        if (childDec == null)
            return true;
        if (myDec instanceof HTMLElementDeclaration) {
            if (((Boolean) ((HTMLElementDeclaration) myDec).getProperty(HTMLCMProperties.IS_JSP)).booleanValue())
                return true;
        }
        if (shouldTerminateAt(myDec, childDec) && !isValidChild(myDec, childDec)) {
            return false;
        }
        String tagName = impl.getTagName();
        if (tagName == null)
            return true;
        String childName = childElement.getTagName();
        if (childName == null)
            return true;
        if (!impl.hasStartTag() && !impl.hasEndTag()) {
            // implicit element
            if (tagName.equalsIgnoreCase(childElement.getTagName()))
                return false;
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.HEAD)) {
                if (!childName.equalsIgnoreCase(HTML40Namespace.ElementName.META) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.TITLE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.LINK) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.STYLE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.BASE) && !childName.equalsIgnoreCase(HTML40Namespace.ElementName.ISINDEX)) {
                    return false;
                }
            }
            Node parent = element.getParentNode();
            if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                IDOMElement parentElement = (IDOMElement) parent;
                if (!parentElement.hasStartTag() && !parentElement.hasEndTag()) {
                    if (!canContain(parentElement, child))
                        return false;
                }
            }
            return true;
        }
        // contexual termination for TABLE content tags
        boolean isTableContent = false;
        if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT)) {
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                return true;
            isTableContent = true;
        } else if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TR)) {
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                return true;
            isTableContent = true;
        } else if (childName.equalsIgnoreCase(HTML40Namespace.ElementName.TD) || childName.equalsIgnoreCase(HTML40Namespace.ElementName.TH)) {
            if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TR) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TBODY) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.THEAD) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TFOOT) || tagName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                return true;
            isTableContent = true;
        }
        if (isTableContent) {
            // if in TABLE
            for (Node parent = element.getParentNode(); parent != null; parent = parent.getParentNode()) {
                if (parent.getNodeType() != Node.ELEMENT_NODE)
                    break;
                IDOMElement parentElement = (IDOMElement) parent;
                String parentName = parentElement.getTagName();
                if (parentName == null)
                    continue;
                if (parentName.equalsIgnoreCase(HTML40Namespace.ElementName.TABLE))
                    return false;
            }
        }
        if (tagName.equalsIgnoreCase(HTML40Namespace.ElementName.EMBED)) {
            if (!childName.equalsIgnoreCase(HTML40Namespace.ElementName.NOEMBED))
                return false;
        }
    } else if (child.getNodeType() == Node.TEXT_NODE) {
        String tagName = impl.getTagName();
        if (tagName != null && tagName.equalsIgnoreCase(HTML40Namespace.ElementName.EMBED)) {
            IDOMText text = (IDOMText) child;
            if (!text.isElementContentWhitespace())
                return false;
        }
    } else if (child.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        if (impl.isImplicitTag())
            return false;
    }
    return true;
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) HTMLElementDeclaration(org.eclipse.wst.html.core.internal.contentmodel.HTMLElementDeclaration) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

HTMLElementDeclaration (org.eclipse.wst.html.core.internal.contentmodel.HTMLElementDeclaration)1 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)1 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)1 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)1 IDOMText (org.eclipse.wst.xml.core.internal.provisional.document.IDOMText)1 Node (org.w3c.dom.Node)1