Skip to content

Commit ca7eab0

Browse files
authored
fix: non-string values are passed to XmlPrelim attribute value (#218)
1 parent f214679 commit ca7eab0

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
lines changed

native/yex/src/yinput.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Prelim for NifXmlFragmentPrelim {
6565
#[module = "Yex.XmlElementPrelim"]
6666
pub struct NifXmlElementPrelim {
6767
tag: String,
68-
attributes: HashMap<String, String>,
68+
attributes: HashMap<String, NifAny>,
6969
children: Vec<NifXmlIn>,
7070
}
7171
impl XmlPrelim for NifXmlElementPrelim {}
@@ -81,7 +81,7 @@ impl Prelim for NifXmlElementPrelim {
8181
fn integrate(self, txn: &mut TransactionMut, inner_ref: BranchPtr) {
8282
let xml = XmlElementRef::from(inner_ref);
8383
for (key, value) in self.attributes {
84-
xml.insert_attribute(txn, key, value);
84+
xml.insert_attribute(txn, key, value.0);
8585
}
8686
for value in self.children {
8787
xml.push_back(txn, value);
@@ -92,7 +92,7 @@ impl Prelim for NifXmlElementPrelim {
9292
#[derive(NifStruct)]
9393
#[module = "Yex.XmlTextPrelim"]
9494
pub struct NifXmlTextPrelim {
95-
attributes: HashMap<String, String>,
95+
attributes: HashMap<String, NifAny>,
9696
delta: NifYInputDelta,
9797
}
9898

@@ -107,7 +107,7 @@ impl Prelim for NifXmlTextPrelim {
107107
fn integrate(self, txn: &mut TransactionMut, inner_ref: BranchPtr) {
108108
let text_ref = XmlTextRef::from(inner_ref);
109109
for (key, value) in self.attributes {
110-
text_ref.insert_attribute(txn, key, value);
110+
text_ref.insert_attribute(txn, key, value.0);
111111
}
112112
text_ref.apply_delta(txn, self.delta.0);
113113
}

test/shared_type/xml_element_test.exs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,45 @@ defmodule YexXmlElementTest do
551551
%{doc: d1, xml_fragment: f}
552552
end
553553

554+
test "insert prelim with any attribute types", %{xml_fragment: xml_fragment} do
555+
assert :ok ==
556+
Yex.XmlFragment.insert(xml_fragment, 0, %Yex.XmlElementPrelim{
557+
tag: "blockContainer",
558+
attributes: %{"id" => "f6d3b168-05b2-4eb7-8d4e-132beb6fdc5b"},
559+
children: [
560+
%Yex.XmlElementPrelim{
561+
tag: "checkListItem",
562+
attributes: %{
563+
"backgroundColor" => "default",
564+
"checked" => false,
565+
"data" => %{"custom" => "data"},
566+
"indentation" => 0,
567+
"itemType" => <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>>,
568+
"lineHeight" => 1.5,
569+
"array" => [1, 2, 3],
570+
"textAlignment" => "left",
571+
"textColor" => "default"
572+
},
573+
children: []
574+
}
575+
]
576+
})
577+
578+
{:ok, root} = Yex.XmlFragment.fetch(xml_fragment, 0)
579+
assert "blockContainer" == Yex.XmlElement.get_tag(root)
580+
assert "f6d3b168-05b2-4eb7-8d4e-132beb6fdc5b" == Yex.XmlElement.get_attribute(root, "id")
581+
582+
{:ok, child} = Yex.XmlElement.fetch(root, 0)
583+
assert false == Yex.XmlElement.get_attribute(child, "checked")
584+
assert 0 == Yex.XmlElement.get_attribute(child, "indentation")
585+
assert 1.5 == Yex.XmlElement.get_attribute(child, "lineHeight")
586+
assert [1, 2, 3] == Yex.XmlElement.get_attribute(child, "array")
587+
assert %{"custom" => "data"} == Yex.XmlElement.get_attribute(child, "data")
588+
589+
assert <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>> ==
590+
Yex.XmlElement.get_attribute(child, "itemType")
591+
end
592+
554593
test "XmlElementPrelim.new", %{xml_fragment: xml_fragment} do
555594
XmlFragment.push(
556595
xml_fragment,

test/shared_type/xml_fragment_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,22 @@ defmodule YexXmlFragmentTest do
462462
# Ensure protocol implementation is exercised
463463
assert Yex.Output.as_prelim(f) == XmlFragment.as_prelim(f)
464464
end
465+
466+
test "load blocknote_data.bin and as_prelim" do
467+
path = Path.expand("../test_data/blocknote_data.bin", __DIR__)
468+
{:ok, bin} = File.read(path)
469+
doc = Yex.Doc.new()
470+
Yex.apply_update(doc, bin)
471+
prelim = Yex.Doc.get_xml_fragment(doc, "document-store") |> Yex.Output.as_prelim()
472+
473+
new_doc = Yex.Doc.new()
474+
frag = Yex.Doc.get_xml_fragment(new_doc, "document-store")
475+
476+
Enum.each(prelim.children, fn child ->
477+
Yex.XmlFragment.insert(frag, Yex.XmlFragment.length(frag), child)
478+
end)
479+
480+
assert frag |> Yex.Output.as_prelim() == prelim
481+
end
465482
end
466483
end

test/shared_type/xml_text_test.exs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,58 @@ defmodule YexXmlTextTest do
215215
end
216216
end
217217

218+
describe "XmlTextPrelim" do
219+
setup do
220+
d1 = Doc.with_options(%Doc.Options{client_id: 1})
221+
f = Doc.get_xml_fragment(d1, "xml")
222+
%{doc: d1, xml_fragment: f}
223+
end
224+
225+
test "insert prelim with any attribute types", %{xml_fragment: xml_fragment} do
226+
assert :ok =
227+
XmlFragment.insert(xml_fragment, 0, %Yex.XmlTextPrelim{
228+
delta: [
229+
%{
230+
attributes: %{
231+
"link" => %{
232+
"class" => nil,
233+
"indentation" => 0,
234+
"itemType" => <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>>,
235+
"lineHeight" => 1.5,
236+
"array" => [1, 2, 3],
237+
"href" => "http://google.com",
238+
"rel" => "noopener noreferrer nofollow",
239+
"target" => "_blank"
240+
}
241+
},
242+
insert: "http://google.com"
243+
}
244+
],
245+
attributes: %{}
246+
})
247+
248+
{:ok, text} = XmlFragment.fetch(xml_fragment, 0)
249+
250+
assert [
251+
%{
252+
insert: "http://google.com",
253+
attributes: %{
254+
"link" => %{
255+
"class" => nil,
256+
"indentation" => 0,
257+
"itemType" => <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>>,
258+
"lineHeight" => 1.5,
259+
"array" => [1, 2, 3],
260+
"href" => "http://google.com",
261+
"rel" => "noopener noreferrer nofollow",
262+
"target" => "_blank"
263+
}
264+
}
265+
}
266+
] == Yex.XmlText.to_delta(text)
267+
end
268+
end
269+
218270
describe "as_prelim" do
219271
setup do
220272
d1 = Doc.with_options(%Doc.Options{client_id: 1})

test/test_data/blocknote_data.bin

6.28 KB
Binary file not shown.

0 commit comments

Comments
 (0)