@@ -68,6 +68,8 @@ namespace {
68
68
ERL_NIF_TERM atom_xmlstreamstart;
69
69
ERL_NIF_TERM atom_xmlstreamend;
70
70
ERL_NIF_TERM atom_pretty;
71
+ ERL_NIF_TERM atom_escaped;
72
+ ERL_NIF_TERM atom_cdata;
71
73
ERL_NIF_TERM atom_true;
72
74
constexpr const unsigned char EMPTY[1 ] = {0 };
73
75
@@ -154,8 +156,9 @@ ERL_NIF_TERM make_attr_tuple(ParseCtx &ctx,
154
156
155
157
ERL_NIF_TERM get_xmlcdata (ParseCtx &ctx,
156
158
rapidxml::xml_node<unsigned char > *node) {
157
- return enif_make_tuple2 (ctx.env , atom_xmlcdata,
158
- to_subbinary (ctx, node->value (), node->value_size ()));
159
+ return enif_make_tuple3 (ctx.env , atom_xmlcdata,
160
+ to_subbinary (ctx, node->value (), node->value_size ()),
161
+ atom_escaped);
159
162
}
160
163
161
164
ERL_NIF_TERM merge_data_nodes (ParseCtx &ctx,
@@ -170,7 +173,7 @@ ERL_NIF_TERM merge_data_nodes(ParseCtx &ctx,
170
173
node = node->next_sibling ();
171
174
}
172
175
173
- return enif_make_tuple2 (ctx.env , atom_xmlcdata, bin);
176
+ return enif_make_tuple3 (ctx.env , atom_xmlcdata, bin, atom_escaped );
174
177
}
175
178
176
179
void append_pending_data_nodes (ParseCtx &ctx,
@@ -299,7 +302,15 @@ bool build_cdata(ErlNifEnv *env, xml_document &doc, const ERL_NIF_TERM elem[],
299
302
if (!enif_inspect_iolist_as_binary (env, elem[1 ], &bin))
300
303
return false ;
301
304
302
- auto child = doc.impl .allocate_node (rapidxml::node_data);
305
+ rapidxml::node_type cdata_type;
306
+ if (enif_compare (atom_escaped, elem[2 ]) == 0 )
307
+ cdata_type = rapidxml::node_data;
308
+ else if (enif_compare (atom_cdata, elem[2 ]) == 0 )
309
+ cdata_type = rapidxml::node_cdata;
310
+ else
311
+ return false ;
312
+
313
+ auto child = doc.impl .allocate_node (cdata_type);
303
314
child->value (bin.size > 0 ? bin.data : EMPTY, bin.size );
304
315
node.append_node (child);
305
316
return true ;
@@ -336,7 +347,7 @@ bool build_attrs(ErlNifEnv *env, xml_document &doc, ERL_NIF_TERM attrs,
336
347
bool build_el (ErlNifEnv *env, xml_document &doc, const ERL_NIF_TERM elem[],
337
348
rapidxml::xml_node<unsigned char > &node) {
338
349
ErlNifBinary name;
339
- if (!enif_inspect_iolist_as_binary (env, elem[1 ], &name))
350
+ if (!enif_inspect_binary (env, elem[1 ], &name))
340
351
return false ;
341
352
342
353
auto child = doc.impl .allocate_node (rapidxml::node_element);
@@ -358,7 +369,7 @@ bool build_child(ErlNifEnv *env, xml_document &doc, ERL_NIF_TERM child,
358
369
if (!enif_get_tuple (env, child, &arity, &tuple))
359
370
return false ;
360
371
361
- if (arity == 2 && enif_compare (atom_xmlcdata, tuple[0 ]) == 0 ) {
372
+ if (arity == 3 && enif_compare (atom_xmlcdata, tuple[0 ]) == 0 ) {
362
373
if (!build_cdata (env, doc, tuple, node))
363
374
return false ;
364
375
} else if (arity == 4 && enif_compare (atom_xmlel, tuple[0 ]) == 0 ) {
@@ -443,6 +454,8 @@ static int load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info) {
443
454
atom_xmlstreamstart = enif_make_atom (env, " xmlstreamstart" );
444
455
atom_xmlstreamend = enif_make_atom (env, " xmlstreamend" );
445
456
atom_pretty = enif_make_atom (env, " pretty" );
457
+ atom_escaped = enif_make_atom (env, " escaped" );
458
+ atom_cdata = enif_make_atom (env, " cdata" );
446
459
atom_true = enif_make_atom (env, " true" );
447
460
448
461
get_static_doc ().impl .set_allocator (enif_alloc, enif_free);
@@ -608,7 +621,15 @@ static ERL_NIF_TERM escape_cdata(ErlNifEnv *env, int argc,
608
621
if (!enif_inspect_iolist_as_binary (env, argv[0 ], &bin))
609
622
return enif_make_badarg (env);
610
623
611
- rapidxml::xml_node<unsigned char > node (rapidxml::node_data);
624
+ rapidxml::node_type cdata_type;
625
+ if (enif_compare (atom_escaped, argv[1 ]) == 0 )
626
+ cdata_type = rapidxml::node_data;
627
+ else if (enif_compare (atom_cdata, argv[1 ]) == 0 )
628
+ cdata_type = rapidxml::node_cdata;
629
+ else
630
+ return enif_make_badarg (env);
631
+
632
+ rapidxml::xml_node<unsigned char > node (cdata_type);
612
633
node.value (bin.data , bin.size );
613
634
return node_to_binary (env, node, rapidxml::print_no_indenting);
614
635
}
@@ -647,7 +668,7 @@ static ERL_NIF_TERM reset_parser(ErlNifEnv *env, int argc,
647
668
648
669
static ErlNifFunc nif_funcs[] = {
649
670
{" create" , 2 , create}, {" parse" , 1 , parse},
650
- {" parse_next" , 2 , parse_next}, {" escape_cdata" , 1 , escape_cdata},
671
+ {" parse_next" , 2 , parse_next}, {" escape_cdata" , 2 , escape_cdata},
651
672
{" to_binary" , 2 , to_binary}, {" reset_parser" , 1 , reset_parser}};
652
673
}
653
674
0 commit comments