feat(core): Re-add xmlns for StanzaHandler

This commit is contained in:
PapaTutuWawa 2023-05-07 00:13:25 +02:00
parent 483cb0d7f1
commit f3d906e69b
2 changed files with 27 additions and 21 deletions

View File

@ -1,5 +1,6 @@
import 'package:moxlib/moxlib.dart'; import 'package:moxlib/moxlib.dart';
import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/data.dart';
import 'package:moxxmpp/src/namespaces.dart';
import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stanza.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
@ -55,6 +56,7 @@ class StanzaHandler extends Handler {
this.tagName, this.tagName,
this.priority = 0, this.priority = 0,
this.stanzaTag, this.stanzaTag,
this.xmlns,
}); });
/// If specified, then the stanza must contain a direct child with a tag equal to /// If specified, then the stanza must contain a direct child with a tag equal to
@ -69,6 +71,11 @@ class StanzaHandler extends Handler {
/// If specified, the matching stanza must have a tag equal to [stanzaTag]. /// If specified, the matching stanza must have a tag equal to [stanzaTag].
final String? stanzaTag; final String? stanzaTag;
/// If specified, then the stanza must have a xmlns attribute equal to [xmlns].
/// This defaults to [stanzaXmlns], but can be set to any other value or null. This
/// is useful, for example, for components.
final String? xmlns;
/// The priority after which [StanzaHandler]s are sorted. /// The priority after which [StanzaHandler]s are sorted.
final int priority; final int priority;
@ -81,11 +88,9 @@ class StanzaHandler extends Handler {
if (stanzaTag != null) { if (stanzaTag != null) {
matches &= node.tag == stanzaTag; matches &= node.tag == stanzaTag;
} }
// if (xmlns != null) { if (xmlns != null) {
// matches &= node.xmlns == xmlns; matches &= node.xmlns == xmlns;
// if (flag != null) }
// print('${node.xmlns} == $xmlns');
// }
if (tagName != null) { if (tagName != null) {
final firstTag = node.firstTag(tagName!, xmlns: tagXmlns); final firstTag = node.firstTag(tagName!, xmlns: tagXmlns);

View File

@ -122,23 +122,24 @@ void main() {
expect(handler.matches(stanza2), false); expect(handler.matches(stanza2), false);
}); });
// test('Test matching stanzas with a different xmlns', () { test('Test matching stanzas with a different xmlns', () {
// final handler = StanzaHandler( final handler = StanzaHandler(
// callback: (stanza, _) async => StanzaHandlerData( callback: (stanza, _) async => StanzaHandlerData(
// true, true,
// false, false,
// null, null,
// stanza, stanza,
// ), ),
// ); xmlns: componentAcceptXmlns,
);
// expect(handler.matches(Stanza.iq(xmlns: stanzaXmlns)), false); expect(handler.matches(Stanza.iq(xmlns: stanzaXmlns)), false);
// expect(handler.matches(Stanza.message(xmlns: stanzaXmlns)), false); expect(handler.matches(Stanza.message(xmlns: stanzaXmlns)), false);
// expect(handler.matches(Stanza.presence(xmlns: stanzaXmlns)), false); expect(handler.matches(Stanza.presence(xmlns: stanzaXmlns)), false);
// expect(handler.matches(Stanza.iq(xmlns: componentAcceptXmlns)), true); expect(handler.matches(Stanza.iq(xmlns: componentAcceptXmlns)), true);
// expect(handler.matches(stanza1), false); expect(handler.matches(stanza1), false);
// expect(handler.matches(stanza2), false); expect(handler.matches(stanza2), false);
// }); });
test('sorting', () { test('sorting', () {
final handlerList = [ final handlerList = [