Skip to content

Commit cd3f64f

Browse files
committed
Merge branch 'maint'
2 parents f44fb97 + fa1d1bc commit cd3f64f

File tree

7 files changed

+68
-13
lines changed

7 files changed

+68
-13
lines changed

lib/xmerl/src/xmerl.erl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ export_content([#xmlText{value = Text, type = cdata} | Es], Callbacks) ->
265265
[apply_cdata_cb(Callbacks, Text) | export_content(Es, Callbacks)];
266266
export_content([#xmlPI{} | Es], Callbacks) ->
267267
export_content(Es, Callbacks);
268-
export_content([#xmlComment{} | Es], Callbacks) ->
269-
export_content(Es, Callbacks);
268+
export_content([#xmlComment{value = Text} | Es], Callbacks) ->
269+
[apply_comment_cb(Callbacks, Text) | export_content(Es, Callbacks)];
270270
export_content([#xmlDecl{} | Es], Callbacks) ->
271271
export_content(Es, Callbacks);
272272
export_content([E | Es], Callbacks) ->
@@ -306,8 +306,8 @@ export_element(E = #xmlElement{name = Tag,
306306
tagdef(Tag,Pos,Parents,Args,CBs);
307307
export_element(#xmlPI{}, _CBs) ->
308308
[];
309-
export_element(#xmlComment{}, _CBs) ->
310-
[];
309+
export_element(#xmlComment{value = Text}, CBs) ->
310+
apply_comment_cb(CBs, Text);
311311
export_element(#xmlDecl{}, _CBs) ->
312312
[].
313313

@@ -337,8 +337,8 @@ export_element(E=#xmlElement{name = Tag,
337337
tagdef(Tag,Pos,Parents,Args,Callbacks);
338338
export_element(#xmlPI{}, _CallbackModule, CallbackState) ->
339339
CallbackState;
340-
export_element(#xmlComment{},_CallbackModule, CallbackState) ->
341-
CallbackState;
340+
export_element(#xmlComment{value = Text},CallbackModule,_CallbackState) ->
341+
apply_comment_cb(CallbackModule,Text);
342342
export_element(#xmlDecl{},_CallbackModule, CallbackState) ->
343343
CallbackState.
344344

@@ -390,6 +390,9 @@ apply_text_cb(Ms, Text) ->
390390
apply_cdata_cb(Ms, Text) ->
391391
apply_cb(Ms, '#cdata#', '#cdata#', [Text]).
392392

393+
apply_comment_cb(Ms, Text) ->
394+
apply_cb(Ms, '#comment#', '#comment#', [Text]).
395+
393396
apply_tag_cb(Ms, F, Args) ->
394397
apply_cb(Ms, F, '#element#', Args).
395398

lib/xmerl/src/xmerl_html.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
'#element#'/5,
3434
'#text#'/1,
3535
'#cdata#'/1,
36+
'#comment#'/1,
3637
p/4]).
3738

3839
-import(xmerl_lib, [start_tag/2, end_tag/1, is_empty_data/1,
39-
find_attribute/2, export_text/1]).
40+
find_attribute/2, export_text/1, export_comment/1]).
4041

4142
-include("xmerl.hrl").
4243

@@ -53,6 +54,10 @@
5354
'#cdata#'(Text) ->
5455
export_text(Text).
5556

57+
%% The '#comment#' function is called for every comment element.
58+
'#comment#'(Text) ->
59+
export_comment(Text).
60+
5661
%% The '#root#' tag is called when the entire structure has been
5762
%% exported. It does not appear in the structure itself.
5863

lib/xmerl/src/xmerl_lib.erl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
expand_content/3, normalize_element/1, normalize_element/3,
3131
expand_element/1, expand_element/3, expand_attributes/1,
3232
expand_attributes/3, export_text/1, flatten_text/1, export_cdata/1,
33+
export_comment/1,
3334
export_attribute/1, markup/2, markup/3, simplify_element/1,
3435
simplify_content/1, start_tag/1, start_tag/2, end_tag/1,
3536
empty_tag/1, empty_tag/2,is_empty_data/1, find_attribute/2,
@@ -104,6 +105,21 @@ export_cdata([], []) ->
104105
export_cdata(Bin, Cont) ->
105106
export_cdata(binary_to_list(Bin), Cont).
106107

108+
%% Export comment
109+
export_comment(T) ->
110+
R = "<!--" ++ export_comment(T, []),
111+
R ++ "-->".
112+
export_comment([C | T], Cont) when is_integer(C) ->
113+
[C | export_comment(T, Cont)];
114+
export_comment([T | T1], Cont) ->
115+
export_comment(T, [T1 | Cont]);
116+
export_comment([], [T | Cont]) ->
117+
export_comment(T, Cont);
118+
export_comment([], []) ->
119+
[];
120+
export_comment(Bin, Cont) ->
121+
export_comment(binary_to_list(Bin), Cont).
122+
107123
%% Convert attribute value to a flat string, escaping characters `"',
108124
%% `<' and `&'. (Note that single-quote characters are not escaped; the
109125
%% markup-generating functions (`start_tag', `end_tag', ...) always use

lib/xmerl/src/xmerl_text.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
-export(['#root#'/4,
3232
'#element#'/5,
3333
'#text#'/1,
34-
'#cdata#'/1]).
34+
'#cdata#'/1,
35+
'#comment#'/1]).
3536

3637
-include("xmerl.hrl").
3738

@@ -46,6 +47,10 @@
4647
%% Handled the same as text.
4748
'#cdata#'(Text) -> Text.
4849

50+
%% The '#comment#' function is called for every comment element.
51+
%% Comment value is not exported since there is no markup.
52+
'#comment#'(_Text) -> [].
53+
4954
%% The '#root#' tag is called when the entire structure has been
5055
%% exported. It does not appear in the structure itself.
5156

lib/xmerl/src/xmerl_xml.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
-export(['#root#'/4,
3232
'#element#'/5,
3333
'#text#'/1,
34-
'#cdata#'/1]).
34+
'#cdata#'/1,
35+
'#comment#'/1]).
3536

36-
-import(xmerl_lib, [markup/3, empty_tag/2, export_text/1, export_cdata/1]).
37+
-import(xmerl_lib, [markup/3, empty_tag/2, export_text/1, export_cdata/1,
38+
export_comment/1]).
3739

3840
-include("xmerl.hrl").
3941
-include("xmerl_internal.hrl").
@@ -52,6 +54,10 @@
5254
%?dbg("Cdata=~p~n",[Text]),
5355
export_cdata(Text).
5456

57+
%% The '#comment#' function is called for every comment element.
58+
'#comment#'(Text) ->
59+
export_comment(Text).
60+
5561
%% The '#root#' tag is called when the entire structure has been
5662
%% exported. It does not appear in the structure itself.
5763

lib/xmerl/src/xmerl_xml_indent.erl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434

3535
-export(['#root#'/4,
3636
'#element#'/5,
37-
'#text#'/1]).
37+
'#text#'/1,
38+
'#comment#'/1]).
3839

39-
-import(xmerl_lib, [markup/3, empty_tag/2, export_text/1]).
40+
-import(xmerl_lib, [markup/3, empty_tag/2, export_text/1, export_comment/1]).
4041

4142
-include("xmerl.hrl").
4243
-include("xmerl_internal.hrl").
@@ -51,6 +52,13 @@
5152
export_text(Text).
5253

5354

55+
% The '#comment#' function is called for every comment element.
56+
57+
'#comment#'(Text) ->
58+
%% Returning a list allows is_char/1 to return true
59+
%% when indenting the contents of an element.
60+
[ export_comment(Text) ].
61+
5462
%% The '#root#' tag is called when the entire structure has been
5563
%% exported. It does not appear in the structure itself.
5664

lib/xmerl/test/xmerl_SUITE.erl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ groups() ->
5757
{misc, [],
5858
[latin1_alias, syntax_bug1, syntax_bug2, syntax_bug3,
5959
pe_ref1, copyright, testXSEIF, export_simple1, export,
60-
export_cdata,
60+
export_cdata, export_comments,
6161
default_attrs_bug, xml_ns, scan_splits_string_bug,
6262
allow_entities_test]},
6363
{eventp_tests, [], [sax_parse_and_export]},
@@ -325,6 +325,17 @@ export_cdata(Config) ->
325325
InData = list_to_binary(Exported),
326326
ok.
327327

328+
export_comments(Config) ->
329+
InData = <<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
330+
<doc>
331+
<!-- top comment --><a>Test...</a>
332+
<!-- bottom comment --></doc>">>,
333+
Prolog = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"],
334+
{E, _} = xmerl_scan:string(binary:bin_to_list(InData)),
335+
Exported = xmerl:export([E],xmerl_xml,[{prolog,Prolog}]),
336+
InData = list_to_binary(Exported),
337+
ok.
338+
328339
%%----------------------------------------------------------------------
329340

330341
sax_parse_and_export(Config) ->
@@ -784,6 +795,7 @@ xml_namespace_indented() ->
784795
"\n <title>Cheaper by the Dozen</title>"
785796
"\n <isbn:number>1568491379</isbn:number>"
786797
"\n <notes>"
798+
"\n <!-- make HTML the default namespace for some comments -->"
787799
"\n <p xmlns=\"urn:w3-org-ns:HTML\">This is a <i>funny</i> book!</p>"
788800
"\n </notes>"
789801
"\n</book>".

0 commit comments

Comments
 (0)