summaryrefslogtreecommitdiff
path: root/node_modules/posthtml-match-helper/lib/index.js
blob: 6e7ca642f49f8c58784fec6c49477ad86c955bce (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const selectorReg = /^([^#\.\[]+)?(?:#([^\.\[]+))?(?:\.((?:[^\[\]\\]|\\.)+))?((?:\[[^\]]*\])+)?$/; // eslint-disable-line
const attributeReg = /^([a-zA-Z0-9_-]*[^~|^$*!=])(?:([~|^$*!]?)=['"]?([^'"]*)['"]?)?$/;
const splitReg = /\s*,\s*/;

function expandMatcher(matcher) {
  if (typeof matcher === "string") {
    const match = matcher.match(selectorReg);

    if (match) {
      matcher = {};
      const tag = match[1];
      const id = match[2];
      const className = match[3];
      const attrs = match[4];
      let attributes;

      if (tag) {
        matcher.tag = tag;
      }

      if (attrs) {
        attributes = expandAttributes(attrs);
      } else if (id || className) {
        attributes = {};
      }

      if (id) {
        attributes.id = id;
      }

      if (className) {
        attributes.class = new RegExp(getCombinations(className.split(".")).map((order) => {
          return "(?:^|\\s)" + order.join("\\s(?:.*?\\s)?") + "(?:\\s|$)";
        }).join("|"));
      }

      if (attributes) {
        matcher.attrs = attributes;
      }
    } else {
      matcher = {tag: matcher};
    }
  }

  return matcher;
}

function cssAttrToRegExp(value, operator) {
  let reg;

  switch (operator) {
    case "~":
      reg = "(?:^|\\s)" + value + "(?:\\s|$)";
      break;

    case "|":
      reg = "^" + value + "(?:-|$)";
      break;

    case "^":
      reg = "^" + value;
      break;

    case "$":
      reg = value + "$";
      break;

    case "*":
      reg = value;
      break;

    case "!":
      reg = "^((?!" + value + ")[\\s\\S])*$";
      break;

    default:
      reg = "^" + value + "$";
      break;
  }

  return new RegExp(reg);
}

function expandAttributes(attrs) {
  attrs = attrs.slice(1, -1);

  if (attrs.length > 0) {
    attrs = attrs.split("][");
    const attrObject = {};
    let l = attrs.length;
    let attrMatch;
    let name;
    let operator;
    let value;

    while (l--) {
      attrMatch = attrs[l].match(attributeReg);

      if (attrMatch) {
        name = attrMatch[1];
        operator = attrMatch[2];
        value = attrMatch[3];

        if (!value) {
          value = true;
        }

        attrObject[name] = (operator) ? cssAttrToRegExp(value, operator) : value;
      }
    }

    return attrObject;
  }
}

function getCombinations(values, subresult) {
  subresult = subresult || [];
  let result = [];

  for (const value of values) {
    if (subresult.indexOf(value) < 0) {
      const _subresult = subresult.concat([value]);

      if (_subresult.length < values.length) {
        result = result.concat(getCombinations(values, _subresult));
      } else {
        result.push(_subresult);
      }
    }
  }

  return result;
}

export default function (matcher) {
  if (typeof matcher === "string") {
    if (matcher.match(splitReg)) {
      matcher = matcher.split(splitReg);
    } else {
      return expandMatcher(matcher);
    }
  }

  if (Array.isArray(matcher)) {
    return matcher.map(expandMatcher);
  }

  return matcher;
}