Common Lisp Package: CSS-SELECTORS-TEST

README:

css-selectors: a jquery-style dom query language

CSS-selectors is a query language for finding specific nodes in cxml dom documents and dom sub trees.

API

query

(query (or css-selector-string match-fn) (or nodes *document*))

Similar to jQuery('css-selector', NodeList or document)

Returns a list of matching nodes.

if the second argument is a css-selector, compile it (as with compile-css-node-matcher) into a matcher function and call query again with it

if the second argument is a function, it is assumed to take a single node and to test whether or not that node matches the css-selector match function

If the css-selector-string is constantp, it will be compiled at compile time

node-matches?

(node-matches? node-node (or match-fn css-selector-string))

if the second argument is a css-selector, compile it (as with compile-css-node-matcher) into a matcher function and call node-matches again with it

if the second argument is a function, it is assumed to take a single node and to test whether or not that node matches the css-selector match function

If the css-selector-string is constantp, it will be compiled at compile time

parse-results

(parse-results inp)

Parses a css3 selector into an AST representing the selector expression. Probably only useful for debugging or implementing a different compiler.

Uses clppcre and VERY basic flex parser (defined in parse.lisp) to create a lexer. Combines that lexer with a parser generated by cl-yacc and runs the input through the parse.

compile-css-node-matcher

(compile-css-node-matcher inp)

Turns a parse-tree or css-selector-string into a compiled lambda matcher function this matcher function can be used with query and node-matches?. The matcher function takes two arguments

Pseudo Selectors

Implementing new pseudo selectors

Any function defined in the :pseudo (:css-selectors.pseudo) will be available as a pseudo selector (see pseudo.lisp for examples).

All pseudo selectors should be of the following format: (defun pseudo:MY-NEW-PSEUDO (node &optional sub-sel-function) ...)

If your pseudo selector has no args, then it should signal an error if a sub-selction-function is passed to it and vice-versa

Authors

;; Copyright (c) 2011 Russ Tyndall , Acceleration.net http://www.acceleration.net ;; All rights reserved. ;; ;; Redistribution and use in source and binary forms, with or without ;; modification, are permitted provided that the following conditions are ;; met: ;; ;; - Redistributions of source code must retain the above copyright ;; notice, this list of conditions and the following disclaimer. ;; ;; - Redistributions in binary form must reproduce the above copyright ;; notice, this list of conditions and the following disclaimer in the ;; documentation and/or other materials provided with the distribution. ;; ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

FUNCTION

Private

ALL-GROUP-MATCHES-AS-STRING (REGEX TARGET)

Expects a single matching group

COMPILE-CSS-NODE-MATCHER (INP)

Given a string, returns a matcher-function of a single node that will tell you whether or not the node matches

DOS-SAFE-READ-LINE (STREAM &OPTIONAL (EOF-ERROR-P T) EOF-VALUE RECURSIVE-P)

readline that can read unix or dos lines

HANDLE-QUOTED-RULES (PRODUCTION)

quotes in flex productions should be direct string matches

LOG-TIME (&OPTIONAL (TIME (GET-UNIVERSAL-TIME)) STREAM)

returns a date as ${mon}/${d}/${y} ${h}:${min}:{s}, defaults to get-universal-time

MAKE-ATTRIB-MATCHER (ATTRIB MATCH-TYPE MATCH-TO)

attrib-matcher

NODE-MATCHES? (NODE INP)

Given a node and a CSS selector, see if the given node matches that selector

PARSE-RESULTS (&OPTIONAL (INP #foo, .foo #bar .bast, .foo > bar[src~=blech], .foo:hover))

Mostly used for debugging purposes Takes a string CSS selector and turns it into an AST

QUERY (INP &OPTIONAL (TREES *DOCUMENT*))

Given a css selector, attempt to find the matching nodes in the passed in dom-trees (defaults to the document)

REPLACE-ALL (STRING PART REPLACEMENT &KEY (TEST #'CHAR=) STREAM)

Returns a new string in which all the occurences of the part is replaced with replacement. [FROM http://cl-cookbook.sourceforge.net/strings.html#manip]

TRIM-AND-NULLIFY (S)

trims the whitespace from a string returning nil if trimming produces an empty string or the string 'nil'

Undocumented

%NODE-MATCHES? (NODE INP)

%QUERY (INP &OPTIONAL (TREES *DOCUMENT*))

%RULE (ARGS BODY)

%RULE-SET (NAME RULES)

ARG-LIST-VAR-NAMES (ARGS)

ATTRIB-INCLUDES? (NODE ATTRIB VALUE)

BUT-FIRST (S)

BUT-LAST (S)

BUT-QUOTES (S)

CSS.INFO (MESSAGE &REST ARGS)

EXPAND-RETURN-VALUE (RTN)

IS-SPECIAL? (S)

KEYWORDIZE (S)

LEX-RESULTS (&OPTIONAL (INP #foo, .foo #bar, .foo > bar[src~=blech]))

MAKE-AND-MATCHER (FORMS)

MAKE-CHILD-MATCHER (PARENT-MATCHER CHILD-MATCHER)

MAKE-CLASS-MATCHER (CLASS)

MAKE-CSS3-LEXER (INP)

MAKE-ELT-MATCHER (TAG)

MAKE-HASH-MATCHER (ID)

MAKE-IMMEDIATE-CHILD-MATCHER (PARENT-MATCHER CHILD-MATCHER)

MAKE-IMMEDIATLY-PRECEDED-BY-MATCHER (THIS-MATCHER SIBLING-MATCHER)

MAKE-MATCHER-AUX (TREE)

MAKE-NODE-MATCHER (EXPRESSION)

MAKE-NTH-PSEUDO-MATCHER (PSEUDO MUL ADD)

MAKE-OR-MATCHER (FORMS)

MAKE-PRECEDED-BY-MATCHER (THIS-MATCHER SIBLING-MATCHER)

MAKE-PSEUDO-MATCHER (PSEUDO SUBMATCHER)

PARSE-XML (STRING)

READ-FLEX-FILE (FILE &AUX CI? (DEFS (MAKE-HASH-TABLE TEST 'EQUALP)) DONE-WITH-DEFS?)

REPLACE-EXPANSIONS (DEFS NEW-REGEX)

RUN-TESTS (&KEY SUITES TESTS (USE-DEBUGGER T))

STRING-UPCASE? (S)

SYMBOLIZE (S)

TEST-CSS-COMPILATION-RESULT-COMPILATION

TIL-OPEN (S)

TRIM-WHITESPACE (S)

UNIQUIFY-AND-REINTERN (X &OPTIONAL (PACKAGE *PACKAGE*))

MACRO

Private

LOG-AROUND ((LOG-NAME MESSAGE &REST ARGS) &BODY BODY)

Logs the beginning and end of a body. ARGS are evaluated twice

TIME-AND-LOG-AROUND ((LOG-NAME MESSAGE &REST ARGS) &BODY BODY)

Logs the beginning and end of a body. ARGS are evaluated twice

Undocumented

DEFTEST (NAME (&REST ARGS) &BODY BODY)

LAM (ARGS &BODY BODY)

RULE (ARGS &BODY BODY)

RULE-SET (NAME &BODY RULES)

TEST-W/DOC (NAME (&REST ARGS) &BODY BODY)

GENERIC-FUNCTION

Private

%DO-QUERY (MATCHER NODE &KEY FIRST? (FIRST? NIL))

matches selector inp against the node if first? is T then return the first matching node

CHILD-ELEMENTS (NODE)

Returns all child elements of this node

CHILD-NODES (NODE)

Returns all child nodes of this node

DOCUMENT-OF (NODE)

Returns the document of the given node

ELEMENT-P (NODE)

Is this an element

GET-ATTRIBUTE (NODE ATTRIB)

retrieves the value of a given attribute of the node

PARENT-ELEMENT (NODE)

returns the parent element of this node

PARENT-ELEMENTS (NODE)

Returns all parent elements of this node

PARENT-NODE (NODE)

returns the parent node of this node

PREVIOUS-SIBLING (NODE)

returns the parent element of this node

TAG-NAME (ELT)

retrieves the name of the given element

VARIABLE

Private

*IGNORE-NAMESPACES*

ignore tag name spaces when matching, for now the parser doesnt support parsing namespaced tags, so lets ignore tag namespaces

Undocumented

*CSS3-SELECTOR-PARSER*

+ARTICLE-TITLE+

+COMMON-WHITE-SPACE-TRIMBAG+

+CSS3-LEX-PRODUCTIONS+

+DOC+

+END-OF-DEFS+

+HEADER+

+ISSUE-8-DOC+

+NODES-AND-MATCHING-SELECTORS+

+NODES-AND-MATCHING-SELECTORS-COMPILED+

+NODES-AND-MATCHING-SELECTORS-PARSED+

+OPTION+

+OPTION-CASE-INSENSITIVE+

+RETURN-VALUE+