96 CPArray _backwardStack;
97 CPArray _forwardStack;
99 BOOL _ignoreLoadStart;
103 id _downloadDelegate;
104 id _frameLoadDelegate;
106 id _resourceLoadDelegate;
114 Function _loadCallback;
117 int _effectiveScrollMode;
118 BOOL _contentIsAccessible;
119 CPTimer _contentSizeCheckTimer;
120 int _contentSizePollCount;
122 int _loadHTMLStringTimer;
124 BOOL _drawsBackground;
127 - (id)initWithFrame:(CGRect)frameRect frameName:(
CPString)frameName groupName:(
CPString)groupName
129 if (
self = [
self initWithFrame:frameRect])
131 _iframe.name = frameName;
137 - (id)initWithFrame:(CGRect)aFrame
139 if (
self = [super initWithFrame:aFrame])
145 _contentIsAccessible = YES;
148 _drawsBackground = YES;
152 [
self _initDOMWithFrame:aFrame];
158 - (id)_initDOMWithFrame:(CGRect)aFrame
160 _ignoreLoadStart = YES;
161 _ignoreLoadEnd = YES;
163 _iframe = document.createElement(
"iframe");
164 _iframe.name =
"iframe_" + FLOOR(RAND() * 10000);
165 _iframe.style.width =
"100%";
166 _iframe.style.height =
"100%";
167 _iframe.style.borderWidth =
"0px";
168 _iframe.frameBorder =
"0";
170 [
self _applyBackgroundColor];
172 _loadCallback =
function()
175 if (!_ignoreLoadStart)
178 [
self _startedLoading];
181 [_backwardStack addObject:_mainFrameURL];
184 _mainFrameURL = _iframe.src;
187 [_forwardStack removeAllObjects];
190 _ignoreLoadStart = NO;
194 [
self _finishedLoading];
202 if (_iframe.addEventListener)
203 _iframe.addEventListener(
"load", _loadCallback,
false);
204 else if (_iframe.attachEvent)
205 _iframe.attachEvent(
"onload", _loadCallback);
207 _frameView = [[
CPView alloc] initWithFrame:[
self bounds]];
208 [_frameView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
210 _scrollView = [[
CPScrollView alloc] initWithFrame:[
self bounds]];
211 [_scrollView setAutohidesScrollers:YES];
212 [_scrollView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
213 [_scrollView setDocumentView:_frameView];
215 _frameView._DOMElement.appendChild(_iframe);
217 [
self _updateEffectiveScrollMode];
219 [
self addSubview:_scrollView];
222 - (void)setFrameSize:(CGSize)aSize
224 [
super setFrameSize:aSize];
225 [
self _resizeWebFrame];
228 - (void)viewDidUnhide
232 [_frameView setFrameSize:[_scrollView contentSize]];
233 [
self _resizeWebFrame];
234 [
self _scheduleContentSizeCheck];
237 - (void)_attachScrollEventIfNecessary
242 var win = [
self DOMWindow];
244 if (win && win.addEventListener)
246 var scrollEventHandler =
function(anEvent)
248 var frameBounds = [
self bounds],
249 frameCenter = CGPointMake(CGRectGetMidX(frameBounds), CGRectGetMidY(frameBounds)),
250 windowOrigin = [
self convertPoint:frameCenter toView:nil],
251 globalOrigin = [[
self window] convertBaseToBridge:windowOrigin];
253 anEvent._overrideLocation = globalOrigin;
254 [[[
self window] platformWindow] scrollEvent:anEvent];
257 win.addEventListener(
"DOMMouseScroll", scrollEventHandler,
false);
258 win.addEventListener(
"wheel", scrollEventHandler,
false);
262 - (void)_resizeWebFrame
267 if (![
self _isVisible])
274 var visibleRect = [_frameView visibleRect];
275 [_frameView setFrameSize:CGSizeMake(CGRectGetMaxX(visibleRect), CGRectGetMaxY(visibleRect))];
278 var win = [
self DOMWindow];
280 if (win && win.document && win.document.body)
282 var
width = win.document.body.scrollWidth,
283 height = win.document.body.scrollHeight;
285 _iframe.setAttribute(
"width", width);
286 _iframe.setAttribute(
"height", height);
288 [_frameView setFrameSize:CGSizeMake(width, height)];
294 if (!win || !win.document)
296 CPLog.warn(
"using default size 800*1600");
297 [_frameView setFrameSize:CGSizeMake(800, 1600)];
301 [_frameView scrollRectToVisible:visibleRect];
316 - (void)setScrollMode:(
int)aScrollMode
318 if (_scrollMode == aScrollMode)
321 _scrollMode = aScrollMode;
323 [
self _updateEffectiveScrollMode];
335 - (int)effectiveScrollMode
337 return _effectiveScrollMode;
340 - (void)_updateEffectiveScrollMode
354 CPLog.warn(
self +
" unable to use CPWebViewScrollAppKit scroll mode due to same origin policy.");
358 if (_newScrollMode !== _effectiveScrollMode)
359 [
self _setEffectiveScrollMode:_newScrollMode];
362 - (void)_setEffectiveScrollMode:(
int)aScrollMode
364 _effectiveScrollMode = aScrollMode;
366 _ignoreLoadStart = YES;
367 _ignoreLoadEnd = YES;
369 var parent = _iframe.parentNode;
371 parent.removeChild(_iframe);
375 [_scrollView setHasHorizontalScroller:YES];
376 [_scrollView setHasVerticalScroller:YES];
378 _iframe.setAttribute(
"scrolling",
"no");
382 [_scrollView setHasHorizontalScroller:NO];
383 [_scrollView setHasVerticalScroller:NO];
385 _iframe.setAttribute(
"scrolling",
"no");
389 [_scrollView setHasHorizontalScroller:NO];
390 [_scrollView setHasVerticalScroller:NO];
392 _iframe.setAttribute(
"scrolling",
"auto");
394 [_frameView setFrameSize:[_scrollView bounds].size];
397 parent.appendChild(_iframe);
398 [
self _applyBackgroundColor];
400 [
self _resizeWebFrame];
403 - (void)_maybePollWebFrameSize
406 [
self _resizeWebFrame];
408 [_contentSizeCheckTimer invalidate];
430 [_frameView setFrameSize:[_scrollView contentSize]];
432 [
self _startedLoading];
434 _ignoreLoadStart = YES;
442 - (void)_loadMainFrameURL
444 [
self _startedLoading];
446 _ignoreLoadStart = YES;
448 _url = _mainFrameURL;
461 _contentIsAccessible = [cpurl _passesSameOriginPolicy];
462 [
self _updateEffectiveScrollMode];
468 else if (_html !== nil)
473 _contentIsAccessible = YES;
474 [
self _updateEffectiveScrollMode];
478 if (_loadHTMLStringTimer !== nil)
480 window.clearTimeout(_loadHTMLStringTimer);
481 _loadHTMLStringTimer = nil;
485 _loadHTMLStringTimer = window.setTimeout(
function()
487 var win = [
self DOMWindow];
493 win.document.write(_html ||
"<html><body></body></html>");
495 window.setTimeout(_loadCallback, 1);
500 - (void)_startedLoading
506 if ([_frameLoadDelegate respondsToSelector:
@selector(webView:didStartProvisionalLoadForFrame:)])
507 [_frameLoadDelegate webView:
self didStartProvisionalLoadForFrame:nil];
510 - (void)_finishedLoading
514 [
self _resizeWebFrame];
515 [
self _attachScrollEventIfNecessary];
517 [
self _scheduleContentSizeCheck];
521 if ([_frameLoadDelegate respondsToSelector:
@selector(webView:didFinishLoadForFrame:)])
522 [_frameLoadDelegate webView:
self didFinishLoadForFrame:nil];
525 - (void)_scheduleContentSizeCheck
527 [_contentSizeCheckTimer invalidate];
546 _contentSizePollCount = 0;
567 return _mainFrameURL;
578 [_backwardStack addObject:_mainFrameURL];
580 _mainFrameURL = URLString;
581 [_forwardStack removeAllObjects];
583 [
self _loadMainFrameURL];
593 if (_backwardStack.length > 0)
596 [_forwardStack addObject:_mainFrameURL];
598 _mainFrameURL = [_backwardStack lastObject];
599 [_backwardStack removeLastObject];
601 [
self _loadMainFrameURL];
616 if (_forwardStack.length > 0)
619 [_backwardStack addObject:_mainFrameURL];
621 _mainFrameURL = [_forwardStack lastObject];
622 [_forwardStack removeLastObject];
624 [
self _loadMainFrameURL];
640 return (_backwardStack.length > 0);
651 return (_forwardStack.length > 0);
654 - (WebBackForwardList)backForwardList
657 return { back: _backwardStack, forward: _forwardStack };
666 _iframe.parentNode.removeChild(_iframe);
674 - (DOMWindow)DOMWindow
678 return (_iframe.contentDocument && _iframe.contentDocument.defaultView) || _iframe.contentWindow;
695 if (!_wso || win != [_wso window])
716 return result ? String(result) : nil;
725 - (JSObject)objectByEvaluatingJavaScriptFromString:(
CPString)script
737 - (DOMCSSStyleDeclaration)computedStyleForElement:(DOMElement)element pseudoElement:(
CPString)pseudoElement
744 return win.document.defaultView.getComputedStyle(element, pseudoElement);
753 - (BOOL)drawsBackground
755 return _drawsBackground;
769 - (void)setDrawsBackground:(BOOL)drawsBackground
771 if (drawsBackground == _drawsBackground)
774 _drawsBackground = drawsBackground;
776 [
self _applyBackgroundColor];
781 [
super setBackgroundColor:aColor];
782 [
self _applyBackgroundColor];
785 - (void)_applyBackgroundColor
790 _iframe.allowtransparency = !_drawsBackground;
791 _iframe.style.backgroundColor = _drawsBackground ? [bgColor cssString] :
"transparent";
803 - (@action)takeStringURLFrom:(
id)sender
813 - (@action)goBack:(
id)sender
823 - (@action)goForward:(
id)sender
833 - (@action)stopLoading:(
id)sender
843 - (@action)reload:(
id)sender
846 if (!_url && (_html !== nil))
849 [
self _loadMainFrameURL];
858 - (@action)print:(
id)sender
866 alert(
'Please click the webpage and select "Print" from the "File" menu');
875 - (id)downloadDelegate
877 return _downloadDelegate;
880 - (void)setDownloadDelegate:(
id)anObject
882 _downloadDelegate = anObject;
885 - (id)frameLoadDelegate
887 return _frameLoadDelegate;
890 - (void)setFrameLoadDelegate:(
id)anObject
892 _frameLoadDelegate = anObject;
897 return _policyDelegate;
900 - (void)setPolicyDelegate:(
id)anObject
902 _policyDelegate = anObject;
905 - (id)resourceLoadDelegate
907 return _resourceLoadDelegate;
910 - (void)setResourceLoadDelegate:(
id)anObject
912 _resourceLoadDelegate = anObject;
920 - (void)setUIDelegate:(
id)anObject
922 _UIDelegate = anObject;
940 - (id)initWithWindow:(Window)aWindow
942 if (
self = [super init])
956 - (id)callWebScriptMethod:(
CPString)methodName withArguments:(CPArray)args
959 if (typeof _window[methodName] ==
"function")
963 return _window[methodName].apply(_window, args);
982 return _window.eval(script);
1013 self = [
super initWithCoder:aCoder];
1018 _mainFrameURL = nil;
1019 _backwardStack = [];
1024 [
self _initDOMWithFrame:[
self frame]];
1027 if (![
self backgroundColor])
1030 [
self _updateEffectiveScrollMode];
1043 var actualSubviews = _subviews;
1045 [
super encodeWithCoder:aCoder];
1046 _subviews = actualSubviews;
1060 - (BOOL)_passesSameOriginPolicy
1068 if (![
self scheme] && ![
self host] && ![
self port])
1071 return ([documentURL scheme] == [
self scheme] && [documentURL host] == [
self host] && [documentURL port] == [
self port]);