⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cssparser.cpp

📁 monqueror一个很具有参考价值的源玛
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        break;    case 'h':        type = CSSPrimitiveValue::CSS_HZ;        unit = FREQUENCY;        break;    case 'k':        type = CSSPrimitiveValue::CSS_KHZ;        unit = FREQUENCY;        break;    }    if(unit & allowedUnits)    {        //kdDebug( 6080 ) << "found allowed number " << value << ", unit " << type << endl;        return new CSSPrimitiveValueImpl(value, type);    }    return 0;}// ------------------- begin font property ---------------------/*  Parser for the font property of CSS.  See  http://www.w3.org/TR/REC-CSS2/fonts.html#propdef-font for details.  Written by Jasmin Blanchette (jasmin@trolltech.com) on 2000-08-16.*/#include <qstring.h>#include <qstringlist.h>enum { Tok_Eoi, Tok_Slash, Tok_Comma, Tok_String, Tok_Symbol };class FontParser {public:    QChar yyCh;    QString yyIn;    unsigned int yyPos;    QString yyStr;    int getChar() {	return ( yyPos == yyIn.length() ) ? QChar('\0') : QChar(yyIn[yyPos++]);    }    void startTokenizer( const QString& str ) {	yyIn = str.simplifyWhiteSpace();	yyPos = 0;	yyCh = getChar();    }    int getToken()    {	yyStr = QString::null;	if ( yyCh == '\0' )	    return Tok_Eoi;	if ( yyCh == QChar(' ') )	    yyCh = getChar();	if ( yyCh == QChar('/') ) {	    yyCh = getChar();	    return Tok_Slash;	} else if ( yyCh == QChar(',') ) {	    yyCh = getChar();	    return Tok_Comma;	} else if ( yyCh == QChar('"') ) {	    yyCh = getChar();	    while ( yyCh != QChar('"') && yyCh != '\0' ) {		yyStr += yyCh;		yyCh = getChar();	    }	    yyCh = getChar();	    return Tok_String;	} else {	    while ( yyCh != '/' && yyCh != ',' && yyCh != '\0' && yyCh != ' ') {		yyStr += yyCh;		yyCh = getChar();	    }	    return Tok_Symbol;	}    }    int yyTok;    bool match( int tok )    {	bool matched = ( yyTok == tok );	if ( matched )	    yyTok = getToken();	return matched;    }    bool matchFontStyle( QString *fstyle )    {	bool matched = ( yyTok == Tok_Symbol &&			 (yyStr == "normal" || yyStr == "italic" ||			  yyStr == "oblique" || yyStr == "inherit") );	if ( matched ) {	    *fstyle = yyStr;	    yyTok = getToken();	}	return matched;    }    bool matchFontVariant( QString *fvariant )    {	bool matched = ( yyTok == Tok_Symbol &&			 (yyStr == "normal" || yyStr == "small-caps"			  || yyStr == "inherit") );	if ( matched ) {	    *fvariant = yyStr;	    yyTok = getToken();	}	return matched;    }    bool matchFontWeight( QString *fweight )    {	bool matched = ( yyTok == Tok_Symbol );	if ( matched ) {	    if ( yyStr.length() == 3 ) {		matched = ( yyStr[0].unicode() >= '1' &&			    yyStr[0].unicode() <= '9' &&			    yyStr.right(2) == QString::fromLatin1("00") );	    } else {		matched = ( yyStr == "normal" || yyStr == "bold" ||			    yyStr == "bolder" || yyStr == "lighter" ||			    yyStr == "inherit" );	    }	}	if ( matched ) {	    *fweight = yyStr;	    yyTok = getToken();	}	return matched;    }    bool matchFontSize( QString *fsize )    {	bool matched = ( yyTok == Tok_Symbol );	if ( matched ) {	    *fsize = yyStr;	    yyTok = getToken();	}	return matched;    }    bool matchLineHeight( QString *lheight )    {	bool matched = ( yyTok == Tok_Symbol );	if ( matched ) {	    *lheight = yyStr;	    yyTok = getToken();	}	return matched;    }    bool matchNameFamily( QString *ffamily )    {	bool matched = ( yyTok == Tok_String || yyTok == Tok_Symbol );	if ( matched ) {	    *ffamily = yyStr;	    yyTok = getToken();	}	return matched;    }    bool matchFontFamily( QString *ffamily )    {	QStringList t;#if 0	// ###	if ( yyTok == Tok_String && yyStr == "inherit" ) {	    t.clear();	    yyTok = getToken();	    return TRUE;	}#endif	QString name;	do {	    if ( !matchNameFamily(&name) )		return FALSE;	    t.append( name );	} while ( match(Tok_Comma) );	*ffamily = t.join(", ");	return TRUE;    }    bool matchRealFont( QString *fstyle, QString *fvariant, QString *fweight,			       QString *fsize, QString *lheight, QString *ffamily )    {	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,                              bool important, QList<CSSProperty> *propList){    QString str( curP, endP - curP );    QString fstyle;    QString fvariant;    QString fweight;    QString fsize;    QString lheight;    QString ffamily;    FontParser f;    f.startTokenizer( str );    //qDebug( "%s", str.latin1() );    if ( f.yyIn == "caption" || f.yyIn == "icon" || f.yyIn == "menu" ||         f.yyIn == "message-box" || f.yyIn == "small-caption" ||         f.yyIn == "status-bar" || f.yyIn == "inherit" ) {//        kdDebug(0) << "system font requested..." << endl;    } else {        f.yyTok = f.getToken();        if ( f.matchRealFont(&fstyle, &fvariant, &fweight, &fsize, &lheight,                           &ffamily) ) {// 	    qDebug( "  %s %s %s %s / %s", fstyle.latin1(),//                     fvariant.latin1(), fweight.latin1(), fsize.latin1(),//                     lheight.latin1() );            if(!fstyle.isNull())                parseValue(fstyle.unicode(), fstyle.unicode()+fstyle.length(),                           CSS_PROP_FONT_STYLE,                           important, propList);            if(!fvariant.isNull())                parseValue(fvariant.unicode(), fvariant.unicode()+fvariant.length(),                           CSS_PROP_FONT_VARIANT,                           important, propList);            if(!fweight.isNull())                parseValue(fweight.unicode(), fweight.unicode()+fweight.length(),                           CSS_PROP_FONT_WEIGHT,                           important, propList);            if(!fsize.isNull())                parseValue(fsize.unicode(), fsize.unicode()+fsize.length(),                           CSS_PROP_FONT_SIZE,                           important, propList);            if(!lheight.isNull())                parseValue(lheight.unicode(), lheight.unicode()+lheight.length(),                           CSS_PROP_LINE_HEIGHT,                           important, propList);            if(!ffamily.isNull())                parseValue(ffamily.unicode(), ffamily.unicode()+ffamily.length(),                           CSS_PROP_FONT_FAMILY,                           important, propList);            return true;        }    }    return false;}// ---------------- end font property --------------------------CSSStyleRuleImpl *StyleBaseImpl::parseStyleRule(const QChar *&curP, const QChar *endP){    //kdDebug( 6080 ) << "style rule is \'" << QString(curP, endP-curP) << "\'" << endl;    const QChar *startP;    QList<CSSSelector> *slist;    QList<CSSProperty> *plist;    startP = curP;    curP = parseToChar(startP, endP, '{', false);    if (!curP)        return(0);#ifdef CSS_DEBUG//    kdDebug( 6080 ) << "selector is \'" << QString(startP, curP-startP) << "\'" << endl;#endif    slist = parseSelector(startP, curP );    curP++; // need to get past the '{' from above    startP = curP;    curP = parseToChar(startP, endP, '}', false);#ifdef CSS_DEBUG//    kdDebug( 6080 ) << "rules are \'" << QString(startP, curP-startP) << "\'" << endl;#endif    if (!curP)    {        delete slist;        return(0);    }    plist = parseProperties(startP, curP );    curP++; // need to get past the '}' from above    if (!plist || !slist)    {        // Useless rule        delete slist;        delete plist;#ifdef CSS_DEBUG//        kdDebug( 6080 ) << "bad style rule" << endl;#endif        return 0;    }    // return the newly created rule    CSSStyleRuleImpl *rule = new CSSStyleRuleImpl(this);    CSSStyleDeclarationImpl *decl = new CSSStyleDeclarationImpl(rule, plist);    rule->setSelector(slist);    rule->setDeclaration(decl);    // ### set selector and value    return rule;}CSSRuleImpl *StyleBaseImpl::parseRule(const QChar *&curP, const QChar *endP){    const char *comment = "<!--";    const QChar *startP;    int count = 0;       curP = parseSpace( curP, endP );    startP = curP;       // The code below ignores any occurances of    // the beginning and/or the end of a html    // comment tag    while (startP && (startP < endP))    {       if(*startP == comment[count])	 count++;       else	 break;       if(count == 4)       {	  curP = ++startP;	  break;       }       ++startP;    }       comment = "-->";    while (startP && (startP < endP))    {       if(*startP == comment[count])	 count++;       else	 break;       if(count == 3)       {	  curP = ++startP;	  break;       }       ++startP;    }       CSSRuleImpl *rule = 0;    if(!curP) return 0;#ifdef CSS_DEBUG//    kdDebug( 6080 ) << "parse rule: current = " << curP->latin1() << endl;#endif    if (*curP == '@' )    {        rule = parseAtRule(curP, endP);    }    else    {        rule = parseStyleRule(curP, endP);        if( rule )            hasInlinedDecl = true;  // set flag to true iff we have a valid inlined decl.    }    if(curP) curP++;    return rule;}// remove comments, replace character escapes and simplify spacingQString StyleBaseImpl::preprocess(const QString &str){    QString processed;    bool sq = false;    bool dq = false;    bool comment = false;    bool firstChar = false;    hasInlinedDecl = false; // reset the inilned decl. flag    const QChar *ch = str.unicode();    const QChar *last = str.unicode()+str.length();    while(ch < last) {        if ( !comment && !sq && *ch == '"' ) {            dq = !dq;            processed += *ch;        } else if ( !comment && !dq && *ch == '\'' ) {            dq = !dq;            processed += *ch;        } else if ( comment ) {            if ( firstChar && *ch == '/' ) {                comment = false;                firstChar = false;            } else if ( *ch == '*' )                firstChar = true;            else                firstChar = false;        } else if ( !sq && !dq ) {            // check for comment            if ( firstChar ) {                if ( *ch == '*' ) {                    comment = true;                } else {                    processed += '/';                    processed += *ch;                }                firstChar = false;            } else if ( *ch == '/' )                firstChar = true;            else if ( *ch == '}' ) {		processed += *ch;		processed += QChar(' ');	    } else                 processed += *ch;        }        else            processed += *ch;        ++ch;    }    return processed;}// ------------------------------------------------------------------------------StyleListImpl::~StyleListImpl(){    StyleBaseImpl *n;    if(!m_lstChildren) return;    for( n = m_lstChildren->first(); n != 0; n = m_lstChildren->next() )    {        n->setParent(0);        if(n->deleteMe()) delete n;    }    delete m_lstChildren;}// --------------------------------------------------------------------------------CSSSelector::CSSSelector(void): tag(0), tagHistory(0){    attr = 0;    match = None;    relation = Descendant;    nonCSSHint = false;}CSSSelector::~CSSSelector(void){    if (tagHistory)    {        delete tagHistory;    }}void CSSSelector::print(void){//    kdDebug( 6080 ) << "[Selector: tag = " <<       tag << ", attr = \"" << attr << "\", value = \"" << value.string().latin1() << "\" relation = " << (int)relation << endl;}int CSSSelector::specificity(){    if ( nonCSSHint )        return 0;    int s = 0;    if(tag != -1) s = 1;    switch(match)    {    case Exact:        if(attr == ATTR_ID)        {            s += 100;            break;        }    case Set:    case List:    case Hyphen:    case Pseudo:        s += 10;    case None:        break;    }    if(tagHistory)        s += tagHistory->specificity();    return s;}// ----------------------------------------------------------------------------CSSProperty::~CSSProperty(){    if(m_value) m_value->deref();}void CSSProperty::setValue(CSSValueImpl *val){    if(m_value) m_value->deref();    m_value = val;    if(m_value) m_value->ref();}CSSValueImpl *CSSProperty::value(){    return m_value;}

⌨️ 快捷键说明

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