initial test cases on views
This commit is contained in:
parent
cc4bcb341e
commit
5d62bbd9b6
3
tests/more-tests/view.expected
Normal file
3
tests/more-tests/view.expected
Normal file
|
@ -0,0 +1,3 @@
|
|||
""
|
||||
"some text"
|
||||
(html (head) (body (p "hello world, this is a test") (div (@ (id "a div")) "some text")))
|
21
tests/more-tests/view.rkt
Normal file
21
tests/more-tests/view.rkt
Normal file
|
@ -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")))
|
|
@ -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")
|
||||
|
|
|
@ -74,4 +74,6 @@
|
|||
xexp?
|
||||
xexp->dom
|
||||
|
||||
view->xexp
|
||||
|
||||
))
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
}());
|
|
@ -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."))
|
||||
|
|
Loading…
Reference in New Issue
Block a user