Skip to content

Commit

Permalink
Merge pull request #270 from johanneswilm/main
Browse files Browse the repository at this point in the history
add docAttr step
  • Loading branch information
sciyoshi committed Nov 2, 2023
2 parents a367867 + fff67f9 commit c8b31ae
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This package provides Python implementations of the following
[ProseMirror](https://prosemirror.net/) packages:

- [`prosemirror-model`](https://github.com/ProseMirror/prosemirror-model) version 1.18.1
- [`prosemirror-transform`](https://github.com/ProseMirror/prosemirror-transform) version 1.7.5
- [`prosemirror-transform`](https://github.com/ProseMirror/prosemirror-transform) version 1.8.0
- [`prosemirror-test-builder`](https://github.com/ProseMirror/prosemirror-test-builder)
- [`prosemirror-schema-basic`](https://github.com/ProseMirror/prosemirror-schema-basic) version 1.1.2
- [`prosemirror-schema-list`](https://github.com/ProseMirror/prosemirror-schema-list)
Expand Down
15 changes: 14 additions & 1 deletion prosemirror/test_builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@

from .build import builders

nodes = add_list_nodes(_schema.spec["nodes"], "paragraph block*", "block")

nodes.update(
{
"doc": {
"content": "block+",
"attrs": {"meta": {"default": None}},
}
}
)

test_schema = Schema(
{
"nodes": add_list_nodes(_schema.spec["nodes"], "paragraph block*", "block"),
"nodes": nodes,
"marks": _schema.spec["marks"],
}
)
Expand All @@ -15,6 +26,8 @@
test_schema,
{
"doc": {"nodeType": "doc"},
"docMetaOne": {"nodeType": "doc", "meta": 1},
"docMetaTwo": {"nodeType": "doc", "meta": 2},
"p": {"nodeType": "paragraph"},
"pre": {"nodeType": "code_block"},
"h1": {"nodeType": "heading", "level": 1},
Expand Down
47 changes: 47 additions & 0 deletions prosemirror/transform/doc_attr_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from .step import Step, StepMap, StepResult


class DocAttrStep(Step):
def __init__(self, attr, value):
super().__init__()
self.attr = attr
self.value = value

def apply(self, doc):
attrs = {}
for name in doc.attrs:
attrs[name] = doc.attrs[name]
attrs[self.attr] = self.value
updated = doc.type.create(attrs, doc.content, doc.marks)
return StepResult.ok(updated)

def get_map(self):
return StepMap.empty

def invert(self, doc):
return DocAttrStep(self.attr, doc.attrs[self.attr])

def map(self, mapping):
return self

def to_json(self):
json_data = {
"stepType": "docAttr",
"attr": self.attr,
"value": self.value,
}

return json_data

@staticmethod
def from_json(schema, json_data):
if isinstance(json_data, str):
import json

json_data = json.loads(json_data)
if not isinstance(json_data["attr"], str):
raise ValueError("Invalid input for DocAttrStep.from_json")
return DocAttrStep(json_data["attr"], json_data["value"])


Step.json_id("docAttr", DocAttrStep)
4 changes: 4 additions & 0 deletions prosemirror/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from . import replace, structure
from .attr_step import AttrStep
from .doc_attr_step import DocAttrStep
from .map import Mapping
from .mark_step import AddMarkStep, AddNodeMarkStep, RemoveMarkStep, RemoveNodeMarkStep
from .replace import close_fragment, covered_depths, fits_trivially, replace_step
Expand Down Expand Up @@ -482,6 +483,9 @@ def set_node_markup(self, pos, type, attrs, marks=None):
def set_node_attribute(self, pos, attr, value):
return self.step(AttrStep(pos, attr, value))

def set_doc_attribute(self, attr, value):
return self.step(DocAttrStep(attr, value))

def add_node_mark(self, pos, mark):
return self.step(AddNodeMarkStep(pos, mark))

Expand Down
15 changes: 15 additions & 0 deletions tests/prosemirror_transform/tests/test_trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from prosemirror.transform import Transform, TransformError, find_wrapping, lift_target

doc = out["doc"]
docMetaOne = out["docMetaOne"]
docMetaTwo = out["docMetaTwo"]
blockquote = out["blockquote"]
pre = out["pre"]
h1 = out["h1"]
Expand Down Expand Up @@ -568,6 +570,19 @@ def test_set_node_attribute(doc, expect, attr, value, test_transform):
test_transform(tr, expect)


@pytest.mark.parametrize(
"doc,expect,attr,value",
[
(doc(h1("foo")), docMetaOne(h1("foo")), "meta", 1),
(docMetaOne(h1("foo")), docMetaTwo(h1("foo")), "meta", 2),
(docMetaTwo(h1("foo")), doc(h1("foo")), "meta", None),
],
)
def test_set_doc_attribute(doc, expect, attr, value, test_transform):
tr = Transform(doc).set_doc_attribute(attr, value)
test_transform(tr, expect)


@pytest.mark.parametrize(
"doc,source,expect",
[
Expand Down

0 comments on commit c8b31ae

Please sign in to comment.