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:(CPRect)frameRect frameName:(
CPString)frameName groupName:(
CPString)groupName
129 if (
self = [
self initWithFrame:frameRect])
131 _iframe.name = frameName;
137 - (id)initWithFrame:(CPRect)aFrame
139 if (
self = [super initWithFrame:aFrame])
145 _contentIsAccessible = YES;
148 _drawsBackground = YES;
152 [
self _initDOMWithFrame:aFrame];
158 - (id)_initDOMWithFrame:(CPRect)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];
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:(CPSize)aSize
225 [
self _resizeWebFrame];
228 - (void)viewDidUnhide
232 [_frameView setFrameSize:[_scrollView contentSize]];
233 [
self _resizeWebFrame];
234 [
self _scheduleContentSizeCheck];
237 - (void)_attachScrollEventIfNecessary
243 try { win = [
self DOMWindow]; }
catch (e) {}
245 if (win && win.addEventListener)
247 var scrollEventHandler =
function(anEvent)
249 var frameBounds = [
self bounds],
250 frameCenter = CGPointMake(CGRectGetMidX(frameBounds), CGRectGetMidY(frameBounds)),
251 windowOrigin = [
self convertPoint:frameCenter toView:nil],
252 globalOrigin = [[
self window] convertBaseToBridge:windowOrigin];
254 anEvent._overrideLocation = globalOrigin;
255 [[[
self window] platformWindow] scrollEvent:anEvent];
258 win.addEventListener(
"DOMMouseScroll", scrollEventHandler,
false);
262 - (void)_resizeWebFrame
267 if (![
self _isVisible])
274 var visibleRect = [_frameView visibleRect];
275 [_frameView setFrameSize:CGSizeMake(CGRectGetMaxX(visibleRect), CGRectGetMaxY(visibleRect))];
279 try { win = [
self DOMWindow]; }
catch (e) {}
281 if (win && win.document && win.document.body)
283 var
width = win.document.body.scrollWidth,
284 height = win.document.body.scrollHeight;
286 _iframe.setAttribute(
"width", width);
287 _iframe.setAttribute(
"height", height);
289 [_frameView setFrameSize:CGSizeMake(width, height)];
295 if (!win || !win.document)
297 CPLog.warn(
"using default size 800*1600");
298 [_frameView setFrameSize:CGSizeMake(800, 1600)];
302 [_frameView scrollRectToVisible:visibleRect];
317 - (void)setScrollMode:(
int)aScrollMode
319 if (_scrollMode == aScrollMode)
322 _scrollMode = aScrollMode;
324 [
self _updateEffectiveScrollMode];
336 - (int)effectiveScrollMode
338 return _effectiveScrollMode;
341 - (void)_updateEffectiveScrollMode
355 CPLog.warn(
self +
" unable to use CPWebViewScrollAppKit scroll mode due to same origin policy.");
359 if (_newScrollMode !== _effectiveScrollMode)
360 [
self _setEffectiveScrollMode:_newScrollMode];
363 - (void)_setEffectiveScrollMode:(
int)aScrollMode
365 _effectiveScrollMode = aScrollMode;
367 _ignoreLoadStart = YES;
368 _ignoreLoadEnd = YES;
370 var parent = _iframe.parentNode;
372 parent.removeChild(_iframe);
376 [_scrollView setHasHorizontalScroller:YES];
377 [_scrollView setHasVerticalScroller:YES];
379 _iframe.setAttribute(
"scrolling",
"no");
383 [_scrollView setHasHorizontalScroller:NO];
384 [_scrollView setHasVerticalScroller:NO];
386 _iframe.setAttribute(
"scrolling",
"no");
390 [_scrollView setHasHorizontalScroller:NO];
391 [_scrollView setHasVerticalScroller:NO];
393 _iframe.setAttribute(
"scrolling",
"auto");
395 [_frameView setFrameSize:[_scrollView bounds].size];
398 parent.appendChild(_iframe);
399 [
self _applyBackgroundColor];
401 [
self _resizeWebFrame];
404 - (void)_maybePollWebFrameSize
407 [
self _resizeWebFrame];
409 [_contentSizeCheckTimer invalidate];
431 [_frameView setFrameSize:[_scrollView contentSize]];
433 [
self _startedLoading];
435 _ignoreLoadStart = YES;
443 - (void)_loadMainFrameURL
445 [
self _startedLoading];
447 _ignoreLoadStart = YES;
449 _url = _mainFrameURL;
462 _contentIsAccessible = [cpurl _passesSameOriginPolicy];
463 [
self _updateEffectiveScrollMode];
469 else if (_html !== nil)
474 _contentIsAccessible = YES;
475 [
self _updateEffectiveScrollMode];
479 if (_loadHTMLStringTimer !== nil)
481 window.clearTimeout(_loadHTMLStringTimer);
482 _loadHTMLStringTimer = nil;
486 _loadHTMLStringTimer = window.setTimeout(
function()
488 var win = [
self DOMWindow];
494 win.document.write(_html ||
"<html><body></body></html>");
496 window.setTimeout(_loadCallback, 1);
501 - (void)_startedLoading
507 if ([_frameLoadDelegate respondsToSelector:
@selector(webView:didStartProvisionalLoadForFrame:)])
508 [_frameLoadDelegate webView:
self didStartProvisionalLoadForFrame:nil];
511 - (void)_finishedLoading
515 [
self _resizeWebFrame];
516 [
self _attachScrollEventIfNecessary];
518 [
self _scheduleContentSizeCheck];
522 if ([_frameLoadDelegate respondsToSelector:
@selector(webView:didFinishLoadForFrame:)])
523 [_frameLoadDelegate webView:
self didFinishLoadForFrame:nil];
526 - (void)_scheduleContentSizeCheck
528 [_contentSizeCheckTimer invalidate];
547 _contentSizePollCount = 0;
568 return _mainFrameURL;
579 [_backwardStack addObject:_mainFrameURL];
580 _mainFrameURL = URLString;
581 [_forwardStack removeAllObjects];
583 [
self _loadMainFrameURL];
593 if (_backwardStack.length > 0)
596 [_forwardStack addObject:_mainFrameURL];
597 _mainFrameURL = [_backwardStack lastObject];
598 [_backwardStack removeLastObject];
600 [
self _loadMainFrameURL];
614 if (_forwardStack.length > 0)
617 [_backwardStack addObject:_mainFrameURL];
618 _mainFrameURL = [_forwardStack lastObject];
619 [_forwardStack removeLastObject];
621 [
self _loadMainFrameURL];
636 return (_backwardStack.length > 0);
647 return (_forwardStack.length > 0);
650 - (WebBackForwardList)backForwardList
653 return { back: _backwardStack, forward: _forwardStack };
662 _iframe.parentNode.removeChild(_iframe);
670 - (DOMWindow)DOMWindow
672 return (_iframe.contentDocument && _iframe.contentDocument.defaultView) || _iframe.contentWindow;
683 if (!_wso || win != [_wso window])
703 return result ? String(result) : nil;
712 - (JSObject)objectByEvaluatingJavaScriptFromString:(
CPString)script
724 - (DOMCSSStyleDeclaration)computedStyleForElement:(DOMElement)element pseudoElement:(
CPString)pseudoElement
730 return win.document.defaultView.getComputedStyle(element, pseudoElement);
739 - (BOOL)drawsBackground
741 return _drawsBackground;
755 - (void)setDrawsBackground:(BOOL)drawsBackground
757 if (drawsBackground == _drawsBackground)
759 _drawsBackground = drawsBackground;
761 [
self _applyBackgroundColor];
767 [
self _applyBackgroundColor];
770 - (void)_applyBackgroundColor
775 _iframe.allowtransparency = !_drawsBackground;
776 _iframe.style.backgroundColor = _drawsBackground ? [bgColor cssString] :
"transparent";
788 - (@action)takeStringURLFrom:(
id)sender
798 - (@action)goBack:(
id)sender
808 - (@action)goForward:(
id)sender
818 - (@action)stopLoading:(
id)sender
828 - (@action)reload:(
id)sender
831 if (!_url && (_html !== nil))
834 [
self _loadMainFrameURL];
843 - (@action)print:(
id)sender
851 alert(
'Please click the webpage and select "Print" from the "File" menu');
860 - (id)downloadDelegate
862 return _downloadDelegate;
864 - (void)setDownloadDelegate:(
id)anObject
866 _downloadDelegate = anObject;
868 - (id)frameLoadDelegate
870 return _frameLoadDelegate;
872 - (void)setFrameLoadDelegate:(
id)anObject
874 _frameLoadDelegate = anObject;
878 return _policyDelegate;
880 - (void)setPolicyDelegate:(
id)anObject
882 _policyDelegate = anObject;
884 - (id)resourceLoadDelegate
886 return _resourceLoadDelegate;
888 - (void)setResourceLoadDelegate:(
id)anObject
890 _resourceLoadDelegate = anObject;
896 - (void)setUIDelegate:(
id)anObject
898 _UIDelegate = anObject;
916 - (id)initWithWindow:(Window)aWindow
918 if (
self = [super init])
934 if (typeof _window[methodName] ==
"function")
937 return _window[methodName].apply(args);
953 return _window.eval(script);
992 [
self _initDOMWithFrame:[
self frame]];
995 if (![
self backgroundColor])
998 [
self _updateEffectiveScrollMode];
1011 var actualSubviews = _subviews;
1014 _subviews = actualSubviews;
1028 - (BOOL)_passesSameOriginPolicy
1035 if (![
self scheme] && ![
self host] && ![
self port])
1038 return ([documentURL scheme] == [
self scheme] && [documentURL host] == [
self host] && [documentURL port] == [
self port]);