1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 10:29:48 +02:00

add html serializer tests

This commit is contained in:
Ian Storm Taylor
2016-07-14 18:40:16 -07:00
parent 6e6fc116a2
commit f1a6066ec5
35 changed files with 556 additions and 75 deletions

View File

@@ -116,7 +116,7 @@ function deserializeNode(object) {
type: object.type,
data: object.data,
isVoid: object.isVoid,
nodes: Block.createList(object.nodes.map(deserializeNode))
nodes: Block.createList((object.nodes || []).map(deserializeNode))
})
}
case 'inline': {
@@ -124,12 +124,12 @@ function deserializeNode(object) {
type: object.type,
data: object.data,
isVoid: object.isVoid,
nodes: Inline.createList(object.nodes.map(deserializeNode))
nodes: Inline.createList((object.nodes || []).map(deserializeNode))
})
}
case 'text': {
return Text.create({
characters: deserializeRanges(object.ranges)
characters: object.ranges ? deserializeRanges(object.ranges) : ''
})
}
default: {

View File

@@ -0,0 +1,23 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'blockquote': {
return {
kind: 'block',
type: 'quote',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<blockquote><p>one</p></blockquote>

View File

@@ -0,0 +1,17 @@
nodes:
- type: quote
isVoid: false
data: {}
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,17 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
data: { key: 'value' },
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p>one</p>

View File

@@ -0,0 +1,14 @@
nodes:
- type: paragraph
isVoid: false
data:
key: value
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,16 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
isVoid: true
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p>one</p>

View File

@@ -0,0 +1,7 @@
nodes:
- type: paragraph
isVoid: true
data: {}
nodes:
- characters: []

View File

@@ -0,0 +1,16 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p>one</p>

View File

@@ -0,0 +1,13 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,30 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'a': {
return {
kind: 'inline',
type: 'link',
nodes: next(el.children)
}
}
case 'b': {
return {
kind: 'inline',
type: 'hashtag',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><a><b>one</b></a></p>

View File

@@ -0,0 +1,21 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: false
data: {}
nodes:
- type: hashtag
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,26 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'a': {
return {
kind: 'inline',
type: 'link',
nodes: next(el.children),
data: {
href: el.attribs.href
}
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><a href="https://google.com">one</a></p>

View File

@@ -0,0 +1,18 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: false
data:
href: https://google.com
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,23 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'a': {
return {
kind: 'inline',
type: 'link',
isVoid: true
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><a>one</a></p>

View File

@@ -0,0 +1,11 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: true
data: {}
nodes:
- characters: []

View File

@@ -0,0 +1,23 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'a': {
return {
kind: 'inline',
type: 'link',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><a>one</a></p>

View File

@@ -0,0 +1,17 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,30 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'em': {
return {
kind: 'mark',
type: 'italic',
nodes: next(el.children)
}
}
case 'strong': {
return {
kind: 'mark',
type: 'bold',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><em><strong>o</strong></em><em><strong>n</strong></em><em><strong>e</strong></em></p>

View File

@@ -0,0 +1,25 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks:
- type: bold
data: {}
- type: italic
data: {}
- text: n
marks:
- type: bold
data: {}
- type: italic
data: {}
- text: e
marks:
- type: bold
data: {}
- type: italic
data: {}

View File

@@ -0,0 +1,30 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'em': {
return {
kind: 'mark',
type: 'italic',
nodes: next(el.children)
}
}
case 'strong': {
return {
kind: 'mark',
type: 'bold',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><em><strong>one</strong></em></p>

View File

@@ -0,0 +1,25 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks:
- type: bold
data: {}
- type: italic
data: {}
- text: n
marks:
- type: bold
data: {}
- type: italic
data: {}
- text: e
marks:
- type: bold
data: {}
- type: italic
data: {}

View File

@@ -0,0 +1,23 @@
export default [
{
deserialize(el, next) {
switch (el.tagName) {
case 'p': {
return {
kind: 'block',
type: 'paragraph',
nodes: next(el.children)
}
}
case 'em': {
return {
kind: 'mark',
type: 'italic',
nodes: next(el.children)
}
}
}
}
}
]

View File

@@ -0,0 +1 @@
<p><em>one</em></p>

View File

@@ -0,0 +1,19 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks:
- type: italic
data: {}
- text: n
marks:
- type: italic
data: {}
- text: e
marks:
- type: italic
data: {}

View File

@@ -6,85 +6,111 @@ import { Html, Plain, Raw } from '../..'
import { equal, strictEqual } from '../helpers/assert-json'
import { resolve } from 'path'
/**
* Serializers.
*/
const SERIALIZERS = {
html: (dir) => {
const module = require(dir)
const html = new Html(module)
return {
extension: 'html',
deserialize: html.deserialize,
serialize: html.serialize,
read: file => fs.readFileSync(file, 'utf8').trim()
}
},
plain: (dir) => ({
extension: 'txt',
deserialize: Plain.deserialize,
serialize: Plain.serialize,
read: file => fs.readFileSync(file, 'utf8').trim()
}),
raw: (dir) => ({
extension: 'yaml',
deserialize: Raw.deserialize,
serialize: Raw.serialize,
read: file => readMetadata.sync(file)
})
}
/**
* Tests.
*/
describe('serializers', () => {
const serializers = fs.readdirSync(resolve(__dirname, './fixtures'))
describe('html', () => {
describe('deserialize()', () => {
const dir = resolve(__dirname, './fixtures/html/deserialize')
const tests = fs.readdirSync(dir)
for (const serializer of serializers) {
describe(serializer, () => {
describe('deserialize()', () => {
const dir = resolve(__dirname, './fixtures', serializer, 'deserialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const Serializer = SERIALIZERS[serializer](innerDir)
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const input = Serializer.read(resolve(innerDir, `input.${Serializer.extension}`))
const state = Serializer.deserialize(input)
const json = state.document.toJS()
strictEqual(clean(json), expected)
})
}
})
describe('serialize()', () => {
const dir = resolve(__dirname, './fixtures', serializer, 'serialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const Serializer = SERIALIZERS[serializer](innerDir)
const input = require(resolve(innerDir, 'input.js')).default
const expected = Serializer.read(resolve(innerDir, `output.${Serializer.extension}`))
const serialized = Serializer.serialize(input)
debugger
strictEqual(serialized, expected)
})
}
})
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const html = new Html(require(innerDir).default)
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const input = fs.readFileSync(resolve(innerDir, 'input.html'), 'utf8')
const state = html.deserialize(input)
const json = state.document.toJS()
strictEqual(clean(json), expected)
})
}
})
}
describe('serialize()', () => {
const dir = resolve(__dirname, './fixtures/html/serialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const html = new Html(require(innerDir).default)
const input = require(resolve(innerDir, 'input.js')).default
const expected = fs.readFileSync(resolve(innerDir, 'output.html'), 'utf8')
const serialized = html.serialize(input)
strictEqual(serialized, expected.trim())
})
}
})
})
describe('plain', () => {
describe('deserialize()', () => {
const dir = resolve(__dirname, './fixtures/plain/deserialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const input = fs.readFileSync(resolve(innerDir, 'input.txt'), 'utf8')
const state = Plain.deserialize(input.trim())
const json = state.document.toJS()
strictEqual(clean(json), expected)
})
}
})
describe('serialize()', () => {
const dir = resolve(__dirname, './fixtures/plain/serialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const input = require(resolve(innerDir, 'input.js')).default
const expected = fs.readFileSync(resolve(innerDir, 'output.txt'), 'utf8')
const serialized = Plain.serialize(input)
strictEqual(serialized, expected.trim())
})
}
})
})
describe('raw', () => {
describe('deserialize()', () => {
const dir = resolve(__dirname, './fixtures/raw/deserialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const input = readMetadata.sync(resolve(innerDir, 'input.yaml'))
const state = Raw.deserialize(input)
const json = state.document.toJS()
strictEqual(clean(json), expected)
})
}
})
describe('serialize()', () => {
const dir = resolve(__dirname, './fixtures/raw/serialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const input = require(resolve(innerDir, 'input.js')).default
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const serialized = Raw.serialize(input)
strictEqual(serialized, expected)
})
}
})
})
})
/**