Passing an unknown element id to core/spi.wrap returns null, rather than throwing an exception
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/05bded12 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/05bded12 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/05bded12 Branch: refs/heads/5.4-js-rewrite Commit: 05bded126e0efc58f7ef4cd85ceed4f9349a7e39 Parents: 48127ff Author: Howard M. Lewis Ship <[email protected]> Authored: Fri Aug 10 18:01:23 2012 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Fri Aug 10 18:01:23 2012 -0700 ---------------------------------------------------------------------- .../coffeescript/META-INF/modules/core/spi.coffee | 21 ++++++++++----- tapestry-core/src/test/app1/JavaScriptTests.tml | 7 +++++ .../integration/app1/pages/test-pubsub.coffee | 10 ++----- .../integration/app1/pages/test-spi.coffee | 12 ++++++++ .../integration/app1/pages/JavaScriptTests.java | 2 +- 5 files changed, 37 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/05bded12/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee index 5f9dfdc..a7a946b 100644 --- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee +++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee @@ -145,12 +145,12 @@ define ["_", "prototype"], (_) -> # Exposes the original element as property `element`. class ElementWrapper - constructor: (element) -> - @element = $(element) + # Passed the DOM Element + constructor: (@element) -> - # Hides the wrapped element, setting its display to 'none'. - # - # Returns this ElementWrapper. + # Hides the wrapped element, setting its display to 'none'. + # + # Returns this ElementWrapper. hide: -> @element.hide() this @@ -406,10 +406,17 @@ define ["_", "prototype"], (_) -> return new EventHandler(elements, (split events), match, handler) # Returns an ElementWrapper for the provided DOM element that includes key behaviors: + # # * element - a DOM element, or the window, or the unique id of a DOM element - # Returns the ElementWrapper. + # + # Returns the ElementWrapper, or null if no DOM element with the given id exists. wrap: (element) -> - throw new Error("Attempt to wrap a null DOM element") unless element + if _.isString element + element = $ element + return null unless element + else + throw new Error("Attempt to wrap a null DOM element") unless element + new ElementWrapper element # Returns a wrapped version of the document.body element. Care must be take to not invoke this function before the http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/05bded12/tapestry-core/src/test/app1/JavaScriptTests.tml ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/app1/JavaScriptTests.tml b/tapestry-core/src/test/app1/JavaScriptTests.tml index fe0ba07..c2aa34e 100644 --- a/tapestry-core/src/test/app1/JavaScriptTests.tml +++ b/tapestry-core/src/test/app1/JavaScriptTests.tml @@ -12,5 +12,12 @@ <t:pagelink page="index">back to index</t:pagelink> </li> </ul> + + +<!-- The rest are test elements for the core/spi tests, etc. --> + +<div id="spi-test1"/> + + </body> </html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/05bded12/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-pubsub.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-pubsub.coffee b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-pubsub.coffee index b7b6e62..c0e3b71 100644 --- a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-pubsub.coffee +++ b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-pubsub.coffee @@ -1,6 +1,6 @@ -module "PubSub" - require ["core/pubsub"], (pubsub) -> + module "core/pubsub" + test "export aliases", -> ok pubsub.on is pubsub.respondTo, "on and respondTo" ok pubsub.off is pubsub.stopResponding, "off and stopResponding" @@ -74,7 +74,6 @@ require ["core/pubsub"], (pubsub) -> deepEqual log, ["b:beta-second"], "only 'beta' responder invoked after removal" test "respondFirst is invoked first", -> - log = [] pubsub.on "stim", -> log.push "alpha" @@ -82,7 +81,4 @@ require ["core/pubsub"], (pubsub) -> pubsub.fire "stim" - deepEqual log, ["bravo", "alpha"], "first responder invoked first" - - - + deepEqual log, ["bravo", "alpha"], "first responder invoked first" \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/05bded12/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee new file mode 100644 index 0000000..9b88524 --- /dev/null +++ b/tapestry-core/src/test/coffeescript/org/apache/tapestry5/integration/app1/pages/test-spi.coffee @@ -0,0 +1,12 @@ +require ["core/spi"], (spi) -> + module "core/spi" + + test "get wrapped element by id", -> + e = spi.wrap "spi-test1" + + ok e != null, "element found and wrapped" + + test "get wrapped element by unknown id is null", -> + e = spi.wrap "spi-does-not-exist-element" + + ok e == null, "element not found and null" \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/05bded12/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java index 1568180..9c98348 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.java @@ -3,7 +3,7 @@ package org.apache.tapestry5.integration.app1.pages; import org.apache.tapestry5.annotations.Import; @Import(library = { - "context:qunit/qunit-1.9.0.js", "test-pubsub.js"}, stylesheet = "context:qunit/qunit-1.9.0.css") + "context:qunit/qunit-1.9.0.js", "test-pubsub.js", "test-spi.js"}, stylesheet = "context:qunit/qunit-1.9.0.css") public class JavaScriptTests { }
