Skip to content

Commit

Permalink
Move Mark-deserializing into Mark.fromJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Sep 12, 2016
1 parent 9a7866c commit c02f428
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
27 changes: 19 additions & 8 deletions src/mark.js
Expand Up @@ -16,14 +16,6 @@ class Mark {
this.attrs = attrs
}

// :: () → Object
// Convert this mark to a JSON-serializeable representation.
toJSON() {
let obj = {_: this.type.name}
for (let attr in this.attrs) obj[attr] = this.attrs[attr]
return obj
}

// :: ([Mark]) → [Mark]
// Given a set of marks, create a new set which contains this one as
// well, in the right position. If this mark is already in the set,
Expand Down Expand Up @@ -73,6 +65,25 @@ class Mark {
return true
}

// :: () → Object
// Convert this mark to a JSON-serializeable representation.
toJSON() {
let obj = {_: this.type.name}
for (let attr in this.attrs) obj[attr] = this.attrs[attr]
return obj
}

// :: (Schema, Object) → Mark
static fromJSON(schema, json) {
let type = schema.marks[json._]
let attrs = null
for (let prop in json) if (prop != "_") {
if (!attrs) attrs = Object.create(null)
attrs[prop] = json[prop]
}
return attrs ? type.create(attrs) : type.instance
}

// :: ([Mark], [Mark]) → bool
// Test whether two sets of marks are identical.
static sameSet(a, b) {
Expand Down
8 changes: 1 addition & 7 deletions src/schema.js
Expand Up @@ -452,13 +452,7 @@ class Schema {
// Deserialize a mark from its JSON representation. This method is
// bound.
markFromJSON(json) {
let type = this.marks[json._]
let attrs = null
for (let prop in json) if (prop != "_") {
if (!attrs) attrs = Object.create(null)
attrs[prop] = json[prop]
}
return attrs ? type.create(attrs) : type.instance
return Mark.fromJSON(this, json)
}

// :: (string) → NodeType
Expand Down

0 comments on commit c02f428

Please sign in to comment.