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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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