#lang scheme/base
(require srfi/13
"base.ss"
"core.ss")
(define (url-ref)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium history current))))))
(define (title-ref)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getTitle))))))
(define (inner-html-ref selector)
(normalize-html-case
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getInnerHTML) ,selector))))))
(define (inner-html-ref* selector)
(map normalize-html-case
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllInnerHTML) ,selector))))))
(define (text-content-ref selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getTextContent) ,selector)))))
(define (text-content-ref* selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllTextContent) ,selector)))))
(define-syntax-rule (js-ref expr)
(respond/expr
(lambda (embed-url)
(js ((function ()
(with (!dot Delirium (getWindow))
(return expr))))))))
(define (jquery-path-ref selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getJQueryReference) ,selector)))))
(define (jquery-path-ref* selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllJQueryReferences) ,selector)))))
(define (xpath-supported?)
(js-ref (!dot Delirium xPathSupported)))
(define (xpath-path-ref selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getXPathReference) ,selector)))))
(define (xpath-path-ref* selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllXPathReferences) ,selector)))))
(define (normalize-html-case html)
(and html (string-trim-both (regexp-replace* #px"[<]/?[^\\s]+" html string-downcase))))
(provide js-ref)
(provide/contract
[url-ref (-> string?)]
[title-ref (-> string?)]
[inner-html-ref (-> javascript-expression? (or/c string? #f))]
[inner-html-ref* (-> javascript-expression? (listof string?))]
[text-content-ref (-> javascript-expression? (or/c string? #f))]
[text-content-ref* (-> javascript-expression? (listof string?))]
[jquery-path-ref (-> javascript-expression? (or/c string? #f))]
[jquery-path-ref* (-> javascript-expression? (listof string?))]
[xpath-supported? (-> boolean?)]
[xpath-path-ref (-> javascript-expression? (or/c string? #f))]
[xpath-path-ref* (-> javascript-expression? (listof string?))])