Search in sources :

Example 11 with CSSTextParser

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

the class BorderTopShorthandAdapter 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 color = null, style = null, width = null;
    PropCMProperty propColor = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_TOP_COLOR);
    PropCMProperty propStyle = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_TOP_STYLE);
    PropCMProperty propWidth = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_TOP_WIDTH);
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) {
            if (propStyle.canHave(tokens[i].image))
                style = tokens[i].image;
            else if (propWidth.canHave(tokens[i].image))
                width = tokens[i].image;
            else if (propColor.canHave(tokens[i].image))
                color = tokens[i].image;
        } else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER) {
            width = tokens[i].image;
        } else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_HASH) {
            color = tokens[i].image;
        } else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION) {
            StringBuffer buf = new StringBuffer();
            while (i < tokens.length) {
                if (tokens[i].kind == CSSRegionContexts.CSS_COMMENT) {
                    i++;
                    continue;
                }
                buf.append(tokens[i].image);
                if (tokens[i++].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE)
                    break;
            }
            i--;
            color = buf.toString();
        }
    }
    if (propColor == propDest)
        return color;
    else if (propStyle == propDest)
        return style;
    else if (propWidth == propDest)
        return width;
    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 12 with CSSTextParser

use of org.eclipse.wst.css.core.internal.parserz.CSSTextParser 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 13 with CSSTextParser

use of org.eclipse.wst.css.core.internal.parserz.CSSTextParser 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 14 with CSSTextParser

use of org.eclipse.wst.css.core.internal.parserz.CSSTextParser 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 15 with CSSTextParser

use of org.eclipse.wst.css.core.internal.parserz.CSSTextParser 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)

Aggregations

CSSTextParser (org.eclipse.wst.css.core.internal.parserz.CSSTextParser)33 CSSTextToken (org.eclipse.wst.css.core.internal.parserz.CSSTextToken)28 PropCMProperty (org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty)18 Iterator (java.util.Iterator)3 Collection (java.util.Collection)2 List (java.util.List)2 ICSSPrimitiveValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSPrimitiveValue)2 ICSSValue (org.eclipse.wst.css.core.internal.provisional.document.ICSSValue)2 ICSSValueList (org.eclipse.wst.css.core.internal.provisional.document.ICSSValueList)2 ArrayList (java.util.ArrayList)1 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