Skip to content

Commit

Permalink
Merge pull request #271 from johanneswilm/1.7.1-1.7.5
Browse files Browse the repository at this point in the history
1.7.1-1.7.5
  • Loading branch information
sciyoshi authored Nov 2, 2023
2 parents 74bce8d + 3e74c4a commit a367867
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 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.1
- [`prosemirror-transform`](https://github.com/ProseMirror/prosemirror-transform) version 1.7.5
- [`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
7 changes: 5 additions & 2 deletions prosemirror/transform/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ def can_split(doc, pos, depth=None, types_after=None):
if node.type.spec.get("isolating"):
return False
rest = node.content.cut_by_index(index, node.child_count)
if types_after and len(types_after) > i:
override_child = types_after[i + 1]
rest = rest.replace_child(
0, override_child["type"].create(override_child.get("attrs"))
)
after = None
if types_after and len(types_after) > i:
after = types_after[i]
if not after:
after = node
if isinstance(after, dict):
if after != node:
rest = rest.replace_child(0, after["type"].create(after.get("attrs")))
if not node.can_replace(index + 1, node.child_count) or not after[
"type"
].valid_content(rest):
Expand Down
36 changes: 29 additions & 7 deletions prosemirror/transform/transform.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import Union

from prosemirror.model import Fragment, Mark, MarkType, Node, NodeType, Slice
Expand Down Expand Up @@ -156,24 +157,43 @@ def clear_incompatible(self, pos, parent_type, match=None):
if match is None:
match = parent_type.content_match
node = self.doc.node_at(pos)
del_steps = []
repl_steps = []
cur = pos + 1
for i in range(node.child_count):
child = node.child(i)
end = cur + child.node_size
allowed = match.match_type(child.type, child.attrs)
if not allowed:
del_steps.append(ReplaceStep(cur, end, Slice.empty))
repl_steps.append(ReplaceStep(cur, end, Slice.empty))
else:
match = allowed
for j in range(len(child.marks)):
if not parent_type.allows_mark_type(child.marks[j].type):
self.step(RemoveMarkStep(cur, end, child.marks[j]))
if child.is_text and not parent_type.spec.get("code"):
newline = re.compile(r"\r?\n|\r")
slice = None
m = newline.search(child.text)
while m:
if slice is None:
slice = Slice(
Fragment.from_(
parent_type.schema.text(
" ", parent_type.allowed_marks(child.marks)
)
),
0,
0,
)
repl_steps.append(
ReplaceStep(cur + m.start(), cur + m.end(), slice)
)
m = newline.search(child.text, m.end())
cur = end
if not match.valid_end:
fill = match.fill_before(Fragment.empty, True)
self.replace(cur, cur, Slice(fill, 0, 0))
for item in reversed(del_steps):
for item in reversed(repl_steps):
self.step(item)
return self

Expand Down Expand Up @@ -240,11 +260,13 @@ def replace_range(self, from_, to, slice):

d = preferred_depth - 1
while d >= 0:
type = left_nodes[d].type
def_ = defines_content(type)
if def_ and from__.node(preferred_target_index).type.name != type.name:
left_node = left_nodes[d]
def_ = defines_content(left_node.type)
if def_ and not left_node.same_markup(
from__.node(abs(preferred_target) - 1)
):
preferred_depth = d
elif def_ or not type.is_text_block:
elif def_ or not left_node.type.is_text_block:
break
d -= 1

Expand Down
22 changes: 22 additions & 0 deletions tests/prosemirror_transform/tests/test_trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,18 @@ def test_wrap(doc, expect, type, attrs, test_transform):
"code_block",
None,
),
(
doc(p("<a>one", img(), "two", img(), "three")),
doc(pre("onetwothree")),
"code_block",
None,
),
(
doc(pre("<a>one\ntwo\nthree")),
doc(p("one two three")),
"paragraph",
None,
),
],
)
def test_set_block_type(doc, expect, node_type, attrs, test_transform):
Expand Down Expand Up @@ -718,6 +730,16 @@ def test_set_node_attribute(doc, expect, attr, value, test_transform):
doc(blockquote(blockquote(blockquote(p("hi"))))).slice(3, 6, True),
doc(p("hi")),
),
(
doc(ul(li(p("list1"), blockquote(p("<a>"))))),
doc(blockquote(p("<a>one<b>"))),
doc(ul(li(p("list1"), blockquote(p("one"))))),
),
(
doc(ul(li(p("list1"), ul(li(p("list2"), blockquote(p("<a>"))))))),
doc(blockquote(p("<a>one<b>"))),
doc(ul(li(p("list1"), ul(li(p("list2"), blockquote(p("one"))))))),
),
],
)
def test_replace(doc, source, expect, test_transform):
Expand Down

0 comments on commit a367867

Please sign in to comment.