~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/bindings/objc/DOMHTML.mm

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004-2006 Apple Computer, Inc.  All rights reserved.
 
3
 * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 * 1. Redistributions of source code must retain the above copyright
 
9
 *    notice, this list of conditions and the following disclaimer.
 
10
 * 2. Redistributions in binary form must reproduce the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer in the
 
12
 *    documentation and/or other materials provided with the distribution.
 
13
 *
 
14
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
15
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
17
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
18
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
19
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
20
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
21
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
22
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
24
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
25
 */
 
26
 
 
27
#import "config.h"
 
28
#import "DOMHTML.h"
 
29
 
 
30
#import "CSSHelper.h"
 
31
#import "DOMExtensions.h"
 
32
#import "DOMInternal.h"
 
33
#import "DOMPrivate.h"
 
34
#import "DocumentFragment.h"
 
35
#import "FrameView.h"
 
36
#import "HTMLDocument.h"
 
37
#import "HTMLInputElement.h"
 
38
#import "HTMLObjectElement.h"
 
39
#import "KURL.h"
 
40
#import "Range.h"
 
41
#import "RenderTextControl.h"
 
42
#import "markup.h"
 
43
 
 
44
//------------------------------------------------------------------------------------------
 
45
// DOMHTMLDocument
 
46
 
 
47
@implementation DOMHTMLDocument (DOMHTMLDocumentExtensions)
 
48
 
 
49
- (DOMDocumentFragment *)createDocumentFragmentWithMarkupString:(NSString *)markupString baseURL:(NSURL *)baseURL
 
50
{
 
51
    return [DOMDocumentFragment _wrapDocumentFragment:createFragmentFromMarkup([self _document], markupString, [baseURL absoluteString]).get()];
 
52
}
 
53
 
 
54
- (DOMDocumentFragment *)createDocumentFragmentWithText:(NSString *)text
 
55
{
 
56
    // FIXME: Since this is not a contextual fragment, it won't handle whitespace properly.
 
57
    return [DOMDocumentFragment _wrapDocumentFragment:createFragmentFromText([self _document]->createRange().get(), text).get()];
 
58
}
 
59
 
 
60
@end
 
61
 
 
62
@implementation DOMHTMLDocument (WebPrivate)
 
63
 
 
64
- (DOMDocumentFragment *)_createDocumentFragmentWithMarkupString:(NSString *)markupString baseURLString:(NSString *)baseURLString
 
65
{
 
66
    NSURL *baseURL = WebCore::KURL([self _document]->completeURL(WebCore::parseURL(baseURLString)).deprecatedString()).getNSURL();
 
67
    return [self createDocumentFragmentWithMarkupString:markupString baseURL:baseURL];
 
68
}
 
69
 
 
70
- (DOMDocumentFragment *)_createDocumentFragmentWithText:(NSString *)text
 
71
{
 
72
    return [self createDocumentFragmentWithText:text];
 
73
}
 
74
 
 
75
@end
 
76
 
 
77
#pragma mark DOM EXTENSIONS
 
78
 
 
79
@implementation DOMHTMLInputElement(FormAutoFillTransition)
 
80
 
 
81
- (BOOL)_isTextField
 
82
{
 
83
    // We could make this public API as-is, or we could change it into a method that returns whether
 
84
    // the element is a text field or a button or ... ?
 
85
    static NSArray *textInputTypes = nil;
 
86
#ifndef NDEBUG
 
87
    static NSArray *nonTextInputTypes = nil;
 
88
#endif
 
89
    
 
90
    NSString *fieldType = [self type];
 
91
    
 
92
    // No type at all is treated as text type
 
93
    if ([fieldType length] == 0)
 
94
        return YES;
 
95
    
 
96
    if (textInputTypes == nil)
 
97
        textInputTypes = [[NSSet alloc] initWithObjects:@"text", @"password", @"search", @"isindex", nil];
 
98
    
 
99
    BOOL isText = [textInputTypes containsObject:[fieldType lowercaseString]];
 
100
    
 
101
#ifndef NDEBUG
 
102
    if (nonTextInputTypes == nil)
 
103
        nonTextInputTypes = [[NSSet alloc] initWithObjects:@"checkbox", @"radio", @"submit", @"reset", @"file", @"hidden", @"image", @"button", @"range", nil];
 
104
    
 
105
    // Catch cases where a new input type has been added that's not in these lists.
 
106
    ASSERT(isText || [nonTextInputTypes containsObject:[fieldType lowercaseString]]);
 
107
#endif    
 
108
    
 
109
    return isText;
 
110
}
 
111
 
 
112
- (NSRect)_rectOnScreen
 
113
{
 
114
    // Returns bounding rect of text field, in screen coordinates.
 
115
    NSRect result = [self boundingBox];
 
116
    if (![self _HTMLInputElement]->document()->view())
 
117
        return result;
 
118
 
 
119
    NSView* view = [self _HTMLInputElement]->document()->view()->getDocumentView();
 
120
    result = [view convertRect:result toView:nil];
 
121
    result.origin = [[view window] convertBaseToScreen:result.origin];
 
122
    return result;
 
123
}
 
124
 
 
125
- (void)_replaceCharactersInRange:(NSRange)targetRange withString:(NSString *)replacementString selectingFromIndex:(int)index
 
126
{
 
127
    WebCore::HTMLInputElement* inputElement = [self _HTMLInputElement];
 
128
    if (inputElement) {
 
129
        WebCore::String newValue = inputElement->value();
 
130
        newValue.replace(targetRange.location, targetRange.length, replacementString);
 
131
        inputElement->setValue(newValue);
 
132
        inputElement->setSelectionRange(index, newValue.length());
 
133
    }
 
134
}
 
135
 
 
136
- (NSRange)_selectedRange
 
137
{
 
138
    WebCore::HTMLInputElement* inputElement = [self _HTMLInputElement];
 
139
    if (inputElement) {
 
140
        int start = inputElement->selectionStart();
 
141
        int end = inputElement->selectionEnd();
 
142
        return NSMakeRange(start, end - start); 
 
143
    }
 
144
    return NSMakeRange(NSNotFound, 0);
 
145
}    
 
146
 
 
147
- (void)_setAutofilled:(BOOL)filled
 
148
{
 
149
    // This notifies the input element that the content has been autofilled
 
150
    // This allows WebKit to obey the -webkit-autofill pseudo style, which
 
151
    // changes the background color.
 
152
    WebCore::HTMLInputElement* inputElement = [self _HTMLInputElement];
 
153
    if (inputElement)
 
154
        inputElement->setAutofilled(filled);
 
155
}
 
156
 
 
157
@end
 
158
 
 
159
@implementation DOMHTMLSelectElement(FormAutoFillTransition)
 
160
 
 
161
- (void)_activateItemAtIndex:(int)index
 
162
{
 
163
    // FIXME: Needs implementation for non-NSView <select>!
 
164
}
 
165
 
 
166
@end
 
167
 
 
168
@implementation DOMHTMLInputElement (FormPromptAdditions)
 
169
- (BOOL)_isEdited
 
170
{
 
171
    WebCore::RenderObject *renderer = [self _node]->renderer();
 
172
    if (renderer && [self _isTextField])
 
173
        return static_cast<WebCore::RenderTextControl *>(renderer)->isUserEdited();
 
174
    
 
175
    return NO;
 
176
}
 
177
@end
 
178
 
 
179
@implementation DOMHTMLTextAreaElement (FormPromptAdditions)
 
180
- (BOOL)_isEdited
 
181
{
 
182
    WebCore::RenderObject *renderer = [self _node]->renderer();
 
183
    if (renderer)
 
184
        return static_cast<WebCore::RenderTextControl *>(renderer)->isUserEdited();
 
185
    
 
186
    return NO;
 
187
}
 
188
@end