From 5d62bbd9b62fc3752db42c730a2c05281309ce17 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Wed, 7 Sep 2011 16:22:23 -0400 Subject: [PATCH] initial test cases on views --- tests/more-tests/view.expected | 3 ++ tests/more-tests/view.rkt | 21 +++++++++++ tests/run-more-tests.rkt | 1 + web-world/impl.rkt | 2 + web-world/js-impl.js | 69 ++++++++++++++++++++++++++++++++++ web-world/racket-impl.rkt | 8 +++- 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/more-tests/view.expected create mode 100644 tests/more-tests/view.rkt diff --git a/tests/more-tests/view.expected b/tests/more-tests/view.expected new file mode 100644 index 0000000..e66ca31 --- /dev/null +++ b/tests/more-tests/view.expected @@ -0,0 +1,3 @@ +"" +"some text" +(html (head) (body (p "hello world, this is a test") (div (@ (id "a div")) "some text"))) diff --git a/tests/more-tests/view.rkt b/tests/more-tests/view.rkt new file mode 100644 index 0000000..57aeac5 --- /dev/null +++ b/tests/more-tests/view.rkt @@ -0,0 +1,21 @@ +#lang planet dyoo/whalesong + +(require (planet dyoo/whalesong/web-world)) + +(define view (->view (xexp->dom `(html (head) + (body (p "hello world, this is a test") + (div (@ (id "a div")))))))) +(define new-view + (view-focus view "a div")) + +(view-text new-view) ;; should be "" + +(define updated-new-view + (update-view-text new-view "some text")) + +(view-text updated-new-view) ;; should be "some text" + +(view->xexp (view-up (view-up updated-new-view))) +;; should be: +; (html (head) (body (p "hello world, this is a test") +; (div (@ (id "a div")) "some text"))) \ No newline at end of file diff --git a/tests/run-more-tests.rkt b/tests/run-more-tests.rkt index 6c21fbb..4a4e2a9 100644 --- a/tests/run-more-tests.rkt +++ b/tests/run-more-tests.rkt @@ -26,3 +26,4 @@ (test "more-tests/hello-bf.rkt") (test "more-tests/conform.rkt") (test "more-tests/earley.rkt") +(test "more-tests/view.rkt") diff --git a/web-world/impl.rkt b/web-world/impl.rkt index 0a0a896..2856220 100644 --- a/web-world/impl.rkt +++ b/web-world/impl.rkt @@ -74,4 +74,6 @@ xexp? xexp->dom + view->xexp + )) diff --git a/web-world/js-impl.js b/web-world/js-impl.js index 3717fe0..42292af 100644 --- a/web-world/js-impl.js +++ b/web-world/js-impl.js @@ -100,6 +100,10 @@ var isMockView = plt.baselib.makeClassPredicate(MockView); + MockView.prototype.toString = function() { + return "<#view>"; + }; + MockView.prototype.getPendingActions = function() { return plt.baselib.lists.listToArray(this.pendingActions).reverse(); }; @@ -1282,6 +1286,61 @@ return false; }; + var domToXexp = function(dom) { + var child, attrs, name, convertedChildren, i; + if (dom.nodeType === 1) { + attrs = plt.baselib.lists.EMPTY; + name = plt.baselib.symbols.makeSymbol(dom.nodeName.toLowerCase()); + child = dom.firstChild; + convertedChildren = plt.baselib.lists.EMPTY; + + for (i = 0; i < dom.attributes.length; i++) { + attrs = plt.baselib.lists.makePair( + plt.baselib.lists.makeList(plt.baselib.symbols.makeSymbol(dom.attributes[i].nodeName), + dom.attributes[i].nodeValue), + attrs); + } + while(child !== null) { + if (child.nodeType === 1) { + convertedChildren = + plt.baselib.lists.makePair( + domToXexp(child), + convertedChildren); + } else if (child.nodeType === 3) { + convertedChildren = plt.baselib.lists.makePair( + domToXexp(child), + convertedChildren); + } + // Ignore other types. + child = child.nextSibling; + } + + if (attrs === plt.baselib.lists.EMPTY) { + return plt.baselib.lists.makePair( + name, + plt.baselib.lists.reverse(convertedChildren)); + } else { + return plt.baselib.lists.makePair( + name, + plt.baselib.lists.makePair( + plt.baselib.lists.makePair(plt.baselib.symbols.makeSymbol("@"), + attrs), + plt.baselib.lists.reverse(convertedChildren))); + } + } else if (dom.nodeType === 3) { + return dom.nodeValue; + } else { + // If we can't convert it, return false. + return false; + } + }; + + + + + + + @@ -1734,5 +1793,15 @@ }); + EXPORTS['view->xexp'] = makePrimitiveProcedure( + 'view->xexp', + 1, + function(MACHINE) { + var mockView = checkMockView(MACHINE, 'view-hide', 0); + var domNode = mockView.cursor.top().node; + return domToXexp(domNode); + }); + + ////////////////////////////////////////////////////////////////////// }()); \ No newline at end of file diff --git a/web-world/racket-impl.rkt b/web-world/racket-impl.rkt index bdd1202..325da57 100644 --- a/web-world/racket-impl.rkt +++ b/web-world/racket-impl.rkt @@ -28,7 +28,9 @@ open-output-element xexp? - xexp->dom) + xexp->dom + view->xexp + ) (define (big-bang world . handlers) @@ -169,3 +171,7 @@ (define (xexp->dom x) (error 'xexp->dom "Please run in JavaScript context.")) + + +(define (view->xexp x) + (error 'view->xexp "Please run in JavaScript context."))