lQuery Syntax Macro (v3)
Shinmera Jun 23rd, 2013 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- (defmacro $ (&rest actions)
- "The main lQuery function to select and manipulate the DOM. The base syntax is as follows:
- ($ \"selector\" (function args*)*)
- Strings will evaluate to selectors and always act on the current set of elements.
- Function pointers will be called with the current set of elements passed to the function.
- A single element or a list replaces the current set of elements.
- An inline list represents a function call, although nodefun- and lquery functions take precedence.
- ($ (serialize)) evaluates (basically) to (nodefun-serialize working-nodes). However, if the named
- function is not a nodefun, packages are searched for a matching symbol in the following order:
- lquery, *PACKAGE*, cl. If the function cannot be found in any of those, it is silently dropped."
- `(let ((working-nodes (list *LQUERY-MASTER-DOCUMENT*)))
- ,@(loop for action in actions collect
- `(setf working-nodes
- ;Apparently function pointers are lists during macro expansion time so we need to catch that case.
- ,(if (and (listp action) (not (eq (first action) 'FUNCTION)))
- (let ((first (car action)))
- (cond ((dom:node-p first) action)
- ((find-symbol (mkstr 'nodefun- first) :lquery)
- `(,(find-symbol (mkstr 'nodefun- first) :lquery)
- ,@(cdr action)))
- (T (loop with name = (mkstr first)
- for package in (list :lquery *PACKAGE* :cl)
- for symbol = (find-symbol name package)
- if symbol
- do (return `(,symbol ,@(cdr action)))))))
- `($-helper ,action :working-nodes working-nodes)))))
- (defgeneric $-helper (action &key &allow-other-keys)
- (:documentation "Helper function to determine what to do with a given variable at execution of the $ statement."))
- (defmethod $-helper (action &key)
- (defmethod $-helper ((action function) &key working-nodes)
- (funcall action working-nodes))
- (defmethod $-helper ((action string) &key working-nodes)
- (css:query action working-nodes))
- (defmethod $-helper ((action dom:node) &key)
- (list action))
RAW Paste Data