cssparser.cpp
来自「将konqueror浏览器移植到ARM9 2410中」· C++ 代码 · 共 2,040 行 · 第 1/5 页
CPP
2,040 行
strictParsing = _strictParsing; m_yyTok = TOK_NONE; } int getToken() { m_yyStr = QString::null; if ( m_yyChar == '\0' ) return TOK_EOI; if ( m_yyChar == QChar(' ') ) m_yyChar = getChar(); if ( m_yyChar == QChar('/') ) { m_yyChar = getChar(); return TOK_SLASH; } else if ( m_yyChar == QChar(',') ) { m_yyChar = getChar(); return TOK_COMMA; } else if ( m_yyChar == QChar('"') ) { m_yyChar = getChar(); while ( m_yyChar != QChar('"') && m_yyChar != '\0' ) { m_yyStr += m_yyChar; m_yyChar = getChar(); } m_yyChar = getChar(); return TOK_STRING; } else if ( m_yyChar == QChar('\'') ) { m_yyChar = getChar(); while ( m_yyChar != QChar('\'') && m_yyChar != '\0' ) { m_yyStr += m_yyChar; m_yyChar = getChar(); } m_yyChar = getChar(); return TOK_STRING; } else { while ( m_yyChar != '/' && m_yyChar != ',' && m_yyChar != '\0' && m_yyChar != ' ') { m_yyStr += m_yyChar; m_yyChar = getChar(); } return TOK_SYMBOL; } } bool match( int tok ) { if ( m_yyTok == tok ) { m_yyTok = getToken(); return true; } return false; } bool matchFontStyle( QString *fstyle ) { if ( m_yyTok == TOK_SYMBOL ) { const struct css_value *cssval = findValue(m_yyStr.latin1(), m_yyStr.length()); if (cssval) { int id = cssval->id; if ( id == CSS_VAL_NORMAL || id == CSS_VAL_ITALIC || id == CSS_VAL_OBLIQUE || id == CSS_VAL_INHERIT ) { *fstyle = m_yyStr; m_yyTok = getToken(); return true; } } } return false; } bool matchFontVariant( QString *fvariant ) { if ( m_yyTok == TOK_SYMBOL ) { const struct css_value *cssval = findValue(m_yyStr.latin1(), m_yyStr.length()); if (cssval) { int id = cssval->id; if (id == CSS_VAL_NORMAL || id == CSS_VAL_SMALL_CAPS || id == CSS_VAL_INHERIT) { *fvariant = m_yyStr; m_yyTok = getToken(); return true; } } } return false; } bool matchFontWeight( QString *fweight ) { if ( m_yyTok == TOK_SYMBOL ) { const struct css_value *cssval = findValue(m_yyStr.latin1(), m_yyStr.length()); if (cssval) { int id = cssval->id; if ((id >= CSS_VAL_NORMAL && id <= CSS_VAL_900) || id == CSS_VAL_INHERIT ) { *fweight = m_yyStr; m_yyTok = getToken(); return true; } } } return false ; } bool matchFontSize( QString *fsize ) { if ( m_yyTok == TOK_SYMBOL ) { *fsize = m_yyStr; m_yyTok = getToken(); return true; } return false; } bool matchLineHeight( QString *lheight ) { if ( m_yyTok == TOK_SYMBOL ) { *lheight = m_yyStr; m_yyTok = getToken(); return true; } return false; } bool matchNameFamily( QString *ffamily ) { //kdDebug( 6080 ) << "matchNameFamily: [" << *ffamily << "]" << endl; bool matched = false; if ( m_yyTok == TOK_SYMBOL || ( m_yyTok == TOK_STRING && !strictParsing ) ) { // accept quoted "serif" only in non strict mode. *ffamily = m_yyStr; // unquoted courier new should return courier new while( (m_yyTok = getToken()) == TOK_SYMBOL ) { *ffamily += " " + m_yyStr; } matched = true; } else if ( m_yyTok == TOK_STRING ) { //kdDebug( 6080 ) << "[" << m_yyStr << "]" << endl; const struct css_value *cssval = findValue(m_yyStr.latin1(), m_yyStr.length()); if (!cssval || !(cssval->id >= CSS_VAL_SERIF && cssval->id <= CSS_VAL_MONOSPACE)) { *ffamily = m_yyStr; m_yyTok = getToken(); matched = true; } } return matched; } bool matchFontFamily( QString *ffamily ) { //kdDebug( 6080 ) << "matchFontFamily: [" << *ffamily << "]" << endl; QStringList t; if ( !matchFontFamily( &t ) ) return false; *ffamily = t.join(", "); return TRUE; } bool matchFontFamily ( QStringList *ffamily ) { if ( m_yyTok == TOK_NONE ) m_yyTok = getToken();#if 0 // ### if ( m_yyTok == TOK_STRING && m_yyStr == "inherit" ) { ffamily->clear(); m_yyTok = getToken(); return TRUE; }#endif QString name; do { if ( !matchNameFamily(&name) ) return FALSE; ffamily->append( name ); } while ( match(TOK_COMMA) ); return true; } bool matchRealFont( QString *fstyle, QString *fvariant, QString *fweight, QString *fsize, QString *lheight, QString *ffamily ) { //kdDebug( 6080 ) << "matchRealFont(..)" << endl; bool metFstyle = matchFontStyle( fstyle ); bool metFvariant = matchFontVariant( fvariant ); matchFontWeight( fweight ); if ( !metFstyle ) metFstyle = matchFontStyle( fstyle ); if ( !metFvariant ) matchFontVariant( fvariant ); if ( !metFstyle ) matchFontStyle( fstyle ); if ( !matchFontSize(fsize) ) return FALSE; if ( match(TOK_SLASH) ) { if ( !matchLineHeight(lheight) ) return FALSE; } if ( !matchFontFamily(ffamily) ) return FALSE; return true; }};bool StyleBaseImpl::parseFont(const QChar *curP, const QChar *endP){ QString str( curP, endP - curP ); QString fstyle, fvariant, fweight, fsize, lheight, ffamily; FontParser fontParser; fontParser.startTokenizer( str, strictParsing ); //kdDebug( 6080 ) << str << endl; const struct css_value *cssval = findValue(fontParser.m_yyIn.latin1(), fontParser.m_yyIn.length()); if (cssval) { //kdDebug( 6080 ) << "System fonts requested: [" << str << "]" << endl; QFont sysFont; switch (cssval->id) { case CSS_VAL_MENU: sysFont = KGlobalSettings::menuFont(); break; case CSS_VAL_CAPTION: sysFont = KGlobalSettings::windowTitleFont(); break; case CSS_VAL_STATUS_BAR: case CSS_VAL_ICON: case CSS_VAL_MESSAGE_BOX: case CSS_VAL_SMALL_CAPTION: default: sysFont = KGlobalSettings::generalFont(); break; } if (sysFont.italic()) { fstyle = "italic"; } else { fstyle = "normal"; } if (sysFont.bold()) { fweight = "bold"; } else { fweight = "normal"; } fsize.sprintf("%dpx", sysFont.pixelSize()); ffamily = sysFont.family(); } else { fontParser.m_yyTok = fontParser.getToken(); if (!(fontParser.matchRealFont(&fstyle, &fvariant, &fweight, &fsize, &lheight, &ffamily))) { return false; } } //kdDebug(6080) << "[" << fstyle << "] [" << fvariant << "] [" << fweight << "] [" // << fsize << "] / [" << lheight << "] [" << ffamily << "]" << endl; if(!fstyle.isNull()) parseValue(fstyle.unicode(), fstyle.unicode()+fstyle.length(), CSS_PROP_FONT_STYLE); if(!fvariant.isNull()) parseValue(fvariant.unicode(), fvariant.unicode()+fvariant.length(), CSS_PROP_FONT_VARIANT); if(!fweight.isNull()) parseValue(fweight.unicode(), fweight.unicode()+fweight.length(), CSS_PROP_FONT_WEIGHT); if(!fsize.isNull()) parseValue(fsize.unicode(), fsize.unicode()+fsize.length(), CSS_PROP_FONT_SIZE); if(!lheight.isNull()) parseValue(lheight.unicode(), lheight.unicode()+lheight.length(), CSS_PROP_LINE_HEIGHT); if(!ffamily.isNull()) parseValue(ffamily.unicode(), ffamily.unicode()+ffamily.length(), CSS_PROP_FONT_FAMILY); return true;}// ---------------- end font property --------------------------bool StyleBaseImpl::parseValue( const QChar *curP, const QChar *endP, int propId, bool important, QList<CSSProperty> *propList){ m_bImportant = important; m_propList = propList; return parseValue(curP, endP, propId);}bool StyleBaseImpl::parseValue( const QChar *curP, const QChar *endP, int propId){ if (curP==endP) {return 0; /* e.g.: width="" */} QString value(curP, endP - curP); value = value.lower().stripWhiteSpace();#ifdef CSS_DEBUG kdDebug( 6080 ) << "id [" << getPropertyName(propId).string() << "] parseValue [" << value << "]" << endl;#endif int len = value.length(); const char *val = value.latin1(); CSSValueImpl *parsedValue = 0; // We are using this so often const struct css_value *cssval = findValue(val, len); if (cssval && cssval->id == CSS_VAL_INHERIT) { parsedValue = new CSSInheritedValueImpl(); // inherited value } else { switch(propId) { /* The comment to the left defines all valid value of this properties as defined * in CSS 2, Appendix F. Property index */ /* All the CSS properties are not supported by the renderer at the moment. * Note that all the CSS2 Aural properties are only checked, if CSS_AURAL is defined * (see parseAuralValues). As we don't support them at all this seems reasonable. */ case CSS_PROP_SIZE: // <length>{1,2} | auto | portrait | landscape | inherit case CSS_PROP_QUOTES: // [<string> <string>]+ | none | inherit case CSS_PROP_TEXT_SHADOW: // none | [<color> || <length> <length> <length>? ,]* // [<color> || <length> <length> <length>?] | inherit case CSS_PROP_CONTENT: // [ <string> | <uri> | <counter> | attr(X) | open-quote | // close-quote | no-open-quote | no-close-quote ]+ | inherit case CSS_PROP_UNICODE_BIDI: // normal | embed | bidi-override | inherit case CSS_PROP_WHITE_SPACE: // normal | pre | nowrap | inherit case CSS_PROP_FONT_STRETCH: // normal | wider | narrower | ultra-condensed | extra-condensed | condensed | // semi-condensed | semi-expanded | expanded | extra-expanded | ultra-expanded | // inherit case CSS_PROP_PAGE: // <identifier> | auto // ### CHECK case CSS_PROP_PAGE_BREAK_AFTER: // auto | always | avoid | left | right | inherit case CSS_PROP_PAGE_BREAK_BEFORE: // auto | always | avoid | left | right | inherit case CSS_PROP_PAGE_BREAK_INSIDE: // avoid | auto | inherit case CSS_PROP_POSITION: // static | relative | absolute | fixed | inherit case CSS_PROP_EMPTY_CELLS: // show | hide | inherit case CSS_PROP_TABLE_LAYOUT: // auto | fixed | inherit { const struct css_value *cssval = findValue(val, len); if (cssval) { parsedValue = new CSSPrimitiveValueImpl(cssval->id); } // ### To be done break; } case CSS_PROP_CLIP: // <shape> | auto | inherit { int i; if ( cssval && cssval->id == CSS_VAL_AUTO ) parsedValue = new CSSPrimitiveValueImpl( cssval->id ); else { // only shape in CSS2 is rect( top right bottom left ) QString str = QConstString( const_cast<QChar*>( curP ), endP - curP ).string(); // the CSS specs are not really clear if there should be commas in here or not. We accept both spaces and commas. str.replace( QRegExp( "," ), " " ); str = str.simplifyWhiteSpace(); if ( str.find( "rect", 0, false ) != 0 ) break; int pos = str.find( '(', 4 ); int end = str.findRev( ')' ); if ( end <= pos ) break; str = str.mid( pos + 1, end - pos - 1 ); str = str.simplifyWhiteSpace(); str += " "; //qDebug("rect = '%s'", str.latin1() ); pos = 0; RectImpl *rect = new RectImpl(); for ( i = 0; i < 4; i++ ) { int space; space = str.find( ' ', pos ); const QChar *start = str.unicode() + pos; const QChar *end = str.unicode() + space; //qDebug("part: from=%d, to=%d", pos, space ); if ( start >= end ) goto cleanup; CSSPrimitiveValueImpl *length = 0; if ( str.find( "auto", pos, FALSE ) == pos ) length = new CSSPrimitiveValueImpl(value, CSSPrimitiveValue::CSS_PX); else length = parseUnit( start, end, LENGTH ); if ( !length ) goto cleanup; switch ( i ) { case 0: rect->setTop( length ); break; case 1: rect->setRight( length ); break; case 2: rect->setBottom( length ); break; case 3: rect->setLeft( length ); break; } pos = space + 1; } parsedValue = new CSSPrimitiveValueImpl( rect ); //qDebug(" passed rectangle parsing"); break; cleanup: qDebug(" rectangle parsing failed, i=%d", i); delete rect; } break; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?