Skip to content

Instantly share code, notes, and snippets.

@boucher
Created January 31, 2010 22:54
Show Gist options
  • Save boucher/291293 to your computer and use it in GitHub Desktop.
Save boucher/291293 to your computer and use it in GitHub Desktop.
diff --git a/AppKit/Platform/DOM/CPPlatform.j b/AppKit/Platform/DOM/CPPlatform.j
index 47af4b1..60ac591 100644
--- a/AppKit/Platform/DOM/CPPlatform.j
+++ b/AppKit/Platform/DOM/CPPlatform.j
@@ -21,6 +21,7 @@
*/
CPPlatformDidClearBodyElementNotification = @"CPPlatformDidClearBodyElementNotification";
+CPPlatformWillClearBodyElementNotification = @"CPPlatformWillClearBodyElementNotification";
var screenNeedsInitialization = NO,
mainBodyElement = nil;
@@ -91,6 +92,10 @@ var screenNeedsInitialization = NO,
screenNeedsInitialization = NO;
+ [[CPNotificationCenter defaultCenter]
+ postNotificationName:CPPlatformWillClearBodyElementNotification
+ object:self];
+
var bodyElement = [self mainBodyElement];
// Get rid of any of the original contents of the page.
diff --git a/AppKit/Platform/DOM/CPPlatformString.j b/AppKit/Platform/DOM/CPPlatformString.j
index e0431b0..381fe6a 100644
--- a/AppKit/Platform/DOM/CPPlatformString.j
+++ b/AppKit/Platform/DOM/CPPlatformString.j
@@ -22,8 +22,8 @@
#include "../CoreGraphics/CGGeometry.h"
-
var DOMSpanElement = nil,
+ DOMIFrameElement = nil,
DefaultFont = nil;
@implementation CPPlatformString : CPBasePlatformString
@@ -39,11 +39,17 @@ var DOMSpanElement = nil,
selector:@selector(platformDidClearBodyElement:)
name:CPPlatformDidClearBodyElementNotification
object:CPPlatform];
+
+ [[CPNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(platformWillClearBodyElement:)
+ name:CPPlatformWillClearBodyElementNotification
+ object:CPPlatform];
}
+ (void)createDOMElements
{
- var DOMIFrameElement = document.createElement("iframe");
+ DOMIFrameElement = document.createElement("iframe");
// necessary for Safari caching bug:
DOMIFrameElement.name = "iframe_" + FLOOR(RAND() * 10000);
DOMIFrameElement.style.position = "absolute";
@@ -80,11 +86,24 @@ var DOMSpanElement = nil,
DOMDivElement.appendChild(DOMSpanElement);
}
++ (void)removeDOMElements
+{
+ var bodyElement = [CPPlatform mainBodyElement];
+ bodyElement.removeChild(DOMIFrameElement);
+ DOMSpanElement = nil;
+ DOMIFrameElement = nil;
+}
+
+ (void)platformDidClearBodyElement:(CPNotification)aNotification
{
[self createDOMElements];
}
++ (void)platformWillClearBodyElement:(CPNotification)aNotification
+{
+ [self removeDOMElements];
+}
+
+ (CGSize)sizeOfString:(CPString)aString withFont:(CPFont)aFont forWidth:(float)aWidth
{
if (!aFont)
@@ -95,39 +114,43 @@ var DOMSpanElement = nil,
aFont = DefaultFont;
}
+ var shouldRemove = !DOMSpanElement;
+ if (shouldRemove)
+ [self createDOMElements];
+
var style = DOMSpanElement.style;
- if (aWidth === NULL)
+ if (!aWidth)
{
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.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);
+ if (DOMSpanElement.clientWidth === 0 || DOMSpanElement.clientHeight === 0)
+ {alert('returning size 0'); debugger;}
+
+ var size = _CGSizeMake(DOMSpanElement.clientWidth, DOMSpanElement.clientHeight);
+
+ if (shouldRemove)
+ [self removeDOMElements];
+
+ return size;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment