Compare commits

..

5 Commits

Author SHA1 Message Date
Kevin Jahns
b1192fbb46 v13.0.0-32 -- distribution files 2017-11-14 21:31:19 -08:00
Kevin Jahns
2e3240b379 13.0.0-32 2017-11-14 21:31:11 -08:00
Kevin Jahns
2558652356 fix attribute filter (it used to filter everything) 2017-11-14 21:19:39 -08:00
Kevin Jahns
783cbd63fc 13.0.0-31 2017-11-14 20:44:12 -08:00
Kevin Jahns
41be80e751 fix y-xml server environment 2017-11-14 20:43:30 -08:00
12 changed files with 327 additions and 439 deletions

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "yjs",
"version": "13.0.0-30",
"version": "13.0.0-32",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

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

View File

@@ -5,7 +5,7 @@ import YMap from '../YMap.js'
import YXmlFragment from './YXmlFragment.js'
export default class YXmlElement extends YXmlFragment {
constructor (arg1, arg2) {
constructor (arg1, arg2, _document) {
super()
this.nodeName = null
this._scrollElement = null
@@ -13,7 +13,7 @@ export default class YXmlElement extends YXmlFragment {
this.nodeName = arg1.toUpperCase()
} else if (arg1 != null && arg1.nodeType != null && arg1.nodeType === arg1.ELEMENT_NODE) {
this.nodeName = arg1.nodeName
this._setDom(arg1)
this._setDom(arg1, _document)
} else {
this.nodeName = 'UNDEFINED'
}
@@ -26,14 +26,12 @@ export default class YXmlElement extends YXmlFragment {
struct.nodeName = this.nodeName
return struct
}
_setDom (dom) {
_setDom (dom, _document) {
if (this._dom != null) {
throw new Error('Only call this method if you know what you are doing ;)')
} else if (dom._yxml != null) { // TODO do i need to check this? - no.. but for dev purps..
throw new Error('Already bound to an YXml type')
} else {
this._dom = dom
dom._yxml = this
// tag is already set in constructor
// set attributes
let attrNames = []
@@ -46,8 +44,8 @@ export default class YXmlElement extends YXmlFragment {
let attrValue = dom.getAttribute(attrName)
this.setAttribute(attrName, attrValue)
}
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes))
this._bindToDom(dom)
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes), _document)
this._bindToDom(dom, _document)
return dom
}
}
@@ -115,7 +113,6 @@ export default class YXmlElement extends YXmlFragment {
let dom = this._dom
if (dom == null) {
dom = _document.createElement(this.nodeName)
this._dom = dom
dom._yxml = this
let attrs = this.getAttributes()
for (let key in attrs) {
@@ -124,7 +121,7 @@ export default class YXmlElement extends YXmlFragment {
this.forEach(yxml => {
dom.appendChild(yxml.getDom(_document))
})
this._bindToDom(dom)
this._bindToDom(dom, _document)
}
return dom
}

View File

@@ -9,7 +9,7 @@ import YXmlEvent from './YXmlEvent.js'
import { logID } from '../../MessageHandler/messageToString.js'
import diff from 'fast-diff'
function domToYXml (parent, doms) {
function domToYXml (parent, doms, _document) {
const types = []
doms.forEach(d => {
if (d._yxml != null && d._yxml !== false) {
@@ -20,7 +20,7 @@ function domToYXml (parent, doms) {
if (d.nodeType === d.TEXT_NODE) {
type = new YXmlText(d)
} else if (d.nodeType === d.ELEMENT_NODE) {
type = new YXmlFragment._YXmlElement(d, parent._domFilter)
type = new YXmlFragment._YXmlElement(d, parent._domFilter, _document)
} else {
throw new Error('Unsupported node!')
}
@@ -98,7 +98,9 @@ export default class YXmlFragment extends YArray {
} catch (e) {
console.error(e)
}
this._domObserver.takeRecords()
if (this._domObserver !== null) {
this._domObserver.takeRecords()
}
token = true
}
}
@@ -162,113 +164,116 @@ export default class YXmlFragment extends YArray {
this._dom = null
}
}
insertDomElementsAfter (prev, doms) {
const types = domToYXml(this, doms)
insertDomElementsAfter (prev, doms, _document) {
const types = domToYXml(this, doms, _document)
this.insertAfter(prev, types)
return types
}
insertDomElements (pos, doms) {
const types = domToYXml(this, doms)
insertDomElements (pos, doms, _document) {
const types = domToYXml(this, doms, _document)
this.insert(pos, types)
return types
}
getDom () {
return this._dom
}
bindToDom (dom) {
bindToDom (dom, _document) {
if (this._dom != null) {
this._unbindFromDom()
}
if (dom._yxml != null) {
dom._yxml._unbindFromDom()
}
if (MutationObserver == null) {
throw new Error('Not able to bind to a DOM element, because MutationObserver is not available!')
}
dom.innerHTML = ''
this._dom = dom
dom._yxml = this
this.forEach(t => {
dom.insertBefore(t.getDom(), null)
dom.insertBefore(t.getDom(_document), null)
})
this._bindToDom(dom)
this._bindToDom(dom, _document)
}
// binds to a dom element
// Only call if dom and YXml are isomorph
_bindToDom (dom) {
if (this._parent === null || this._parent._dom != null || typeof MutationObserver === 'undefined') {
// only bind if parent did not already bind
_bindToDom (dom, _document) {
_document = _document || document
this._dom = dom
dom._yxml = this
// TODO: refine this..
if ((this.constructor !== YXmlFragment && this._parent !== this._y) || this._parent === null) {
// TODO: only top level YXmlFragment can bind. Also allow YXmlElements..
return
}
this._y.on('beforeTransaction', () => {
this._domObserverListener(this._domObserver.takeRecords())
})
this._y.on('beforeTransaction', beforeTransactionSelectionFixer)
this._y.on('afterTransaction', afterTransactionSelectionFixer)
// Apply Y.Xml events to dom
this.observeDeep(reflectChangesOnDom.bind(this))
this.observeDeep(events => {
reflectChangesOnDom.call(this, events, _document)
})
// Apply Dom changes on Y.Xml
this._domObserverListener = mutations => {
this._mutualExclude(() => {
this._y.transact(() => {
let diffChildren = new Set()
mutations.forEach(mutation => {
const dom = mutation.target
const yxml = dom._yxml
if (yxml == null) {
// dom element is filtered
return
}
switch (mutation.type) {
case 'characterData':
var diffs = diff(yxml.toString(), dom.nodeValue)
var pos = 0
for (var i = 0; i < diffs.length; i++) {
var d = diffs[i]
if (d[0] === 0) { // EQUAL
pos += d[1].length
} else if (d[0] === -1) { // DELETE
yxml.delete(pos, d[1].length)
} else { // INSERT
yxml.insert(pos, d[1])
pos += d[1].length
}
}
break
case 'attributes':
let name = mutation.attributeName
// check if filter accepts attribute
if (this._domFilter(dom, [name]).length > 0 && this.constructor !== YXmlFragment) {
var val = dom.getAttribute(name)
if (yxml.getAttribute(name) !== val) {
if (val == null) {
yxml.removeAttribute(name)
} else {
yxml.setAttribute(name, val)
if (typeof MutationObserver !== 'undefined') {
this._y.on('beforeTransaction', () => {
this._domObserverListener(this._domObserver.takeRecords())
})
this._domObserverListener = mutations => {
this._mutualExclude(() => {
this._y.transact(() => {
let diffChildren = new Set()
mutations.forEach(mutation => {
const dom = mutation.target
const yxml = dom._yxml
if (yxml == null) {
// dom element is filtered
return
}
switch (mutation.type) {
case 'characterData':
var diffs = diff(yxml.toString(), dom.nodeValue)
var pos = 0
for (var i = 0; i < diffs.length; i++) {
var d = diffs[i]
if (d[0] === 0) { // EQUAL
pos += d[1].length
} else if (d[0] === -1) { // DELETE
yxml.delete(pos, d[1].length)
} else { // INSERT
yxml.insert(pos, d[1])
pos += d[1].length
}
}
}
break
case 'childList':
diffChildren.add(mutation.target)
break
break
case 'attributes':
let name = mutation.attributeName
// check if filter accepts attribute
if (this._domFilter(dom, [name]).length > 0 && yxml.constructor !== YXmlFragment) {
var val = dom.getAttribute(name)
if (yxml.getAttribute(name) !== val) {
if (val == null) {
yxml.removeAttribute(name)
} else {
yxml.setAttribute(name, val)
}
}
}
break
case 'childList':
diffChildren.add(mutation.target)
break
}
})
for (let dom of diffChildren) {
if (dom._yxml != null && dom._yxml !== false) {
applyChangesFromDom(dom)
}
}
})
for (let dom of diffChildren) {
if (dom._yxml != null && dom._yxml !== false) {
applyChangesFromDom(dom)
}
}
})
}
this._domObserver = new MutationObserver(this._domObserverListener)
this._domObserver.observe(dom, {
childList: true,
attributes: true,
characterData: true,
subtree: true
})
}
this._domObserver = new MutationObserver(this._domObserverListener)
this._domObserver.observe(dom, {
childList: true,
attributes: true,
characterData: true,
subtree: true
})
return dom
}
_logString () {

View File

@@ -135,7 +135,7 @@ export function applyChangesFromDom (dom) {
}
}
export function reflectChangesOnDom (events) {
export function reflectChangesOnDom (events, _document) {
// Make sure that no filtered attributes are applied to the structure
// if they were, delete them
/*
@@ -183,9 +183,9 @@ export function reflectChangesOnDom (events) {
})
if (event.childListChanged) {
// create fragment of undeleted nodes
const fragment = document.createDocumentFragment()
const fragment = _document.createDocumentFragment()
yxml.forEach(function (t) {
fragment.append(t.getDom())
fragment.appendChild(t.getDom(_document))
})
// remove remainding nodes
let lastChild = dom.lastChild
@@ -194,7 +194,7 @@ export function reflectChangesOnDom (events) {
lastChild = dom.lastChild
}
// insert fragment of undeleted nodes
dom.append(fragment)
dom.appendChild(fragment)
}
}
/* TODO: smartscrolling

View File

@@ -153,7 +153,7 @@ export async function initArrays (t, opts) {
result['array' + i] = y.define('array', Y.Array)
result['map' + i] = y.define('map', Y.Map)
result['xml' + i] = y.define('xml', Y.XmlElement)
y.get('xml', Y.Xml).setDomFilter(function (d, attrs) {
y.get('xml').setDomFilter(function (d, attrs) {
if (d.nodeName === 'HIDDEN') {
return null
} else {

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

184
y.node.js
View File

@@ -1,7 +1,7 @@
/**
* yjs - A framework for real-time p2p shared editing on any data
* @version v13.0.0-30
* @version v13.0.0-32
* @license MIT
*/
@@ -2776,7 +2776,7 @@ function applyChangesFromDom (dom) {
}
}
function reflectChangesOnDom (events) {
function reflectChangesOnDom (events, _document) {
// Make sure that no filtered attributes are applied to the structure
// if they were, delete them
/*
@@ -2824,9 +2824,9 @@ function reflectChangesOnDom (events) {
});
if (event.childListChanged) {
// create fragment of undeleted nodes
const fragment = document.createDocumentFragment();
const fragment = _document.createDocumentFragment();
yxml.forEach(function (t) {
fragment.append(t.getDom());
fragment.appendChild(t.getDom(_document));
});
// remove remainding nodes
let lastChild = dom.lastChild;
@@ -2835,7 +2835,7 @@ function reflectChangesOnDom (events) {
lastChild = dom.lastChild;
}
// insert fragment of undeleted nodes
dom.append(fragment);
dom.appendChild(fragment);
}
}
/* TODO: smartscrolling
@@ -3778,7 +3778,7 @@ function merge_tuples (diffs, start, length) {
/* global MutationObserver */
function domToYXml (parent, doms) {
function domToYXml (parent, doms, _document) {
const types = [];
doms.forEach(d => {
if (d._yxml != null && d._yxml !== false) {
@@ -3789,7 +3789,7 @@ function domToYXml (parent, doms) {
if (d.nodeType === d.TEXT_NODE) {
type = new YXmlText(d);
} else if (d.nodeType === d.ELEMENT_NODE) {
type = new YXmlFragment._YXmlElement(d, parent._domFilter);
type = new YXmlFragment._YXmlElement(d, parent._domFilter, _document);
} else {
throw new Error('Unsupported node!')
}
@@ -3867,7 +3867,9 @@ class YXmlFragment extends YArray {
} catch (e) {
console.error(e);
}
this._domObserver.takeRecords();
if (this._domObserver !== null) {
this._domObserver.takeRecords();
}
token = true;
}
};
@@ -3931,113 +3933,116 @@ class YXmlFragment extends YArray {
this._dom = null;
}
}
insertDomElementsAfter (prev, doms) {
const types = domToYXml(this, doms);
insertDomElementsAfter (prev, doms, _document) {
const types = domToYXml(this, doms, _document);
this.insertAfter(prev, types);
return types
}
insertDomElements (pos, doms) {
const types = domToYXml(this, doms);
insertDomElements (pos, doms, _document) {
const types = domToYXml(this, doms, _document);
this.insert(pos, types);
return types
}
getDom () {
return this._dom
}
bindToDom (dom) {
bindToDom (dom, _document) {
if (this._dom != null) {
this._unbindFromDom();
}
if (dom._yxml != null) {
dom._yxml._unbindFromDom();
}
if (MutationObserver == null) {
throw new Error('Not able to bind to a DOM element, because MutationObserver is not available!')
}
dom.innerHTML = '';
this._dom = dom;
dom._yxml = this;
this.forEach(t => {
dom.insertBefore(t.getDom(), null);
dom.insertBefore(t.getDom(_document), null);
});
this._bindToDom(dom);
this._bindToDom(dom, _document);
}
// binds to a dom element
// Only call if dom and YXml are isomorph
_bindToDom (dom) {
if (this._parent === null || this._parent._dom != null || typeof MutationObserver === 'undefined') {
// only bind if parent did not already bind
_bindToDom (dom, _document) {
_document = _document || document;
this._dom = dom;
dom._yxml = this;
// TODO: refine this..
if ((this.constructor !== YXmlFragment && this._parent !== this._y) || this._parent === null) {
// TODO: only top level YXmlFragment can bind. Also allow YXmlElements..
return
}
this._y.on('beforeTransaction', () => {
this._domObserverListener(this._domObserver.takeRecords());
});
this._y.on('beforeTransaction', beforeTransactionSelectionFixer);
this._y.on('afterTransaction', afterTransactionSelectionFixer);
// Apply Y.Xml events to dom
this.observeDeep(reflectChangesOnDom.bind(this));
this.observeDeep(events => {
reflectChangesOnDom.call(this, events, _document);
});
// Apply Dom changes on Y.Xml
this._domObserverListener = mutations => {
this._mutualExclude(() => {
this._y.transact(() => {
let diffChildren = new Set();
mutations.forEach(mutation => {
const dom = mutation.target;
const yxml = dom._yxml;
if (yxml == null) {
// dom element is filtered
return
}
switch (mutation.type) {
case 'characterData':
var diffs = diff_1(yxml.toString(), dom.nodeValue);
var pos = 0;
for (var i = 0; i < diffs.length; i++) {
var d = diffs[i];
if (d[0] === 0) { // EQUAL
pos += d[1].length;
} else if (d[0] === -1) { // DELETE
yxml.delete(pos, d[1].length);
} else { // INSERT
yxml.insert(pos, d[1]);
pos += d[1].length;
}
}
break
case 'attributes':
let name = mutation.attributeName;
// check if filter accepts attribute
if (this._domFilter(dom, [name]).length > 0 && this.constructor !== YXmlFragment) {
var val = dom.getAttribute(name);
if (yxml.getAttribute(name) !== val) {
if (val == null) {
yxml.removeAttribute(name);
} else {
yxml.setAttribute(name, val);
if (typeof MutationObserver !== 'undefined') {
this._y.on('beforeTransaction', () => {
this._domObserverListener(this._domObserver.takeRecords());
});
this._domObserverListener = mutations => {
this._mutualExclude(() => {
this._y.transact(() => {
let diffChildren = new Set();
mutations.forEach(mutation => {
const dom = mutation.target;
const yxml = dom._yxml;
if (yxml == null) {
// dom element is filtered
return
}
switch (mutation.type) {
case 'characterData':
var diffs = diff_1(yxml.toString(), dom.nodeValue);
var pos = 0;
for (var i = 0; i < diffs.length; i++) {
var d = diffs[i];
if (d[0] === 0) { // EQUAL
pos += d[1].length;
} else if (d[0] === -1) { // DELETE
yxml.delete(pos, d[1].length);
} else { // INSERT
yxml.insert(pos, d[1]);
pos += d[1].length;
}
}
}
break
case 'childList':
diffChildren.add(mutation.target);
break
break
case 'attributes':
let name = mutation.attributeName;
// check if filter accepts attribute
if (this._domFilter(dom, [name]).length > 0 && yxml.constructor !== YXmlFragment) {
var val = dom.getAttribute(name);
if (yxml.getAttribute(name) !== val) {
if (val == null) {
yxml.removeAttribute(name);
} else {
yxml.setAttribute(name, val);
}
}
}
break
case 'childList':
diffChildren.add(mutation.target);
break
}
});
for (let dom of diffChildren) {
if (dom._yxml != null && dom._yxml !== false) {
applyChangesFromDom(dom);
}
}
});
for (let dom of diffChildren) {
if (dom._yxml != null && dom._yxml !== false) {
applyChangesFromDom(dom);
}
}
});
};
this._domObserver = new MutationObserver(this._domObserverListener);
this._domObserver.observe(dom, {
childList: true,
attributes: true,
characterData: true,
subtree: true
});
};
this._domObserver = new MutationObserver(this._domObserverListener);
this._domObserver.observe(dom, {
childList: true,
attributes: true,
characterData: true,
subtree: true
});
}
return dom
}
_logString () {
@@ -4049,7 +4054,7 @@ class YXmlFragment extends YArray {
// import diff from 'fast-diff'
class YXmlElement extends YXmlFragment {
constructor (arg1, arg2) {
constructor (arg1, arg2, _document) {
super();
this.nodeName = null;
this._scrollElement = null;
@@ -4057,7 +4062,7 @@ class YXmlElement extends YXmlFragment {
this.nodeName = arg1.toUpperCase();
} else if (arg1 != null && arg1.nodeType != null && arg1.nodeType === arg1.ELEMENT_NODE) {
this.nodeName = arg1.nodeName;
this._setDom(arg1);
this._setDom(arg1, _document);
} else {
this.nodeName = 'UNDEFINED';
}
@@ -4070,14 +4075,12 @@ class YXmlElement extends YXmlFragment {
struct.nodeName = this.nodeName;
return struct
}
_setDom (dom) {
_setDom (dom, _document) {
if (this._dom != null) {
throw new Error('Only call this method if you know what you are doing ;)')
} else if (dom._yxml != null) { // TODO do i need to check this? - no.. but for dev purps..
throw new Error('Already bound to an YXml type')
} else {
this._dom = dom;
dom._yxml = this;
// tag is already set in constructor
// set attributes
let attrNames = [];
@@ -4090,8 +4093,8 @@ class YXmlElement extends YXmlFragment {
let attrValue = dom.getAttribute(attrName);
this.setAttribute(attrName, attrValue);
}
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes));
this._bindToDom(dom);
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes), _document);
this._bindToDom(dom, _document);
return dom
}
}
@@ -4159,7 +4162,6 @@ class YXmlElement extends YXmlFragment {
let dom = this._dom;
if (dom == null) {
dom = _document.createElement(this.nodeName);
this._dom = dom;
dom._yxml = this;
let attrs = this.getAttributes();
for (let key in attrs) {
@@ -4168,7 +4170,7 @@ class YXmlElement extends YXmlFragment {
this.forEach(yxml => {
dom.appendChild(yxml.getDom(_document));
});
this._bindToDom(dom);
this._bindToDom(dom, _document);
}
return dom
}

File diff suppressed because one or more lines are too long

382
y.test.js
View File

@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global['y-tests'] = {})));
}(this, (function (exports) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
class N {
// A created node is always red!
@@ -1448,10 +1448,6 @@ function logID (id) {
}
}
/**
* Delete all items in an ID-range
* TODO: implement getItemCleanStartNode for better performance (only one lookup)
*/
function deleteItemRange (y, user, clock, range) {
const createDelete = y.connector._forwardAppliedStructs;
let item = y.os.getItemCleanStart(new ID(user, clock));
@@ -1556,13 +1552,6 @@ function transactionTypeChanged (y, type, sub) {
}
}
/**
* Helper utility to split an Item (see _splitAt)
* - copy all properties from a to b
* - connect a to b
* - assigns the correct _id
* - save b to os
*/
function splitHelper (y, a, b, diff) {
const aID = a._id;
b._id = new ID(aID.user, aID.clock + diff);
@@ -1913,7 +1902,6 @@ class EventHandler {
}
}
// restructure children as if they were inserted one after another
function integrateChildren (y, start) {
let right;
do {
@@ -2773,7 +2761,7 @@ function applyChangesFromDom (dom) {
}
}
function reflectChangesOnDom (events) {
function reflectChangesOnDom (events, _document) {
// Make sure that no filtered attributes are applied to the structure
// if they were, delete them
/*
@@ -2821,9 +2809,9 @@ function reflectChangesOnDom (events) {
});
if (event.childListChanged) {
// create fragment of undeleted nodes
const fragment = document.createDocumentFragment();
const fragment = _document.createDocumentFragment();
yxml.forEach(function (t) {
fragment.append(t.getDom());
fragment.appendChild(t.getDom(_document));
});
// remove remainding nodes
let lastChild = dom.lastChild;
@@ -2832,7 +2820,7 @@ function reflectChangesOnDom (events) {
lastChild = dom.lastChild;
}
// insert fragment of undeleted nodes
dom.append(fragment);
dom.appendChild(fragment);
}
}
/* TODO: smartscrolling
@@ -2953,7 +2941,7 @@ let relativeSelection = null;
let beforeTransactionSelectionFixer;
if (typeof getSelection !== 'undefined') {
beforeTransactionSelectionFixer = function _beforeTransactionSelectionFixer (y, remote) {
beforeTransactionSelectionFixer = function _beforeTransactionSelectionFixer (y, transaction, remote) {
if (!remote) {
return
}
@@ -2976,7 +2964,7 @@ if (typeof getSelection !== 'undefined') {
beforeTransactionSelectionFixer = function _fakeBeforeTransactionSelectionFixer () {};
}
function afterTransactionSelectionFixer (y, remote) {
function afterTransactionSelectionFixer (y, transaction, remote) {
if (relativeSelection === null || !remote) {
return
}
@@ -3775,7 +3763,7 @@ function merge_tuples (diffs, start, length) {
/* global MutationObserver */
function domToYXml (parent, doms) {
function domToYXml (parent, doms, _document) {
const types = [];
doms.forEach(d => {
if (d._yxml != null && d._yxml !== false) {
@@ -3786,7 +3774,7 @@ function domToYXml (parent, doms) {
if (d.nodeType === d.TEXT_NODE) {
type = new YXmlText(d);
} else if (d.nodeType === d.ELEMENT_NODE) {
type = new YXmlFragment._YXmlElement(d, parent._domFilter);
type = new YXmlFragment._YXmlElement(d, parent._domFilter, _document);
} else {
throw new Error('Unsupported node!')
}
@@ -3864,7 +3852,9 @@ class YXmlFragment extends YArray {
} catch (e) {
console.error(e);
}
this._domObserver.takeRecords();
if (this._domObserver !== null) {
this._domObserver.takeRecords();
}
token = true;
}
};
@@ -3928,113 +3918,116 @@ class YXmlFragment extends YArray {
this._dom = null;
}
}
insertDomElementsAfter (prev, doms) {
const types = domToYXml(this, doms);
insertDomElementsAfter (prev, doms, _document) {
const types = domToYXml(this, doms, _document);
this.insertAfter(prev, types);
return types
}
insertDomElements (pos, doms) {
const types = domToYXml(this, doms);
insertDomElements (pos, doms, _document) {
const types = domToYXml(this, doms, _document);
this.insert(pos, types);
return types
}
getDom () {
return this._dom
}
bindToDom (dom) {
bindToDom (dom, _document) {
if (this._dom != null) {
this._unbindFromDom();
}
if (dom._yxml != null) {
dom._yxml._unbindFromDom();
}
if (MutationObserver == null) {
throw new Error('Not able to bind to a DOM element, because MutationObserver is not available!')
}
dom.innerHTML = '';
this._dom = dom;
dom._yxml = this;
this.forEach(t => {
dom.insertBefore(t.getDom(), null);
dom.insertBefore(t.getDom(_document), null);
});
this._bindToDom(dom);
this._bindToDom(dom, _document);
}
// binds to a dom element
// Only call if dom and YXml are isomorph
_bindToDom (dom) {
if (this._parent === null || this._parent._dom != null || typeof MutationObserver === 'undefined') {
// only bind if parent did not already bind
_bindToDom (dom, _document) {
_document = _document || document;
this._dom = dom;
dom._yxml = this;
// TODO: refine this..
if ((this.constructor !== YXmlFragment && this._parent !== this._y) || this._parent === null) {
// TODO: only top level YXmlFragment can bind. Also allow YXmlElements..
return
}
this._y.on('beforeTransaction', () => {
this._domObserverListener(this._domObserver.takeRecords());
});
this._y.on('beforeTransaction', beforeTransactionSelectionFixer);
this._y.on('afterTransaction', afterTransactionSelectionFixer);
// Apply Y.Xml events to dom
this.observeDeep(reflectChangesOnDom.bind(this));
this.observeDeep(events => {
reflectChangesOnDom.call(this, events, _document);
});
// Apply Dom changes on Y.Xml
this._domObserverListener = mutations => {
this._mutualExclude(() => {
this._y.transact(() => {
let diffChildren = new Set();
mutations.forEach(mutation => {
const dom = mutation.target;
const yxml = dom._yxml;
if (yxml == null) {
// dom element is filtered
return
}
switch (mutation.type) {
case 'characterData':
var diffs = diff_1(yxml.toString(), dom.nodeValue);
var pos = 0;
for (var i = 0; i < diffs.length; i++) {
var d = diffs[i];
if (d[0] === 0) { // EQUAL
pos += d[1].length;
} else if (d[0] === -1) { // DELETE
yxml.delete(pos, d[1].length);
} else { // INSERT
yxml.insert(pos, d[1]);
pos += d[1].length;
}
}
break
case 'attributes':
let name = mutation.attributeName;
// check if filter accepts attribute
if (this._domFilter(dom, [name]).length > 0 && this.constructor !== YXmlFragment) {
var val = dom.getAttribute(name);
if (yxml.getAttribute(name) !== val) {
if (val == null) {
yxml.removeAttribute(name);
} else {
yxml.setAttribute(name, val);
if (typeof MutationObserver !== 'undefined') {
this._y.on('beforeTransaction', () => {
this._domObserverListener(this._domObserver.takeRecords());
});
this._domObserverListener = mutations => {
this._mutualExclude(() => {
this._y.transact(() => {
let diffChildren = new Set();
mutations.forEach(mutation => {
const dom = mutation.target;
const yxml = dom._yxml;
if (yxml == null) {
// dom element is filtered
return
}
switch (mutation.type) {
case 'characterData':
var diffs = diff_1(yxml.toString(), dom.nodeValue);
var pos = 0;
for (var i = 0; i < diffs.length; i++) {
var d = diffs[i];
if (d[0] === 0) { // EQUAL
pos += d[1].length;
} else if (d[0] === -1) { // DELETE
yxml.delete(pos, d[1].length);
} else { // INSERT
yxml.insert(pos, d[1]);
pos += d[1].length;
}
}
}
break
case 'childList':
diffChildren.add(mutation.target);
break
break
case 'attributes':
let name = mutation.attributeName;
// check if filter accepts attribute
if (this._domFilter(dom, [name]).length > 0 && this.constructor !== YXmlFragment) {
var val = dom.getAttribute(name);
if (yxml.getAttribute(name) !== val) {
if (val == null) {
yxml.removeAttribute(name);
} else {
yxml.setAttribute(name, val);
}
}
}
break
case 'childList':
diffChildren.add(mutation.target);
break
}
});
for (let dom of diffChildren) {
if (dom._yxml != null && dom._yxml !== false) {
applyChangesFromDom(dom);
}
}
});
for (let dom of diffChildren) {
if (dom._yxml != null && dom._yxml !== false) {
applyChangesFromDom(dom);
}
}
});
};
this._domObserver = new MutationObserver(this._domObserverListener);
this._domObserver.observe(dom, {
childList: true,
attributes: true,
characterData: true,
subtree: true
});
};
this._domObserver = new MutationObserver(this._domObserverListener);
this._domObserver.observe(dom, {
childList: true,
attributes: true,
characterData: true,
subtree: true
});
}
return dom
}
_logString () {
@@ -4046,7 +4039,7 @@ class YXmlFragment extends YArray {
// import diff from 'fast-diff'
class YXmlElement extends YXmlFragment {
constructor (arg1, arg2) {
constructor (arg1, arg2, _document) {
super();
this.nodeName = null;
this._scrollElement = null;
@@ -4054,7 +4047,7 @@ class YXmlElement extends YXmlFragment {
this.nodeName = arg1.toUpperCase();
} else if (arg1 != null && arg1.nodeType != null && arg1.nodeType === arg1.ELEMENT_NODE) {
this.nodeName = arg1.nodeName;
this._setDom(arg1);
this._setDom(arg1, _document);
} else {
this.nodeName = 'UNDEFINED';
}
@@ -4067,14 +4060,12 @@ class YXmlElement extends YXmlFragment {
struct.nodeName = this.nodeName;
return struct
}
_setDom (dom) {
_setDom (dom, _document) {
if (this._dom != null) {
throw new Error('Only call this method if you know what you are doing ;)')
} else if (dom._yxml != null) { // TODO do i need to check this? - no.. but for dev purps..
throw new Error('Already bound to an YXml type')
} else {
this._dom = dom;
dom._yxml = this;
// tag is already set in constructor
// set attributes
let attrNames = [];
@@ -4087,8 +4078,8 @@ class YXmlElement extends YXmlFragment {
let attrValue = dom.getAttribute(attrName);
this.setAttribute(attrName, attrValue);
}
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes));
this._bindToDom(dom);
this.insertDomElements(0, Array.prototype.slice.call(dom.childNodes), _document);
this._bindToDom(dom, _document);
return dom
}
}
@@ -4156,7 +4147,6 @@ class YXmlElement extends YXmlFragment {
let dom = this._dom;
if (dom == null) {
dom = _document.createElement(this.nodeName);
this._dom = dom;
dom._yxml = this;
let attrs = this.getAttributes();
for (let key in attrs) {
@@ -4165,7 +4155,7 @@ class YXmlElement extends YXmlFragment {
this.forEach(yxml => {
dom.appendChild(yxml.getDom(_document));
});
this._bindToDom(dom);
this._bindToDom(dom, _document);
}
return dom
}
@@ -4406,16 +4396,16 @@ class NamedEventHandler {
}
class ReverseOperation {
constructor (y) {
constructor (y, transaction) {
this.created = new Date();
const beforeState = y._transaction.beforeState;
const beforeState = transaction.beforeState;
this.toState = new ID(y.userID, y.ss.getState(y.userID) - 1);
if (beforeState.has(y.userID)) {
this.fromState = new ID(y.userID, beforeState.get(y.userID));
} else {
this.fromState = this.toState;
}
this.deletedStructs = y._transaction.deletedStructs;
this.deletedStructs = transaction.deletedStructs;
}
}
@@ -4475,9 +4465,9 @@ class UndoManager {
this._redoing = false;
const y = scope._y;
this.y = y;
y.on('afterTransaction', (y, remote) => {
if (!remote && y._transaction.changedParentTypes.has(scope)) {
let reverseOperation = new ReverseOperation(y);
y.on('afterTransaction', (y, transaction, remote) => {
if (!remote && transaction.changedParentTypes.has(scope)) {
let reverseOperation = new ReverseOperation(y, transaction);
if (!this._undoing) {
let lastUndoOp = this._undoBuffer.length > 0 ? this._undoBuffer[this._undoBuffer.length - 1] : null;
if (lastUndoOp !== null && reverseOperation.created - lastUndoOp.created <= options.captureTimeout) {
@@ -4534,7 +4524,7 @@ var y = d * 365.25;
* @api public
*/
var ms = function(val, options) {
var index = function(val, options) {
options = options || {};
var type = typeof val;
if (type === 'string' && val.length > 0) {
@@ -4676,7 +4666,7 @@ exports.coerce = coerce;
exports.disable = disable;
exports.enable = enable;
exports.enabled = enabled;
exports.humanize = ms;
exports.humanize = index;
/**
* The currently active debug mode names, and names to skip.
@@ -4735,8 +4725,8 @@ function createDebug(namespace) {
// set `diff` timestamp
var curr = +new Date();
var ms$$1 = curr - (prevTime || curr);
self.diff = ms$$1;
var ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
@@ -4755,19 +4745,19 @@ function createDebug(namespace) {
}
// apply any `formatters` transformations
var index = 0;
var index$$1 = 0;
args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
// if we encounter an escaped % then don't increase the array index
if (match === '%%') return match;
index++;
index$$1++;
var formatter = exports.formatters[format];
if ('function' === typeof formatter) {
var val = args[index];
var val = args[index$$1];
match = formatter.call(self, val);
// now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
args.splice(index$$1, 1);
index$$1--;
}
return match;
});
@@ -5433,8 +5423,8 @@ class Y$2 extends NamedEventHandler {
transact (f, remote = false) {
let initialCall = this._transaction === null;
if (initialCall) {
this.emit('beforeTransaction', this, remote);
this._transaction = new Transaction(this);
this.emit('beforeTransaction', this, this._transaction, remote);
}
try {
f(this);
@@ -5465,11 +5455,8 @@ class Y$2 extends NamedEventHandler {
type._deepEventHandler.callEventListeners(transaction, events);
}
});
if (this._transaction === null) {
// when all changes & events are processed, emit afterTransaction event
// only when there are no more transactions
this.emit('afterTransaction', this, remote);
}
// when all changes & events are processed, emit afterTransaction event
this.emit('afterTransaction', this, transaction, remote);
}
}
// fake _start for root properties (y.set('name', type))
@@ -5739,7 +5726,7 @@ if (typeof Y !== 'undefined') {
}
var chance_1 = createCommonjsModule(function (module, exports) {
// Chance.js 1.0.12
// Chance.js 1.0.10
// http://chancejs.com
// (c) 2013 Victor Quinn
// Chance may be freely distributed or modified under the MIT license.
@@ -5804,7 +5791,7 @@ var chance_1 = createCommonjsModule(function (module, exports) {
return this;
}
Chance.prototype.VERSION = "1.0.12";
Chance.prototype.VERSION = "1.0.10";
// Random helper functions
function initOptions(options, defaults) {
@@ -6023,16 +6010,6 @@ var chance_1 = createCommonjsModule(function (module, exports) {
return integer.toString(16);
};
Chance.prototype.letter = function(options) {
options = initOptions(options, {casing: 'lower'});
var pool = "abcdefghijklmnopqrstuvwxyz";
var letter = this.character({pool: pool});
if (options.casing === 'upper') {
letter = letter.toUpperCase();
}
return letter;
};
/**
* Return a random string
*
@@ -6975,25 +6952,8 @@ var chance_1 = createCommonjsModule(function (module, exports) {
return this.word({length: options.length}) + '@' + (options.domain || this.domain());
};
/**
* #Description:
* ===============================================
* Generate a random Facebook id, aka fbid.
*
* NOTE: At the moment (Sep 2017), Facebook ids are
* "numeric strings" of length 16.
* However, Facebook Graph API documentation states that
* "it is extremely likely to change over time".
* @see https://developers.facebook.com/docs/graph-api/overview/
*
* #Examples:
* ===============================================
* chance.fbid() => '1000035231661304'
*
* @return [string] facebook id
*/
Chance.prototype.fbid = function () {
return '10000' + this.string({pool: "1234567890", length: 11});
return parseInt('10000' + this.natural({max: 100000000000}), 10);
};
Chance.prototype.google_analytics = function () {
@@ -7834,72 +7794,8 @@ var chance_1 = createCommonjsModule(function (module, exports) {
// -- End Regional
// -- Music --
Chance.prototype.note = function(options) {
// choices for 'notes' option:
// flatKey - chromatic scale with flat notes (default)
// sharpKey - chromatic scale with sharp notes
// flats - just flat notes
// sharps - just sharp notes
// naturals - just natural notes
// all - naturals, sharps and flats
options = initOptions(options, { notes : 'flatKey'});
var scales = {
naturals: ['C', 'D', 'E', 'F', 'G', 'A', 'B'],
flats: ['D♭', 'E♭', 'G♭', 'A♭', 'B♭'],
sharps: ['C♯', 'D♯', 'F♯', 'G♯', 'A♯']
};
scales.all = scales.naturals.concat(scales.flats.concat(scales.sharps));
scales.flatKey = scales.naturals.concat(scales.flats);
scales.sharpKey = scales.naturals.concat(scales.sharps);
return this.pickone(scales[options.notes]);
};
Chance.prototype.midi_note = function(options) {
var min = 0;
var max = 127;
options = initOptions(options, { min : min, max : max });
return this.integer({min: options.min, max: options.max});
};
Chance.prototype.chord_quality = function(options) {
options = initOptions(options, { jazz: true });
var chord_qualities = ['maj', 'min', 'aug', 'dim'];
if (options.jazz){
chord_qualities = [
'maj7',
'min7',
'7',
'sus',
'dim',
'ø'
];
}
return this.pickone(chord_qualities);
};
Chance.prototype.chord = function (options) {
options = initOptions(options);
return this.note(options) + this.chord_quality(options);
};
Chance.prototype.tempo = function (options) {
var min = 40;
var max = 320;
options = initOptions(options, {min: min, max: max});
return this.integer({min: options.min, max: options.max});
};
// -- End Music
// -- Miscellaneous --
// Coin - Flip, flip, flipadelphia
Chance.prototype.coin = function(options) {
return this.bool() ? "heads" : "tails";
};
// Dice - For all the board game geeks out there, myself included ;)
function diceFn (range) {
return function () {
@@ -13089,19 +12985,6 @@ var chance_1 = createCommonjsModule(function (module, exports) {
var chance_2 = chance_1.Chance;
/**
* Try to merge all items in os with their successors.
*
* Some transformations (like delete) fragment items.
* Item(c: 'ab') + Delete(1,1) + Delete(0, 1) -> Item(c: 'a',deleted);Item(c: 'b',deleted)
*
* This functions merges the fragmented nodes together:
* Item(c: 'a',deleted);Item(c: 'b',deleted) -> Item(c: 'ab', deleted)
*
* TODO: The Tree implementation does not support deletions in-spot.
* This is why all deletions must be performed after the traversal.
*
*/
function defragmentItemContent (y) {
const os = y.os;
if (os.length < 2) {
@@ -13259,7 +13142,7 @@ async function initArrays (t, opts) {
result['array' + i] = y.define('array', Y$1.Array);
result['map' + i] = y.define('map', Y$1.Map);
result['xml' + i] = y.define('xml', Y$1.XmlElement);
y.get('xml', Y$1.Xml).setDomFilter(function (d, attrs) {
y.get('xml').setDomFilter(function (d, attrs) {
if (d.nodeName === 'HIDDEN') {
return null
} else {
@@ -13380,7 +13263,7 @@ async function applyRandomTests (t, mods, iterations) {
return initInformation
}
var index$1 = function (str) {
var index$1$1 = function (str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase();
});
@@ -13566,7 +13449,7 @@ function parserForArrayFormat(opts) {
function encode(value, opts) {
if (opts.encode) {
return opts.strict ? index$1(value) : encodeURIComponent(value);
return opts.strict ? index$1$1(value) : encodeURIComponent(value);
}
return value;
@@ -13678,7 +13561,7 @@ var stringify = function (obj, opts) {
}).join('&') : '';
};
var index = {
var index$2 = {
extract: extract,
parse: parse$1,
stringify: stringify
@@ -13693,7 +13576,7 @@ const browserSupport =
function createTestLink (params) {
if (typeof location !== 'undefined') {
var query = index.parse(location.search);
var query = index$2.parse(location.search);
delete query.test;
delete query.seed;
delete query.args;
@@ -13703,7 +13586,7 @@ function createTestLink (params) {
query[name] = params[name];
}
}
return location.protocol + '//' + location.host + location.pathname + '?' + index.stringify(query) + location.hash
return location.protocol + '//' + location.host + location.pathname + '?' + index$2.stringify(query) + location.hash
}
}
@@ -13714,7 +13597,7 @@ class TestHandler {
this.repeatingRun = 0;
this.tests = {};
if (typeof location !== 'undefined') {
this.opts = index.parse(location.search);
this.opts = index$2.parse(location.search);
if (this.opts.case != null) {
this.opts.case = Number(this.opts.case);
}
@@ -13855,7 +13738,7 @@ function createCommonjsModule$1(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var isBrowser = typeof index !== 'undefined';
var isBrowser = typeof index$2 !== 'undefined';
var environment = {
isBrowser: isBrowser
@@ -18404,8 +18287,8 @@ var stacktraceGps = createCommonjsModule$1(function (module, exports) {
* @returns {String} original representation of the base64-encoded string.
*/
function _atob(b64str) {
if (typeof index !== 'undefined' && index.atob) {
return index.atob(b64str);
if (typeof index$2 !== 'undefined' && index$2.atob) {
return index$2.atob(b64str);
} else {
throw new Error('You must supply a polyfill for window.atob in this environment');
}
@@ -18915,7 +18798,7 @@ var stacktrace = createCommonjsModule$1(function (module, exports) {
}));
});
index.stacktrace = stacktrace;
index$2.stacktrace = stacktrace;
function test (testDescription, ...args) {
let location = stacktrace.getSync()[1];
@@ -18924,6 +18807,9 @@ function test (testDescription, ...args) {
testHandler.register(testCase);
}
//# sourceMappingURL=cutest.mjs.map
test('set property', async function xml0 (t) {
var { users, xml0, xml1 } = await initArrays(t, { users: 2 });
xml0.setAttribute('height', 10);
@@ -19328,7 +19214,5 @@ test('y-xml: Random tests (1000)', async function xmlRandom1000 (t) {
await applyRandomTests(t, xmlTransactions, 1000);
});
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=y.test.js.map

File diff suppressed because one or more lines are too long