Skip to content

Commit

Permalink
Merge pull request #67 from esl/bugfix/subelement_with_name_and_ns
Browse files Browse the repository at this point in the history
Fix subelement_with_name_and_ns bug #38
  • Loading branch information
chrzaszcz authored Nov 13, 2023
2 parents d14eea2 + f86f554 commit 9814aa2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
18 changes: 4 additions & 14 deletions src/exml_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,11 @@ subelement_with_name_and_ns(Element, Name, NS) ->
-spec subelement_with_name_and_ns(exml:element(), binary(), binary(), Other) ->
exml:element() | Other.
subelement_with_name_and_ns(Element, Name, NS, Default) ->
case subelement(Element, Name, undefined) of
undefined ->
case subelements_with_name_and_ns(Element, Name, NS) of
[] ->
Default;
#xmlcdata{} ->
Default;
#xmlel{} = SubElement ->
subelement_or_default(SubElement, NS, Default)
end.

subelement_or_default(SubElement, NS, Default) ->
case attr(SubElement, <<"xmlns">>) of
NS ->
SubElement;
_ ->
Default
[FirstElem | _] ->
FirstElem
end.

-spec subelements(exml:element(), binary()) -> [exml:element()].
Expand Down
9 changes: 8 additions & 1 deletion test/exml_query_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-include_lib("eunit/include/eunit.hrl").
-include("exml.hrl").

-compile(export_all).
-compile([export_all, nowarn_export_all]).

-define(MY_SPOON, xml(<<"<spoon whose='my'>",
"<problem no='1' xmlns='urn:issues'>is too big</problem>",
Expand Down Expand Up @@ -131,6 +131,13 @@ element_with_name_and_ns_query_test() ->
[{element_with_ns, <<"displayed">>,
<<"urn:xmpp:chat-markers:0">>}])).

element_with_name_and_ns_two_names_only_one_ns_query_test() ->
Elem1 = #xmlel{name = <<"a">>, attrs = [{<<"xmlns">>, <<"ns1">>}]},
Elem2 = #xmlel{name = <<"a">>, attrs = [{<<"xmlns">>, <<"ns2">>}]},
Xml = #xmlel{name = <<"element">>, children = [Elem1, Elem2]},
?assertEqual(Elem2, exml_query:subelement_with_name_and_ns(Xml, <<"a">>, <<"ns2">>)),
?assertEqual(Elem2, exml_query:path(Xml, [{element_with_ns, <<"a">>, <<"ns2">>}])).

no_element_with_name_and_ns_query_test() ->
?assertEqual(none,
exml_query:subelement_with_name_and_ns(chat_marker(),
Expand Down

0 comments on commit 9814aa2

Please sign in to comment.