fixed 10 tests

This commit is contained in:
Kevin Jahns
2019-04-06 13:00:32 +02:00
parent 61d9d96d15
commit 1b17b5e400
12 changed files with 133 additions and 120 deletions

View File

@@ -14,7 +14,6 @@ import {
ItemBinary,
createID,
getItemCleanStart,
getItemCleanEnd,
Y, Snapshot, Transaction, EventHandler, YEvent, AbstractItem, // eslint-disable-line
} from '../internals.js'
@@ -327,7 +326,7 @@ export const typeArrayGet = (type, index) => {
* @param {Array<Object<string,any>|Array<any>|number|string|ArrayBuffer>} content
*/
export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem, content) => {
const left = referenceItem
let left = referenceItem
const right = referenceItem === null ? parent._start : referenceItem.right
/**
* @type {Array<Object|Array|number>}
@@ -335,8 +334,8 @@ export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem,
let jsonContent = []
const packJsonContent = () => {
if (jsonContent.length > 0) {
const item = new ItemJSON(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, jsonContent)
item.integrate(transaction)
left = new ItemJSON(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, jsonContent)
left.integrate(transaction)
jsonContent = []
}
}
@@ -353,11 +352,14 @@ export const typeArrayInsertGenericsAfter = (transaction, parent, referenceItem,
switch (c.constructor) {
case ArrayBuffer:
// @ts-ignore c is definitely an ArrayBuffer
new ItemBinary(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, c).integrate(transaction)
left = new ItemBinary(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, c)
// @ts-ignore
left.integrate(transaction)
break
default:
if (c instanceof AbstractType) {
new ItemType(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, c).integrate(transaction)
left = new ItemType(nextID(transaction), left, left === null ? null : left.lastId, right, right === null ? null : right.id, parent, null, c)
left.integrate(transaction)
} else {
throw new Error('Unexpected content type in insert operation')
}
@@ -401,10 +403,11 @@ export const typeArrayInsertGenerics = (transaction, parent, index, content) =>
*/
export const typeArrayDelete = (transaction, parent, index, length) => {
let n = parent._start
// compute the first item to be deleted
for (; n !== null; n = n.right) {
if (!n.deleted && n.countable) {
if (index <= n.length) {
if (index < n.length) {
if (index < n.length && index > 0) {
n = getItemCleanStart(transaction.y.store, createID(n.id.client, n.id.clock + index))
}
break
@@ -412,16 +415,20 @@ export const typeArrayDelete = (transaction, parent, index, length) => {
index -= n.length
}
}
// delete all items until done
while (length > 0 && n !== null) {
if (!n.deleted) {
if (length < n.length) {
getItemCleanEnd(transaction.y.store, createID(n.id.client, n.id.clock + length))
getItemCleanStart(transaction.y.store, createID(n.id.client, n.id.clock + length))
}
n.delete(transaction)
length -= n.length
}
n = n.right
}
if (length > 0) {
throw error.create('array length exceeded')
}
}
/**

View File

@@ -601,14 +601,15 @@ export class YText extends AbstractType {
toString () {
let str = ''
/**
* @type {any}
* @type {AbstractItem|null}
*/
let n = this._start
while (n !== null) {
if (!n._deleted && n._countable) {
str += n._content
if (!n.deleted && n.countable && n.constructor === ItemString) {
// @ts-ignore
str += n.string
}
n = n._right
n = n.right
}
return str
}
@@ -693,8 +694,9 @@ export class YText extends AbstractType {
const currentAttributes = new Map()
let str = ''
/**
* @type {any}
* @type {AbstractItem|null}
*/
// @ts-ignore
let n = this._start
function packStr () {
if (str.length > 0) {
@@ -702,7 +704,7 @@ export class YText extends AbstractType {
/**
* @type {Object<string,any>}
*/
let attributes = {}
const attributes = {}
let addAttributes = false
for (let [key, value] of currentAttributes) {
addAttributes = true
@@ -711,7 +713,7 @@ export class YText extends AbstractType {
/**
* @type {Object<string,any>}
*/
let op = { insert: str }
const op = { insert: str }
if (addAttributes) {
op.attributes = attributes
}
@@ -725,28 +727,30 @@ export class YText extends AbstractType {
case ItemString:
const cur = currentAttributes.get('ychange')
if (snapshot !== undefined && !isVisible(n, snapshot)) {
if (cur === undefined || cur.user !== n._id.user || cur.state !== 'removed') {
if (cur === undefined || cur.user !== n.id.client || cur.state !== 'removed') {
packStr()
currentAttributes.set('ychange', { user: n._id.user, state: 'removed' })
currentAttributes.set('ychange', { user: n.id.client, state: 'removed' })
}
} else if (prevSnapshot !== undefined && !isVisible(n, prevSnapshot)) {
if (cur === undefined || cur.user !== n._id.user || cur.state !== 'added') {
if (cur === undefined || cur.user !== n.id.client || cur.state !== 'added') {
packStr()
currentAttributes.set('ychange', { user: n._id.user, state: 'added' })
currentAttributes.set('ychange', { user: n.id.client, state: 'added' })
}
} else if (cur !== undefined) {
packStr()
currentAttributes.delete('ychange')
}
str += n._content
// @ts-ignore
str += n.string
break
case ItemFormat:
packStr()
// @ts-ignore
updateCurrentAttributes(currentAttributes, n)
break
}
}
n = n._right
n = n.right
}
packStr()
return ops

View File

@@ -52,16 +52,17 @@ import * as decoding from 'lib0/decoding.js'
export class YXmlTreeWalker {
/**
* @param {YXmlFragment | YXmlElement} root
* @param {function(AbstractType<any>):boolean} f
* @param {function(AbstractType<any>):boolean} [f]
*/
constructor (root, f) {
this._filter = f || (() => true)
constructor (root, f = () => true) {
this._filter = f
this._root = root
/**
* @type {ItemType | null}
*/
// @ts-ignore
this._currentNode = root._start
this._firstCall = true
}
[Symbol.iterator] () {
return this
@@ -75,34 +76,36 @@ export class YXmlTreeWalker {
*/
next () {
let n = this._currentNode
if (n !== null && (!this._firstCall || n.deleted || !this._filter(n.type))) { // if first call, we check if we can use the first item
do {
if (!n.deleted && (n.type.constructor === YXmlElement || n.type.constructor === YXmlFragment) && n.type._start !== null) {
// walk down in the tree
// @ts-ignore
n = n.type._start
} else {
// walk right or up in the tree
while (n !== null) {
if (n.right !== null) {
// @ts-ignore
n = n.right
break
} else if (n.parent === this._root) {
n = null
} else {
n = n.parent._item
}
}
}
} while (n !== null && (n.deleted || !this._filter(n.type)))
}
this._firstCall = false
this._currentNode = n
if (n === null) {
// @ts-ignore return undefined if done=true (the expected result)
return { value: undefined, done: true }
}
const nextValue = n
do {
if (!n.deleted && (n.constructor === YXmlElement || n.constructor === YXmlFragment) && n.type._start !== null) {
// walk down in the tree
// @ts-ignore
n = n.type._start
} else {
// walk right or up in the tree
while (n !== null) {
if (n.right !== null) {
// @ts-ignore
n = n.right
break
} else if (n.parent === this._root) {
n = null
} else {
n = n.parent._item
}
}
}
} while (n !== null && (n.deleted || !this._filter(n.type)))
this._currentNode = n
// @ts-ignore
return { value: nextValue.type, done: false }
return { value: n.type, done: false }
}
}