Files
cappuccino/AppKit/Platform/DOM/CPPlatformString.j
T
Ross Boucher 5366713790 Closes #429. Doesn't necessarily address the root cause however.
The problem stems from a bug in Safari which is allowing callbacks
to interrupt running code (which should be impossible in JavaScript).
The change essentially makes it so that in the case where this happens,
which is rare, we'll end up accidentally creating an extra iframe.
This isn't ideal, but it also isn't harmful, versus the current
situation where code will simply break.
2010-01-31 15:07:23 -08:00

154 lines
4.4 KiB
Plaintext

/*
* CPPlatformString.j
* AppKit
*
* Created by Francisco Tolmasky.
* Copyright 2010, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../CoreGraphics/CGGeometry.h"
var DOMSpanElement = nil,
DOMIFrameElement = nil,
DefaultFont = nil;
@implementation CPPlatformString : CPBasePlatformString
{
}
+ (void)bootstrap
{
[self createDOMElements];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(platformDidClearBodyElement:)
name:CPPlatformDidClearBodyElementNotification
object:CPPlatform];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(platformWillClearBodyElement:)
name:CPPlatformWillClearBodyElementNotification
object:CPPlatform];
}
+ (void)createDOMElements
{
if (DOMIFrameElement)
return;
DOMIFrameElement = document.createElement("iframe");
// necessary for Safari caching bug:
DOMIFrameElement.name = "iframe_" + FLOOR(RAND() * 10000);
DOMIFrameElement.style.position = "absolute";
DOMIFrameElement.style.left = "-100px";
DOMIFrameElement.style.top = "-100px";
DOMIFrameElement.style.width = "1px";
DOMIFrameElement.style.height = "1px";
DOMIFrameElement.style.borderWidth = "0px";
DOMIFrameElement.style.overflow = "hidden";
DOMIFrameElement.style.zIndex = 100000000000;
var bodyElement = [CPPlatform mainBodyElement];
bodyElement.appendChild(DOMIFrameElement);
var DOMIFrameDocument = (DOMIFrameElement.contentDocument || DOMIFrameElement.contentWindow.document);
DOMIFrameDocument.write("<html><head></head><body></body></html>");
DOMIFrameDocument.close();
// IE needs this wide <div> to prevent unwanted text wrapping:
var DOMDivElement = DOMIFrameDocument.createElement("div");
DOMDivElement.style.position = "absolute";
DOMDivElement.style.width = "100000px";
DOMIFrameDocument.body.appendChild(DOMDivElement);
DOMSpanElement = DOMIFrameDocument.createElement("span");
DOMSpanElement.style.position = "absolute";
DOMSpanElement.style.whiteSpace = "pre";
DOMSpanElement.style.visibility = "visible";
DOMSpanElement.style.padding = "0px";
DOMSpanElement.style.margin = "0px";
DOMDivElement.appendChild(DOMSpanElement);
}
+ (void)removeDOMElements
{
var iframe = DOMIFrameElement,
bodyElement = [CPPlatform mainBodyElement];
DOMSpanElement = nil;
DOMIFrameElement = nil;
bodyElement.removeChild(iframe);
}
+ (void)platformDidClearBodyElement:(CPNotification)aNotification
{
[self createDOMElements];
}
+ (void)platformWillClearBodyElement:(CPNotification)aNotification
{
[self removeDOMElements];
}
+ (CGSize)sizeOfString:(CPString)aString withFont:(CPFont)aFont forWidth:(float)aWidth
{
if (!aFont)
{
if (!DefaultFont)
DefaultFont = [CPFont systemFontOfSize:12.0];
aFont = DefaultFont;
}
if (!DOMIFrameElement)
[self createDOMElements];
var style = DOMSpanElement.style;
if (!aWidth)
{
style.width = "";
style.whiteSpace = "pre";
}
else
{
style.width = ROUND(aWidth) + "px";
style.wordWrap = "break-word";
style.whiteSpace = "-o-pre-wrap";
style.whiteSpace = "-pre-wrap";
style.whiteSpace = "-moz-pre-wrap";
style.whiteSpace = "pre-wrap";
}
style.font = [aFont cssString];
if (CPFeatureIsCompatible(CPJavascriptInnerTextFeature))
DOMSpanElement.innerText = aString;
else if (CPFeatureIsCompatible(CPJavascriptTextContentFeature))
DOMSpanElement.textContent = aString;
return _CGSizeMake(DOMSpanElement.clientWidth, DOMSpanElement.clientHeight);
}
@end