From fe10c99d1b1668701fda675d0f33d9a1070195b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrg=20Lehni?= <juerg@scratchdisk.com>
Date: Wed, 12 Jun 2013 19:30:23 -0700
Subject: [PATCH] Implement standard behavior of #_hitTest() for items without
 children, based on #_contains().

This should cover the minimum of what's needed for PointText.
---
 src/item/Item.js       | 6 ++++++
 src/item/Shape.js      | 5 ++---
 src/project/Project.js | 1 +
 test/lib/helpers.js    | 2 ++
 test/tests/TextItem.js | 8 +++++++-
 5 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/item/Item.js b/src/item/Item.js
index e861f951..503c0922 100644
--- a/src/item/Item.js
+++ b/src/item/Item.js
@@ -1322,6 +1322,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
 		}
 		// We only implement it here for items with rectangular content,
 		// for anything else we need to override #contains()
+		// TODO: There currently is no caching for the results of direct calls
+		// to this._getBounds('getBounds') (without the application of the
+		// internal matrix). Performance improvements could be achieved if
+		// these were cached too. See #_getCachedBounds().
 		return point.isInside(this._getBounds('getBounds'));
 	},
 
@@ -1420,6 +1424,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
 				var res = this._children[i].hitTest(point, options);
 				if (res) return res;
 			}
+		} else if (this.hasFill() && this._contains(point)) {
+			return new HitResult('fill', this);
 		}
 	},
 
diff --git a/src/item/Shape.js b/src/item/Shape.js
index 100c1c66..4a9b4fe1 100644
--- a/src/item/Shape.js
+++ b/src/item/Shape.js
@@ -81,10 +81,9 @@ var Shape = Item.extend(/** @lends Shape# */{
 		return matrix ? matrix._transformBounds(rect) : rect;
 	},
 
-	_hitTest: function(point, options) {
-		if (this.hasFill() && this.contains(point))
-			return new HitResult('fill', this);
+	_hitTest: function _hitTest(point, options) {
 		// TODO: Implement stroke!
+		return _hitTest.base.apply(this, arguments);
 	},
 
 	statics: {
diff --git a/src/project/Project.js b/src/project/Project.js
index 8775d417..dfeb4830 100644
--- a/src/project/Project.js
+++ b/src/project/Project.js
@@ -331,6 +331,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
 					if (item._boundsSelected) {
 						// We need to call the internal _getBounds, to get non-
 						// transformed bounds.
+						// TODO: Implement caching for these too!
 						var coords = mx._transformCorners(
 								item._getBounds('getBounds'));
 						// Now draw a rectangle that connects the transformed
diff --git a/test/lib/helpers.js b/test/lib/helpers.js
index 7170c747..37556bc8 100644
--- a/test/lib/helpers.js
+++ b/test/lib/helpers.js
@@ -12,6 +12,8 @@
 
 // Register a jsDump parser for Base.
 QUnit.jsDump.setParser('Base', function (obj, stack) {
+	// Just compare the string representation of classes inheriting from Base,
+	// since they hide the internal values.
 	return obj.toString();
 });
 
diff --git a/test/tests/TextItem.js b/test/tests/TextItem.js
index fb43ba15..b3fe64f6 100644
--- a/test/tests/TextItem.js
+++ b/test/tests/TextItem.js
@@ -14,9 +14,15 @@ module('TextItem');
 
 test('PointText', function() {
 	var text = new PointText({
+		font: 'Arial',
+		fontSize: 14,
 		point: [100, 100],
 		content: 'Hello World!'
 	});
-	equals(text.point, { x: 100, y: 100 });
 	equals(text.fillColor, { red: 0, green: 0, blue: 0 }, 'text.fillColor should be black by default');
+	equals(text.point, { x: 100, y: 100 });
+	equals(text.bounds.point, { x: 100, y: 87.4 });
+	equals(function() {
+		return text.hitTest(text.bounds.center) != null;
+	}, true);
 });