Search in sources :

Example 11 with CSSTextToken

use of org.eclipse.wst.css.core.internal.parserz.CSSTextToken in project webtools.sourceediting by eclipse.

the class LineStyleProviderForEmbeddedCSS method prepareRegions.

public boolean prepareRegions(ITypedRegion typedRegion, int lineRequestStart, int lineRequestLength, Collection holdResults) {
    int regionStart = typedRegion.getOffset();
    int regionEnd = regionStart + typedRegion.getLength();
    IStructuredDocumentRegion wholeRegion = getDocument().getRegionAtCharacterOffset(regionStart);
    if (wholeRegion == null)
        return false;
    List tokens = null;
    int offset = typedRegion.getOffset();
    List cache = getCachedParsingResult(wholeRegion);
    if (cache == null) {
        try {
            String content = getDocument().get(typedRegion.getOffset(), typedRegion.getLength());
            // if region is an XML tag then in CSS style attribute, else in style tag
            int mode;
            if (wholeRegion.getType() == DOMRegionContext.XML_TAG_NAME) {
                mode = CSSTextParser.MODE_DECLARATION;
            } else {
                mode = CSSTextParser.MODE_STYLESHEET;
            }
            CSSTextParser parser = new CSSTextParser(mode, content);
            tokens = parser.getTokenList();
            cacheParsingResult(wholeRegion, tokens);
        } catch (BadLocationException e) {
            Logger.logException("Given a bad ITypedRegion: " + typedRegion, e);
        }
    } else {
        tokens = cache;
    }
    boolean result = false;
    if (tokens != null && 0 < tokens.size()) {
        int start = offset;
        int end = start;
        Iterator i = tokens.iterator();
        while (i.hasNext()) {
            CSSTextToken token = (CSSTextToken) i.next();
            end = start + token.length;
            int styleLength = token.length;
            /* The token starts in the region */
            if (regionStart <= start && start < regionEnd) {
                /* [239415] The region may not span the total length of the token -
					 * Adjust the length so that it doesn't overlap with other style ranges */
                if (regionEnd < end)
                    styleLength = regionEnd - start;
                addStyleRange(holdResults, getAttributeFor(token.kind), start, styleLength);
            } else /* The region starts in the token */
            if (start <= regionStart && regionStart < end) {
                /* The token may not span the total length of the region */
                if (end < regionEnd)
                    styleLength = end - regionStart;
                else
                    /* Bugzilla 282218 */
                    styleLength = regionEnd - regionStart;
                addStyleRange(holdResults, getAttributeFor(token.kind), regionStart, styleLength);
            }
            start += token.length;
        }
        result = true;
    }
    return result;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Iterator(java.util.Iterator) CSSTextToken(org.eclipse.wst.css.core.internal.parserz.CSSTextToken) List(java.util.List) CSSTextParser(org.eclipse.wst.css.core.internal.parserz.CSSTextParser) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 12 with CSSTextToken

use of org.eclipse.wst.css.core.internal.parserz.CSSTextToken in project webtools.sourceediting by eclipse.

the class FontShorthandAdapter method extract.

/**
 */
public String extract(String source, PropCMProperty propDest) {
    CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, source);
    CSSTextToken[] tokens = parser.getTokens();
    if (tokens.length <= 0) {
        return null;
    }
    String style = null, variant = null, weight = null, size = null, height = null, family = null;
    PropCMProperty propFont = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT);
    PropCMProperty propStyle = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_STYLE);
    PropCMProperty propVariant = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_VARIANT);
    PropCMProperty propWeight = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_WEIGHT);
    PropCMProperty propSize = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_SIZE);
    PropCMProperty propHeight = PropCMProperty.getInstanceOf(PropCMProperty.P_LINE_HEIGHT);
    PropCMProperty propFamily = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_FAMILY);
    boolean bNormalSpecified = false;
    int i = 0;
    for (; i < tokens.length; i++) {
        if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
            // first of all, check font idents
            if (i == 0) {
                for (int j = 0; j < propFont.getNumChild(); j++) {
                    Object obj = propFont.getChildAt(i);
                    if (obj instanceof String && tokens[i].image.compareToIgnoreCase(obj.toString()) == 0)
                        return null;
                }
            }
            // value "normal" is shared !!
            if (tokens[i].image.equalsIgnoreCase(IValID.V_NORMAL)) {
                bNormalSpecified = true;
            } else {
                if (propStyle.canHave(tokens[i].image))
                    style = tokens[i].image;
                else if (propVariant.canHave(tokens[i].image))
                    variant = tokens[i].image;
                else if (propWeight.canHave(tokens[i].image))
                    weight = tokens[i].image;
                else if (propSize.canHave(tokens[i].image)) {
                    size = tokens[i].image;
                    // if size found, break loop
                    break;
                }
            }
        } else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER && weight == null && propWeight.canHave(tokens[i].image)) {
            weight = tokens[i].image;
        } else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PERCENTAGE) {
            size = tokens[i].image;
            // if size found, break loop
            break;
        }
    }
    if (bNormalSpecified) {
        if (style == null)
            style = IValID.V_NORMAL;
        if (variant == null)
            variant = IValID.V_NORMAL;
        if (weight == null)
            weight = IValID.V_NORMAL;
    }
    // skip whitespace
    for (i++; i < tokens.length; i++) {
        if (tokens[i].kind != CSSRegionContexts.CSS_S || tokens[i].kind != CSSRegionContexts.CSS_DECLARATION_VALUE_S)
            break;
    }
    // line-height ?
    if (i < tokens.length && tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR && tokens[i].image.equals("/")) {
        // $NON-NLS-1$
        for (i++; i < tokens.length; i++) {
            if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PERCENTAGE || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
                height = tokens[i++].image;
                break;
            }
        }
    }
    // font-family
    StringBuffer buf = new StringBuffer();
    for (; i < tokens.length; i++) {
        if (tokens[i].kind == CSSRegionContexts.CSS_COMMENT)
            // $NON-NLS-1$
            buf.append(" ");
        else
            buf.append(tokens[i].image);
    }
    family = buf.toString().trim();
    if (propStyle == propDest)
        return style;
    else if (propVariant == propDest)
        return variant;
    else if (propWeight == propDest)
        return weight;
    else if (propSize == propDest)
        return size;
    else if (propHeight == propDest)
        return height;
    else if (propFamily == propDest)
        return family;
    else
        return null;
}
Also used : PropCMProperty(org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty) CSSTextToken(org.eclipse.wst.css.core.internal.parserz.CSSTextToken) CSSTextParser(org.eclipse.wst.css.core.internal.parserz.CSSTextParser)

Example 13 with CSSTextToken

use of org.eclipse.wst.css.core.internal.parserz.CSSTextToken in project webtools.sourceediting by eclipse.

the class FontShorthandAdapter method expand.

/**
 */
public boolean expand(String source, CSSPropertyContext dest) {
    CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, source);
    CSSTextToken[] tokens = parser.getTokens();
    if (tokens.length <= 0) {
        return false;
    }
    String style = null, variant = null, weight = null, size = null, height = null, family = null;
    PropCMProperty propFont = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT);
    PropCMProperty propStyle = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_STYLE);
    PropCMProperty propVariant = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_VARIANT);
    PropCMProperty propWeight = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_WEIGHT);
    PropCMProperty propSize = PropCMProperty.getInstanceOf(PropCMProperty.P_FONT_SIZE);
    boolean bNormalSpecified = false;
    int i = 0;
    for (; i < tokens.length; i++) {
        if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
            // first of all, check font idents
            if (i == 0) {
                for (int j = 0; j < propFont.getNumChild(); j++) {
                    Object obj = propFont.getChildAt(i);
                    if (obj instanceof String && tokens[i].image.compareToIgnoreCase(obj.toString()) == 0)
                        return false;
                }
            }
            // value "normal" is shared !!
            if (tokens[i].image.equalsIgnoreCase(IValID.V_NORMAL)) {
                bNormalSpecified = true;
            } else {
                if (propStyle.canHave(tokens[i].image))
                    style = tokens[i].image;
                else if (propVariant.canHave(tokens[i].image))
                    variant = tokens[i].image;
                else if (propWeight.canHave(tokens[i].image))
                    weight = tokens[i].image;
                else if (propSize.canHave(tokens[i].image)) {
                    size = tokens[i].image;
                    // if size found, break loop
                    break;
                }
            }
        } else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER && weight == null && propWeight.canHave(tokens[i].image)) {
            weight = tokens[i].image;
        } else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PERCENTAGE) {
            size = tokens[i].image;
            // if size found, break loop
            break;
        }
    }
    if (bNormalSpecified) {
        if (style == null)
            style = IValID.V_NORMAL;
        if (variant == null)
            variant = IValID.V_NORMAL;
        if (weight == null)
            weight = IValID.V_NORMAL;
    }
    // skip whitespace
    for (i++; i < tokens.length; i++) {
        if (tokens[i].kind != CSSRegionContexts.CSS_S || tokens[i].kind != CSSRegionContexts.CSS_DECLARATION_VALUE_S)
            break;
    }
    // line-height ?
    if (i < tokens.length && tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR && tokens[i].image.equals("/")) {
        // $NON-NLS-1$
        for (i++; i < tokens.length; i++) {
            if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PERCENTAGE || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
                height = tokens[i++].image;
                break;
            }
        }
    }
    // font-family
    StringBuffer buf = new StringBuffer();
    for (; i < tokens.length; i++) {
        if (tokens[i].kind == CSSRegionContexts.CSS_COMMENT)
            // $NON-NLS-1$
            buf.append(" ");
        else
            buf.append(tokens[i].image);
    }
    family = buf.toString().trim();
    dest.set(PropCMProperty.P_FONT_STYLE, style);
    dest.set(PropCMProperty.P_FONT_VARIANT, variant);
    dest.set(PropCMProperty.P_FONT_WEIGHT, weight);
    dest.set(PropCMProperty.P_FONT_SIZE, size);
    dest.set(PropCMProperty.P_LINE_HEIGHT, height);
    dest.set(PropCMProperty.P_FONT_FAMILY, family);
    return true;
}
Also used : PropCMProperty(org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty) CSSTextToken(org.eclipse.wst.css.core.internal.parserz.CSSTextToken) CSSTextParser(org.eclipse.wst.css.core.internal.parserz.CSSTextParser)

Example 14 with CSSTextToken

use of org.eclipse.wst.css.core.internal.parserz.CSSTextToken in project webtools.sourceediting by eclipse.

the class ListStyleShorthandAdapter method expand.

/**
 */
public boolean expand(String source, CSSPropertyContext dest) {
    CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, source);
    CSSTextToken[] tokens = parser.getTokens();
    if (tokens.length <= 0) {
        return false;
    }
    // $NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
    String image = "", pos = "", type = "";
    PropCMProperty propPos = PropCMProperty.getInstanceOf(PropCMProperty.P_LIST_STYLE_POSITION);
    PropCMProperty propType = PropCMProperty.getInstanceOf(PropCMProperty.P_LIST_STYLE_TYPE);
    PropCMProperty propImage = PropCMProperty.getInstanceOf(PropCMProperty.P_LIST_STYLE_IMAGE);
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
            if (propPos.canHave(tokens[i].image))
                pos = tokens[i].image;
            else {
                // value="none" is shared !!
                if (propType.canHave(tokens[i].image))
                    type = tokens[i].image;
                if (propImage.canHave(tokens[i].image))
                    image = tokens[i].image;
            }
        } else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_URI) {
            image = tokens[i].image;
        }
    }
    dest.set(propPos.getName(), pos);
    dest.set(propType.getName(), type);
    dest.set(propImage.getName(), image);
    return true;
}
Also used : PropCMProperty(org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty) CSSTextToken(org.eclipse.wst.css.core.internal.parserz.CSSTextToken) CSSTextParser(org.eclipse.wst.css.core.internal.parserz.CSSTextParser)

Example 15 with CSSTextToken

use of org.eclipse.wst.css.core.internal.parserz.CSSTextToken in project webtools.sourceediting by eclipse.

the class PaddingShorthandAdapter method extract.

/**
 */
public String extract(String source, PropCMProperty propDest) {
    CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, source);
    CSSTextToken[] tokens = parser.getTokens();
    if (tokens.length <= 0) {
        return null;
    }
    String[] idents = new String[4];
    int j = 0;
    for (int i = 0; i < tokens.length; i++) {
        if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PERCENTAGE)
            idents[j++] = tokens[i].image;
        if (j == 4)
            break;
    }
    if (j == 0)
        return null;
    else if (j == 1)
        return idents[0];
    else if (j == 2) {
        if (propDest.getName() == PropCMProperty.P_PADDING_TOP || propDest.getName() == PropCMProperty.P_PADDING_BOTTOM)
            return idents[0];
        else
            return idents[1];
    } else if (j == 3) {
        if (propDest.getName() == PropCMProperty.P_PADDING_TOP)
            return idents[0];
        else if (propDest.getName() == PropCMProperty.P_PADDING_BOTTOM)
            return idents[2];
        else
            return idents[1];
    } else {
        if (propDest.getName() == PropCMProperty.P_PADDING_TOP)
            return idents[0];
        else if (propDest.getName() == PropCMProperty.P_PADDING_RIGHT)
            return idents[1];
        else if (propDest.getName() == PropCMProperty.P_PADDING_BOTTOM)
            return idents[2];
        else if (propDest.getName() == PropCMProperty.P_PADDING_LEFT)
            return idents[3];
        else
            return null;
    }
}
Also used : CSSTextToken(org.eclipse.wst.css.core.internal.parserz.CSSTextToken) CSSTextParser(org.eclipse.wst.css.core.internal.parserz.CSSTextParser)

Aggregations

CSSTextToken (org.eclipse.wst.css.core.internal.parserz.CSSTextToken)33 CSSTextParser (org.eclipse.wst.css.core.internal.parserz.CSSTextParser)28 PropCMProperty (org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty)16 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)1 ICSSSelectorList (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1