json.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* eslint-disable
  2. no-return-assign,
  3. no-undef,
  4. no-useless-catch,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. // This is the implementation of the JSON OT type.
  16. //
  17. // Spec is here: https://github.com/josephg/ShareJS/wiki/JSON-Operations
  18. let text
  19. if (typeof WEB !== 'undefined' && WEB !== null) {
  20. ;({ text } = exports.types)
  21. } else {
  22. text = require('./text')
  23. }
  24. const json = {}
  25. json.name = 'json'
  26. json.create = () => null
  27. json.invertComponent = function (c) {
  28. const c_ = { p: c.p }
  29. if (c.si !== undefined) {
  30. c_.sd = c.si
  31. }
  32. if (c.sd !== undefined) {
  33. c_.si = c.sd
  34. }
  35. if (c.oi !== undefined) {
  36. c_.od = c.oi
  37. }
  38. if (c.od !== undefined) {
  39. c_.oi = c.od
  40. }
  41. if (c.li !== undefined) {
  42. c_.ld = c.li
  43. }
  44. if (c.ld !== undefined) {
  45. c_.li = c.ld
  46. }
  47. if (c.na !== undefined) {
  48. c_.na = -c.na
  49. }
  50. if (c.lm !== undefined) {
  51. c_.lm = c.p[c.p.length - 1]
  52. c_.p = c.p.slice(0, c.p.length - 1).concat([c.lm])
  53. }
  54. return c_
  55. }
  56. json.invert = op =>
  57. Array.from(op.slice().reverse()).map(c => json.invertComponent(c))
  58. json.checkValidOp = function (op) {}
  59. const isArray = o => Object.prototype.toString.call(o) === '[object Array]'
  60. json.checkList = function (elem) {
  61. if (!isArray(elem)) {
  62. throw new Error('Referenced element not a list')
  63. }
  64. }
  65. json.checkObj = function (elem) {
  66. if (elem.constructor !== Object) {
  67. throw new Error(
  68. `Referenced element not an object (it was ${JSON.stringify(elem)})`
  69. )
  70. }
  71. }
  72. json.apply = function (snapshot, op) {
  73. json.checkValidOp(op)
  74. op = clone(op)
  75. const container = { data: clone(snapshot) }
  76. try {
  77. for (let i = 0; i < op.length; i++) {
  78. const c = op[i]
  79. let parent = null
  80. let parentkey = null
  81. let elem = container
  82. let key = 'data'
  83. for (const p of Array.from(c.p)) {
  84. parent = elem
  85. parentkey = key
  86. elem = elem[key]
  87. key = p
  88. if (parent == null) {
  89. throw new Error('Path invalid')
  90. }
  91. }
  92. if (c.na !== undefined) {
  93. // Number add
  94. if (typeof elem[key] !== 'number') {
  95. throw new Error('Referenced element not a number')
  96. }
  97. elem[key] += c.na
  98. } else if (c.si !== undefined) {
  99. // String insert
  100. if (typeof elem !== 'string') {
  101. throw new Error(
  102. `Referenced element not a string (it was ${JSON.stringify(elem)})`
  103. )
  104. }
  105. parent[parentkey] = elem.slice(0, key) + c.si + elem.slice(key)
  106. } else if (c.sd !== undefined) {
  107. // String delete
  108. if (typeof elem !== 'string') {
  109. throw new Error('Referenced element not a string')
  110. }
  111. if (elem.slice(key, key + c.sd.length) !== c.sd) {
  112. throw new Error('Deleted string does not match')
  113. }
  114. parent[parentkey] = elem.slice(0, key) + elem.slice(key + c.sd.length)
  115. } else if (c.li !== undefined && c.ld !== undefined) {
  116. // List replace
  117. json.checkList(elem)
  118. // Should check the list element matches c.ld
  119. elem[key] = c.li
  120. } else if (c.li !== undefined) {
  121. // List insert
  122. json.checkList(elem)
  123. elem.splice(key, 0, c.li)
  124. } else if (c.ld !== undefined) {
  125. // List delete
  126. json.checkList(elem)
  127. // Should check the list element matches c.ld here too.
  128. elem.splice(key, 1)
  129. } else if (c.lm !== undefined) {
  130. // List move
  131. json.checkList(elem)
  132. if (c.lm !== key) {
  133. const e = elem[key]
  134. // Remove it...
  135. elem.splice(key, 1)
  136. // And insert it back.
  137. elem.splice(c.lm, 0, e)
  138. }
  139. } else if (c.oi !== undefined) {
  140. // Object insert / replace
  141. json.checkObj(elem)
  142. // Should check that elem[key] == c.od
  143. elem[key] = c.oi
  144. } else if (c.od !== undefined) {
  145. // Object delete
  146. json.checkObj(elem)
  147. // Should check that elem[key] == c.od
  148. delete elem[key]
  149. } else {
  150. throw new Error('invalid / missing instruction in op')
  151. }
  152. }
  153. } catch (error) {
  154. // TODO: Roll back all already applied changes. Write tests before implementing this code.
  155. throw error
  156. }
  157. return container.data
  158. }
  159. // Checks if two paths, p1 and p2 match.
  160. json.pathMatches = function (p1, p2, ignoreLast) {
  161. if (p1.length !== p2.length) {
  162. return false
  163. }
  164. for (let i = 0; i < p1.length; i++) {
  165. const p = p1[i]
  166. if (p !== p2[i] && (!ignoreLast || i !== p1.length - 1)) {
  167. return false
  168. }
  169. }
  170. return true
  171. }
  172. json.append = function (dest, c) {
  173. let last
  174. c = clone(c)
  175. if (
  176. dest.length !== 0 &&
  177. json.pathMatches(c.p, (last = dest[dest.length - 1]).p)
  178. ) {
  179. if (last.na !== undefined && c.na !== undefined) {
  180. return (dest[dest.length - 1] = { p: last.p, na: last.na + c.na })
  181. } else if (
  182. last.li !== undefined &&
  183. c.li === undefined &&
  184. c.ld === last.li
  185. ) {
  186. // insert immediately followed by delete becomes a noop.
  187. if (last.ld !== undefined) {
  188. // leave the delete part of the replace
  189. return delete last.li
  190. } else {
  191. return dest.pop()
  192. }
  193. } else if (
  194. last.od !== undefined &&
  195. last.oi === undefined &&
  196. c.oi !== undefined &&
  197. c.od === undefined
  198. ) {
  199. return (last.oi = c.oi)
  200. } else if (c.lm !== undefined && c.p[c.p.length - 1] === c.lm) {
  201. return null // don't do anything
  202. } else {
  203. return dest.push(c)
  204. }
  205. } else {
  206. return dest.push(c)
  207. }
  208. }
  209. json.compose = function (op1, op2) {
  210. json.checkValidOp(op1)
  211. json.checkValidOp(op2)
  212. const newOp = clone(op1)
  213. for (const c of Array.from(op2)) {
  214. json.append(newOp, c)
  215. }
  216. return newOp
  217. }
  218. json.normalize = function (op) {
  219. const newOp = []
  220. if (!isArray(op)) {
  221. op = [op]
  222. }
  223. for (const c of Array.from(op)) {
  224. if (c.p == null) {
  225. c.p = []
  226. }
  227. json.append(newOp, c)
  228. }
  229. return newOp
  230. }
  231. // hax, copied from test/types/json. Apparently this is still the fastest way to deep clone an object, assuming
  232. // we have browser support for JSON.
  233. // http://jsperf.com/cloning-an-object/12
  234. const clone = o => JSON.parse(JSON.stringify(o))
  235. json.commonPath = function (p1, p2) {
  236. p1 = p1.slice()
  237. p2 = p2.slice()
  238. p1.unshift('data')
  239. p2.unshift('data')
  240. p1 = p1.slice(0, p1.length - 1)
  241. p2 = p2.slice(0, p2.length - 1)
  242. if (p2.length === 0) {
  243. return -1
  244. }
  245. let i = 0
  246. while (p1[i] === p2[i] && i < p1.length) {
  247. i++
  248. if (i === p2.length) {
  249. return i - 1
  250. }
  251. }
  252. }
  253. // transform c so it applies to a document with otherC applied.
  254. json.transformComponent = function (dest, c, otherC, type) {
  255. let oc
  256. c = clone(c)
  257. if (c.na !== undefined) {
  258. c.p.push(0)
  259. }
  260. if (otherC.na !== undefined) {
  261. otherC.p.push(0)
  262. }
  263. const common = json.commonPath(c.p, otherC.p)
  264. const common2 = json.commonPath(otherC.p, c.p)
  265. const cplength = c.p.length
  266. const otherCplength = otherC.p.length
  267. if (c.na !== undefined) {
  268. c.p.pop()
  269. } // hax
  270. if (otherC.na !== undefined) {
  271. otherC.p.pop()
  272. }
  273. if (otherC.na) {
  274. if (
  275. common2 != null &&
  276. otherCplength >= cplength &&
  277. otherC.p[common2] === c.p[common2]
  278. ) {
  279. if (c.ld !== undefined) {
  280. oc = clone(otherC)
  281. oc.p = oc.p.slice(cplength)
  282. c.ld = json.apply(clone(c.ld), [oc])
  283. } else if (c.od !== undefined) {
  284. oc = clone(otherC)
  285. oc.p = oc.p.slice(cplength)
  286. c.od = json.apply(clone(c.od), [oc])
  287. }
  288. }
  289. json.append(dest, c)
  290. return dest
  291. }
  292. if (
  293. common2 != null &&
  294. otherCplength > cplength &&
  295. c.p[common2] === otherC.p[common2]
  296. ) {
  297. // transform based on c
  298. if (c.ld !== undefined) {
  299. oc = clone(otherC)
  300. oc.p = oc.p.slice(cplength)
  301. c.ld = json.apply(clone(c.ld), [oc])
  302. } else if (c.od !== undefined) {
  303. oc = clone(otherC)
  304. oc.p = oc.p.slice(cplength)
  305. c.od = json.apply(clone(c.od), [oc])
  306. }
  307. }
  308. if (common != null) {
  309. let from, p, to
  310. const commonOperand = cplength === otherCplength
  311. // transform based on otherC
  312. if (otherC.na !== undefined) {
  313. // this case is handled above due to icky path hax
  314. } else if (otherC.si !== undefined || otherC.sd !== undefined) {
  315. // String op vs string op - pass through to text type
  316. if (c.si !== undefined || c.sd !== undefined) {
  317. if (!commonOperand) {
  318. throw new Error('must be a string?')
  319. }
  320. // Convert an op component to a text op component
  321. const convert = function (component) {
  322. const newC = { p: component.p[component.p.length - 1] }
  323. if (component.si) {
  324. newC.i = component.si
  325. } else {
  326. newC.d = component.sd
  327. }
  328. return newC
  329. }
  330. const tc1 = convert(c)
  331. const tc2 = convert(otherC)
  332. const res = []
  333. text._tc(res, tc1, tc2, type)
  334. for (const tc of Array.from(res)) {
  335. const jc = { p: c.p.slice(0, common) }
  336. jc.p.push(tc.p)
  337. if (tc.i != null) {
  338. jc.si = tc.i
  339. }
  340. if (tc.d != null) {
  341. jc.sd = tc.d
  342. }
  343. json.append(dest, jc)
  344. }
  345. return dest
  346. }
  347. } else if (otherC.li !== undefined && otherC.ld !== undefined) {
  348. if (otherC.p[common] === c.p[common]) {
  349. // noop
  350. if (!commonOperand) {
  351. // we're below the deleted element, so -> noop
  352. return dest
  353. } else if (c.ld !== undefined) {
  354. // we're trying to delete the same element, -> noop
  355. if (c.li !== undefined && type === 'left') {
  356. // we're both replacing one element with another. only one can
  357. // survive!
  358. c.ld = clone(otherC.li)
  359. } else {
  360. return dest
  361. }
  362. }
  363. }
  364. } else if (otherC.li !== undefined) {
  365. if (
  366. c.li !== undefined &&
  367. c.ld === undefined &&
  368. commonOperand &&
  369. c.p[common] === otherC.p[common]
  370. ) {
  371. // in li vs. li, left wins.
  372. if (type === 'right') {
  373. c.p[common]++
  374. }
  375. } else if (otherC.p[common] <= c.p[common]) {
  376. c.p[common]++
  377. }
  378. if (c.lm !== undefined) {
  379. if (commonOperand) {
  380. // otherC edits the same list we edit
  381. if (otherC.p[common] <= c.lm) {
  382. c.lm++
  383. }
  384. }
  385. }
  386. // changing c.from is handled above.
  387. } else if (otherC.ld !== undefined) {
  388. if (c.lm !== undefined) {
  389. if (commonOperand) {
  390. if (otherC.p[common] === c.p[common]) {
  391. // they deleted the thing we're trying to move
  392. return dest
  393. }
  394. // otherC edits the same list we edit
  395. p = otherC.p[common]
  396. from = c.p[common]
  397. to = c.lm
  398. if (p < to || (p === to && from < to)) {
  399. c.lm--
  400. }
  401. }
  402. }
  403. if (otherC.p[common] < c.p[common]) {
  404. c.p[common]--
  405. } else if (otherC.p[common] === c.p[common]) {
  406. if (otherCplength < cplength) {
  407. // we're below the deleted element, so -> noop
  408. return dest
  409. } else if (c.ld !== undefined) {
  410. if (c.li !== undefined) {
  411. // we're replacing, they're deleting. we become an insert.
  412. delete c.ld
  413. } else {
  414. // we're trying to delete the same element, -> noop
  415. return dest
  416. }
  417. }
  418. }
  419. } else if (otherC.lm !== undefined) {
  420. if (c.lm !== undefined && cplength === otherCplength) {
  421. // lm vs lm, here we go!
  422. from = c.p[common]
  423. to = c.lm
  424. const otherFrom = otherC.p[common]
  425. const otherTo = otherC.lm
  426. if (otherFrom !== otherTo) {
  427. // if otherFrom == otherTo, we don't need to change our op.
  428. // where did my thing go?
  429. if (from === otherFrom) {
  430. // they moved it! tie break.
  431. if (type === 'left') {
  432. c.p[common] = otherTo
  433. if (from === to) {
  434. // ugh
  435. c.lm = otherTo
  436. }
  437. } else {
  438. return dest
  439. }
  440. } else {
  441. // they moved around it
  442. if (from > otherFrom) {
  443. c.p[common]--
  444. }
  445. if (from > otherTo) {
  446. c.p[common]++
  447. } else if (from === otherTo) {
  448. if (otherFrom > otherTo) {
  449. c.p[common]++
  450. if (from === to) {
  451. // ugh, again
  452. c.lm++
  453. }
  454. }
  455. }
  456. // step 2: where am i going to put it?
  457. if (to > otherFrom) {
  458. c.lm--
  459. } else if (to === otherFrom) {
  460. if (to > from) {
  461. c.lm--
  462. }
  463. }
  464. if (to > otherTo) {
  465. c.lm++
  466. } else if (to === otherTo) {
  467. // if we're both moving in the same direction, tie break
  468. if (
  469. (otherTo > otherFrom && to > from) ||
  470. (otherTo < otherFrom && to < from)
  471. ) {
  472. if (type === 'right') {
  473. c.lm++
  474. }
  475. } else {
  476. if (to > from) {
  477. c.lm++
  478. } else if (to === otherFrom) {
  479. c.lm--
  480. }
  481. }
  482. }
  483. }
  484. }
  485. } else if (c.li !== undefined && c.ld === undefined && commonOperand) {
  486. // li
  487. from = otherC.p[common]
  488. to = otherC.lm
  489. p = c.p[common]
  490. if (p > from) {
  491. c.p[common]--
  492. }
  493. if (p > to) {
  494. c.p[common]++
  495. }
  496. } else {
  497. // ld, ld+li, si, sd, na, oi, od, oi+od, any li on an element beneath
  498. // the lm
  499. //
  500. // i.e. things care about where their item is after the move.
  501. from = otherC.p[common]
  502. to = otherC.lm
  503. p = c.p[common]
  504. if (p === from) {
  505. c.p[common] = to
  506. } else {
  507. if (p > from) {
  508. c.p[common]--
  509. }
  510. if (p > to) {
  511. c.p[common]++
  512. } else if (p === to) {
  513. if (from > to) {
  514. c.p[common]++
  515. }
  516. }
  517. }
  518. }
  519. } else if (otherC.oi !== undefined && otherC.od !== undefined) {
  520. if (c.p[common] === otherC.p[common]) {
  521. if (c.oi !== undefined && commonOperand) {
  522. // we inserted where someone else replaced
  523. if (type === 'right') {
  524. // left wins
  525. return dest
  526. } else {
  527. // we win, make our op replace what they inserted
  528. c.od = otherC.oi
  529. }
  530. } else {
  531. // -> noop if the other component is deleting the same object (or any
  532. // parent)
  533. return dest
  534. }
  535. }
  536. } else if (otherC.oi !== undefined) {
  537. if (c.oi !== undefined && c.p[common] === otherC.p[common]) {
  538. // left wins if we try to insert at the same place
  539. if (type === 'left') {
  540. json.append(dest, { p: c.p, od: otherC.oi })
  541. } else {
  542. return dest
  543. }
  544. }
  545. } else if (otherC.od !== undefined) {
  546. if (c.p[common] === otherC.p[common]) {
  547. if (!commonOperand) {
  548. return dest
  549. }
  550. if (c.oi !== undefined) {
  551. delete c.od
  552. } else {
  553. return dest
  554. }
  555. }
  556. }
  557. }
  558. json.append(dest, c)
  559. return dest
  560. }
  561. if (typeof WEB !== 'undefined' && WEB !== null) {
  562. if (!exports.types) {
  563. exports.types = {}
  564. }
  565. // This is kind of awful - come up with a better way to hook this helper code up.
  566. exports._bt(json, json.transformComponent, json.checkValidOp, json.append)
  567. // [] is used to prevent closure from renaming types.text
  568. exports.types.json = json
  569. } else {
  570. module.exports = json
  571. require('./helpers').bootstrapTransform(
  572. json,
  573. json.transformComponent,
  574. json.checkValidOp,
  575. json.append
  576. )
  577. }