Sha256: d339f7fc4148b44e9591048818ccc3c3e1bf5e6a84c6c51e0f7d834d055be3a8

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

// This code was heavily inspired by the rails-ujs project.
// Copyright (c) 2007-2021 Rails Core team.
const elementPrototype = Element.prototype as any

const m: (this: Element, selector: string) => boolean =
  elementPrototype.matches ||
  elementPrototype.matchesSelector ||
  elementPrototype.mozMatchesSelector ||
  elementPrototype.msMatchesSelector ||
  elementPrototype.oMatchesSelector ||
  elementPrototype.webkitMatchesSelector

// Checks if the given native dom element matches the selector
// element::
//   native DOM element
// selector::
//   CSS selector string or
//   a JavaScript object with `selector` and `exclude` properties
//   Examples: "form", { selector: "form", exclude: "form[data-remote='true']"}
export const matches = (element, selector) => {
  if (selector.exclude != null) {
    return m.call(element, selector.selector) && !m.call(element, selector.exclude)
  } else {
    return m.call(element, selector)
  }
}

// get and set data on a given element using "expando properties"
// See: https://developer.mozilla.org/en-US/docs/Glossary/Expando
const expando = "_ujsData"

export const getData = (element, key) => (element[expando] != null ? element[expando][key] : undefined)

export const setData = (element, key, value) => {
  if (element[expando] == null) {
    element[expando] = {}
  }

  return (element[expando][key] = value)
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shoelace-rails-0.4.1 src/turbolinks/utils/dom.ts
shoelace-rails-0.4.0 src/turbolinks/utils/dom.ts
shoelace-rails-0.3.0 src/turbolinks/utils/dom.ts
shoelace-rails-0.2.0 src/turbolinks/utils/dom.ts
shoelace-rails-0.1.0 src/turbolinks/utils/dom.ts