KTextTemplate

outputstream.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "outputstream.h"
11
12#include "safestring.h"
13
14using namespace KTextTemplate;
15
17 : m_stream(nullptr)
18{
19}
20
22 : m_stream(stream)
23{
24}
25
27
29{
30 // This could be replaced by QString::toHtmlEscaped()
31 // but atm it does not escape single quotes
32 QString rich;
33 const int len = input.length();
34 rich.reserve(int(len * 1.1));
35 for (int i = 0; i < len; ++i) {
36 const QChar ch = input.at(i);
37 if (ch == QLatin1Char('<'))
38 rich += QLatin1String("&lt;");
39 else if (ch == QLatin1Char('>'))
40 rich += QLatin1String("&gt;");
41 else if (ch == QLatin1Char('&'))
42 rich += QLatin1String("&amp;");
43 else if (ch == QLatin1Char('"'))
44 rich += QLatin1String("&quot;");
45 else if (ch == QLatin1Char('\''))
46 rich += QLatin1String("&#39;");
47 else
48 rich += ch;
49 }
50 rich.squeeze();
51 return rich;
52}
53
55{
56 return escape(input.get());
57}
58
60{
61 if (!input.isSafe())
62 return escape(input.get());
63 return input;
64}
65
70
72{
73 if (m_stream)
74 (*m_stream) << input;
75 return *this;
76}
77
79{
80 if (m_stream) {
81 if (input.needsEscape())
82 (*m_stream) << escape(input.get());
83 else
84 (*m_stream) << input.get();
85 }
86 return *this;
87}
88/*
89OutputStream& OutputStream::operator<<(const
90KTextTemplate::OutputStream::Escape& e)
91{
92 ( *m_stream ) << escape( e.m_content );
93 return *this;
94}*/
95
97{
98 if (m_stream)
99 (*m_stream) << stream->readAll();
100 return *this;
101}
102/*
103KTextTemplate::OutputStream::MarkSafe::MarkSafe(const QString& input)
104 : m_safe( false ), m_content( input )
105{
106
107}
108
109KTextTemplate::OutputStream::MarkSafe::MarkSafe(const KTextTemplate::SafeString&
110input) : m_safe( input.isSafe() ), m_content( input.get() )
111{
112
113}
114*/
The OutputStream class is used to render templates to a QTextStream.
virtual ~OutputStream()
Destructor.
OutputStream()
Creates a null OutputStream.
QString conditionalEscape(const KTextTemplate::SafeString &input) const
Returns after escaping it, unless input is "safe", in which case, input is returned unmodified.
virtual QSharedPointer< OutputStream > clone(QTextStream *stream) const
Returns a cloned OutputStream with the same filtering behaviour.
OutputStream & operator<<(const QString &input)
Writes input to the stream after escaping it.
virtual QString escape(const QString &input) const
Returns an escaped version of input.
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:81
bool needsEscape() const
Whether the string needs to be escaped.
bool isSafe() const
Whether the string is safe.
const NestedString & get() const
Returns the String held by this SafeString
Definition safestring.h:283
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
const QChar at(qsizetype position) const const
qsizetype length() const const
void reserve(qsizetype size)
void squeeze()
QString readAll()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:10:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.