OSDN Git Service

Merge branch 'post-2.4.2'
[tjqt4port/tj2qt4.git] / taskjuggler / HTMLPrimitives.cpp
1 /*
2  * HTMLPrimitives.cpp - TaskJuggler
3  *
4  * Copyright (c) 2001, 2002, 2003, 2004 by Chris Schlaeger <cs@kde.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * $Id$
11  */
12
13 /* The following encoding table was copied from the Qt library sources since
14  * this information is not available over the public API. */
15
16 #include "HTMLPrimitives.h"
17
18 #include <ctype.h>
19
20 #include <qcstring.h>
21 #include <qmap.h>
22
23 QString
24 HTMLPrimitives::htmlFilter(const QString& s) const
25 {
26     QString out;
27     bool parTags = false;
28     for (uint i = 0; i < s.length(); i++)
29     {
30         QString repl;
31         if (s[i] == '<')
32         {
33             /* Preserve HTML tags */
34             uint j = i + 1;
35             if (j < s.length() && s[j] == '/')
36                 j++;
37             uint tagNameLen = 0;
38             for ( ; j < s.length() && isalpha(s[j]); ++j)
39                 tagNameLen++;
40             if (j < s.length() && s[j] == '/')
41                 j++;
42             if (s[j] == '>' && tagNameLen > 0)
43             {
44                 repl = s.mid(i, j - i + 1);
45                 i = j;
46             }
47             else
48                 repl = "&lt;";
49         }
50         else if (s[i] == '>')
51             repl = "&gt;";
52         else if (s[i] == '&')
53             repl = "&amp;";
54         else if (s[i] == '"')
55             repl = "&quot;";
56         else if (s.mid(i, 2) == "\n\n")
57         {
58             // Expand double line breaks to HTML paragraphs.
59             repl = "</p><p>";
60             parTags = true;
61             i++;
62         }
63         else if(s[i].row() != 0 || s[i].cell() >= 128)
64         {
65             // Quote all non-ASCII characters as hex values
66             repl.sprintf("&#x%02x%02x;", s[i].row(), s[i].cell());
67         }
68
69         if (repl.isEmpty())
70             out += s[i];
71         else
72             out += repl;
73     }
74
75     return parTags ? QString("<p>") + out + "</p>" : out;
76 }
77
78