Cutelee  6.2.0
autoescape.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 "autoescape.h"
22 
23 #include "exception.h"
24 #include "parser.h"
25 #include "template.h"
26 #include "util.h"
27 
28 #include <QtCore/QFile>
29 
30 AutoescapeNodeFactory::AutoescapeNodeFactory() {}
31 
32 Node *AutoescapeNodeFactory::getNode(const QString &tagContent, Parser *p) const
33 {
34  auto expr = tagContent.split(QLatin1Char(' '), Qt::SkipEmptyParts);
35  if (expr.size() != 2) {
36  throw Cutelee::Exception(
37  TagSyntaxError, QStringLiteral("autoescape takes two arguments."));
38  }
39 
40  auto strState = expr.at(1);
41  int state;
42  if (strState == QStringLiteral("on"))
43  state = AutoescapeNode::On;
44  else if (strState == QStringLiteral("off"))
45  state = AutoescapeNode::Off;
46  else {
47  throw Cutelee::Exception(TagSyntaxError,
48  QStringLiteral("argument must be 'on' or 'off'"));
49  }
50 
51  auto n = new AutoescapeNode(state, p);
52 
53  auto list = p->parse(n, QStringLiteral("endautoescape"));
54  p->removeNextToken();
55 
56  n->setList(list);
57 
58  return n;
59 }
60 
61 AutoescapeNode::AutoescapeNode(int state, QObject *parent)
62  : Node(parent), m_state(state)
63 {
64 }
65 
66 void AutoescapeNode::setList(const NodeList &list) { m_list = list; }
67 
69 {
70  const auto old_setting = c->autoEscape();
71  c->setAutoEscape(m_state == On);
72  m_list.render(stream, c);
73  c->setAutoEscape(old_setting);
74 }
NodeList parse(Node *parent, const QStringList &stopAt={})
Definition: parser.cpp:180
The Context class holds the context to render a Template with.
Definition: context.h:118
Base class for all nodes.
Definition: node.h:77
Utility functions used throughout Cutelee.
SkipEmptyParts
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:80
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:48
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: autoescape.cpp:32
A list of Nodes with some convenience API for rendering them.
Definition: node.h:147
void render(OutputStream *stream, Context *c) const override
Definition: autoescape.cpp:68
void removeNextToken()
Definition: parser.cpp:297
void render(OutputStream *stream, Context *c) const
Definition: node.cpp:177
An exception for use when implementing template tags.
Definition: exception.h:84