1/*
2 * Copyright (C) 2008 Nuanti Ltd.
3 * Copyright (C) 2009 Jan Alonzo
4 * Copyright (C) 2012 Igalia S.L.
5 * Copyright (C) 2013 Samsung Electronics
6 *
7 * Portions from Mozilla a11y, copyright as follows:
8 *
9 * The Original Code is mozilla.org code.
10 *
11 * The Initial Developer of the Original Code is
12 * Sun Microsystems, Inc.
13 * Portions created by the Initial Developer are Copyright (C) 2002
14 * the Initial Developer. All Rights Reserved.
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Library General Public
18 * License as published by the Free Software Foundation; either
19 * version 2 of the License, or (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Library General Public License for more details.
25 *
26 * You should have received a copy of the GNU Library General Public License
27 * along with this library; see the file COPYING.LIB. If not, write to
28 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 * Boston, MA 02110-1301, USA.
30 */
31
32#include "config.h"
33#include "WebKitAccessibleInterfaceDocument.h"
34
35#if HAVE(ACCESSIBILITY)
36
37#include "AccessibilityObject.h"
38#include "Document.h"
39#include "DocumentType.h"
40#include "WebKitAccessible.h"
41#include "WebKitAccessibleUtil.h"
42
43using namespace WebCore;
44
45static AccessibilityObject* core(AtkDocument* document)
46{
47 if (!WEBKIT_IS_ACCESSIBLE(document))
48 return 0;
49
50 return &webkitAccessibleGetAccessibilityObject(WEBKIT_ACCESSIBLE(document));
51}
52
53static const gchar* documentAttributeValue(AtkDocument* document, const gchar* attribute)
54{
55 Document* coreDocument = core(document)->document();
56 if (!coreDocument)
57 return 0;
58
59 String value;
60 AtkCachedProperty atkCachedProperty;
61
62 if (!g_ascii_strcasecmp(attribute, "DocType") && coreDocument->doctype()) {
63 value = coreDocument->doctype()->name();
64 atkCachedProperty = AtkCachedDocumentType;
65 } else if (!g_ascii_strcasecmp(attribute, "Encoding")) {
66 value = coreDocument->charset();
67 atkCachedProperty = AtkCachedDocumentEncoding;
68 } else if (!g_ascii_strcasecmp(attribute, "URI")) {
69 value = coreDocument->documentURI();
70 atkCachedProperty = AtkCachedDocumentURI;
71 }
72
73 if (!value.isEmpty())
74 return webkitAccessibleCacheAndReturnAtkProperty(WEBKIT_ACCESSIBLE(document), atkCachedProperty, value.utf8());
75
76 return 0;
77}
78
79static const gchar* webkitAccessibleDocumentGetAttributeValue(AtkDocument* document, const gchar* attribute)
80{
81 g_return_val_if_fail(ATK_IS_DOCUMENT(document), 0);
82 returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(document), 0);
83
84 return documentAttributeValue(document, attribute);
85}
86
87static AtkAttributeSet* webkitAccessibleDocumentGetAttributes(AtkDocument* document)
88{
89 g_return_val_if_fail(ATK_IS_DOCUMENT(document), 0);
90 returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(document), 0);
91
92 AtkAttributeSet* attributeSet = nullptr;
93 const gchar* attributes[] = { "DocType", "Encoding", "URI" };
94
95 for (unsigned i = 0; i < G_N_ELEMENTS(attributes); i++) {
96 const gchar* value = documentAttributeValue(document, attributes[i]);
97 if (value)
98 attributeSet = addToAtkAttributeSet(attributeSet, attributes[i], value);
99 }
100
101 return attributeSet;
102}
103
104static const gchar* webkitAccessibleDocumentGetLocale(AtkDocument* document)
105{
106 g_return_val_if_fail(ATK_IS_DOCUMENT(document), 0);
107 returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(document), 0);
108
109 // The logic to resolve locale has been moved to
110 // AtkObject::get_object_locale() virtual method. However, to avoid breaking
111 // clients expecting the deprecated AtkDocumentIface::get_document_locale()
112 // to be overriden, method is kept and chained up to
113 // AtkObject::get_object_locale(). <https://bugs.webkit.org/show_bug.cgi?id=115647>
114 return atk_object_get_object_locale(ATK_OBJECT(document));
115}
116
117void webkitAccessibleDocumentInterfaceInit(AtkDocumentIface* iface)
118{
119 iface->get_document_attribute_value = webkitAccessibleDocumentGetAttributeValue;
120 iface->get_document_attributes = webkitAccessibleDocumentGetAttributes;
121 iface->get_document_locale = webkitAccessibleDocumentGetLocale;
122}
123
124#endif
125