Skip to content

Commit

Permalink
Merge pull request #1781 from romainmenke/fix-issue-1778--practical-s…
Browse files Browse the repository at this point in the history
…nowy-owl-25291b279a

recalculate the insertion index after normalizing a node
  • Loading branch information
ai committed Sep 30, 2022
2 parents e1538a4 + 7278432 commit 9454dcc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/container.js
Expand Up @@ -176,16 +176,16 @@ class Container extends Node {
}

insertBefore(exist, add) {
exist = this.index(exist)

let existIndex = this.index(exist)
let type = exist === 0 ? 'prepend' : false
let nodes = this.normalize(add, this.proxyOf.nodes[exist], type).reverse()
for (let node of nodes) this.proxyOf.nodes.splice(exist, 0, node)
let nodes = this.normalize(add, this.proxyOf.nodes[existIndex], type).reverse()
existIndex = this.index(exist)
for (let node of nodes) this.proxyOf.nodes.splice(existIndex, 0, node)

let index
for (let id in this.indexes) {
index = this.indexes[id]
if (exist <= index) {
if (existIndex <= index) {
this.indexes[id] = index + nodes.length
}
}
Expand All @@ -196,15 +196,15 @@ class Container extends Node {
}

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 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 (exist < index) {
if (existIndex < index) {
this.indexes[id] = index + nodes.length
}
}
Expand Down
36 changes: 36 additions & 0 deletions test/container.test.ts
Expand Up @@ -636,6 +636,24 @@ test('insertBefore() receives array', () => {
is(a.toString(), 'a{ color: red; width: 1; height: 2; z-index: 1 }')
})

test('insertBefore() receives pre-existing child node - a', () => {
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
let declA = (a.first as Rule).nodes[0];
let declC = (a.first as Rule).nodes[2];
declC.before(declA);

is(a.toString(), 'a{ color: red; align-items: start; z-index: 1 }')
})

test('insertBefore() receives pre-existing child node - b', () => {
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
let declA = (a.first as Rule).nodes[0];
let declC = (a.first as Rule).nodes[2];
declA.before(declC);

is(a.toString(), 'a{ z-index: 1; align-items: start; color: red }')
})

test('insertAfter() inserts child', () => {
let rule = parse('a { a: 1; b: 2 }').first as Rule
rule.insertAfter(0, { prop: 'c', value: '3' })
Expand Down Expand Up @@ -666,6 +684,24 @@ test('insertAfter() receives array', () => {
is(a.toString(), 'a{ color: red; width: 1; height: 2; z-index: 1 }')
})

test('insertAfter() receives pre-existing child node - a', () => {
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
let declA = (a.first as Rule).nodes[0];
let declC = (a.first as Rule).nodes[2];
declC.after(declA);

is(a.toString(), 'a{ color: red; z-index: 1; align-items: start }')
})

test('insertAfter() receives pre-existing child node - b', () => {
let a = parse('a{ align-items: start; color: red; z-index: 1 }')
let declA = (a.first as Rule).nodes[0];
let declC = (a.first as Rule).nodes[2];
declA.after(declC);

is(a.toString(), 'a{ align-items: start; z-index: 1; color: red }')
})

test('removeChild() removes by index', () => {
let rule = parse('a { a: 1; b: 2 }').first as Rule
rule.removeChild(1)
Expand Down

0 comments on commit 9454dcc

Please sign in to comment.