refactor: refine types

This commit is contained in:
Mikael Sand
2019-10-21 19:38:29 +03:00
parent c59654c2dc
commit 10c2d0ee64
+27 -24
View File
@@ -23,7 +23,7 @@ import csstree, {
SelectorList, SelectorList,
} from 'css-tree'; } from 'css-tree';
// @ts-ignore // @ts-ignore
import cssSelect, { CSSselect } from 'css-select'; import cssSelect, { Adapter, CSSselect } from 'css-select';
import stable from 'stable'; import stable from 'stable';
/* /*
@@ -42,26 +42,26 @@ function isTag(node: CSSselect.Node): node is CSSselect.ElementNode {
// get the parent of the node // get the parent of the node
// getParent: ( node:Node ) => parentNode:Node // getParent: ( node:Node ) => parentNode:Node
// returns null when no parent exists // returns null when no parent exists
function getParent(node: AST) { function getParent(node: CSSselect.Node): CSSselect.Node {
return node.parent || null; return node.parent || null;
} }
// get the node's children // get the node's children
// getChildren: ( node:Node ) => children:[Node] // getChildren: ( node:Node ) => children:[Node]
function getChildren(node: AST) { function getChildren(node: CSSselect.Node): Array<CSSselect.Node> {
return node.children || []; return node.children || [];
} }
// get the name of the tag // get the name of the tag
// getName: ( elem:ElementNode ) => tagName:String // getName: ( elem:ElementNode ) => tagName:String
function getName(elemAst: AST) { function getName(elem: CSSselect.ElementNode): string {
return elemAst.tag; return elem.tag;
} }
// get the text content of the node, and its children if it has any // get the text content of the node, and its children if it has any
// getText: ( node:Node ) => text:String // getText: ( node:Node ) => text:String
// returns empty string when there is no text // returns empty string when there is no text
function getText() { function getText(_node: CSSselect.Node): string {
return ''; return '';
} }
@@ -130,7 +130,7 @@ function getSiblings(node: CSSselect.Node): Array<CSSselect.Node> {
} }
// does the element have the named attribute? // 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); return elem.props.hasOwnProperty(name);
} }
@@ -182,6 +182,26 @@ function findAll(
return result; return result;
} }
const adapter: Adapter<AST | string, AST> = {
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. * 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) { function querySelectorAll(document: AST, selectors: string) {
return cssSelect(selectors, document, cssSelectOpts); return cssSelect(selectors, document, cssSelectOpts);
} }
const cssSelectOpts = {
xmlMode: true,
adapter: {
removeSubsets,
existsOne,
getSiblings,
hasAttrib,
findOne,
findAll,
isTag,
getParent,
getChildren,
getName,
getText,
getAttributeValue,
},
};
type FlatPseudoSelector = { type FlatPseudoSelector = {
item: ListItem<CssNode>; item: ListItem<CssNode>;