2 * Copyright (C) 2004-2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #import "DOMExtensions.h"
32 #import "DOMInternal.h"
33 #import "DOMPrivate.h"
34 #import "DocumentFragment.h"
36 #import "HTMLDocument.h"
37 #import "HTMLInputElement.h"
38 #import "HTMLObjectElement.h"
40 #import "RenderTextControl.h"
43 //------------------------------------------------------------------------------------------
46 @implementation DOMHTMLDocument (DOMHTMLDocumentExtensions)
48 - (DOMDocumentFragment *)createDocumentFragmentWithMarkupString:(NSString *)markupString baseURL:(NSURL *)baseURL
50 return [DOMDocumentFragment _wrapDocumentFragment:createFragmentFromMarkup([self _document], markupString, [baseURL absoluteString]).get()];
53 - (DOMDocumentFragment *)createDocumentFragmentWithText:(NSString *)text
55 // FIXME: Since this is not a contextual fragment, it won't handle whitespace properly.
56 return [DOMDocumentFragment _wrapDocumentFragment:createFragmentFromText([self _document]->createRange().get(), text).get()];
61 @implementation DOMHTMLDocument (WebPrivate)
63 - (DOMDocumentFragment *)_createDocumentFragmentWithMarkupString:(NSString *)markupString baseURLString:(NSString *)baseURLString
65 NSURL *baseURL = [self _document]->completeURL(WebCore::parseURL(baseURLString));
66 return [self createDocumentFragmentWithMarkupString:markupString baseURL:baseURL];
69 - (DOMDocumentFragment *)_createDocumentFragmentWithText:(NSString *)text
71 return [self createDocumentFragmentWithText:text];
76 #pragma mark DOM EXTENSIONS
78 @implementation DOMHTMLInputElement(FormAutoFillTransition)
82 // We could make this public API as-is, or we could change it into a method that returns whether
83 // the element is a text field or a button or ... ?
84 static NSArray *textInputTypes = nil;
86 static NSArray *nonTextInputTypes = nil;
89 NSString *fieldType = [self type];
91 // No type at all is treated as text type
92 if ([fieldType length] == 0)
95 if (textInputTypes == nil)
96 textInputTypes = [[NSSet alloc] initWithObjects:@"text", @"password", @"search", @"isindex", nil];
98 BOOL isText = [textInputTypes containsObject:[fieldType lowercaseString]];
101 if (nonTextInputTypes == nil)
102 nonTextInputTypes = [[NSSet alloc] initWithObjects:@"checkbox", @"radio", @"submit", @"reset", @"file", @"hidden", @"image", @"button", @"range", nil];
104 // Catch cases where a new input type has been added that's not in these lists.
105 ASSERT(isText || [nonTextInputTypes containsObject:[fieldType lowercaseString]]);
111 - (NSRect)_rectOnScreen
113 // Returns bounding rect of text field, in screen coordinates.
114 NSRect result = [self boundingBox];
115 if (![self _HTMLInputElement]->document()->view())
118 NSView* view = [self _HTMLInputElement]->document()->view()->documentView();
119 result = [view convertRect:result toView:nil];
120 result.origin = [[view window] convertBaseToScreen:result.origin];
124 - (void)_replaceCharactersInRange:(NSRange)targetRange withString:(NSString *)replacementString selectingFromIndex:(int)index
126 WebCore::HTMLInputElement* inputElement = [self _HTMLInputElement];
128 WebCore::String newValue = inputElement->value();
129 newValue.replace(targetRange.location, targetRange.length, replacementString);
130 inputElement->setValue(newValue);
131 inputElement->setSelectionRange(index, newValue.length());
135 - (NSRange)_selectedRange
137 WebCore::HTMLInputElement* inputElement = [self _HTMLInputElement];
139 int start = inputElement->selectionStart();
140 int end = inputElement->selectionEnd();
141 return NSMakeRange(start, end - start);
143 return NSMakeRange(NSNotFound, 0);
146 - (void)_setAutofilled:(BOOL)filled
148 // This notifies the input element that the content has been autofilled
149 // This allows WebKit to obey the -webkit-autofill pseudo style, which
150 // changes the background color.
151 WebCore::HTMLInputElement* inputElement = [self _HTMLInputElement];
153 inputElement->setAutofilled(filled);
158 @implementation DOMHTMLSelectElement(FormAutoFillTransition)
160 - (void)_activateItemAtIndex:(int)index
162 // FIXME: Needs implementation for non-NSView <select>!
167 @implementation DOMHTMLInputElement (FormPromptAdditions)
170 WebCore::RenderObject *renderer = [self _node]->renderer();
171 if (renderer && [self _isTextField])
172 return static_cast<WebCore::RenderTextControl *>(renderer)->isUserEdited();
178 @implementation DOMHTMLTextAreaElement (FormPromptAdditions)
181 WebCore::RenderObject *renderer = [self _node]->renderer();
183 return static_cast<WebCore::RenderTextControl *>(renderer)->isUserEdited();