def test_can_remote_multiple_excluded_marks(): schema = Schema({ "nodes": { "doc": { "content": "text*" }, "text": {} }, "marks": { "big": { "excludes": "small1 small2" }, "small1": {}, "small2": {} }, }) tr = Transform( schema.node( "doc", None, schema.text("hi", [schema.mark("small1"), schema.mark("small2")]), )) assert len(tr.doc.first_child.marks) == 2 tr.add_mark(0, 2, schema.mark("big")) assert len(tr.doc.first_child.marks) == 1
class TestTopLevelMarkReplace: ms = Schema({ "nodes": { **schema.spec["nodes"], **({ "doc": { **schema.spec["nodes"].get("doc"), "marks": "_" } }), }, "marks": schema.spec["marks"], }) def test_preserves_mark_on_block_nodes(self): ms = self.ms tr = Transform( ms.node( "doc", None, [ ms.node("paragraph", None, [ms.text("hey")], [ms.mark("em")]), ms.node("paragraph", None, [ms.text("ok")], [ms.mark("strong")]), ], )) tr.replace(2, 7, tr.doc.slice(2, 7)) assert tr.doc.eq(tr.before) def test_preserves_marks_on_open_slice_block_nodes(self): ms = self.ms tr = Transform( ms.node("doc", None, [ms.node("paragraph", None, [ms.text("a")])])) tr.replace( 3, 3, ms.node( "doc", None, [ms.node("paragraph", None, [ms.text("b")], [ms.mark("em")])], ).slice(1, 3), ) assert tr.doc.child_count == 2 assert len(tr.doc.last_child.marks) == 1
def test_does_not_remove_non_excluded_marks_of_the_same_type(): schema = Schema({ "nodes": { "doc": { "content": "text*" }, "text": {} }, "marks": { "comment": { "excludes": "", "attrs": { "id": {} } } }, }) tr = Transform( schema.node("doc", None, schema.text("hi", [schema.mark("comment", {"id": 10})]))) tr.add_mark(0, 2, schema.mark("comment", {"id": 20})) assert len(tr.doc.first_child.marks) == 2
"parseDOM": [{ "tag": "i" }, { "tag": "em" }, { "style": "font-style=italic" }], "toDOM": const(["em", 0]), }, "strong": { "parseDOM": [{ "tag": "strong" }, { "tag": "b" }, { "style": "font-weight" }], "toDOM": const(["strong", 0]), }, "code": { "parseDOM": [{ "tag": "code" }], "toDOM": const(["code", 0]) }, }, } schema = Schema(spec)
class TestEnforcingHeadingAndBody: nodes_sepc = schema.spec["nodes"].copy() nodes_sepc.update({ "doc": { **nodes_sepc["doc"], "content": "heading body" }, "body": { "content": "block+" }, }) hb_schema = Schema({"nodes": nodes_sepc, "marks": schema.spec["marks"]}) hb = builders( hb_schema, { "p": { "nodeType": "paragraph" }, "b": { "nodeType": "body" }, "h": { "nodeType": "heading", "level": 1 }, }, ) def test_can_unwrap_a_paragraph_when_replacing_into_a_strict_schema(self): hb = self.hb tr = Transform(hb["doc"](hb["h"]("Head"), hb["b"](hb["p"]("Content")))) tr.replace(0, tr.doc.content.size, tr.doc.slice(7, 16)) assert tr.doc.eq(hb["doc"](hb["h"]("Content"), hb["b"](hb["p"]()))) def test_can_unwrap_a_body_after_a_placed_node(self): hb = self.hb doc = hb["doc"] h = hb["h"] b = hb["b"] p = hb["p"] tr = Transform(hb["doc"](hb["h"]("Head"), hb["b"](hb["p"]("Content")))) tr.replace(7, 7, tr.doc.slice(0, tr.doc.content.size)) assert tr.doc.eq( doc(h("Head"), b(h("Head"), p("Content"), p("Content")))) def test_can_wrap_a_paragraph_in_a_body_even_when_its_not_the_first_node( self): hb = self.hb doc = hb["doc"] h = hb["h"] b = hb["b"] p = hb["p"] tr = Transform(doc(h("Head"), b(p("One"), p("Two")))) tr.replace(0, tr.doc.content.size, tr.doc.slice(8, 16)) assert tr.doc.eq(doc(h("One"), b(p("Two")))) def test_can_split_a_fragment_and_place_its_children_in_different_parents( self): hb = self.hb doc = hb["doc"] h = hb["h"] b = hb["b"] p = hb["p"] tr = Transform(doc(h("Head"), b(h("One"), p("Two")))) tr.replace(0, tr.doc.content.size, tr.doc.slice(7, 17)) assert tr.doc.eq(doc(h("One"), b(p("Two")))) def test_will_insert_filler_nodes_before_a_node_when_necessary(self): hb = self.hb doc = hb["doc"] h = hb["h"] b = hb["b"] p = hb["p"] tr = Transform(doc(h("Head"), b(p("One")))) tr.replace(0, tr.doc.content.size, tr.doc.slice(6, tr.doc.content.size)) assert tr.doc.eq(doc(h(), b(p("One"))))
import os import json from copy import deepcopy from prosemirror.model import Node, Schema from prosemirror.transform import Step from django.conf import settings schema_json_path = os.path.join(settings.PROJECT_PATH, "static-libs/json/schema.json") if os.path.exists(schema_json_path): with open(schema_json_path) as schema_file: schema = Schema(json.loads(schema_file.read())) else: schema = None def from_json(json): return Node.from_json(schema, json) def apply(steps, node): for step_obj in steps: step = Step.from_json(schema, step_obj) step_result = step.apply(node) if step_result.ok: node = step_result.doc else: return False
def test_doesnt_return_true_when_the_split_off_content_doesnt_fit_in_the_given_node_type( self): s = Schema({ "nodes": { "doc": { "content": "chapter+" }, "para": { "content": "text*", "group": "block" }, "head": { "content": "text*", "marks": "" }, "figure": { "content": "caption figureimage", "group": "block" }, "quote": { "content": "block+", "group": "block" }, "figureimage": {}, "caption": { "content": "text*", "marks": "" }, "sect": { "content": "head block* sect*" }, "closing": { "content": "text*" }, "text": { "group": "inline" }, "fixed": { "content": "head para closing", "group": "block" }, "title": { "content": "text*" }, "chapter": { "content": "title scene+" }, "scene": { "content": "para+" }, } }) assert not can_split( s.node( "doc", None, s.node( "chapter", None, [ s.node("title", None, s.text("title")), s.node("scene", None, s.node("para", None, s.text("scene"))), ], ), ), 4, 1, [{ "type": s.nodes["scene"] }], )
schema = Schema({ "nodes": { "doc": { "content": "head? block* sect* closing?" }, "para": { "content": "text*", "group": "block" }, "head": { "content": "text*", "marks": "" }, "figure": { "content": "caption figureimage", "group": "block" }, "quote": { "content": "block+", "group": "block" }, "figureimage": {}, "caption": { "content": "text*", "marks": "" }, "sect": { "content": "head block* sect*" }, "closing": { "content": "text*" }, "text": { "group": "inline" }, "fixed": { "content": "head para closing", "group": "block" }, }, "marks": { "em": {} }, })
from prosemirror.model import Schema from prosemirror.schema.basic import schema from prosemirror.schema.list import add_list_nodes from .build import builders test_schema = Schema({ "nodes": add_list_nodes(schema.spec["nodes"], "paragraph block*", "block"), "marks": schema.spec["marks"], }) out = builders( test_schema, { "p": { "nodeType": "paragraph" }, "pre": { "nodeType": "code_block" }, "h1": { "nodeType": "heading", "level": 1 }, "h2": { "nodeType": "heading", "level": 2 }, "h3": {
"parseDOM": [{ "tag": "i" }, { "tag": "em" }, { "style": "font-style=italic" }] }, "strong": { "parseDOM": [{ "tag": "strong" }, { "tag": "b" }, { "style": "font-weight" }] }, "code": { "parseDOM": [{ "tag": "code" }] }, }, } basic_schema = Schema(basic_spec) basic_doc = json.loads( '{"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Fellow"}]},{"type":"paragraph","content":[{"type":"text","text":"Test "},{"type":"text","marks":[{"type":"strong"}],"text":"this"},{"type":"text","text":" text"}]}]}' ) doc_node = Node.from_json(basic_schema, basic_doc)
custom_schema = Schema({ "nodes": { "doc": { "content": "paragraph+" }, "paragraph": { "content": "text*" }, "text": {}, }, "marks": { "remark": { "attrs": { "id": {} }, "excludes": "", "inclusive": False }, "user": { "attrs": { "id": {} }, "excludes": "_" }, "strong": { "excludes": "em-group" }, "em": { "group": "em-group" }, }, })