Advertisement
Shinmera

lQuery Wrap-All

Aug 23rd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.87 KB | None | 0 0
  1. (define-node-list-function wrap-all (working-nodes html-or-nodes)
  2.   "Wrap an HTML structure around all elements inside their next (common) parent."
  3.   (loop with wrapper-template = (first (nodes-or-build html-or-nodes))
  4.      with parentmap = (make-hash-table)
  5.      for node in working-nodes
  6.      for parent = (dom:parent-node node)
  7.      do (setf (gethash parent parentmap)
  8.               (append (gethash parent parentmap) (list node)))
  9.      finally (loop for parent being the hash-keys of parentmap
  10.                 for children being the hash-values of parentmap
  11.                 for wrapper = (dom:clone-node wrapper-template T)
  12.                 for index = (first (sort (nodefun-child-index children) #'<))
  13.                 do (buildnode:insert-nodes (nodefun-deepest wrapper) 0 children)
  14.                    (buildnode:insert-nodes parent index wrapper)))
  15.   working-nodes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement