Skip to content

moveManual causes undefined setAttribute #50

@subITCSS

Description

@subITCSS

Describe the bug

We get
can't access property "setAttribute", eo[Gn] is undefined

based on

_moveManual: function () {
    var this$ = this;
    var dotElems = this.$.container.querySelectorAll('.slider__dot'), i;
    for (i = 0; i < this.totalSlides; ++i) {
      dotElems[i].setAttribute("aria-label", "Slide " + (parseInt(dotElems[i].getAttribute('aria-posinset')) + 1) + " selector");
      dotElems[i].addEventListener('click', function (e) {
        this$.movePos(e.target.getAttribute('aria-posinset'));
      });
    };
    if (this.totalSlides) {
      this._dotStyles = window.getComputedStyle(dotElems[0]);
    }
  },

Expected behavior

The code should be able to handle such state.

_moveManual: function () {
    const container = this.$?.container;
    if (!container) return;

    const dotElems = container.querySelectorAll('.slider__dot');
    if (!dotElems.length) return;

    dotElems.forEach((dot, index) => {
      if (!dot) return;

      const posAttr = dot.getAttribute('aria-posinset');
      const pos = parseInt(posAttr, 10);

      const safePos = Number.isNaN(pos) ? index + 1 : pos;

      dot.setAttribute('aria-label', `Slide ${safePos} selector`);

      if (!dot._hasClickHandler) {
        dot.addEventListener('click', (e) => {
          const target = e.currentTarget;
          const attr = target.getAttribute('aria-posinset');
          const parsed = parseInt(attr, 10);

          if (!Number.isNaN(parsed)) {
            this.movePos(parsed - 1);
          }
        });
        dot._hasClickHandler = true;
      }
    });

    this._dotStyles = window.getComputedStyle(dotElems[0]);
  },

Minimal reproducible example

No response

Add-on Version

3.0.1

Vaadin Version

24.10.1

Additional information

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions