summaryrefslogtreecommitdiff
path: root/lib/ephy-output-encoding.c
blob: 2a7c1d0a8b6ffa87581d78d5a6bbeeb84ad4ca70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © Red Hat Inc.
 *
 *  This file is part of Epiphany.
 *
 *  Epiphany is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Epiphany is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "ephy-output-encoding.h"

#include <glib.h>

char *
ephy_encode_for_html_entity (const char *input)
{
  GString *str = g_string_new (input);

  g_string_replace (str, "&", "&amp;", 0);
  g_string_replace (str, "<", "&lt;", 0);
  g_string_replace (str, ">", "&gt;", 0);
  g_string_replace (str, "\"", "&quot;", 0);
  g_string_replace (str, "'", "&#x27;", 0);
  g_string_replace (str, "/", "&#x2F;", 0);

  return g_string_free (str, FALSE);
}

char *
ephy_encode_for_html_attribute (const char *input)
{
  GString *str;
  const char *c = input;

  if (!g_utf8_validate (input, -1, NULL))
    return g_strdup ("");

  str = g_string_new (NULL);
  do {
    gunichar u = g_utf8_get_char (c);
    if (g_unichar_isalnum (u))
      g_string_append_unichar (str, u);
    else
      g_string_append_printf (str, "&#x%02x;", u);
    c = g_utf8_next_char (c);
  } while (*c);

  return g_string_free (str, FALSE);
}