cssparser.cpp

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,746 行 · 第 1/5 页

CPP
1,746
字号
    }    counter->m_listStyle = CSS_VAL_DECIMAL - CSS_VAL_DISC;    i = args->next();    if (i) {        if (i->unit != Value::Operator || i->iValue != ',') goto invalid;        i = args->next();        if (i->unit != CSSPrimitiveValue::CSS_IDENT) goto invalid;        if (i->id < CSS_VAL_DISC || i->id > CSS_VAL__KHTML_CLOSE_QUOTE) goto invalid;        counter->m_listStyle = i->id - CSS_VAL_DISC;    }    return new CSSPrimitiveValueImpl(counter);invalid:    delete counter;    return 0;}CSSValueImpl* CSSParser::parseBackgroundColor(){    int id = valueList->current()->id;    if (id == CSS_VAL__KHTML_TEXT || id == CSS_VAL_TRANSPARENT ||        (id >= CSS_VAL_AQUA && id <= CSS_VAL_WINDOWTEXT) || id == CSS_VAL_MENU ||        (id >= CSS_VAL_GREY && id < CSS_VAL__KHTML_TEXT && !strict))       return new CSSPrimitiveValueImpl(id);    return parseColor();}CSSValueImpl* CSSParser::parseBackgroundImage(){    if (valueList->current()->id == CSS_VAL_NONE)        return new CSSImageValueImpl();    if (valueList->current()->unit == CSSPrimitiveValue::CSS_URI) {        DOMString uri = khtml::parseURL(domString(valueList->current()->string));        if (!uri.isEmpty())            return new CSSImageValueImpl(DOMString(KURL(styleElement->baseURL(), uri.string()).url()),                                         styleElement);    }    return 0;}CSSValueImpl* CSSParser::parseBackgroundPositionXY(bool& xFound, bool& yFound){    int id = valueList->current()->id;    if (id == CSS_VAL_LEFT || id == CSS_VAL_TOP || id == CSS_VAL_RIGHT || id == CSS_VAL_BOTTOM || id == CSS_VAL_CENTER) {        int percent = 0;        if (id == CSS_VAL_LEFT || id == CSS_VAL_RIGHT) {            if (xFound)                return 0;            xFound = true;            if (id == CSS_VAL_RIGHT)                percent = 100;        }        else if (id == CSS_VAL_TOP || id == CSS_VAL_BOTTOM) {            if (yFound)                return 0;            yFound = true;            if (id == CSS_VAL_BOTTOM)                percent = 100;        }        else if (id == CSS_VAL_CENTER)            // Center is ambiguous, so we're not sure which position we've found yet, an x or a y.            percent = 50;        return new CSSPrimitiveValueImpl(percent, CSSPrimitiveValue::CSS_PERCENTAGE);    }    if (validUnit(valueList->current(), FPercent|FLength, strict))        return new CSSPrimitiveValueImpl(valueList->current()->fValue,                                         (CSSPrimitiveValue::UnitTypes)valueList->current()->unit);    return 0;}void CSSParser::parseBackgroundPosition(CSSValueImpl*& value1, CSSValueImpl*& value2){    value1 = value2 = 0;    Value* value = valueList->current();    // Parse the first value.  We're just making sure that it is one of the valid keywords or a percentage/length.    bool value1IsX = false, value1IsY = false;    value1 = parseBackgroundPositionXY(value1IsX, value1IsY);    if (!value1)        return;    // It only takes one value for background-position to be correctly parsed if it was specified in a shorthand (since we    // can assume that any other values belong to the rest of the shorthand).  If we're not parsing a shorthand, though, the    // value was explicitly specified for our property.    value = valueList->next();    // First check for the comma.  If so, we are finished parsing this value or value pair.    if (value && value->unit == Value::Operator && value->iValue == ',')        value = 0;    bool value2IsX = false, value2IsY = false;    if (value) {        value2 = parseBackgroundPositionXY(value2IsX, value2IsY);        if (value2)            valueList->next();        else {            if (!inParseShortHand) {                delete value1;                value1 = 0;                return;            }        }    }    if (!value2)        // Only one value was specified.  If that value was not a keyword, then it sets the x position, and the y position        // is simply 50%.  This is our default.        // For keywords, the keyword was either an x-keyword (left/right), a y-keyword (top/bottom), or an ambiguous keyword (center).        // For left/right/center, the default of 50% in the y is still correct.        value2 = new CSSPrimitiveValueImpl(50, CSSPrimitiveValue::CSS_PERCENTAGE);    if (value1IsY || value2IsX) {        // Swap our two values.        CSSValueImpl* val = value2;        value2 = value1;        value1 = val;    }}bool CSSParser::parseBackgroundProperty(int propId, int& propId1, int& propId2,                                        CSSValueImpl*& retValue1, CSSValueImpl*& retValue2){    CSSValueListImpl *values = 0, *values2 = 0;    Value* val;    CSSValueImpl *value = 0, *value2 = 0;    bool allowComma = false;    retValue1 = retValue2 = 0;    propId1 = propId;    propId2 = propId;    if (propId == CSS_PROP_BACKGROUND_POSITION) {        propId1 = CSS_PROP_BACKGROUND_POSITION_X;        propId2 = CSS_PROP_BACKGROUND_POSITION_Y;    }    while ((val = valueList->current())) {        CSSValueImpl *currValue = 0, *currValue2 = 0;        if (allowComma) {            if (val->unit != Value::Operator || val->iValue != ',')                goto failed;            valueList->next();            allowComma = false;        }        else {            switch (propId) {                case CSS_PROP_BACKGROUND_ATTACHMENT:                    if (val->id == CSS_VAL_SCROLL || val->id == CSS_VAL_FIXED) {                        currValue = new CSSPrimitiveValueImpl(val->id);                        valueList->next();                    }                    break;                case CSS_PROP_BACKGROUND_COLOR:                    currValue = parseBackgroundColor();                    if (currValue)                        valueList->next();                    break;                case CSS_PROP_BACKGROUND_IMAGE:                    currValue = parseBackgroundImage();                    if (currValue)                        valueList->next();                    break;                case CSS_PROP_BACKGROUND_POSITION:                    parseBackgroundPosition(currValue, currValue2);                    // unlike the other functions, parseBackgroundPosition advances the valueList pointer                    break;                case CSS_PROP_BACKGROUND_POSITION_X: {                    bool xFound = false, yFound = true;                    currValue = parseBackgroundPositionXY(xFound, yFound);                    if (currValue)                        valueList->next();                    break;                }                case CSS_PROP_BACKGROUND_POSITION_Y: {                    bool xFound = true, yFound = false;                    currValue = parseBackgroundPositionXY(xFound, yFound);                    if (currValue)                        valueList->next();                    break;                }                case CSS_PROP_BACKGROUND_REPEAT:                    if (val->id >= CSS_VAL_REPEAT && val->id <= CSS_VAL_NO_REPEAT) {                        currValue = new CSSPrimitiveValueImpl(val->id);                        valueList->next();                    }                    break;            }            if (!currValue)                goto failed;            if (value && !values) {                values = new CSSValueListImpl();                values->append(value);                value = 0;            }            if (value2 && !values2) {                values2 = new CSSValueListImpl();                values2->append(value2);                value2 = 0;            }            if (values)                values->append(currValue);            else                value = currValue;            if (currValue2) {                if (values2)                    values2->append(currValue2);                else                    value2 = currValue2;            }            allowComma = true;        }        // When parsing the 'background' shorthand property, we let it handle building up the lists for all        // properties.        if (inParseShortHand)            break;    }    if (values && values->length()) {        retValue1 = values;        if (values2 && values2->length())            retValue2 = values2;        return true;    }    if (value) {        retValue1 = value;        retValue2 = value2;        return true;    }failed:    delete values; delete values2;    delete value; delete value2;    return false;}bool CSSParser::parseShape( int propId, bool important ){    Value *value = valueList->current();    ValueList *args = value->function->args;    QString fname = qString( value->function->name ).lower();    //qDebug( "parseShape: fname: %d", fname.latin1() );    if ( fname != "rect(" || !args )        return false;    // rect( t, r, b, l ) || rect( t r b l )    if ( args->numValues != 4 && args->numValues != 7 )        return false;    RectImpl *rect = new RectImpl();    bool valid = true;    int i = 0;    Value *a = args->current();    while ( a ) {        valid = validUnit( a, FLength, strict );        if ( !valid )            break;        CSSPrimitiveValueImpl *length =            new CSSPrimitiveValueImpl( a->fValue, (CSSPrimitiveValue::UnitTypes) a->unit );        if ( i == 0 )            rect->setTop( length );        else if ( i == 1 )            rect->setRight( length );        else if ( i == 2 )            rect->setBottom( length );        else            rect->setLeft( length );        a = args->next();        if ( a && args->numValues == 7 ) {            if ( a->unit == Value::Operator && a->iValue == ',' ) {                a = args->next();            } else {                valid = false;                break;            }        }        i++;    }    if ( valid ) {        addProperty( propId, new CSSPrimitiveValueImpl( rect ), important );        valueList->next();        return true;    }    delete rect;    return false;}// [ 'font-style' || 'font-variant' || 'font-weight' ]? 'font-size' [ / 'line-height' ]? 'font-family'bool CSSParser::parseFont( bool important ){//     kdDebug(6080) << "parsing font property current=" << valueList->currentValue << endl;    bool valid = true;    Value *value = valueList->current();    FontValueImpl *font = new FontValueImpl;    // optional font-style, font-variant and font-weight    while ( value ) {//         kdDebug( 6080 ) << "got value " << value->id << " / " << (value->unit == CSSPrimitiveValue::CSS_STRING ||        //                                    value->unit == CSSPrimitiveValue::CSS_IDENT ? qString( value->string ) : QString::null )//                         << endl;        int id = value->id;        if ( id ) {            if ( id == CSS_VAL_NORMAL ) {                // do nothing, it's the initial value for all three            }            /*              else if ( id == CSS_VAL_INHERIT ) {              // set all non set ones to inherit              // This is not that simple as the inherit could also apply to the following font-size.              // very ahrd to tell without looking ahead.              inherit = true;                } */            else if ( id == CSS_VAL_ITALIC || id == CSS_VAL_OBLIQUE ) {                if ( font->style )                    goto invalid;                font->style = new CSSPrimitiveValueImpl( id );            } else if ( id == CSS_VAL_SMALL_CAPS ) {                if ( font->variant )                    goto invalid;                font->variant = new CSSPrimitiveValueImpl( id );            } else if ( id >= CSS_VAL_BOLD && id <= CSS_VAL_LIGHTER ) {                if ( font->weight )                    goto invalid;                font->weight = new CSSPrimitiveValueImpl( id );            } else {                valid = false;            }        } else if ( !font->weight && validUnit( value, FInteger|FNonNeg, true ) ) {            int weight = (int)value->fValue;            int val = 0;            if ( weight == 100 )                val = CSS_VAL_100;            else if ( weight == 200 )                val = CSS_VAL_200;            else if ( weight == 300 )                val = CSS_VAL_300;            else if ( weight == 400 )                val = CSS_VAL_400;            else if ( weight == 500 )                val = CSS_VAL_500;            else if ( weight == 600 )                val = CSS_VAL_600;            else if ( weight == 700 )                val = CSS_VAL_700;            else if ( weight

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?