Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / mac / WebSubstringUtil.mm
1 /*
2  * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2011 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "WebSubstringUtil.h"
34
35 #import <Cocoa/Cocoa.h>
36
37 #include "WebFrameImpl.h"
38 #include "bindings/v8/ExceptionStatePlaceholder.h"
39 #include "core/dom/Document.h"
40 #include "core/dom/Element.h"
41 #include "core/dom/Node.h"
42 #include "core/dom/Range.h"
43 #include "core/editing/FrameSelection.h"
44 #include "core/editing/PlainTextRange.h"
45 #include "core/editing/TextIterator.h"
46 #include "core/html/HTMLElement.h"
47 #include "core/frame/Frame.h"
48 #include "core/frame/FrameView.h"
49 #include "core/rendering/RenderObject.h"
50 #include "core/rendering/style/RenderStyle.h"
51 #include "platform/fonts/Font.h"
52 #include "platform/mac/ColorMac.h"
53 #include "public/platform/WebRect.h"
54
55 using namespace WebCore;
56
57 namespace blink {
58
59 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebFrame* webFrame, size_t location, size_t length)
60 {
61     Frame* frame = toWebFrameImpl(webFrame)->frame();
62     if (frame->view()->needsLayout())
63         frame->view()->layout();
64
65     Element* editable = frame->selection().rootEditableElementOrDocumentElement();
66     ASSERT(editable);
67     RefPtr<Range> range(PlainTextRange(location, location + length).createRange(*editable));
68     if (!range)
69         return nil;
70
71     NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init];
72     NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
73
74     unsigned position = 0;
75     for (TextIterator it(range.get()); !it.atEnd() && [string length] < length; it.advance()) {
76         unsigned numCharacters = it.length();
77         if (!numCharacters)
78             continue;
79
80         Node* container = it.range()->startContainer(IGNORE_EXCEPTION);
81         RenderObject* renderer = container->renderer();
82         ASSERT(renderer);
83         if (!renderer)
84             continue;
85
86         RenderStyle* style = renderer->style();
87         NSFont* font = style->font().primaryFont()->getNSFont();
88         // If the platform font can't be loaded, it's likely that the site is
89         // using a web font. For now, just use the default font instead.
90         // TODO(rsesek): Change the font activation flags to allow other processes
91         // to use the font.
92         if (!font)
93           font = [NSFont systemFontOfSize:style->font().fontDescription().computedSize()];
94         [attrs setObject:font forKey:NSFontAttributeName];
95
96         if (style->visitedDependentColor(CSSPropertyColor).alpha())
97             [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyColor)) forKey:NSForegroundColorAttributeName];
98         else
99             [attrs removeObjectForKey:NSForegroundColorAttributeName];
100         if (style->visitedDependentColor(CSSPropertyBackgroundColor).alpha())
101             [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyBackgroundColor)) forKey:NSBackgroundColorAttributeName];
102         else
103             [attrs removeObjectForKey:NSBackgroundColorAttributeName];
104
105         Vector<UChar> characters;
106         it.appendTextTo(characters);
107         NSString* substring =
108             [[[NSString alloc] initWithCharacters:characters.data()
109                                            length:characters.size()] autorelease];
110         [string replaceCharactersInRange:NSMakeRange(position, 0)
111                               withString:substring];
112         [string setAttributes:attrs range:NSMakeRange(position, numCharacters)];
113         position += numCharacters;
114     }
115     return [string autorelease];
116 }
117
118 } // namespace blink