From 10c2d0ee6478f2fa0a27dc7605f286ef2ae0b526 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Mon, 21 Oct 2019 19:38:29 +0300 Subject: [PATCH] refactor: refine types --- src/css.tsx | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/css.tsx b/src/css.tsx index 97d47a67..9dede358 100644 --- a/src/css.tsx +++ b/src/css.tsx @@ -23,7 +23,7 @@ import csstree, { SelectorList, } from 'css-tree'; // @ts-ignore -import cssSelect, { CSSselect } from 'css-select'; +import cssSelect, { Adapter, CSSselect } from 'css-select'; import stable from 'stable'; /* @@ -42,26 +42,26 @@ function isTag(node: CSSselect.Node): node is CSSselect.ElementNode { // get the parent of the node // getParent: ( node:Node ) => parentNode:Node // returns null when no parent exists -function getParent(node: AST) { +function getParent(node: CSSselect.Node): CSSselect.Node { return node.parent || null; } // get the node's children // getChildren: ( node:Node ) => children:[Node] -function getChildren(node: AST) { +function getChildren(node: CSSselect.Node): Array { return node.children || []; } // get the name of the tag // getName: ( elem:ElementNode ) => tagName:String -function getName(elemAst: AST) { - return elemAst.tag; +function getName(elem: CSSselect.ElementNode): string { + return elem.tag; } // get the text content of the node, and its children if it has any // getText: ( node:Node ) => text:String // returns empty string when there is no text -function getText() { +function getText(_node: CSSselect.Node): string { return ''; } @@ -130,7 +130,7 @@ function getSiblings(node: CSSselect.Node): Array { } // does the element have the named attribute? -function hasAttrib(elem: AST, name: string) { +function hasAttrib(elem: CSSselect.ElementNode, name: string): boolean { return elem.props.hasOwnProperty(name); } @@ -182,6 +182,26 @@ function findAll( return result; } +const adapter: Adapter = { + removeSubsets, + existsOne, + getSiblings, + hasAttrib, + findOne, + findAll, + isTag, + getParent, + getChildren, + getName, + getText, + getAttributeValue, +}; + +const cssSelectOpts = { + xmlMode: true, + adapter, +}; + /** * Evaluate a string of CSS selectors against the element and returns matched elements. * @@ -192,23 +212,6 @@ function findAll( function querySelectorAll(document: AST, selectors: string) { return cssSelect(selectors, document, cssSelectOpts); } -const cssSelectOpts = { - xmlMode: true, - adapter: { - removeSubsets, - existsOne, - getSiblings, - hasAttrib, - findOne, - findAll, - isTag, - getParent, - getChildren, - getName, - getText, - getAttributeValue, - }, -}; type FlatPseudoSelector = { item: ListItem;