Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subelement_with_name_and_ns bug #38 #67

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading