marktip 0.5.0__tar.gz → 0.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. {marktip-0.5.0 → marktip-0.6.0}/CMakeLists.txt +1 -1
  2. {marktip-0.5.0 → marktip-0.6.0}/PKG-INFO +16 -4
  3. {marktip-0.5.0 → marktip-0.6.0}/README.md +15 -3
  4. {marktip-0.5.0 → marktip-0.6.0}/pyproject.toml +1 -1
  5. {marktip-0.5.0 → marktip-0.6.0}/src/ast.cpp +138 -82
  6. {marktip-0.5.0 → marktip-0.6.0}/src/marktip.cpp +5 -2
  7. {marktip-0.5.0 → marktip-0.6.0}/tests/test_strict.py +93 -0
  8. {marktip-0.5.0 → marktip-0.6.0}/tests/test_uri_policy.py +12 -0
  9. {marktip-0.5.0 → marktip-0.6.0}/.github/workflows/build-distributions.yml +0 -0
  10. {marktip-0.5.0 → marktip-0.6.0}/.gitignore +0 -0
  11. {marktip-0.5.0 → marktip-0.6.0}/LICENSE +0 -0
  12. {marktip-0.5.0 → marktip-0.6.0}/python/marktip/__init__.py +0 -0
  13. {marktip-0.5.0 → marktip-0.6.0}/scripts/benchmark.py +0 -0
  14. {marktip-0.5.0 → marktip-0.6.0}/src/ast.h +0 -0
  15. {marktip-0.5.0 → marktip-0.6.0}/src/errors.h +0 -0
  16. {marktip-0.5.0 → marktip-0.6.0}/src/parser.cpp +0 -0
  17. {marktip-0.5.0 → marktip-0.6.0}/src/parser.h +0 -0
  18. {marktip-0.5.0 → marktip-0.6.0}/src/serializer.cpp +0 -0
  19. {marktip-0.5.0 → marktip-0.6.0}/src/serializer.h +0 -0
  20. {marktip-0.5.0 → marktip-0.6.0}/tests/test_document.py +0 -0
  21. {marktip-0.5.0 → marktip-0.6.0}/tests/test_errors.py +0 -0
  22. {marktip-0.5.0 → marktip-0.6.0}/tests/test_parse.py +0 -0
  23. {marktip-0.5.0 → marktip-0.6.0}/tests/test_roundtrip.py +0 -0
  24. {marktip-0.5.0 → marktip-0.6.0}/tests/test_serialize.py +0 -0
  25. {marktip-0.5.0 → marktip-0.6.0}/tests/test_validate.py +0 -0
  26. {marktip-0.5.0 → marktip-0.6.0}/third_party/md4c/LICENSE.md +0 -0
  27. {marktip-0.5.0 → marktip-0.6.0}/third_party/md4c/src/entity.c +0 -0
  28. {marktip-0.5.0 → marktip-0.6.0}/third_party/md4c/src/entity.h +0 -0
  29. {marktip-0.5.0 → marktip-0.6.0}/third_party/md4c/src/md4c.c +0 -0
  30. {marktip-0.5.0 → marktip-0.6.0}/third_party/md4c/src/md4c.h +0 -0
  31. {marktip-0.5.0 → marktip-0.6.0}/third_party/md4c/upstream_metadata/README.md +0 -0
@@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
7
  set(CMAKE_CXX_EXTENSIONS OFF)
8
8
 
9
9
  if(NOT DEFINED SKBUILD_PROJECT_VERSION)
10
- set(SKBUILD_PROJECT_VERSION "0.5.0")
10
+ set(SKBUILD_PROJECT_VERSION "0.6.0")
11
11
  endif()
12
12
 
13
13
  set(PYBIND11_FINDPYTHON ON)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: marktip
3
- Version: 0.5.0
3
+ Version: 0.6.0
4
4
  Summary: Fast C++/MD4C Markdown parser and canonical Markdown serializer for Tiptap-style JSON
5
5
  Keywords: markdown,tiptap,md4c,parser
6
6
  Author: Saneaven (Minjae Kyung)
@@ -121,19 +121,29 @@ It is off by default and keyword-only, and it is a `from_dict` option only — t
121
121
  | Node | Accepted attrs |
122
122
  | --- | --- |
123
123
  | `heading` | `level`: 1–6 |
124
- | `codeBlock` | `language`, `info`: single-line `str` |
124
+ | `codeBlock` | `language`, `info`: single-line `str` or `None` |
125
125
  | `bulletList`, `taskList` | `tight`: `bool` |
126
126
  | `orderedList` | `start`: 0–999999999, `tight`: `bool` |
127
127
  | `taskItem` | `checked`: `bool` |
128
128
  | `table` | `colCount`: non-negative `int` |
129
129
  | `tableHeader`, `tableCell` | `align`: `"left"`/`"center"`/`"right"`/`None`, `colspan`: `1`, `rowspan`: `1`, `colwidth`: `None` |
130
- | `image` | `src`, `alt`, `title`: `str` |
130
+ | `image` | `src`, `alt`, `title`: `str` or `None` |
131
131
  | `htmlBlock`, `htmlInline` | `html`: `str` |
132
- | `link` mark | `href`, `title`: `str` |
132
+ | `link` mark | `href`, `title`: `str` or `None` |
133
133
 
134
134
  Every other type takes no attrs at all.
135
135
  Under strict, `bool` and `int` stay distinct — `{"level": True}` is refused rather than read as `1`.
136
136
 
137
+ > **Changed in 0.6.0** — `None` on the attrs above that accept it now means the attr is unset, and the key is dropped on the way in rather than coerced to `""`.
138
+ > Two things move with it: an `image` whose `alt` is `None` *and* which carries content children now renders that child text, and `to_dict()` omits such a key instead of echoing `""`.
139
+
140
+ `None` is accepted exactly where Tiptap's own schema declares `default: null` — `codeBlock`'s `language`/`info`, `image`'s `src`/`alt`/`title`, and `link`'s `href`/`title`.
141
+ ProseMirror serializes every attr in the schema, defaults included, so `None` is what the editor submits for an attr nobody ever filled in, and an unset attr is accepted already; the markdown is the same either way.
142
+ There are two carve-outs.
143
+ `html` is marktip's own attr with no Tiptap counterpart, and the serializer reads it as the node's payload when it is present but empty, so a `None` there would drop content rather than leave an attr unset.
144
+ The rest — `level`, `start`, `tight`, `checked`, `colCount`, `colspan`, `rowspan` — are settings Tiptap gives a non-null default, so a `None` on one of those is a caller mistake and stays refused.
145
+ `align` and `colwidth` are unchanged: `None` was already a *value* there, not an unset marker.
146
+
137
147
  `colspan`/`rowspan`/`colwidth` are *known* attrs whose only accepted values are the no-ops Tiptap's table extension puts on every cell.
138
148
  Refusing the names outright would leave strict unusable for the documents it exists to check.
139
149
  Tiptap's Link extension is the other direction: its default `target` and `rel` have no markdown form, so they are refused, which is the same rule that stops an `onclick` from vanishing silently.
@@ -221,10 +231,12 @@ It deliberately does **not** validate, unless `strict=True` asks it to:
221
231
  - **`attrs` names and value types.**
222
232
  Keys are free-form.
223
233
  `str`, `int`, and `bool` round-trip unchanged; anything else is coerced to a string (`[1, 2]` → `"[1, 2]"`, `1.5` → `"1.5"`, `None` → `""`), so `to_dict()` will not always give back what you passed in.
234
+ The one exception is the attrs [Refusing lossy conversions](#refusing-lossy-conversions) lists as accepting `None`: there the key is dropped instead of coerced, because a `None` on those means the attr was never set.
224
235
  An attr with no markdown form is dropped at serialization — see [Refusing lossy conversions](#refusing-lossy-conversions).
225
236
  - **Per-node required attrs**, `strict` included.
226
237
  A `heading` without `level`, an `image` without `src`, or a `link` mark without `href` is accepted; the serializer substitutes a default rather than failing.
227
238
  `strict` governs the attrs that are present, not which ones must be.
239
+ `{"src": None}` and no `src` key at all are the same thing to every part of the library, the URI policy included.
228
240
  - **Content models.**
229
241
  Which node may contain which is not checked — a `text` node directly under `doc` is accepted and serialized.
230
242
  - **Raw HTML.**
@@ -93,19 +93,29 @@ It is off by default and keyword-only, and it is a `from_dict` option only — t
93
93
  | Node | Accepted attrs |
94
94
  | --- | --- |
95
95
  | `heading` | `level`: 1–6 |
96
- | `codeBlock` | `language`, `info`: single-line `str` |
96
+ | `codeBlock` | `language`, `info`: single-line `str` or `None` |
97
97
  | `bulletList`, `taskList` | `tight`: `bool` |
98
98
  | `orderedList` | `start`: 0–999999999, `tight`: `bool` |
99
99
  | `taskItem` | `checked`: `bool` |
100
100
  | `table` | `colCount`: non-negative `int` |
101
101
  | `tableHeader`, `tableCell` | `align`: `"left"`/`"center"`/`"right"`/`None`, `colspan`: `1`, `rowspan`: `1`, `colwidth`: `None` |
102
- | `image` | `src`, `alt`, `title`: `str` |
102
+ | `image` | `src`, `alt`, `title`: `str` or `None` |
103
103
  | `htmlBlock`, `htmlInline` | `html`: `str` |
104
- | `link` mark | `href`, `title`: `str` |
104
+ | `link` mark | `href`, `title`: `str` or `None` |
105
105
 
106
106
  Every other type takes no attrs at all.
107
107
  Under strict, `bool` and `int` stay distinct — `{"level": True}` is refused rather than read as `1`.
108
108
 
109
+ > **Changed in 0.6.0** — `None` on the attrs above that accept it now means the attr is unset, and the key is dropped on the way in rather than coerced to `""`.
110
+ > Two things move with it: an `image` whose `alt` is `None` *and* which carries content children now renders that child text, and `to_dict()` omits such a key instead of echoing `""`.
111
+
112
+ `None` is accepted exactly where Tiptap's own schema declares `default: null` — `codeBlock`'s `language`/`info`, `image`'s `src`/`alt`/`title`, and `link`'s `href`/`title`.
113
+ ProseMirror serializes every attr in the schema, defaults included, so `None` is what the editor submits for an attr nobody ever filled in, and an unset attr is accepted already; the markdown is the same either way.
114
+ There are two carve-outs.
115
+ `html` is marktip's own attr with no Tiptap counterpart, and the serializer reads it as the node's payload when it is present but empty, so a `None` there would drop content rather than leave an attr unset.
116
+ The rest — `level`, `start`, `tight`, `checked`, `colCount`, `colspan`, `rowspan` — are settings Tiptap gives a non-null default, so a `None` on one of those is a caller mistake and stays refused.
117
+ `align` and `colwidth` are unchanged: `None` was already a *value* there, not an unset marker.
118
+
109
119
  `colspan`/`rowspan`/`colwidth` are *known* attrs whose only accepted values are the no-ops Tiptap's table extension puts on every cell.
110
120
  Refusing the names outright would leave strict unusable for the documents it exists to check.
111
121
  Tiptap's Link extension is the other direction: its default `target` and `rel` have no markdown form, so they are refused, which is the same rule that stops an `onclick` from vanishing silently.
@@ -193,10 +203,12 @@ It deliberately does **not** validate, unless `strict=True` asks it to:
193
203
  - **`attrs` names and value types.**
194
204
  Keys are free-form.
195
205
  `str`, `int`, and `bool` round-trip unchanged; anything else is coerced to a string (`[1, 2]` → `"[1, 2]"`, `1.5` → `"1.5"`, `None` → `""`), so `to_dict()` will not always give back what you passed in.
206
+ The one exception is the attrs [Refusing lossy conversions](#refusing-lossy-conversions) lists as accepting `None`: there the key is dropped instead of coerced, because a `None` on those means the attr was never set.
196
207
  An attr with no markdown form is dropped at serialization — see [Refusing lossy conversions](#refusing-lossy-conversions).
197
208
  - **Per-node required attrs**, `strict` included.
198
209
  A `heading` without `level`, an `image` without `src`, or a `link` mark without `href` is accepted; the serializer substitutes a default rather than failing.
199
210
  `strict` governs the attrs that are present, not which ones must be.
211
+ `{"src": None}` and no `src` key at all are the same thing to every part of the library, the URI policy included.
200
212
  - **Content models.**
201
213
  Which node may contain which is not checked — a `text` node directly under `doc` is accepted and serialized.
202
214
  - **Raw HTML.**
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "marktip"
7
- version = "0.5.0"
7
+ version = "0.6.0"
8
8
  description = "Fast C++/MD4C Markdown parser and canonical Markdown serializer for Tiptap-style JSON"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -125,77 +125,11 @@ py::dict attrs_to_py(const AttrList& attrs) {
125
125
  return out;
126
126
  }
127
127
 
128
- AttrList attrs_from_py(py::dict attrs) {
129
- AttrList out;
130
- for (auto item : attrs) {
131
- std::string key = py_to_string(item.first);
132
- py::handle value = item.second;
133
- if (py::isinstance<py::bool_>(value)) {
134
- set_attr(out, std::move(key), AttrValue::boolean(py::cast<bool>(value)));
135
- } else if (py::isinstance<py::int_>(value)) {
136
- set_attr(out, std::move(key), AttrValue::integer(py::cast<long long>(value)));
137
- } else if (py::isinstance<py::str>(value)) {
138
- set_attr(out, std::move(key), AttrValue::string(py::cast<std::string>(value)));
139
- } else {
140
- set_attr(out, std::move(key), AttrValue::string(py_to_string(value)));
141
- }
142
- }
143
- return out;
144
- }
145
-
146
- py::dict mark_to_py(const Mark& mark) {
147
- py::dict out;
148
- out["type"] = mark.type;
149
- if (!mark.attrs.empty()) {
150
- out["attrs"] = attrs_to_py(mark.attrs);
151
- }
152
- return out;
153
- }
154
-
155
- py::dict node_to_py(const Document& document, std::size_t index) {
156
- const Node& node = document.node(index);
157
- py::dict out;
158
- out["type"] = node.type;
159
-
160
- if (node.type == "text") {
161
- out["text"] = node.text;
162
- }
163
-
164
- if (!node.attrs.empty()) {
165
- out["attrs"] = attrs_to_py(node.attrs);
166
- }
167
-
168
- if (!node.marks.empty()) {
169
- py::list marks;
170
- for (const auto& mark : node.marks) {
171
- marks.append(mark_to_py(mark));
172
- }
173
- out["marks"] = marks;
174
- }
175
-
176
- if (node.type == "doc" || !node.content.empty()) {
177
- py::list content;
178
- for (std::size_t child : node.content) {
179
- content.append(node_to_py(document, child));
180
- }
181
- out["content"] = content;
182
- }
183
-
184
- return out;
185
- }
186
-
187
- // --- strict mode ------------------------------------------------------------
188
- //
189
- // Node and mark *types* are closed, but attrs are not: an attr the serializer never
190
- // reads is dropped without a word. strict=True refuses those instead, from the walk
191
- // that is already happening, so a consumer does not have to re-traverse the tree in
192
- // Python just to find out whether the document survives conversion intact.
128
+ // --- the attr schema --------------------------------------------------------
193
129
  //
194
- // The check reads the raw py::dict rather than the built AttrList, because
195
- // attrs_from_py coerces first: None becomes "" and [100] becomes "[100]", which is
196
- // exactly the difference between an accepted colwidth and a refused one. That also
197
- // makes it from_dict-only, which is the whole story anyway — the parser emits nothing
198
- // but attrs it knows about, in range, so there is nothing for from_markdown to catch.
130
+ // Which attrs each type may carry, and what each may hold.
131
+ // Two readers: attrs_from_py just below, which drops a null that means "unset",
132
+ // and strict mode further down, which refuses whatever markdown would drop or alter.
199
133
 
200
134
  enum class AttrRule {
201
135
  Str, // any str
@@ -209,9 +143,34 @@ enum class AttrRule {
209
143
  NoWidth, // None: GFM tables have no column widths
210
144
  };
211
145
 
146
+ // A second axis, orthogonal to the value grammar: what a None means for this attr.
147
+ //
148
+ // ProseMirror serializes every attr in the schema, defaults included,
149
+ // and Tiptap declares `default: null` for codeBlock's language/info,
150
+ // image's src/alt/title and link's href/title.
151
+ // A None on one of those is not a wrongly typed value, it is the attr never having been set —
152
+ // the absent-key case, which is accepted already and renders the same markdown either way.
153
+ // attrs_from_py drops the key, so the two really are one case everywhere downstream:
154
+ // strict, the serializer, and the URI policy.
155
+ //
156
+ // Refused stays the default wherever Tiptap's default is not null
157
+ // (level, start, tight, checked, colspan): a null there is a caller mistake, not an unset attr.
158
+ // So is html, which is marktip's own attr with no Tiptap counterpart,
159
+ // and which the serializer reads as the payload when it is present but empty —
160
+ // dropping the node's content rather than leaving an attr unset.
161
+ //
162
+ // Align and NoWidth are untouched:
163
+ // those rules take None as a *value* ("no alignment", "no column width") rather than as an unset marker,
164
+ // so they handle it themselves.
165
+ enum class NullAttr {
166
+ Refused, // the default
167
+ Unset, // None accepted: the attr was never set
168
+ };
169
+
212
170
  struct AttrSpec {
213
171
  std::string_view name;
214
172
  AttrRule rule;
173
+ NullAttr null = NullAttr::Refused;
215
174
  };
216
175
 
217
176
  struct AttrTable {
@@ -220,7 +179,8 @@ struct AttrTable {
220
179
  };
221
180
 
222
181
  constexpr AttrSpec kHeadingAttrs[] = {{"level", AttrRule::Level}};
223
- constexpr AttrSpec kCodeBlockAttrs[] = {{"info", AttrRule::InfoStr}, {"language", AttrRule::InfoStr}};
182
+ constexpr AttrSpec kCodeBlockAttrs[] = {{"info", AttrRule::InfoStr, NullAttr::Unset},
183
+ {"language", AttrRule::InfoStr, NullAttr::Unset}};
224
184
  constexpr AttrSpec kTightAttrs[] = {{"tight", AttrRule::Bool}};
225
185
  constexpr AttrSpec kOrderedListAttrs[] = {{"start", AttrRule::ListStart}, {"tight", AttrRule::Bool}};
226
186
  constexpr AttrSpec kTaskItemAttrs[] = {{"checked", AttrRule::Bool}};
@@ -231,9 +191,12 @@ constexpr AttrSpec kCellAttrs[] = {
231
191
  {"colwidth", AttrRule::NoWidth},
232
192
  {"rowspan", AttrRule::NoSpan},
233
193
  };
234
- constexpr AttrSpec kImageAttrs[] = {{"alt", AttrRule::Str}, {"src", AttrRule::Str}, {"title", AttrRule::Str}};
194
+ constexpr AttrSpec kImageAttrs[] = {{"alt", AttrRule::Str, NullAttr::Unset},
195
+ {"src", AttrRule::Str, NullAttr::Unset},
196
+ {"title", AttrRule::Str, NullAttr::Unset}};
235
197
  constexpr AttrSpec kHtmlAttrs[] = {{"html", AttrRule::Str}};
236
- constexpr AttrSpec kLinkAttrs[] = {{"href", AttrRule::Str}, {"title", AttrRule::Str}};
198
+ constexpr AttrSpec kLinkAttrs[] = {{"href", AttrRule::Str, NullAttr::Unset},
199
+ {"title", AttrRule::Str, NullAttr::Unset}};
237
200
 
238
201
  constexpr AttrTable kNoAttrs = {nullptr, 0};
239
202
 
@@ -281,6 +244,100 @@ static_assert(std::size(kNodeAttrTables) == std::size(kKnownNodeTypes),
281
244
  static_assert(std::size(kMarkAttrTables) == std::size(kKnownMarkTypes),
282
245
  "every mark type needs an attr rule table");
283
246
 
247
+ // The schema is small enough that a linear scan beats anything cleverer,
248
+ // and both readers need the same lookup: nullptr when the type does not declare that attr.
249
+ const AttrSpec* find_attr_spec(const AttrTable& table, std::string_view name) {
250
+ for (std::size_t i = 0; i < table.count; ++i) {
251
+ if (table.specs[i].name == name) {
252
+ return &table.specs[i];
253
+ }
254
+ }
255
+ return nullptr;
256
+ }
257
+
258
+ AttrList attrs_from_py(py::dict attrs, const AttrTable& table) {
259
+ AttrList out;
260
+ for (auto item : attrs) {
261
+ std::string key = py_to_string(item.first);
262
+ py::handle value = item.second;
263
+ if (value.is_none()) {
264
+ const AttrSpec* spec = find_attr_spec(table, key);
265
+ if (spec != nullptr && spec->null == NullAttr::Unset) {
266
+ // An attr Tiptap serialized but never set.
267
+ // Dropping the key, rather than coercing it to "",
268
+ // is what makes it the absent-key case for real.
269
+ continue;
270
+ }
271
+ }
272
+ if (py::isinstance<py::bool_>(value)) {
273
+ set_attr(out, std::move(key), AttrValue::boolean(py::cast<bool>(value)));
274
+ } else if (py::isinstance<py::int_>(value)) {
275
+ set_attr(out, std::move(key), AttrValue::integer(py::cast<long long>(value)));
276
+ } else if (py::isinstance<py::str>(value)) {
277
+ set_attr(out, std::move(key), AttrValue::string(py::cast<std::string>(value)));
278
+ } else {
279
+ set_attr(out, std::move(key), AttrValue::string(py_to_string(value)));
280
+ }
281
+ }
282
+ return out;
283
+ }
284
+
285
+ py::dict mark_to_py(const Mark& mark) {
286
+ py::dict out;
287
+ out["type"] = mark.type;
288
+ if (!mark.attrs.empty()) {
289
+ out["attrs"] = attrs_to_py(mark.attrs);
290
+ }
291
+ return out;
292
+ }
293
+
294
+ py::dict node_to_py(const Document& document, std::size_t index) {
295
+ const Node& node = document.node(index);
296
+ py::dict out;
297
+ out["type"] = node.type;
298
+
299
+ if (node.type == "text") {
300
+ out["text"] = node.text;
301
+ }
302
+
303
+ if (!node.attrs.empty()) {
304
+ out["attrs"] = attrs_to_py(node.attrs);
305
+ }
306
+
307
+ if (!node.marks.empty()) {
308
+ py::list marks;
309
+ for (const auto& mark : node.marks) {
310
+ marks.append(mark_to_py(mark));
311
+ }
312
+ out["marks"] = marks;
313
+ }
314
+
315
+ if (node.type == "doc" || !node.content.empty()) {
316
+ py::list content;
317
+ for (std::size_t child : node.content) {
318
+ content.append(node_to_py(document, child));
319
+ }
320
+ out["content"] = content;
321
+ }
322
+
323
+ return out;
324
+ }
325
+
326
+ // --- strict mode ------------------------------------------------------------
327
+ //
328
+ // Node and mark *types* are closed, but attrs are not:
329
+ // an attr the serializer never reads is dropped without a word.
330
+ // strict=True refuses those instead, from the walk that is already happening,
331
+ // so a consumer does not have to re-traverse the tree in Python
332
+ // just to find out whether the document survives conversion intact.
333
+ //
334
+ // The check reads the raw py::dict rather than the built AttrList, because attrs_from_py coerces first:
335
+ // [100] becomes "[100]" and a refused None becomes "",
336
+ // which is exactly the difference between an accepted colwidth and a refused one.
337
+ // That also makes it from_dict-only, which is the whole story anyway —
338
+ // the parser emits nothing but attrs it knows about, in range,
339
+ // so there is nothing for from_markdown to catch.
340
+
284
341
  // Out-of-range values (10 ** 100) come back false rather than raising, so they report as
285
342
  // a document problem with a path, not as an unlocated OverflowError.
286
343
  bool as_int(py::handle value, long long& out) {
@@ -378,19 +435,18 @@ void check_attrs_strict(py::dict attrs, const AttrTable& table, std::string_view
378
435
  const PathStack& path) {
379
436
  for (auto item : attrs) {
380
437
  std::string name = py_to_string(item.first);
381
- const AttrSpec* spec = nullptr;
382
- for (std::size_t i = 0; i < table.count; ++i) {
383
- if (table.specs[i].name == name) {
384
- spec = &table.specs[i];
385
- break;
386
- }
387
- }
438
+ const AttrSpec* spec = find_attr_spec(table, name);
388
439
  if (spec == nullptr) {
389
440
  throw InvalidNodeError("unknown_attr", name,
390
441
  "attr '" + name + "' is not defined for " + kind + " type '" +
391
442
  std::string(owner_type) + "'",
392
443
  format_path(path));
393
444
  }
445
+ // Unset rather than wrongly typed, and attrs_from_py drops the key,
446
+ // so refusing it here would refuse a document identical to one that is accepted.
447
+ if (spec->null == NullAttr::Unset && item.second.is_none()) {
448
+ continue;
449
+ }
394
450
  check_attr_value(item.second, spec->rule, name, path);
395
451
  }
396
452
  }
@@ -409,7 +465,7 @@ Mark mark_from_py(py::dict mark_dict, bool strict, const PathStack& path) {
409
465
  if (strict) {
410
466
  check_attrs_strict(attr_dict, kMarkAttrTables[type_pos], mark.type, "mark", path);
411
467
  }
412
- mark.attrs = attrs_from_py(attr_dict);
468
+ mark.attrs = attrs_from_py(attr_dict, kMarkAttrTables[type_pos]);
413
469
  }
414
470
 
415
471
  return mark;
@@ -439,7 +495,7 @@ void fill_node_from_py(Document& document, std::size_t index, py::dict node_dict
439
495
  if (strict) {
440
496
  check_attrs_strict(attr_dict, kNodeAttrTables[type_pos], node.type, "node", path);
441
497
  }
442
- node.attrs = attrs_from_py(attr_dict);
498
+ node.attrs = attrs_from_py(attr_dict, kNodeAttrTables[type_pos]);
443
499
  }
444
500
 
445
501
  py::object marks = dict_get(node_dict, "marks");
@@ -11,7 +11,7 @@
11
11
  namespace py = pybind11;
12
12
 
13
13
  #ifndef MARKTIP_VERSION
14
- #define MARKTIP_VERSION "0.5.0"
14
+ #define MARKTIP_VERSION "0.6.0"
15
15
  #endif
16
16
 
17
17
  namespace {
@@ -135,7 +135,10 @@ PYBIND11_MODULE(_core, module) {
135
135
  " 'invalid_attr_value' (wrong type or out of range) or 'unrepresentable'"
136
136
  " (well-formed, but GFM cannot carry it, e.g. colspan=2), and .field set to the attr name."
137
137
  " table 'colCount' and a non-header row's 'align' are accepted and ignored,"
138
- " because from_markdown emits both.";
138
+ " because from_markdown emits both."
139
+ " None is accepted on the attrs Tiptap declares `default: null`"
140
+ " (codeBlock language/info, image src/alt/title, link href/title):"
141
+ " it means the attr is unset, and from_dict drops the key rather than coercing it to ''.";
139
142
 
140
143
  module.def("from_markdown", &marktip::from_markdown_py, py::arg("markdown"), py::arg("cjk_friendly") = false,
141
144
  py::arg("html") = true, py::kw_only(), py::arg("link_schemes") = py::none(),
@@ -41,6 +41,14 @@ def link(**attrs):
41
41
  return doc(p({"type": "text", "text": "x", "marks": [{"type": "link", "attrs": attrs}]}))
42
42
 
43
43
 
44
+ def image(**attrs):
45
+ return doc(p({"type": "image", "attrs": attrs}))
46
+
47
+
48
+ def captioned_image(**attrs):
49
+ return doc(p({"type": "image", "attrs": attrs, "content": [text("cap")]}))
50
+
51
+
44
52
  def ordered(**attrs):
45
53
  return doc({"type": "orderedList", "attrs": attrs, "content": [{"type": "listItem", "content": [p(text("a"))]}]})
46
54
 
@@ -177,6 +185,7 @@ def test_bool_attrs_are_not_coerced_under_strict(value, expected):
177
185
  ("py\nx", "invalid_attr_value"), # would push "x" into the code content on reparse
178
186
  ("py\rx", "invalid_attr_value"),
179
187
  (1, "invalid_attr_value"),
188
+ (None, None), # unset, not a wrong type — see below
180
189
  ],
181
190
  )
182
191
  def test_code_fence_info_must_be_a_single_line(language, expected):
@@ -239,6 +248,90 @@ def test_unrepresentable_is_distinct_from_a_malformed_value():
239
248
  assert code_for(doc(node("heading", level=9)), strict=True) == "invalid_attr_value"
240
249
 
241
250
 
251
+ # ── None: an attr Tiptap serialized but never set ────────────────────────────
252
+
253
+
254
+ @pytest.mark.parametrize(
255
+ "ast",
256
+ [
257
+ code_block(language=None),
258
+ code_block(info=None),
259
+ image(src="a.png", alt=None, title=None),
260
+ image(src=None),
261
+ link(href="https://a", title=None),
262
+ link(href=None),
263
+ ],
264
+ )
265
+ def test_none_on_an_optional_attr_is_unset_not_a_wrong_type(ast):
266
+ # Issue #4. ProseMirror serializes every attr in the schema, defaults included,
267
+ # and Tiptap declares `default: null` for each of these,
268
+ # so this is what the editor submits for an attr nobody ever filled in.
269
+ assert code_for(ast, strict=True) is None
270
+
271
+
272
+ @pytest.mark.parametrize(
273
+ "with_null, without",
274
+ [
275
+ (code_block(language=None), code_block()),
276
+ (image(src="a.png", alt=None, title=None), image(src="a.png")),
277
+ (link(href="https://a", title=None), link(href="https://a")),
278
+ (link(href=None), link()),
279
+ ],
280
+ )
281
+ def test_a_null_attr_converts_exactly_like_an_absent_one(with_null, without):
282
+ # Why accepting it is not a loosening:
283
+ # strict admits no markdown here it was not already admitting from the absent-key form.
284
+ assert tm.from_dict(with_null, strict=True).to_markdown() == tm.from_dict(without, strict=True).to_markdown()
285
+
286
+
287
+ def test_a_null_attr_is_dropped_rather_than_coerced_to_empty():
288
+ # The fix is in the coercion and not only in the strict check,
289
+ # which is what makes the equivalence above hold for the serializer and the URI policy too.
290
+ assert "attrs" not in tm.from_dict(code_block(language=None)).to_dict()["content"][0]
291
+ assert tm.from_dict(code_block(language=None, info="")).to_dict()["content"][0]["attrs"] == {"info": ""}
292
+
293
+
294
+ @pytest.mark.parametrize(
295
+ "ast, field",
296
+ [
297
+ (doc(node("heading", level=None)), "level"),
298
+ (ordered(start=None), "start"),
299
+ (doc({"type": "taskList", "attrs": {"tight": None}}), "tight"),
300
+ (doc(node("taskItem", checked=None)), "checked"),
301
+ (doc({"type": "table", "attrs": {"colCount": None}}), "colCount"),
302
+ (cell(colspan=None), "colspan"),
303
+ ],
304
+ )
305
+ def test_none_stays_wrong_where_tiptap_declares_a_non_null_default(ast, field):
306
+ # These are settings rather than optional values.
307
+ # Tiptap gives every one of them a non-null default, so a null is a caller mistake.
308
+ with pytest.raises(tm.InvalidNodeError) as excinfo:
309
+ tm.from_dict(ast, strict=True)
310
+
311
+ assert excinfo.value.code == "invalid_attr_value"
312
+ assert excinfo.value.field == field
313
+
314
+
315
+ def test_a_null_html_attr_is_refused_because_it_erases_the_content():
316
+ # htmlBlock/htmlInline are marktip's own node types, declared by no Tiptap schema,
317
+ # and an html attr that is present but empty is the payload —
318
+ # so a null there is not an unset attr, it is the content drop 0.4.1 fixed.
319
+ nulled = doc({"type": "htmlBlock", "attrs": {"html": None}, "content": [text("<div>hi</div>")]})
320
+ absent = doc({"type": "htmlBlock", "content": [text("<div>hi</div>")]})
321
+
322
+ assert tm.from_dict(nulled).to_markdown() == ""
323
+ assert tm.from_dict(absent).to_markdown() == "<div>hi</div>"
324
+ assert code_for(nulled, strict=True) == "invalid_attr_value"
325
+
326
+
327
+ def test_a_null_alt_falls_back_to_the_child_text_like_an_absent_one():
328
+ # The one place the coercion change is visible in the markdown:
329
+ # an alt of "" is the caption, an unset alt is not, and a null now lands on the second.
330
+ assert tm.from_dict(captioned_image(src="a.png", alt=None)).to_markdown() == "![cap](a.png)"
331
+ assert tm.from_dict(captioned_image(src="a.png")).to_markdown() == "![cap](a.png)"
332
+ assert tm.from_dict(captioned_image(src="a.png", alt="")).to_markdown() == "![](a.png)"
333
+
334
+
242
335
  # ── the two attrs that are accepted and ignored ──────────────────────────────
243
336
 
244
337
 
@@ -117,6 +117,18 @@ def test_a_missing_href_or_src_is_still_accepted():
117
117
  assert code_for(doc(bare_image), image_schemes=(), image_relative="reject") is None
118
118
 
119
119
 
120
+ def test_a_null_href_or_src_is_the_missing_one_not_an_empty_string():
121
+ # Issue #4: Tiptap declares `default: null` for both,
122
+ # so from_dict drops the key rather than coercing it to "".
123
+ # Before 0.6.0 a null reached the policy as a present reference carrying no scheme,
124
+ # and "reject" refused a document the editor produces.
125
+ assert code_for(doc(link(None)), link_schemes=(), link_relative="reject") is None
126
+ assert code_for(doc(image(None)), image_schemes=(), image_relative="reject") is None
127
+
128
+ # An href that is present and empty is still a reference, and still refused.
129
+ assert code_for(doc(link("")), link_relative="reject") == "disallowed_relative_url"
130
+
131
+
120
132
  def test_non_link_marks_and_non_image_nodes_are_left_alone():
121
133
  node = {"type": "text", "text": "x", "marks": [{"type": "bold"}, {"type": "code"}]}
122
134
  ast = doc({"type": "paragraph", "content": [node], "attrs": {"href": "javascript:alert(1)"}})
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes