Files
cappuccino/AppKit/Platform/DOM/CPPlatformString.j
T
Ross Boucher 7ce72a31c5 Rather than throwing out the entire cappuccino body, throw out a specific div if that div is available.
("cappuccino-body"). This let's external libraries, esp. flash ones, add things to the body without
Cappuccino getting rid of them as soon as the page loads.

Closes #421.
2010-01-22 19:40:33 -08:00

130 lines
3.7 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,
DefaultFont = nil;
@implementation CPPlatformString : CPBasePlatformString
{
}
+ (void)bootstrap
{
[self createDOMElements];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(platformDidClearBodyElement:)
name:CPPlatformDidClearBodyElementNotification
object:CPPlatform];
}
+ (void)createDOMElements
{
var DOMIFrameElement = document.createElement("iframe");
DOMIFrameElement.name = name = "iframe_" + FLOOR(RAND() * 10000);
DOMIFrameElement.style.position = "absolute";
DOMIFrameElement.style.left = "-1000px";
DOMIFrameElement.style.top = "-1000px";
// TODO: investigate a better way to make this work in IE:
DOMIFrameElement.style.width = "1000px";
DOMIFrameElement.style.height = "1000px";
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();
DOMSpanElement = DOMIFrameDocument.createElement("span");
DOMSpanElement.style.position = "absolute";
DOMSpanElement.style.whiteSpace = "pre";
DOMSpanElement.style.visibility = "visible";
DOMSpanElement.style.padding = "0px";
DOMSpanElement.style.margin = "0px";
DOMIFrameDocument.body.appendChild(DOMSpanElement);
}
+ (void)platformDidClearBodyElement:(CPNotification)aNotification
{
[self createDOMElements];
}
+ (CGSize)sizeOfString:(CPString)aString withFont:(CPFont)aFont forWidth:(float)aWidth
{
if (!aFont)
{
if (!DefaultFont)
DefaultFont = [CPFont systemFontOfSize:12.0];
aFont = DefaultFont;
}
var style = DOMSpanElement.style;
if (aWidth === NULL)
{
style.width = "";
style.whiteSpace = "pre";
}
else
{
style.width = ROUND(aWidth) + "px";
if (document.attachEvent)
style.wordWrap = "break-word";
else
{
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