Search in sources :

Example 6 with IDOMText

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.

the class DelegatingSourceValidator method computeStartAndEndLocation.

/**
 * Calculates the "better" offsets.
 *
 * @param startOffset -
 *            the offset given by Xerces
 * @param errorMessage -
 *            the Xerces error Message
 * @param selectionStrategy -
 *            the selectionStrategy
 * @param document -
 *            the document
 * @return int[] - position 0 has the start offset of the squiggle range,
 *         position 1 has the endOffset
 */
/*
	 * The way the offsets is calculated is: - find the indexed region
	 * (element) closest to the given offset - if we are between two elements,
	 * choosing left or right element will depend on parameter 'errorSide' -
	 * based on the selectionStrategy choose the underlining strategy (eg
	 * START_TAG means underline the start tag of that element) - use
	 * information from nameOrValue and the DOM to get better offsets
	 * 
	 */
protected int[] computeStartAndEndLocation(int startOffset, String selectionStrategy, String errorSide, String nameOrValue, IDOMDocument document) {
    try {
        int[] startEndPositions = new int[2];
        IndexedRegion region = document.getModel().getIndexedRegion(startOffset);
        IndexedRegion prevRegion = document.getModel().getIndexedRegion(startOffset - 1);
        if (prevRegion != region) {
            // exactly where we need to be.
            if (ERROR_SIDE_LEFT.equals(errorSide)) {
                region = prevRegion;
            }
        }
        // the start of the region where the error was
        if (region != null) {
            startEndPositions[0] = region.getStartOffset();
            startEndPositions[1] = startEndPositions[0];
        } else {
            // this will message will not get added to the IReporter
            // since the length is 0
            startEndPositions[0] = 0;
            startEndPositions[1] = 0;
        }
        if (region instanceof Node) {
            Node node = (Node) region;
            if (START_TAG.equals(selectionStrategy)) {
                // underline the opening tag
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    startEndPositions[0] = element.getStartOffset() + 1;
                    startEndPositions[1] = startEndPositions[0] + element.getTagName().length();
                }
            } else if (END_TAG.equals(selectionStrategy)) {
                // underline the end tag
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    startEndPositions[0] = element.getEndStartOffset();
                    startEndPositions[1] = element.getEndOffset();
                }
            } else if (ATTRIBUTE_NAME.equals(selectionStrategy)) {
                // underline the attribute's name
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    IDOMNode attributeNode = (IDOMNode) (element.getAttributeNode(nameOrValue));
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getStartOffset();
                        startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
                    }
                }
            } else if (ATTRIBUTE_VALUE.equals(selectionStrategy)) {
                // underline the attribute's value
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    IDOMAttr attributeNode = (IDOMAttr) (element.getAttributeNode(nameOrValue));
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getValueRegionStartOffset();
                        String valueRegionText = attributeNode.getValueRegionText();
                        int valueRegionLength = valueRegionText == null ? 0 : valueRegionText.length();
                        startEndPositions[1] = startEndPositions[0] + valueRegionLength;
                    }
                }
            } else if (ALL_ATTRIBUTES.equals(selectionStrategy)) {
                // underline all attributes
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    NamedNodeMap attributes = element.getAttributes();
                    if (attributes != null) {
                        IDOMNode first = (IDOMNode) attributes.item(0);
                        IDOMNode last = (IDOMNode) attributes.item(attributes.getLength() - 1);
                        if ((first != null) && (last != null)) {
                            startEndPositions[0] = first.getStartOffset();
                            startEndPositions[1] = last.getEndOffset();
                        }
                    }
                }
            } else if (TEXT.equals(selectionStrategy)) {
                // underline the text between the tags
                if (node.getNodeType() == Node.TEXT_NODE) {
                    IDOMText textNode = (IDOMText) node;
                    int start = textNode.getStartOffset();
                    String value = textNode.getNodeValue();
                    int index = 0;
                    char curChar = value.charAt(index);
                    // whitespace:
                    while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
                        curChar = value.charAt(index);
                        index++;
                    }
                    if (index > 0) {
                        index--;
                    }
                    start = start + index;
                    startEndPositions[0] = start;
                    startEndPositions[1] = start + value.trim().length();
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    IDOMElement element = (IDOMElement) node;
                    Node child = element.getFirstChild();
                    if (child instanceof IDOMNode) {
                        IDOMNode xmlChild = ((IDOMNode) child);
                        startEndPositions[0] = xmlChild.getStartOffset();
                        startEndPositions[1] = xmlChild.getEndOffset();
                    }
                }
            } else if (FIRST_NON_WHITESPACE_TEXT.equals(selectionStrategy)) {
                // text node
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NodeList nodes = node.getChildNodes();
                    for (int i = 0; i < nodes.getLength(); i++) {
                        Node currentNode = nodes.item(i);
                        if (currentNode.getNodeType() == Node.TEXT_NODE) {
                            // TODO (Trung) I don't think we should call
                            // getNodeValue(), trim(), length()
                            // repeatedly.
                            // This is inefficient, to improve use local
                            // variables to store values.
                            IDOMText textNode = (IDOMText) currentNode;
                            if (textNode.getNodeValue().trim().length() > 0) {
                                String value = textNode.getNodeValue();
                                int index = 0;
                                int start = textNode.getStartOffset();
                                char curChar = value.charAt(index);
                                // skipping over whitespace:
                                while ((curChar == '\n') || (curChar == '\t') || (curChar == '\r') || (curChar == ' ')) {
                                    curChar = value.charAt(index);
                                    index++;
                                }
                                if (index > 0) {
                                    index--;
                                }
                                start = start + index;
                                startEndPositions[0] = start;
                                startEndPositions[1] = start + value.trim().length();
                                break;
                            }
                        }
                    }
                }
            } else if (TEXT_ENTITY_REFERENCE.equals(selectionStrategy)) {
                if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                    startEndPositions[0] = region.getStartOffset();
                    startEndPositions[1] = region.getEndOffset();
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    /*
						 * In this case the undeclared entity might be in one
						 * of the attribute values. Search through the
						 * attributes to find the range of the undeclared
						 * entity.
						 */
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String entity = "&" + nameOrValue + ";";
                    NamedNodeMap attributes = node.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        IDOMAttr attr = (IDOMAttr) attributes.item(i);
                        String nodeValue = attr.getNodeValue();
                        int index = nodeValue.indexOf(entity);
                        if (index != -1) {
                            startEndPositions[0] = attr.getValueRegionStartOffset() + index + 1;
                            startEndPositions[1] = startEndPositions[0] + entity.length();
                        }
                    }
                }
            } else if (VALUE_OF_ATTRIBUTE_WITH_GIVEN_VALUE.equals(selectionStrategy)) {
                // ATTRIBUTE_VALUE ?
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    // here we will search through all attributes for the
                    // one with the
                    // with the value we want:
                    // TODO (Trung) I see a potential problem here.
                    // What happens when there is another attribute having
                    // the same value
                    // with this attribute's buggy value ?
                    // Need to solve when time permits.
                    NamedNodeMap attributes = node.getAttributes();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        IDOMAttr attr = (IDOMAttr) attributes.item(i);
                        String nodeValue = attr.getNodeValue().trim();
                        if (nodeValue.equals(nameOrValue)) {
                            startEndPositions[0] = attr.getValueRegionStartOffset() + 1;
                            startEndPositions[1] = startEndPositions[0] + nodeValue.length();
                            break;
                        }
                    }
                }
            } else if (ATTRIBUTE_NAME_LAST.equals(selectionStrategy)) {
                // underline the last attribute's name
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap attributeMap = node.getAttributes();
                    final int length = attributeMap.getLength();
                    Node tempNode = null;
                    Node attrNode = null;
                    for (int i = 0; i < length; i++) {
                        tempNode = attributeMap.item(i);
                        if (tempNode != null && tempNode.getNodeName().equals(nameOrValue)) {
                            attrNode = tempNode;
                        }
                    }
                    IDOMNode attributeNode = (IDOMNode) (attrNode);
                    if (attributeNode != null) {
                        startEndPositions[0] = attributeNode.getStartOffset();
                        startEndPositions[1] = attributeNode.getStartOffset() + nameOrValue.length();
                    }
                }
            }
        }
        return startEndPositions;
    } finally // catch (Exception e) { // e.printStackTrace();
    // }
    {
    }
// return null;
}
Also used : IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) NamedNodeMap(org.w3c.dom.NamedNodeMap) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) NodeList(org.w3c.dom.NodeList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 7 with IDOMText

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.

the class HTMLDocumentContentValidator method validateContent.

/*
	 * This methods validate nodes other than HTML elements.
	 */
private void validateContent(Iterator children, boolean isFragment) {
    boolean foundDoctype = false;
    while (children.hasNext()) {
        IDOMNode child = (IDOMNode) children.next();
        int error = ErrorState.NONE_ERROR;
        int segType = FMUtil.SEG_WHOLE_TAG;
        switch(child.getNodeType()) {
            case Node.ELEMENT_NODE:
                if (!isFragment) {
                    Element childElem = (Element) child;
                    CMElementDeclaration ced = CMUtil.getDeclaration(childElem);
                    // Undefined element is valid.
                    if (ced == null)
                        continue;
                    // JSP (includes custom tags) and SSI are valid.
                    if (CMUtil.isForeign(childElem) || CMUtil.isSSI(ced))
                        // Defect 186774
                        continue;
                    // report error (invalid content)
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    // mark the whole start tag as error.
                    segType = FMUtil.SEG_START_TAG;
                }
                break;
            case Node.TEXT_NODE:
                if (!isFragment) {
                    // Otherwise, it is invalid content.
                    if (((IDOMText) child).isElementContentWhitespace())
                        continue;
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    segType = FMUtil.SEG_WHOLE_TAG;
                }
                break;
            case Node.DOCUMENT_TYPE_NODE:
                // once.
                if (!foundDoctype) {
                    foundDoctype = true;
                    continue;
                }
                error = ErrorState.DUPLICATE_ERROR;
                segType = FMUtil.SEG_WHOLE_TAG;
                break;
            case Node.COMMENT_NODE:
            // always valid.
            case Node.PROCESSING_INSTRUCTION_NODE:
                continue;
            default:
                if (!isFragment) {
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    segType = FMUtil.SEG_WHOLE_TAG;
                }
                break;
        }
        if (error != ErrorState.NONE_ERROR) {
            Segment errorSeg = FMUtil.getSegment(child, segType);
            if (errorSeg != null)
                reporter.report(new ErrorInfoImpl(error, errorSeg, child));
        }
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Element(org.w3c.dom.Element) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText)

Example 8 with IDOMText

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.

the class HTMLElementContentValidator method validateNode.

// private int countExplicitSiblings(Element parent, String tagName) {
// NodeList children = parent.getChildNodes();
// int count = 0;
// for (int i = 0; i < children.getLength(); i++) {
// Node child = children.item(i);
// if (child.getNodeType() != Node.ELEMENT_NODE)
// continue;
// if (tagName.equalsIgnoreCase(((Element) child).getTagName())) {
// count++;
// }
// }
// return count;
// }
/*
	 * The implementation of the following method is practical but accurate.
	 * The accurate maximum occurrence should be retrieve from the content
	 * model. However, it is useful enough, since almost implicit elements are
	 * HTML, HEAD, or BODY.
	 */
// private int getMaxOccur(Element parent, String childTag) {
// return 1;
// }
// private boolean containsName(String name, Object[] possible) {
// if (name != null && possible != null) {
// for (int i = 0; i < possible.length; i++) {
// if(name.equals(possible[i]))
// return true;
// }
// }
// return false;
// }
private void validateNode(Element target, Node child, CMElementDeclaration edec, List[] extendedContent) {
    // NOTE: If the target element is 'UNKNOWN', that is, it has no
    // element declaration, the content type of the element should be
    // regarded as 'ANY'. -- 9/10/2001
    int contentType = CMElementDeclaration.ANY;
    if (edec != null)
        contentType = edec.getContentType();
    int error = ErrorState.NONE_ERROR;
    int segType = FMUtil.SEG_WHOLE_TAG;
    switch(child.getNodeType()) {
        case Node.ELEMENT_NODE:
            Element childElem = (Element) child;
            // an element of foreign markup languages, just ignore it.
            if (CMUtil.isForeign(childElem))
                return;
            CMElementDeclaration ced = CMUtil.getDeclaration((Element) child);
            // type of the parent content model. -- 10/12/2001
            if (ced == null || CMUtil.isSSI(ced) || (!CMUtil.isHTML(ced)))
                return;
            if (CMUtil.isObsolete(ced)) {
                error = ErrorState.OBSOLETE_TAG_NAME_ERROR;
                break;
            }
            switch(contentType) {
                case CMElementDeclaration.ANY:
                    // Keep going.
                    return;
                case CMElementDeclaration.ELEMENT:
                case CMElementDeclaration.MIXED:
                    if (ced == null)
                        return;
                    if (CMUtil.isValidChild(edec, ced))
                        return;
                    // document is not a XHTML.
                    if (!CMUtil.isXHTML(edec)) {
                        // pure HTML
                        if (CMUtil.isValidInclusion(ced, target))
                            return;
                    }
                    /*
						 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=218143 - 
						 * ModelQuery use not pervasive enough
						 */
                    if (extendedContent[0] == null) {
                        extendedContent[0] = ModelQueryUtil.getModelQuery(target.getOwnerDocument()).getAvailableContent(target, edec, ModelQuery.INCLUDE_CHILD_NODES);
                    }
                    List availableChildElementDeclarations = extendedContent[0];
                    /*
						 * Retrieve and set aside just the element names for faster checking
						 * later.
						 */
                    int availableChildCount = availableChildElementDeclarations.size();
                    String elementName = ced.getElementName().toLowerCase(Locale.US);
                    for (int i = 0; i < availableChildCount; i++) {
                        CMNode cmnode = (CMNode) availableChildElementDeclarations.get(i);
                        if (cmnode.getNodeType() == CMNode.ELEMENT_DECLARATION && cmnode.getNodeName().toLowerCase(Locale.US).equals(elementName)) {
                            return;
                        }
                    }
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    break;
                default:
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    break;
            }
            // Mark the whole START tag as an error segment.
            segType = FMUtil.SEG_START_TAG;
            break;
        case Node.TEXT_NODE:
            switch(contentType) {
                case CMElementDeclaration.ANY:
                case CMElementDeclaration.MIXED:
                case CMElementDeclaration.PCDATA:
                case CMElementDeclaration.CDATA:
                    // Keep going.
                    return;
                case CMElementDeclaration.ELEMENT:
                case CMElementDeclaration.EMPTY:
                    if (((IDOMText) child).isElementContentWhitespace())
                        return;
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    break;
                default:
                    error = ErrorState.INVALID_CONTENT_ERROR;
                    break;
            }
            // Mark the whole node as an error segment.
            segType = FMUtil.SEG_WHOLE_TAG;
            break;
        case Node.COMMENT_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
            if (contentType != CMElementDeclaration.EMPTY)
                return;
            error = ErrorState.INVALID_CONTENT_ERROR;
            // Mark the whole node as an error segment.
            segType = FMUtil.SEG_WHOLE_TAG;
            break;
        case Node.CDATA_SECTION_NODE:
            if (edec.supports(HTMLCMProperties.IS_XHTML) && Boolean.TRUE.equals(edec.getProperty(HTMLCMProperties.IS_XHTML)))
                return;
            if (!edec.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.BODY)) {
                // special case for body element
                switch(contentType) {
                    case CMElementDeclaration.ANY:
                    case CMElementDeclaration.CDATA:
                    case CMElementDeclaration.MIXED:
                    case CMElementDeclaration.PCDATA:
                        return;
                }
            }
            // Mark the whole CDATA section as an error segment
            error = ErrorState.INVALID_CONTENT_ERROR;
            segType = FMUtil.SEG_WHOLE_TAG;
            break;
        case Node.ENTITY_REFERENCE_NODE:
            break;
        default:
            error = ErrorState.INVALID_CONTENT_ERROR;
            // Mark the whole node as an error segment.
            segType = FMUtil.SEG_WHOLE_TAG;
            break;
    }
    if (error != ErrorState.NONE_ERROR) {
        Segment errorSeg = FMUtil.getSegment((IDOMNode) child, segType);
        if (errorSeg != null)
            reporter.report(new ErrorInfoImpl(error, errorSeg, child));
    }
}
Also used : CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) Element(org.w3c.dom.Element) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText) List(java.util.List) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Example 9 with IDOMText

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText 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)

Example 10 with IDOMText

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.

the class HTMLTextFormatter method formatText.

/**
 */
protected void formatText(IDOMNode node, HTMLFormatContraints contraints, int mode) {
    if (node == null)
        return;
    Node parent = node.getParentNode();
    if (parent == null)
        return;
    IDOMText text = (IDOMText) node;
    String source = text.getSource();
    textRegion = text.getFirstStructuredDocumentRegion();
    if (!canFormatText(text)) {
        setWidth(contraints, source);
        return;
    }
    int offset = text.getStartOffset();
    int length = text.getEndOffset() - offset;
    // format adjacent text at once
    if (mode == FORMAT_HEAD) {
        Node next = node.getNextSibling();
        while (next != null && next.getNodeType() == Node.TEXT_NODE) {
            IDOMText nextText = (IDOMText) next;
            length += (nextText.getEndOffset() - nextText.getStartOffset());
            String nextSource = nextText.getSource();
            if (nextSource != null && nextSource.length() > 0) {
                if (source == null)
                    source = nextSource;
                else
                    source += nextSource;
            }
            next = next.getNextSibling();
        }
    } else if (mode == FORMAT_TAIL) {
        Node prev = node.getPreviousSibling();
        while (prev != null && prev.getNodeType() == Node.TEXT_NODE) {
            IDOMText prevText = (IDOMText) prev;
            offset = prevText.getStartOffset();
            length += (prevText.getEndOffset() - offset);
            String prevSource = prevText.getSource();
            if (prevSource != null && prevSource.length() > 0) {
                if (source == null)
                    source = prevSource;
                else
                    source = prevSource + source;
            }
            prev = prev.getPreviousSibling();
        }
    }
    SpaceConverter converter = new SpaceConverter(source, keepBlankLines(contraints));
    int wordLength = getNextWord(converter, contraints, text);
    if (wordLength == 0) {
        // only spaces
        if (!converter.hasSpaces())
            // empty
            return;
        boolean removeSpaces = false;
        if (parent.getNodeType() == Node.ELEMENT_NODE) {
            // check if tags are omitted
            IDOMNode element = (IDOMNode) parent;
            if (node.getPreviousSibling() == null && element.getStartStructuredDocumentRegion() == null) {
                removeSpaces = true;
            } else if (node.getNextSibling() == null && element.getEndStructuredDocumentRegion() == null) {
                removeSpaces = true;
            }
        }
        if (removeSpaces) {
            converter.replaceSpaces(null);
        } else if (!isWidthAvailable(contraints, 2) || canInsertBreakAfter(node) || canInsertBreakBefore(node)) {
            String spaces = null;
            if (node.getNextSibling() == null) {
                // last spaces
                // use parent indent for the end tag
                spaces = getBreakSpaces(parent);
            } else {
                spaces = getBreakSpaces(node);
            }
            converter.replaceSpaces(spaces);
            setWidth(contraints, spaces);
        } else if (canRemoveHeadingSpaces(node) || canRemoveTailingSpaces(node)) {
            converter.replaceSpaces(null);
        } else {
            converter.compressSpaces();
            addWidth(contraints, 1);
        }
    } else {
        String breakSpaces = null;
        // format heading spaces
        boolean hasSpaces = converter.hasSpaces();
        if (mode == FORMAT_TAIL) {
            // keep spaces as is
            addWidth(contraints, converter.getSpaceCount());
        } else if ((hasSpaces && !isWidthAvailable(contraints, wordLength + 1)) || canInsertBreakBefore(node)) {
            breakSpaces = getBreakSpaces(node);
            converter.replaceSpaces(breakSpaces);
            setWidth(contraints, breakSpaces);
        } else {
            if (hasSpaces) {
                if (canRemoveHeadingSpaces(node)) {
                    converter.replaceSpaces(null);
                } else {
                    converter.compressSpaces();
                    addWidth(contraints, 1);
                }
            }
        }
        addWidth(contraints, wordLength);
        // format middle
        wordLength = getNextWord(converter, contraints, text);
        while (wordLength > 0) {
            if (mode != FORMAT_ALL) {
                // keep spaces as is
                addWidth(contraints, converter.getSpaceCount());
            } else if (!isWidthAvailable(contraints, wordLength + 1)) {
                if (breakSpaces == null)
                    breakSpaces = getBreakSpaces(node);
                converter.replaceSpaces(breakSpaces);
                setWidth(contraints, breakSpaces);
            } else {
                converter.compressSpaces();
                addWidth(contraints, 1);
            }
            addWidth(contraints, wordLength);
            wordLength = getNextWord(converter, contraints, text);
        }
        // format tailing spaces
        hasSpaces = converter.hasSpaces();
        if (mode == FORMAT_HEAD) {
            // keep spaces as is
            addWidth(contraints, converter.getSpaceCount());
        } else if ((hasSpaces && !isWidthAvailable(contraints, 2)) || canInsertBreakAfter(node)) {
            if (node.getNextSibling() == null) {
                // last test
                // use parent indent for the end tag
                breakSpaces = getBreakSpaces(parent);
            } else {
                if (breakSpaces == null)
                    breakSpaces = getBreakSpaces(node);
            }
            converter.replaceSpaces(breakSpaces);
            setWidth(contraints, breakSpaces);
        } else {
            if (hasSpaces) {
                if (canRemoveTailingSpaces(node)) {
                    converter.replaceSpaces(null);
                } else {
                    converter.compressSpaces();
                    addWidth(contraints, 1);
                }
            }
        }
    }
    if (converter.isModified()) {
        source = converter.getSource();
        replaceSource(text.getModel(), offset, length, source);
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMText(org.eclipse.wst.xml.core.internal.provisional.document.IDOMText)

Aggregations

IDOMText (org.eclipse.wst.xml.core.internal.provisional.document.IDOMText)10 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)5 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)4 Node (org.w3c.dom.Node)4 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)3 Element (org.w3c.dom.Element)3 List (java.util.List)2 IRegion (org.eclipse.jface.text.IRegion)2 Region (org.eclipse.jface.text.Region)2 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)2 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)2 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)2 NodeList (org.w3c.dom.NodeList)2 ArrayList (java.util.ArrayList)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 Template (org.eclipse.jface.text.templates.Template)1 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)1 TemplateException (org.eclipse.jface.text.templates.TemplateException)1