diff --git a/packages/tiled/lib/src/objects/text.dart b/packages/tiled/lib/src/objects/text.dart index 5ac76e3..0432d1c 100644 --- a/packages/tiled/lib/src/objects/text.dart +++ b/packages/tiled/lib/src/objects/text.dart @@ -74,7 +74,8 @@ class Text { fontFamily: parser.getString('fontFamily', defaults: 'sans-serif'), pixelSize: parser.getInt('pixelSize', defaults: 16), color: parser.getString('color', defaults: '#000000'), - text: parser.getString('text', defaults: ''), + text: parser.getInnerTextOrNull() ?? + parser.getString('text', defaults: ''), hAlign: parser.getHAlign('hAlign', defaults: HAlign.left), vAlign: parser.getVAlign('vAlign', defaults: VAlign.top), bold: parser.getBool('bold', defaults: false), diff --git a/packages/tiled/lib/src/parser.dart b/packages/tiled/lib/src/parser.dart index 3cd278a..eade166 100644 --- a/packages/tiled/lib/src/parser.dart +++ b/packages/tiled/lib/src/parser.dart @@ -11,6 +11,10 @@ class XmlParser extends Parser { final XmlElement element; XmlParser(this.element); + @override + String? getInnerTextOrNull() => + element.innerText.isEmpty ? null : element.innerText; + @override String? getStringOrNull(String name, {String? defaults}) { return element.getAttribute(name) ?? defaults; @@ -46,6 +50,9 @@ class JsonParser extends Parser { final Map json; JsonParser(this.json); + @override + String? getInnerTextOrNull() => null; + @override String? getStringOrNull(String name, {String? defaults}) { return json[name]?.toString() ?? defaults; @@ -75,6 +82,8 @@ class JsonParser extends Parser { } abstract class Parser { + String? getInnerTextOrNull(); + String? getStringOrNull(String name, {String? defaults}); List getChildren(String name); diff --git a/packages/tiled/test/parser_test.dart b/packages/tiled/test/parser_test.dart index 519d5b7..d015dc7 100644 --- a/packages/tiled/test/parser_test.dart +++ b/packages/tiled/test/parser_test.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'dart:math' show Rectangle; import 'dart:ui'; +import 'package:collection/collection.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:tiled/tiled.dart'; import 'package:xml/xml.dart'; @@ -88,7 +89,9 @@ void main() { equals('test_value'), ); expect( - properties.getValue('multiline string'), + properties + .getValue('multiline string') + ?.replaceAll('\r\n', '\n'), equals('Hello,\nWorld'), ); expect( @@ -235,6 +238,15 @@ void main() { test('has the right class_', () { expect(og.class_, equals('objectLayer1Class')); }); + + test('has the right text object with ID 5', () { + final textObject = (og as ObjectGroup) + .objects + .firstWhereOrNull((element) => element.id == 5); + final text = textObject?.text; + expect(text?.wrap, equals(true)); + expect(text?.text, equals('Hello World')); + }); }); });