if can have a variable test or a content check

master
Marc Wäckerlin 8 years ago
parent b4cd0bf006
commit 02aaf6e3ec
  1. 2
      bootstrap.sh
  2. 77
      src/commands.hxx

@ -845,7 +845,7 @@ if testtag AX_USE_DOXYGEN; then
doxyreplace PROJECT_NAME "@PACKAGE_NAME@"
doxyreplace PROJECT_NUMBER "@PACKAGE_VERSION@"
doxyreplace PROJECT_BRIEF "@DESCRIPTION@"
doxyreplace PROJECT_LOGO "@top_srcdir@/@PACKACE_LOGO@"
doxyreplace PROJECT_LOGO "@top_srcdir@/@PACKAGE_LOGO@"
doxyreplace INLINE_INHERITED_MEMB YES
doxyreplace MULTILINE_CPP_IS_BRIEF YES
doxyreplace TAB_SIZE 2

@ -2084,16 +2084,28 @@ class If: public Command {
" <command3>\n"
" <command4>\n"
" <...>\n"
"\n\n"+
tag()+" <selector> -> <text>\n"
" <command1>\n"
" <command2>\n"
" <...>\n"
"else\n"
" <command3>\n"
" <command4>\n"
" <...>\n"
"\n\n"
"Execute commands conditionally. "
"The first variant compares a variable to a value. "
"The comparision <cmp> can be = ^ . ~ < >, "
"which means equal, different, contains, match, "
"less (as integer), bigger (as integer). "
"Match allows a regular expression. "
"The second variant checks for a text in a selector, "
"similar to command exists. "
"There is an optional else part.";
}
QString command() const {
return tag()+" "+_variable+" "+QString(_cmp)+" "+_value
return tag()+" "+_variable+" "+_cmp+" "+_value
+(_script.get()?"\n"+_script->print().join("\n "):"");
}
std::shared_ptr<Command> parse(Script*, QString args,
@ -2101,10 +2113,17 @@ class If: public Command {
int indent) {
std::shared_ptr<If> cmd(new If());
int pos(args.indexOf(QRegularExpression("[=^.~<>]")));
if (pos<0) throw BadArgument(tag()+" needs a comparision, not: "+args);
int len(1);
if (args.contains("->")) {
pos = args.indexOf("->");
len = 2;
cmd->_cmp = "->";
} else {
if (pos<0) throw BadArgument(tag()+" needs a comparision, not: "+args);
cmd->_cmp = args[pos];
}
cmd->_variable = args.left(pos).trimmed();
cmd->_cmp = args[pos].toLatin1();
cmd->_value = args.mid(pos+1).trimmed();
cmd->_value = args.mid(pos+len).trimmed();
cmd->_script = std::shared_ptr<Script>(new Script);
cmd->_script->parse(subCommandBlock(in), file, line+1, indent+1);
if (in.size() && in.first().contains(QRegularExpression("^else *$"))) {
@ -2117,32 +2136,46 @@ class If: public Command {
bool execute(Script* script, QWebFrame* frame) {
Logger log(this, script, false);
QString value(script->replacevars(_value));
QString selector(script->replacevars(_variable));
bool check(false);
switch (_cmp) {
case '=': check = script->variable(_variable)==value;
break;
case '^': check = script->variable(_variable)!=value;
break;
case '.': check = script->variable(_variable).contains(value);
break;
case '~': check =
script->variable(_variable).contains(QRegularExpression(value));
break;
case '<': check = script->variable(_variable).toInt()<value.toInt();
break;
case '>': check = script->variable(_variable).toInt()>value.toInt();
break;
default:;
if (_cmp=="->") {
Q_FOREACH(QWebElement element, frame->findAllElements(selector)) {
if (value.isEmpty() || // just find element
element.toOuterXml().indexOf(value)!=-1 ||
element.toPlainText().indexOf(value)!=-1) {
check = true;
break;
}
}
log(QString("evaluated expression to ")+(check?"true":"false")+": "
+selector+" "+_cmp+" "+value);
} else {
switch (_cmp[0].toLatin1()) {
case '=': check = script->variable(_variable)==value;
break;
case '^': check = script->variable(_variable)!=value;
break;
case '.': check = script->variable(_variable).contains(value);
break;
case '~': check =
script->variable(_variable).contains(QRegularExpression(value));
break;
case '<': check = script->variable(_variable).toInt()<value.toInt();
break;
case '>': check = script->variable(_variable).toInt()>value.toInt();
break;
default:;
}
log(QString("evaluated expression to ")+(check?"true":"false")+":"
+script->variable(_variable)+" "+_cmp+" "+value);
}
log("evaluated expression: "+script->variable(_variable)
+" "+_cmp+" "+value);
if (check) return runScript(this, _script, script, frame);
else if (_else) return runScript(this, _else, script, frame);
return true;
}
private:
QString _variable;
char _cmp;
QString _cmp;
QString _value;
std::shared_ptr<Script> _script;
std::shared_ptr<Script> _else;

Loading…
Cancel
Save