OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved. |
3 * Copyright (C) 2011 Google Inc. All rights reserved. | 3 * Copyright (C) 2011 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 #include "platform/mac/ColorMac.h" | 52 #include "platform/mac/ColorMac.h" |
53 #include "public/platform/WebRect.h" | 53 #include "public/platform/WebRect.h" |
54 #include "public/web/WebHitTestResult.h" | 54 #include "public/web/WebHitTestResult.h" |
55 #include "public/web/WebRange.h" | 55 #include "public/web/WebRange.h" |
56 #include "public/web/WebView.h" | 56 #include "public/web/WebView.h" |
57 #include "web/WebLocalFrameImpl.h" | 57 #include "web/WebLocalFrameImpl.h" |
58 #include "web/WebViewImpl.h" | 58 #include "web/WebViewImpl.h" |
59 | 59 |
60 using namespace blink; | 60 using namespace blink; |
61 | 61 |
62 static NSAttributedString* attributedSubstringFromRange(const Range* range) | 62 static NSAttributedString* attributedSubstringFromRange(const EphemeralRange& ra
nge) |
63 { | 63 { |
64 NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init]
; | 64 NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init]
; |
65 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; | 65 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
66 size_t length = range->endOffset() - range->startOffset(); | 66 size_t length = range.endPosition().computeOffsetInContainerNode() - range.s
tartPosition().computeOffsetInContainerNode(); |
67 | 67 |
68 unsigned position = 0; | 68 unsigned position = 0; |
69 for (TextIterator it(range->startPosition(), range->endPosition()); !it.atEn
d() && [string length] < length; it.advance()) { | 69 for (TextIterator it(range.startPosition(), range.endPosition()); !it.atEnd(
) && [string length] < length; it.advance()) { |
70 unsigned numCharacters = it.length(); | 70 unsigned numCharacters = it.length(); |
71 if (!numCharacters) | 71 if (!numCharacters) |
72 continue; | 72 continue; |
73 | 73 |
74 Node* container = it.currentContainer(); | 74 Node* container = it.currentContainer(); |
75 LayoutObject* layoutObject = container->layoutObject(); | 75 LayoutObject* layoutObject = container->layoutObject(); |
76 ASSERT(layoutObject); | 76 ASSERT(layoutObject); |
77 if (!layoutObject) | 77 if (!layoutObject) |
78 continue; | 78 continue; |
79 | 79 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 LocalFrame* frame = result.innerNode()->document().frame(); | 120 LocalFrame* frame = result.innerNode()->document().frame(); |
121 FrameView* frameView = frame->view(); | 121 FrameView* frameView = frame->view(); |
122 | 122 |
123 EphemeralRange range = frame->rangeForPoint(result.roundedPointInInnerNodeFr
ame()); | 123 EphemeralRange range = frame->rangeForPoint(result.roundedPointInInnerNodeFr
ame()); |
124 if (range.isNull()) | 124 if (range.isNull()) |
125 return nil; | 125 return nil; |
126 | 126 |
127 // Expand to word under point. | 127 // Expand to word under point. |
128 VisibleSelection selection(range); | 128 VisibleSelection selection(range); |
129 selection.expandUsingGranularity(WordGranularity); | 129 selection.expandUsingGranularity(WordGranularity); |
130 RefPtrWillBeRawPtr<Range> wordRange = selection.toNormalizedRange(); | 130 const EphemeralRange wordRange = selection.toNormalizedEphemeralRange(); |
131 | 131 |
132 // Convert to NSAttributedString. | 132 // Convert to NSAttributedString. |
133 NSAttributedString* string = attributedSubstringFromRange(wordRange.get()); | 133 NSAttributedString* string = attributedSubstringFromRange(wordRange); |
134 | 134 |
135 // Compute bottom left corner and convert to AppKit coordinates. | 135 // Compute bottom left corner and convert to AppKit coordinates. |
136 IntRect stringRect = enclosingIntRect(wordRange->boundingRect()); | 136 // TODO(yosin) We shold avoid to create |Range| object. |
| 137 IntRect stringRect = enclosingIntRect(createRange(wordRange)->boundingRect()
); |
137 IntPoint stringPoint = stringRect.minXMaxYCorner(); | 138 IntPoint stringPoint = stringRect.minXMaxYCorner(); |
138 stringPoint.setY(frameView->height() - stringPoint.y()); | 139 stringPoint.setY(frameView->height() - stringPoint.y()); |
139 | 140 |
140 // Adjust for the font's descender. AppKit wants the baseline point. | 141 // Adjust for the font's descender. AppKit wants the baseline point. |
141 if ([string length]) { | 142 if ([string length]) { |
142 NSDictionary* attributes = [string attributesAtIndex:0 effectiveRange:NU
LL]; | 143 NSDictionary* attributes = [string attributesAtIndex:0 effectiveRange:NU
LL]; |
143 NSFont* font = [attributes objectForKey:NSFontAttributeName]; | 144 NSFont* font = [attributes objectForKey:NSFontAttributeName]; |
144 if (font) | 145 if (font) |
145 stringPoint.move(0, ceil(-[font descender])); | 146 stringPoint.move(0, ceil(-[font descender])); |
146 } | 147 } |
147 | 148 |
148 baselinePoint = stringPoint; | 149 baselinePoint = stringPoint; |
149 return string; | 150 return string; |
150 } | 151 } |
151 | 152 |
152 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame*
webFrame, size_t location, size_t length) | 153 NSAttributedString* WebSubstringUtil::attributedSubstringInRange(WebLocalFrame*
webFrame, size_t location, size_t length) |
153 { | 154 { |
154 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); | 155 LocalFrame* frame = toWebLocalFrameImpl(webFrame)->frame(); |
155 if (frame->view()->needsLayout()) | 156 if (frame->view()->needsLayout()) |
156 frame->view()->layout(); | 157 frame->view()->layout(); |
157 | 158 |
158 Element* editable = frame->selection().rootEditableElementOrDocumentElement(
); | 159 Element* editable = frame->selection().rootEditableElementOrDocumentElement(
); |
159 if (!editable) | 160 if (!editable) |
160 return nil; | 161 return nil; |
161 const EphemeralRange ephemeralRange(PlainTextRange(location, location + leng
th).createRange(*editable)); | 162 const EphemeralRange ephemeralRange(PlainTextRange(location, location + leng
th).createRange(*editable)); |
162 if (ephemeralRange.isNull()) | 163 if (ephemeralRange.isNull()) |
163 return nil; | 164 return nil; |
164 | 165 |
165 RefPtrWillBeRawPtr<Range> range = Range::create(ephemeralRange.document(), e
phemeralRange.startPosition(), ephemeralRange.endPosition()); | 166 return attributedSubstringFromRange(ephemeralRange); |
166 return attributedSubstringFromRange(range.get()); | |
167 } | 167 } |
168 | 168 |
169 } // namespace blink | 169 } // namespace blink |
OLD | NEW |