Cutelee  6.1.0
scriptablefilter.cpp
1 /*
2  This file is part of the Cutelee 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 <QtQml/QJSEngine>
27 
28 ScriptableFilter::ScriptableFilter(const QJSValue &filterObject,
29  QJSEngine *engine)
30  : m_filterObject(filterObject), m_scriptEngine(engine)
31 {
32 }
33 
34 ScriptableFilter::~ScriptableFilter() {}
35 
37 {
38  auto safety = m_filterObject.property(QStringLiteral("isSafe"));
39  if (safety.isBool()) {
40  return safety.toBool();
41  }
42  return false;
43 }
44 
46  const QVariant &argument,
47  bool autoescape) const
48 {
49  Q_UNUSED(autoescape)
50  QJSValueList args;
51  if (input.userType() == qMetaTypeId<QVariantList>()) {
52  auto inputList = input.value<QVariantList>();
53  auto array = m_scriptEngine->newArray(inputList.size());
54  for (auto i = 0; i < inputList.size(); ++i) {
55  if (inputList.at(i).canConvert<QObject *>()) {
56  array.setProperty(
57  i, m_scriptEngine->newQObject(inputList.at(i).value<QObject *>()));
58  } else {
59  array.setProperty(i, m_scriptEngine->toScriptValue(inputList.at(i)));
60  }
61  }
62  args << array;
63  } else {
64  if (isSafeString(input)) {
65  auto ssObj = new ScriptableSafeString(m_scriptEngine);
66  ssObj->setContent(getSafeString(input));
67  args << m_scriptEngine->newQObject(ssObj);
68  } else if (input.canConvert<QObject *>()) {
69  args << m_scriptEngine->newQObject(input.value<QObject *>());
70  } else {
71  args << m_scriptEngine->toScriptValue(input);
72  }
73  }
74 
75  if (argument.userType() == qMetaTypeId<SafeString>()) {
76  auto ssObj = new ScriptableSafeString(m_scriptEngine);
77  ssObj->setContent(getSafeString(argument));
78  args << m_scriptEngine->newQObject(ssObj);
79  } else {
80  args << m_scriptEngine->toScriptValue(argument);
81  }
82  auto filterObject = m_filterObject;
83  auto returnValue = filterObject.call(args);
84 
85  if (returnValue.isString()) {
86  return getSafeString(returnValue.toString());
87  } else if (returnValue.isQObject()) {
88  auto returnedObject = qjsvalue_cast<QObject *>(returnValue);
89  auto returnedStringObject
90  = qobject_cast<ScriptableSafeString *>(returnedObject);
91  if (!returnedStringObject)
92  return QVariant();
93  auto returnedString = returnedStringObject->wrappedString();
94  return returnedString;
95  } else if (returnValue.isVariant()) {
96  return qjsvalue_cast<QVariant>(returnValue);
97  } else if (returnValue.isArray()) {
98  return qjsvalue_cast<QVariantList>(returnValue);
99  }
100  return QVariant();
101 }
bool isSafe() const override
QVariant doFilter(const QVariant &input, const QVariant &argument, bool autoescape={}) const override
bool isSafeString(const QVariant &input)
Definition: util.cpp:117
Cutelee::SafeString getSafeString(const QVariant &input)
Definition: util.cpp:108
Utility functions used throughout Cutelee.