OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "WebFrameImpl.h" |
| 33 #include "WebTextHelper.h" |
| 34 |
| 35 #include "ColorMac.h" |
| 36 #include "Document.h" |
| 37 #include "Element.h" |
| 38 #include "Frame.h" |
| 39 #include "FrameView.h" |
| 40 #include "HTMLElement.h" |
| 41 #include "Node.h" |
| 42 #include "Range.h" |
| 43 #include "TextIterator.h" |
| 44 #include "WebRect.h" |
| 45 #include "WebString.h" |
| 46 |
| 47 #import <Cocoa/Cocoa.h> |
| 48 |
| 49 using namespace WebCore; |
| 50 |
| 51 namespace WebKit { |
| 52 |
| 53 // Helper functions. |
| 54 |
| 55 WebFrameImpl* getWebFrameImpl(WebFrame* frame) |
| 56 { |
| 57 return static_cast<WebFrameImpl*>(frame); |
| 58 } |
| 59 |
| 60 Element* rangeScope(WebFrame* webFrame) |
| 61 { |
| 62 Frame* frame = getWebFrameImpl(webFrame)->frame(); |
| 63 Element* selectionRoot = frame->selection()->rootEditableElement(); |
| 64 return selectionRoot ? selectionRoot : frame->document()->documentElement(); |
| 65 } |
| 66 |
| 67 //////////////////////////////////////////////////////////////////////////////// |
| 68 |
| 69 WebTextHelper::WebTextHelper(WebFrame* frame) |
| 70 : m_frame(frame) |
| 71 { |
| 72 } |
| 73 |
| 74 // This function is copied from /WebKit/mac/Misc/WebNSAttributedStringExtras.mm. |
| 75 WebKit::WebString WebTextHelper::substringInRange(uint location, uint length) |
| 76 { |
| 77 Frame* frame = getWebFrameImpl(m_frame)->frame(); |
| 78 if (frame->view()->needsLayout()) |
| 79 frame->view()->layout(); |
| 80 |
| 81 RefPtr<Range> range(TextIterator::rangeFromLocationAndLength(rangeScope(m_fr
ame), location, length)); |
| 82 if (!range) |
| 83 return WebKit::WebString(); |
| 84 |
| 85 NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init]
; |
| 86 NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
| 87 |
| 88 unsigned position = 0; |
| 89 for (TextIterator it(range.get()); !it.atEnd() && [string length] < length;
it.advance()) { |
| 90 unsigned numCharacters = it.length(); |
| 91 if (!numCharacters) |
| 92 continue; |
| 93 |
| 94 ExceptionCode exception = 0; |
| 95 Node* container = it.range()->startContainer(exception); |
| 96 RenderObject* renderer = container->renderer(); |
| 97 ASSERT(renderer); |
| 98 if (!renderer) |
| 99 continue; |
| 100 |
| 101 RenderStyle* style = renderer->style(); |
| 102 NSFont* font = style->font().primaryFont()->getNSFont(); |
| 103 // If the platform font can't be loaded, it's likely that the site is |
| 104 // using a web font. For now, just use the default font instead. |
| 105 // TODO(rsesek): Change the font activation flags to allow other process
es |
| 106 // to use the font. |
| 107 if (!font) |
| 108 font = [NSFont systemFontOfSize:style->font().size()]; |
| 109 [attrs setObject:font forKey:NSFontAttributeName]; |
| 110 |
| 111 if (style->visitedDependentColor(CSSPropertyColor).alpha()) |
| 112 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyCol
or)) forKey:NSForegroundColorAttributeName]; |
| 113 else |
| 114 [attrs removeObjectForKey:NSForegroundColorAttributeName]; |
| 115 if (style->visitedDependentColor(CSSPropertyBackgroundColor).alpha()) |
| 116 [attrs setObject:nsColor(style->visitedDependentColor(CSSPropertyBac
kgroundColor)) forKey:NSBackgroundColorAttributeName]; |
| 117 else |
| 118 [attrs removeObjectForKey:NSBackgroundColorAttributeName]; |
| 119 |
| 120 NSString* substring = |
| 121 [[[NSString alloc] initWithCharactersNoCopy:const_cast<UChar*>(it.ch
aracters()) |
| 122 length:it.length() |
| 123 freeWhenDone:NO] autorelease]; |
| 124 [string replaceCharactersInRange:NSMakeRange(position, 0) |
| 125 withString:substring]; |
| 126 [string setAttributes:attrs range:NSMakeRange(position, numCharacters)]; |
| 127 position += numCharacters; |
| 128 } |
| 129 NSData* archivedData([NSArchiver archivedDataWithRootObject:string]); |
| 130 if (!archivedData) |
| 131 return WebString(); |
| 132 return WebKit::WebString(static_cast<const WebUChar*>([archivedData bytes]), |
| 133 [archivedData length]); |
| 134 } |
| 135 |
| 136 } // namespace WebKit |
OLD | NEW |