Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

recalculate the insertion index after normalizing a node #1781

Merged

Conversation

romainmenke
Copy link
Contributor

@romainmenke romainmenke commented Sep 30, 2022

see : #1778

Current context :

  1. call insertAfter(exist, existingSibling)
  2. set the value of exist to the index of the node in this.nodes
  3. call this.normalize()
  4. this.normalize() removes existingSibling from it's parent. (which is this)
  5. exist is still the pre-calculated index
  6. this.nodes has changed and the indices have changed because existingSibling was removed from it's parent
  7. 馃敟
  insertAfter(exist, add) {
    exist = this.index(exist)

    let nodes = this.normalize(add, this.proxyOf.nodes[exist]).reverse()
    for (let node of nodes) this.proxyOf.nodes.splice(exist + 1, 0, node)

    let index
    for (let id in this.indexes) {
      index = this.indexes[id]
      if (exist < index) {
        this.indexes[id] = index + nodes.length
      }
    }

    this.markDirty()

    return this
  }
normalize(nodes, sample) {
    // ...

    let processed = nodes.map(i => {
      /* c8 ignore next */
      if (!i[my]) Container.rebuild(i)
      i = i.proxyOf
      if (i.parent) i.parent.removeChild(i)
      if (i[isClean]) markDirtyUp(i)
      if (typeof i.raws.before === 'undefined') {
        if (sample && typeof sample.raws.before !== 'undefined') {
          i.raws.before = sample.raws.before.replace(/\S/g, '')
        }
      }
      i.parent = this.proxyOf
      return I
    })

    // ...
  }

Proposed fix

  1. call insertAfter(exist, existingSibling)
  2. set the value of existIndex to the index of the node in this.nodes
  3. call this.normalize()
  4. this.normalize() removes existingSibling from it's parent. (which is this)
  5. set the value of existIndex to the index of the node in this.nodes (re-calculate)
  6. continue
  insertAfter(exist, add) {
    let existIndex = this.index(exist)
    let nodes = this.normalize(add, this.proxyOf.nodes[existIndex]).reverse()
    existIndex = this.index(exist)
    for (let node of nodes) this.proxyOf.nodes.splice(existIndex + 1, 0, node)

    let index
    for (let id in this.indexes) {
      index = this.indexes[id]
      if (existIndex < index) {
        this.indexes[id] = index + nodes.length
      }
    }

    this.markDirty()

    return this
  }

@romainmenke romainmenke force-pushed the fix-issue-1778--practical-snowy-owl-25291b279a branch from cb01880 to 7278432 Compare September 30, 2022 09:07
@ai ai merged commit 9454dcc into postcss:main Sep 30, 2022
@romainmenke romainmenke deleted the fix-issue-1778--practical-snowy-owl-25291b279a branch September 30, 2022 11:29
@ai
Copy link
Member

ai commented Sep 30, 2022

Released in 8.4.17.

@romainmenke
Copy link
Contributor Author

@ai Thank you for reviewing and releasing this 馃檱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants