{"size": 1810, "ext": "js", "max_stars_repo_path": "src/commands/If.js", "max_stars_repo_name": "mbauman/DemoMonkey", "max_stars_repo_stars_event_min_datetime": "2017-02-28T17:14:42.000Z", "max_stars_repo_stars_event_max_datetime": "2017-10-30T08:28:15.000Z", "max_issues_repo_path": "src/commands/If.js", "max_issues_repo_name": "mbauman/DemoMonkey", "max_issues_repo_issues_event_min_datetime": "2017-02-26T12:50:46.000Z", "max_issues_repo_issues_event_max_datetime": "2017-12-07T10:13:51.000Z", "max_forks_repo_path": "src/commands/If.js", "max_forks_repo_name": "mbauman/DemoMonkey", "max_forks_repo_forks_event_min_datetime": "2017-09-23T11:33:37.000Z", "max_forks_repo_forks_event_max_datetime": "2017-10-03T01:23:04.000Z"}
{"max_stars_count": 7, "max_issues_count": 33, "max_forks_count": 2, "avg_line_length": 30.1666666667, "max_line_length": 101, "alphanum_fraction": 0.6850828729}
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import Command from './Command'

class Group extends Command {
  constructor(locationFilter = '', cssFilter = '', thenCmd, location) {
    super()
    this.locationFilter = locationFilter
    this.cssFilter = cssFilter
    this.thenCmd = thenCmd
    this.location = location
  }

  isApplicableForGroup(group) {
    return this.thenCmd.isApplicableForGroup(group)
  }

  /* Copy From OverwriteHTML, move to Command in a later stage */
  _checkLocation() {
    return typeof this.location === 'object' &&
      this.location.toString().includes(this.locationFilter)
  }

  /* This code is also an improvement for SearchAndReplace, might need to update */
  _checkCss(target) {
    if (this.cssFilter === '' || typeof target !== 'object' || typeof target.nodeType !== 'number') {
      return true
    }

    // For text nodes we check css on the parent node
    if (target.nodeType === 3) {
      return this._checkCss(target.parentNode)
    } else if (typeof target.matches === 'function') {
      return target.matches(this.cssFilter)
    }
    return false
  }

  apply(target, key) {
    if (!this._checkLocation() || !this._checkCss(target)) {
      return false
    }

    return this.thenCmd.apply(target, key)
  }
}

export default Group
