summaryrefslogtreecommitdiff
path: root/Source/WebCore/accessibility/atk/WebKitAccessibleInterfaceDocument.cpp
blob: 6752d12d323b290cc15c113a1d4e1348b06f0dc8 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C) 2008 Nuanti Ltd.
 * Copyright (C) 2009 Jan Alonzo
 * Copyright (C) 2012 Igalia S.L.
 * Copyright (C) 2013 Samsung Electronics
 *
 * Portions from Mozilla a11y, copyright as follows:
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Sun Microsystems, Inc.
 * Portions created by the Initial Developer are Copyright (C) 2002
 * the Initial Developer. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitAccessibleInterfaceDocument.h"

#if HAVE(ACCESSIBILITY)

#include "AccessibilityObject.h"
#include "Document.h"
#include "DocumentType.h"
#include "WebKitAccessibleUtil.h"
#include "WebKitAccessibleWrapperAtk.h"

using namespace WebCore;

static AccessibilityObject* core(AtkDocument* document)
{
    if (!WEBKIT_IS_ACCESSIBLE(document))
        return 0;

    return webkitAccessibleGetAccessibilityObject(WEBKIT_ACCESSIBLE(document));
}

static const gchar* documentAttributeValue(AtkDocument* document, const gchar* attribute)
{
    Document* coreDocument = core(document)->document();
    if (!coreDocument)
        return 0;

    String value = String();
    AtkCachedProperty atkCachedProperty;

    if (!g_ascii_strcasecmp(attribute, "DocType") && coreDocument->doctype()) {
        value = coreDocument->doctype()->name();
        atkCachedProperty = AtkCachedDocumentType;
    } else if (!g_ascii_strcasecmp(attribute, "Encoding")) {
        value = coreDocument->charset();
        atkCachedProperty = AtkCachedDocumentEncoding;
    } else if (!g_ascii_strcasecmp(attribute, "URI")) {
        value = coreDocument->documentURI();
        atkCachedProperty = AtkCachedDocumentURI;
    }

    if (!value.isEmpty())
        return cacheAndReturnAtkProperty(ATK_OBJECT(document), atkCachedProperty, value);

    return 0;
}

static const gchar* webkitAccessibleDocumentGetAttributeValue(AtkDocument* document, const gchar* attribute)
{
    g_return_val_if_fail(ATK_IS_DOCUMENT(document), 0);
    returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(document), 0);

    return documentAttributeValue(document, attribute);
}

static AtkAttributeSet* webkitAccessibleDocumentGetAttributes(AtkDocument* document)
{
    g_return_val_if_fail(ATK_IS_DOCUMENT(document), 0);
    returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(document), 0);

    AtkAttributeSet* attributeSet = nullptr;
    const gchar* attributes[] = { "DocType", "Encoding", "URI" };

    for (unsigned i = 0; i < G_N_ELEMENTS(attributes); i++) {
        const gchar* value = documentAttributeValue(document, attributes[i]);
        if (value)
            attributeSet = addToAtkAttributeSet(attributeSet, attributes[i], value);
    }

    return attributeSet;
}

static const gchar* webkitAccessibleDocumentGetLocale(AtkDocument* document)
{
    g_return_val_if_fail(ATK_IS_DOCUMENT(document), 0);
    returnValIfWebKitAccessibleIsInvalid(WEBKIT_ACCESSIBLE(document), 0);

    // The logic to resolve locale has been moved to
    // AtkObject::get_object_locale() virtual method. However, to avoid breaking
    // clients expecting the deprecated AtkDocumentIface::get_document_locale()
    // to be overriden, method is kept and chained up to
    // AtkObject::get_object_locale(). <https://bugs.webkit.org/show_bug.cgi?id=115647>
    return atk_object_get_object_locale(ATK_OBJECT(document));
}

void webkitAccessibleDocumentInterfaceInit(AtkDocumentIface* iface)
{
    iface->get_document_attribute_value = webkitAccessibleDocumentGetAttributeValue;
    iface->get_document_attributes = webkitAccessibleDocumentGetAttributes;
    iface->get_document_locale = webkitAccessibleDocumentGetLocale;
}

#endif