Compare commits

..

6 Commits

Author SHA1 Message Date
Kevin Jahns
256d08cb2a v13.0.0-18 -- distribution files 2017-09-20 11:34:24 +02:00
Kevin Jahns
0e3e561ec7 13.0.0-18 2017-09-20 11:34:03 +02:00
Kevin Jahns
7df46cb731 Merge branch 'master' of github.com:y-js/yjs 2017-09-20 11:30:24 +02:00
Kevin Jahns
40fb16ef32 catch y-* related errors 2017-09-20 11:29:13 +02:00
Kevin Jahns
ada5d36cd5 add more y-xml tests 2017-09-19 03:16:48 +02:00
Kevin Jahns
f537a43e29 implement tests for dom filter 2017-09-18 22:14:45 +02:00
13 changed files with 112 additions and 23964 deletions

View File

@@ -4,8 +4,7 @@
<textarea style="width:80%;" rows=40 id="textfield" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></textarea>
<script src="../../y.js"></script>
<script src="../../../y-array/y-array.js"></script>
<script src="../../../y-text/dist/y-text.js"></script>
<script src="../../../y-memory/y-memory.js"></script>
<script src="../../../y-text/y-text.js"></script>
<script src="../../../y-websockets-client/y-websockets-client.js"></script>
<script src="./index.js"></script>
</body>

View File

@@ -1,8 +1,5 @@
/* global Y */
// eslint-disable-next-line
let search = new URLSearchParams(location.search)
// initialize a shared object. This function call returns a promise!
Y({
db: {
@@ -10,10 +7,9 @@ Y({
},
connector: {
name: 'websockets-client',
room: 'Textarea-example',
room: 'Textarea-example2',
// url: '//localhost:1234',
url: 'https://yjs-v13.herokuapp.com/'
// options: { transports: ['websocket'], upgrade: false }
},
share: {
textarea: 'Text'
@@ -24,7 +20,4 @@ Y({
// bind the textarea to a shared text element
y.share.textarea.bind(document.getElementById('textfield'))
// thats it..
}).catch(() => {
console.log('Something went wrong while creating the instance..')
})

View File

@@ -1,6 +1,6 @@
{
"name": "yjs",
"version": "13.0.0-17",
"version": "13.0.0-18",
"description": "A framework for real-time p2p shared editing on any data",
"main": "./y.node.js",
"browser": "./y.js",

View File

@@ -67,7 +67,11 @@ export default function Utils (Y) {
this._eventListener[name] = listener.filter(e => e !== f)
}
emit (name, value) {
(this._eventListener[name] || []).forEach(l => l(value))
let listener = this._eventListener[name] || []
if (name === 'error' && listener.length === 0) {
console.error(value)
}
listener.forEach(l => l(value))
}
destroy () {
this._eventListener = null

View File

@@ -1,4 +1,3 @@
import extendRBTree from './RedBlackTree'
export default function extend (Y) {
@@ -48,9 +47,13 @@ export default function extend (Y) {
}
transact (makeGen) {
const t = new Transaction(this)
while (makeGen != null) {
makeGen.call(t)
makeGen = this.getNextRequest()
try {
while (makeGen != null) {
makeGen.call(t)
makeGen = this.getNextRequest()
}
} catch (e) {
this.y.emit('error', e)
}
}
destroy () {

View File

@@ -224,16 +224,65 @@ test('move element to a different position', async function xml13 (t) {
await compareUsers(t, users)
})
test('filter node', async function xml14 (t) {
var { users, xml0, xml1 } = await initArrays(t, { users: 3 })
let dom0 = xml0.getDom()
let dom1 = xml1.getDom()
let domFilter = (node, attrs) => {
if (node.nodeName === 'H1') {
return null
} else {
return attrs
}
}
xml0.setDomFilter(domFilter)
xml1.setDomFilter(domFilter)
dom0.append(document.createElement('div'))
dom0.append(document.createElement('h1'))
await flushAll(t, users)
t.assert(dom1.childNodes.length === 1, 'Only one node was not transmitted')
t.assert(dom1.childNodes[0].nodeName === 'DIV', 'div node was transmitted')
await compareUsers(t, users)
})
test('filter attribute', async function xml15 (t) {
var { users, xml0, xml1 } = await initArrays(t, { users: 3 })
let dom0 = xml0.getDom()
let dom1 = xml1.getDom()
let domFilter = (node, attrs) => {
return attrs.filter(name => name !== 'hidden')
}
xml0.setDomFilter(domFilter)
xml1.setDomFilter(domFilter)
dom0.setAttribute('hidden', 'true')
dom0.setAttribute('style', 'height: 30px')
dom0.setAttribute('data-me', '77')
await flushAll(t, users)
t.assert(dom0.getAttribute('hidden') === 'true', 'User 0 still has the attribute')
t.assert(dom1.getAttribute('hidden') == null, 'User 1 did not receive update')
t.assert(dom1.getAttribute('style') === 'height: 30px', 'User 1 received style update')
t.assert(dom1.getAttribute('data-me') === '77', 'User 1 received data update')
await compareUsers(t, users)
})
// TODO: move elements
var xmlTransactions = [
function attributeChange (t, user, chance) {
user.share.xml.getDom().setAttribute(chance.word(), chance.word())
},
function attributeChangeHidden (t, user, chance) {
user.share.xml.getDom().setAttribute('hidden', chance.word())
},
function insertText (t, user, chance) {
let dom = user.share.xml.getDom()
var succ = dom.children.length > 0 ? chance.pickone(dom.children) : null
dom.insertBefore(document.createTextNode(chance.word()), succ)
},
function insertHiddenDom (t, user, chance) {
let dom = user.share.xml.getDom()
var succ = dom.children.length > 0 ? chance.pickone(dom.children) : null
dom.insertBefore(document.createElement('hidden'), succ)
},
function insertDom (t, user, chance) {
let dom = user.share.xml.getDom()
var succ = dom.children.length > 0 ? chance.pickone(dom.children) : null
@@ -274,30 +323,30 @@ var xmlTransactions = [
}
]
test('y-xml: Random tests (10)', async function randomXml10 (t) {
test('y-xml: Random tests (10)', async function xmlRandom10 (t) {
await applyRandomTests(t, xmlTransactions, 10)
})
test('y-xml: Random tests (42)', async function randomXml42 (t) {
test('y-xml: Random tests (42)', async function xmlRandom42 (t) {
await applyRandomTests(t, xmlTransactions, 42)
})
test('y-xml: Random tests (43)', async function randomXml43 (t) {
test('y-xml: Random tests (43)', async function xmlRandom43 (t) {
await applyRandomTests(t, xmlTransactions, 43)
})
test('y-xml: Random tests (44)', async function randomXml44 (t) {
test('y-xml: Random tests (44)', async function xmlRandom44 (t) {
await applyRandomTests(t, xmlTransactions, 44)
})
test('y-xml: Random tests (45)', async function randomXml45 (t) {
test('y-xml: Random tests (45)', async function xmlRandom45 (t) {
await applyRandomTests(t, xmlTransactions, 45)
})
test('y-xml: Random tests (46)', async function randomXml46 (t) {
test('y-xml: Random tests (46)', async function xmlRandom46 (t) {
await applyRandomTests(t, xmlTransactions, 46)
})
test('y-xml: Random tests (47)', async function randomXml47 (t) {
test('y-xml: Random tests (47)', async function xmlRandom47 (t) {
await applyRandomTests(t, xmlTransactions, 47)
})

View File

@@ -2,7 +2,7 @@
import _Y from '../../yjs/src/y.js'
import yArray from '../../y-array/src/y-array.js'
import yText from '../../y-text/src/Text.js'
import yText from '../../y-text/src/y-text.js'
import yMap from '../../y-map/src/y-map.js'
import yXml from '../../y-xml/src/y-xml.js'
import yTest from './test-connector.js'
@@ -48,11 +48,17 @@ export async function garbageCollectUsers (t, users) {
await Promise.all(users.map(u => u.db.emptyGarbageCollector()))
}
export function attrsToObject (attrs) {
export function attrsObject (dom) {
let keys = []
let yxml = dom.__yxml
for (let i = 0; i < dom.attributes.length; i++) {
keys.push(dom.attributes[i].name)
}
keys = yxml._domFilter(dom, keys)
let obj = {}
for (var i = 0; i < attrs.length; i++) {
let attr = attrs[i]
obj[attr.name] = attr.value
for (let i = 0; i < keys.length; i++) {
let key = keys[i]
obj[key] = dom.getAttribute(key)
}
return obj
}
@@ -61,8 +67,10 @@ export function domToJson (dom) {
if (dom.nodeType === document.TEXT_NODE) {
return dom.textContent
} else if (dom.nodeType === document.ELEMENT_NODE) {
let attributes = attrsToObject(dom.attributes)
let children = Array.from(dom.childNodes.values()).map(domToJson)
let attributes = attrsObject(dom, dom.__yxml)
let children = Array.from(dom.childNodes.values())
.filter(d => d.__yxml !== false)
.map(domToJson)
return {
name: dom.nodeName,
children: children,
@@ -198,6 +206,13 @@ export async function initArrays (t, opts) {
for (let name in share) {
result[name + i] = y.share[name]
}
y.share.xml.setDomFilter(function (d, attrs) {
if (d.nodeName === 'HIDDEN') {
return null
} else {
return attrs.filter(a => a !== 'hidden')
}
})
}
result.array0.delete(0, result.array0.length)
if (result.users[0].connector.testRoom != null) {

8
y.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,7 @@
/**
* yjs - A framework for real-time p2p shared editing on any data
* @version v13.0.0-17
* @version v13.0.0-18
* @license MIT
*/
@@ -3452,7 +3452,11 @@ function Utils (Y) {
this._eventListener[name] = listener.filter(e => e !== f);
}
emit (name, value) {
(this._eventListener[name] || []).forEach(l => l(value));
let listener = this._eventListener[name] || [];
if (name === 'error' && listener.length === 0) {
console.error(value);
}
listener.forEach(l => l(value));
}
destroy () {
this._eventListener = null;
@@ -4793,9 +4797,13 @@ function extend (Y) {
}
transact (makeGen) {
const t = new Transaction(this);
while (makeGen != null) {
makeGen.call(t);
makeGen = this.getNextRequest();
try {
while (makeGen != null) {
makeGen.call(t);
makeGen = this.getNextRequest();
}
} catch (e) {
this.y.emit('error', e);
}
}
destroy () {

File diff suppressed because one or more lines are too long

23922
y.test.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long