Search in sources :

Example 46 with IDOMNode

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

the class ElementNodeCleanupHandler method applyAttrNameCase.

protected void applyAttrNameCase(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return;
    int attrNameCase = HTMLCorePreferenceNames.ASIS;
    if (!shouldPreserveCase(element)) {
        if (isXMLTag(element))
            attrNameCase = HTMLCorePreferenceNames.LOWER;
        else
            attrNameCase = getCleanupPreferences().getAttrNameCase();
    }
    NamedNodeMap attributes = node.getAttributes();
    int attributesLength = attributes.getLength();
    for (int i = 0; i < attributesLength; i++) {
        IDOMNode eachAttr = (IDOMNode) attributes.item(i);
        if (hasNestedRegion(eachAttr.getNameRegion()))
            continue;
        String oldAttrName = eachAttr.getNodeName();
        String newAttrName = oldAttrName;
        /*
			 * 254961 - all HTML tag names and attribute names should be in
			 * English even for HTML files in other languages like Japanese or
			 * Turkish. English locale should be used to convert between
			 * uppercase and lowercase (otherwise "link" would be converted to
			 * Turkish "I Overdot Capital").
			 */
        if (attrNameCase == HTMLCorePreferenceNames.LOWER)
            newAttrName = oldAttrName.toLowerCase(Locale.US);
        else if (attrNameCase == HTMLCorePreferenceNames.UPPER)
            newAttrName = oldAttrName.toUpperCase(Locale.US);
        if (newAttrName.compareTo(oldAttrName) != 0) {
            int attrNameStartOffset = eachAttr.getStartOffset();
            int attrNameLength = oldAttrName.length();
            IDOMModel structuredModel = node.getModel();
            IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
            replaceSource(structuredModel, structuredDocument, attrNameStartOffset, attrNameLength, newAttrName);
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 47 with IDOMNode

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

the class ElementNodeCleanupHandler method cleanupCSSAttrValue.

/**
 */
protected void cleanupCSSAttrValue(IDOMNode node) {
    if (node == null || node.getNodeType() != Node.ELEMENT_NODE)
        return;
    IDOMElement element = (IDOMElement) node;
    if (!element.isGlobalTag())
        return;
    // $NON-NLS-1$
    Attr attr = element.getAttributeNode("style");
    if (attr == null)
        return;
    String value = getCSSValue(attr);
    if (value == null)
        return;
    String oldValue = ((IDOMNode) attr).getValueSource();
    if (oldValue != null && value.equals(oldValue))
        return;
    attr.setValue(value);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)

Example 48 with IDOMNode

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

the class ElementNodeCleanupHandler method insertMissingTags.

protected IDOMNode insertMissingTags(IDOMNode node) {
    boolean insertMissingTags = getCleanupPreferences().getInsertMissingTags();
    IDOMNode newNode = node;
    if (insertMissingTags) {
        IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
        if (startTagStructuredDocumentRegion == null) {
            // implicit start tag; generate tag for it
            newNode = insertStartTag(node);
            startTagStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
        }
        IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getEndStructuredDocumentRegion();
        ITextRegionList regionList = startTagStructuredDocumentRegion.getRegions();
        if (startTagStructuredDocumentRegion != null && regionList != null && regionList.get(regionList.size() - 1).getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
        } else {
            if (startTagStructuredDocumentRegion == null) {
                // start tag missing
                if (isStartTagRequired(newNode))
                    newNode = insertStartTag(newNode);
            } else if (endTagStructuredDocumentRegion == null) {
                // end tag missing
                if (isEndTagRequired(newNode))
                    newNode = insertEndTag(newNode);
            }
        }
    }
    return newNode;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)

Example 49 with IDOMNode

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

the class ElementNodeCleanupHandler method applyTagNameCase.

protected IDOMNode applyTagNameCase(IDOMNode node) {
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
        // do nothing
        return node;
    int tagNameCase = HTMLCorePreferenceNames.ASIS;
    if (!shouldPreserveCase(element)) {
        if (isXMLTag(element))
            tagNameCase = HTMLCorePreferenceNames.LOWER;
        else
            tagNameCase = getCleanupPreferences().getTagNameCase();
    }
    String oldTagName = node.getNodeName();
    String newTagName = oldTagName;
    IDOMNode newNode = node;
    /*
		 * 254961 - all HTML tag names and attribute names should be in
		 * English even for HTML files in other languages like Japanese or
		 * Turkish. English locale should be used to convert between uppercase
		 * and lowercase (otherwise "link" would be converted to Turkish "I
		 * Overdot Capital").
		 */
    if (tagNameCase == HTMLCorePreferenceNames.LOWER)
        newTagName = oldTagName.toLowerCase(Locale.US);
    else if (tagNameCase == HTMLCorePreferenceNames.UPPER)
        newTagName = oldTagName.toUpperCase(Locale.US);
    IDOMModel structuredModel = node.getModel();
    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
    IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
    if (startTagStructuredDocumentRegion != null) {
        ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
        if (regions != null && regions.size() > 0) {
            ITextRegion startTagNameRegion = regions.get(1);
            int startTagNameStartOffset = startTagStructuredDocumentRegion.getStartOffset(startTagNameRegion);
            int startTagNameLength = startTagStructuredDocumentRegion.getTextEndOffset(startTagNameRegion) - startTagNameStartOffset;
            if (!newTagName.equals(oldTagName))
                replaceSource(structuredModel, structuredDocument, startTagNameStartOffset, startTagNameLength, newTagName);
            // save
            newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagNameStartOffset);
        // new
        // node
        }
    }
    IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
    if (endTagStructuredDocumentRegion != null) {
        ITextRegionList regions = endTagStructuredDocumentRegion.getRegions();
        if (regions != null && regions.size() > 0) {
            ITextRegion endTagNameRegion = regions.get(1);
            int endTagNameStartOffset = endTagStructuredDocumentRegion.getStartOffset(endTagNameRegion);
            int endTagNameLength = endTagStructuredDocumentRegion.getTextEndOffset(endTagNameRegion) - endTagNameStartOffset;
            if (startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && !newTagName.equals(oldTagName))
                replaceSource(structuredModel, structuredDocument, endTagNameStartOffset, endTagNameLength, newTagName);
        }
    }
    return newNode;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 50 with IDOMNode

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

the class CSSTextNodeCleanupHandler method cleanup.

public Node cleanup(Node node) {
    if (node == null)
        return node;
    IDOMModel model = ((IDOMNode) node).getModel();
    if (model == null)
        return node;
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null)
        return node;
    if (!getCleanupPreferences().getFormatSource())
        return node;
    String content = getCSSContent(node);
    if (content == null)
        return node;
    int offset = ((IDOMNode) node).getStartOffset();
    int length = ((IDOMNode) node).getEndOffset() - offset;
    replaceSource(model, this, offset, length, content);
    return (IDOMNode) model.getIndexedRegion(offset);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Aggregations

IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)250 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)91 Node (org.w3c.dom.Node)63 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)57 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)44 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)43 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)38 List (java.util.List)35 ArrayList (java.util.ArrayList)34 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)30 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)27 Element (org.w3c.dom.Element)27 NodeList (org.w3c.dom.NodeList)23 BadLocationException (org.eclipse.jface.text.BadLocationException)22 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)22 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)20 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)19 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)18 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)18