LCOV - code coverage report
Current view: top level - templates/scriptabletags - scriptablefilter.cpp (source / functions) Hit Total Coverage
Test: test_all.info Lines: 37 48 77.1 %
Date: 2011-06-24 Functions: 5 5 100.0 %
Branches: 80 206 38.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :   This file is part of the Grantlee template system.
       3                 :            : 
       4                 :            :   Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
       5                 :            : 
       6                 :            :   This library is free software; you can redistribute it and/or
       7                 :            :   modify it under the terms of the GNU Lesser General Public
       8                 :            :   License as published by the Free Software Foundation; either version
       9                 :            :   2.1 of the Licence, or (at your option) any later version.
      10                 :            : 
      11                 :            :   This library is distributed in the hope that it will be useful,
      12                 :            :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :   Lesser General Public License for more details.
      15                 :            : 
      16                 :            :   You should have received a copy of the GNU Lesser General Public
      17                 :            :   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
      18                 :            : 
      19                 :            : */
      20                 :            : 
      21                 :            : #include "scriptablefilter.h"
      22                 :            : #include "scriptablesafestring.h"
      23                 :            : 
      24                 :            : #include "util.h"
      25                 :            : 
      26                 :            : #include <QtScript/QScriptEngine>
      27                 :            : 
      28                 :         33 : ScriptableFilter::ScriptableFilter( QScriptValue filterObject, QScriptEngine *engine )
      29         [ +  - ]:         33 :     : m_filterObject( filterObject ), m_scriptEngine( engine )
      30                 :            : {
      31                 :         33 : }
      32                 :            : 
      33         [ +  - ]:         33 : ScriptableFilter::~ScriptableFilter()
      34                 :            : {
      35                 :            : 
      36         [ -  + ]:         66 : }
      37                 :            : 
      38                 :          5 : bool ScriptableFilter::isSafe() const
      39                 :            : {
      40 [ +  - ][ +  - ]:         10 :   QScriptValue safety = m_filterObject.property( QLatin1String( "isSafe" ) );
                 [ +  - ]
      41 [ +  - ][ +  - ]:          5 :   if ( safety.isBool() ) {
      42         [ +  - ]:          5 :     return safety.toBool();
      43                 :            :   }
      44                 :          0 :   return false;
      45                 :            : }
      46                 :            : 
      47                 :          5 : QVariant ScriptableFilter::doFilter( const QVariant &input, const QVariant& argument, bool autoescape ) const
      48                 :            : {
      49                 :            :   Q_UNUSED( autoescape )
      50                 :         10 :   QScriptValueList args;
      51 [ +  - ][ +  + ]:          5 :   if ( input.type() == QVariant::List ) {
      52 [ +  - ][ +  - ]:          6 :     QVariantList inputList = input.toList();
      53 [ +  - ][ +  - ]:          6 :     QScriptValue array = m_scriptEngine->newArray( inputList.size() );
                 [ +  - ]
      54 [ +  - ][ +  + ]:         12 :     for ( int i = 0; i < inputList.size(); ++i ) {
      55 [ +  - ][ +  - ]:          9 :       if ( inputList.at( i ).canConvert<QObject*>() ) {
                 [ -  + ]
      56 [ #  # ][ #  # ]:          0 :         array.setProperty( i, m_scriptEngine->newQObject( inputList.at( i ).value<QObject*>() ) );
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      57                 :            :       } else {
      58 [ +  - ][ +  - ]:          9 :         array.setProperty( i, m_scriptEngine->newVariant( inputList.at( i ) ) );
         [ +  - ][ +  - ]
                 [ +  - ]
      59                 :            :       }
      60                 :            :     }
      61         [ +  - ]:          3 :     args << array;
      62                 :            :   } else {
      63 [ +  - ][ +  - ]:          2 :     if ( isSafeString( input ) ) {
      64 [ +  - ][ +  - ]:          2 :       ScriptableSafeString *ssObj = new ScriptableSafeString( m_scriptEngine );
      65 [ +  - ][ +  - ]:          2 :       ssObj->setContent( getSafeString( input ) );
                 [ +  - ]
      66 [ +  - ][ +  - ]:          2 :       args << m_scriptEngine->newQObject( ssObj );
         [ +  - ][ +  - ]
      67 [ #  # ][ #  # ]:          0 :     } else if ( input.canConvert<QObject*>() ) {
      68 [ #  # ][ #  # ]:          0 :       args << m_scriptEngine->newQObject( input.value<QObject*>() );
         [ #  # ][ #  # ]
                 [ #  # ]
      69                 :            :     } else {
      70 [ #  # ][ #  # ]:          0 :       args << m_scriptEngine->newVariant( input );
                 [ #  # ]
      71                 :            :     }
      72                 :            :   }
      73                 :            : 
      74 [ +  - ][ +  - ]:          5 :   if ( argument.userType() == qMetaTypeId<SafeString>() ) {
                 [ +  + ]
      75 [ +  - ][ +  - ]:          3 :     ScriptableSafeString *ssObj = new ScriptableSafeString( m_scriptEngine );
      76 [ +  - ][ +  - ]:          3 :     ssObj->setContent( getSafeString( argument ) );
                 [ +  - ]
      77 [ +  - ][ +  - ]:          3 :     args << m_scriptEngine->newQObject( ssObj );
         [ +  - ][ +  - ]
      78                 :            :   } else {
      79 [ +  - ][ +  - ]:          2 :     args << m_scriptEngine->newVariant( argument );
                 [ +  - ]
      80                 :            :   }
      81 [ +  - ][ +  - ]:         10 :   QScriptValue filterObject = m_filterObject;
      82 [ +  - ][ +  - ]:         10 :   QScriptValue returnValue = filterObject.call( QScriptValue(), args );
         [ +  - ][ +  - ]
      83                 :            : 
      84 [ +  - ][ +  + ]:          5 :   if ( returnValue.isString() ) {
      85 [ +  - ][ +  - ]:          1 :     return getSafeString( returnValue.toString() );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      86 [ +  - ][ +  - ]:          4 :   } else if ( returnValue.isQObject() ) {
      87         [ +  - ]:          4 :     QObject *returnedObject = qscriptvalue_cast<QObject *>( returnValue );
      88         [ +  - ]:          4 :     ScriptableSafeString *returnedStringObject = qobject_cast<ScriptableSafeString*>( returnedObject );
      89         [ -  + ]:          4 :     if ( !returnedStringObject )
      90         [ #  # ]:          0 :       return QVariant();
      91 [ +  - ][ +  - ]:          8 :     SafeString returnedString = returnedStringObject->wrappedString();
      92         [ +  - ]:          4 :     return returnedString;
      93 [ #  # ][ #  # ]:          0 :   } else if ( returnValue.isVariant() ) {
      94         [ #  # ]:          0 :     return qscriptvalue_cast<QVariant>( returnValue );
      95 [ #  # ][ #  # ]:          0 :   } else if ( returnValue.isArray() ) {
      96 [ #  # ][ #  # ]:          0 :     return qscriptvalue_cast<QVariantList>( returnValue );
                 [ #  # ]
      97                 :            :   }
      98         [ #  # ]:          0 :   return QVariant();
      99                 :            : }
     100                 :            : 

Generated by: LCOV version 1.9