Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let tr = new Transform(ms.node("doc", null, [ms.node("paragraph", null, [ms.text("a")])]))
tr.replace(3, 3, ms.node("doc", null, [
ms.node("paragraph", null, [ms.text("b")], [ms.mark("em")])
]).slice(1, 3))
ist(tr.doc.childCount, 2)
ist(tr.doc.lastChild.marks.length, 1)
})
// A schema that enforces a heading and a body at the top level
let hbSchema = new Schema({
nodes: schema.spec.nodes.append({
doc: Object.assign({}, schema.spec.nodes.get("doc"), {content: "heading body"}),
body: {content: "block+"}
})
})
let hb = builders(hbSchema, {
p: {nodeType: "paragraph"},
b: {nodeType: "body"},
h: {nodeType: "heading", level: 1},
})
it("can unwrap a paragraph when replacing into a strict schema", () => {
let tr = new Transform(hb.doc(hb.h("Head"), hb.b(hb.p("Content"))))
tr.replace(0, tr.doc.content.size, tr.doc.slice(7, 16))
ist(tr.doc, hb.doc(hb.h("Content"), hb.b(hb.p())), eq)
})
it("can unwrap a body after a placed node", () => {
let tr = new Transform(hb.doc(hb.h("Head"), hb.b(hb.p("Content"))))
tr.replace(7, 7, tr.doc.slice(0, tr.doc.content.size))
ist(tr.doc, hb.doc(hb.h("Head"), hb.b(hb.h("Head"), hb.p("Content"), hb.p("Content"))), eq)
})
it("parses unique, non-exclusive, same-typed marks", () => {
let commentSchema = new Schema({
nodes: schema.spec.nodes,
marks: schema.spec.marks.update("comment", {
attrs: { id: { default: null }},
parseDOM: [{
tag: "span.comment",
getAttrs(dom) { return { id: parseInt(dom.getAttribute('data-id'), 10) } }
}],
excludes: '',
toDOM(mark) { return ["span", {class: "comment", "data-id": mark.attrs.id }, 0] }
})
})
let b = builders(commentSchema)
test(b.schema.nodes.doc.createAndFill(undefined, [
b.schema.nodes.paragraph.createAndFill(undefined, [
b.schema.text('double comment', [
b.schema.marks.comment.create({ id: 1 }),
b.schema.marks.comment.create({ id: 2 })
])
])
]),
"<p><span data-id="\"1\"" class="\"comment\""><span data-id="\"2\"" class="\"comment\"">double comment</span></span></p>")()
})
it("serializes non-spanning marks correctly", () => {
let markSchema = new Schema({
nodes: schema.spec.nodes,
marks: schema.spec.marks.update("test", {
parseDOM: [{tag: "test"}],
toDOM() { return ["test", 0] },
spanning: false
})
})
let b = builders(markSchema)
test(b.doc(b.paragraph(b.test("a", b.image({src: "x"}), "b"))),
"<p>a<img src="\"x\"">b</p>")()
})
it("serializes an element and an attribute with XML namespace", () => {
let xmlnsSchema = new Schema({
nodes: {
doc: { content: "svg*" }, text: {},
"svg": {
parseDOM: [{tag: "svg", namespace: 'http://www.w3.org/2000/svg'}],
group: 'block',
toDOM() { return ["http://www.w3.org/2000/svg svg", ["use", { "http://www.w3.org/1999/xlink href": "#svg-id" }]] },
},
},
})
let b = builders(xmlnsSchema)
let d = b.doc(b.svg())
test(d, "<svg></svg>", xmlDocument)()
let dom = xmlDocument.createElement('div')
dom.appendChild(DOMSerializer.fromSchema(xmlnsSchema).serializeFragment(d.content, {document: xmlDocument}))
ist(dom.querySelector('svg').namespaceURI, 'http://www.w3.org/2000/svg')
ist(dom.querySelector('use').namespaceURI, 'http://www.w3.org/2000/svg')
ist(dom.querySelector('use').attributes[0].namespaceURI, 'http://www.w3.org/1999/xlink')
})
it("can parse marks on block nodes", () => {
let commentSchema = new Schema({
nodes: schema.spec.nodes.update("doc", Object.assign({marks: "comment"}, schema.spec.nodes.get("doc"))),
marks: schema.spec.marks.update("comment", {
parseDOM: [{tag: "div.comment"}],
toDOM() { return ["div", {class: "comment"}, 0] }
})
})
let b = builders(commentSchema)
test(b.doc(b.paragraph("one"), b.comment(b.paragraph("two"), b.paragraph(b.strong("three"))), b.paragraph("four")),
"<p>one</p><div class="\"comment\""><p>two</p><p><strong>three</strong></p></div><p>four</p>")()
})
const {TextSelection, NodeSelection} = require("prosemirror-state")
const {schema: baseSchema} = require("prosemirror-schema-basic")
const {tableNodes, cellAround, CellSelection} = require("../dist/")
let schema = new Schema({
nodes: baseSchema.spec.nodes.append(tableNodes({
tableGroup: "block",
cellContent: "block+",
cellAttributes: {
test: {default: "default"}
}
})),
marks: baseSchema.spec.marks
})
let e = module.exports = require("prosemirror-test-builder").builders(schema, {
p: {nodeType: "paragraph"},
tr: {nodeType: "table_row"},
td: {nodeType: "table_cell"},
th: {nodeType: "table_header"}
})
e.c = function(colspan, rowspan) {
return e.td({colspan, rowspan}, e.p("x"))
}
e.c11 = e.c(1, 1)
e.cEmpty = e.td(e.p())
e.cCursor = e.td(e.p("x"))
e.cAnchor = e.td(e.p("x"))
e.cHead = e.td(e.p("x"))
e.h = function(colspan, rowspan) {
const {builders} = require("prosemirror-test-builder")
const {schema} = require("..")
module.exports = builders(schema, {
p: {nodeType: "paragraph"},
h1: {nodeType: "heading", level: 1},
h2: {nodeType: "heading", level: 2},
hr: {nodeType: "horizontal_rule"},
li: {nodeType: "list_item"},
ol: {nodeType: "ordered_list"},
ol3: {nodeType: "ordered_list", order: 3},
ul: {nodeType: "bullet_list"},
pre: {nodeType: "code_block"},
a: {markType: "link", href: "foo"},
br: {nodeType: "hard_break"},
img: {nodeType: "image", src: "img.png", alt: "x"}
})
})
),
flag: createNoteMark(
{
flag: "gu-flag",
correct: "gu-correct"
},
meta => ({
class: meta.hidden ? "note--collapsed" : "",
title: "Test"
})
),
})
});
const build = builders(noteSchema, {
p: {
markType: "paragraph"
}
});
const { doc, p } = build;
const note = (attrs = {}, content) =>
build.note(Object.assign({}, { meta: { type: "note" } }, attrs), content);
const flag = (attrs = {}, content) =>
build.flag(Object.assign({}, { meta: { type: "flag" } }, attrs), content);
const t = (...content) => doc(...content);
const selFor = initDoc => {
>,
GNodes extends string = string,
GMarks extends string = string
>(
testSchema: Schema,
names: GObj,
) => {
return pm.builders(testSchema, {
doc: { nodeType: 'doc' },
p: { nodeType: 'paragraph' },
text: { nodeType: 'text' },
...names,
});
};
const built = pm.builders(schema, {
doc: { nodeType: 'doc' },
p: { nodeType: 'paragraph' },
text: { nodeType: 'text' },
atomInline: { nodeType: 'atomInline' },
atomBlock: { nodeType: 'atomBlock' },
atomContainer: { nodeType: 'atomContainer' },
li: { nodeType: 'listItem' },
ul: { nodeType: 'bulletList' },
ol: { nodeType: 'orderedList' },
table: { nodeType: 'table' },
tr: { nodeType: 'table_row' },
td: { nodeType: 'table_cell' },
th: { nodeType: 'table_cell' },
blockquote: { nodeType: 'blockquote' },
h1: { nodeType: 'heading', level: 1 },
h2: { nodeType: 'heading', level: 2 },
export const pmBuild = <
GObj extends Record = Record<
string,
NodeTypeAttributes | MarkTypeAttributes
>,
GNodes extends string = string,
GMarks extends string = string
>(
testSchema: Schema,
names: GObj,
) => {
return pm.builders(testSchema, {
doc: { nodeType: 'doc' },
p: { nodeType: 'paragraph' },
text: { nodeType: 'text' },
...names,
});
};