File size: 1,450 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as is from '../is';
import * as util from '../util';

import parse from './parse';
import matching from './matching';
import Type from './type';

let Selector = function( selector ){
  this.inputText = selector;
  this.currentSubject = null;
  this.compoundCount = 0;
  this.edgeCount = 0;
  this.length = 0;

  if( selector == null || ( is.string( selector ) && selector.match( /^\s*$/ ) ) ){
    // leave empty

  } else if( is.elementOrCollection( selector ) ){

    this.addQuery({
      checks: [ {
        type: Type.COLLECTION,
        value: selector.collection()
      } ]
    });

  } else if( is.fn( selector ) ){

    this.addQuery({
      checks: [ {
        type: Type.FILTER,
        value: selector
      } ]
    });

  } else if( is.string( selector ) ){
    if( !this.parse( selector ) ){
      this.invalid = true;
    }

  } else {
    util.error( 'A selector must be created from a string; found ', selector );
  }
};

let selfn = Selector.prototype;

[
  parse,
  matching
].forEach( p => util.assign( selfn, p ) );

selfn.text = function(){
  return this.inputText;
};

selfn.size = function(){
  return this.length;
};

selfn.eq = function( i ){
  return this[ i ];
};

selfn.sameText = function( otherSel ){
  return !this.invalid && !otherSel.invalid && this.text() === otherSel.text();
};

selfn.addQuery = function( q ){
  this[ this.length++ ] = q;
};

selfn.selector = selfn.toString;

export default Selector;