zubat
(require zubat) | package: zubat |
1 介绍
纵观现有的库,尚未发现能足够好用的HTML5解释器,sxml是一个XML解析库,它提供了基本方法,一些HTML5特有的解析方式却需要重新实现,为了查找到一个元素需要费一番周折。
在sxml的基础上,实现了一些比较常用的功能,例如document.getElementById。 使用该库需要注意以下几点:
zubat没有document.querySelector,只有类似的node-select。
zubat内部命名可能会与sxml重名,最好能通过前缀区分它们。
2 解析它!
zubat解析利用html-parsing这个库进行解析。
procedure
(input-port->sxml port) → sxml:element?
port : input-port?
procedure
(file->sxml file-path) → sxml:element?
file-path : path-string?
3 元素属性
procedure
el : sxml:element? attr : symbol?
(define el '(main (@ (id "main-id")) "main text")) (node-attr el 'id) ;; -> "main-id" (node-attr el 'class) ;; #f
procedure
(node-attr? el attr) → boolean?
el : sxml:element? attr : symbol?
(define el '(main (@ (id "main-id")) "main text")) (node-attr? el 'id) ;; #t (node-attr? el 'class) ;; #f
procedure
el : sxml:element?
(define el '(main (@ (id "main-id")) "main text")) (node-text el) ;; "main text"
procedure
(node-tag-name el) → string?
el : sxml:element?
(define el '(main (@ (id "main-id")) "main text")) (node-tag-name el) ;; "main"
procedure
el : sxml:element?
(define el '(main (@ (id "main-id")) "main text")) (node-id el) ;; "main-id" (define el '(main (@ (class "main-id")) "main text")) (node-id el) ;; #f
procedure
el : sxml:element? id : string?
(define el '(main (@ (id "main-id")) "main text")) (node-id? el "main-id") ;; #t (node-id? el "main") ;; #f
procedure
(node-class el) → (listof string?)
el : sxml:element?
(define el '(div (@ (class "button")) "primary button")) (node-class el) ;; '("button") (define el '(main (@ (id "main-id")) "main text")) (node-class el) ;; '()
procedure
(node-class? el name) → boolean?
el : sxml:element? name : string?
(define el '(div (@ (class "button")) "primary button")) (node-class? el "button") ;; #t (node-class? el "input") ;; #f
4 查找元素
样式以后再说罢。
procedure
(node-children el) → nodeset?
el : (or/c emtpy? sxml:element?)
procedure
(node-children? root) → boolean?
root : (or/c empty? sxml:element?)
procedure
(node-child root) → (or/c #f sxml:element?)
root : (or/c empty? sxml:element?)
procedure
(node-all-children root) → nodeset?
root : (or/c empty? sxml:elements?)
procedure
(node-select root f) → nodeset?
root : (or/c empty? sxml:element?) f : (-> sxml:element? boolean?)
类似于querySelectorAll,只是node-select接受的是一个高阶函数,不是选择器。
procedure
(node-select-first root f) → (or/c #f sxml:element?)
root : (or/c empty? sxml:element?) f : (-> sxml:element? boolean?)
procedure
(node-select-id root id) → (or/c #f sxml:element?)
root : (or/c empty? sxml:element?) id : string?
5 柯里化
本库同样提供全套柯里化服务。默认以curryr形式柯里化,以便在特定场景使用。
(require (prefix-in zubat: zubat/curry)) (define link-id (compose (zubat:select-id "link") (zubat:node-id))) (link-id el)