Skip to content

Commit

Permalink
Fixed an empty string when parse multiline text from object group.
Browse files Browse the repository at this point in the history
  • Loading branch information
syrokomskyi committed Jan 17, 2024
1 parent 8d765a8 commit 3181d39
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/tiled/lib/src/objects/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions packages/tiled/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +50,9 @@ class JsonParser extends Parser {
final Map<String, dynamic> json;
JsonParser(this.json);

@override
String? getInnerTextOrNull() => null;

@override
String? getStringOrNull(String name, {String? defaults}) {
return json[name]?.toString() ?? defaults;
Expand Down Expand Up @@ -75,6 +82,8 @@ class JsonParser extends Parser {
}

abstract class Parser {
String? getInnerTextOrNull();

String? getStringOrNull(String name, {String? defaults});

List<Parser> getChildren(String name);
Expand Down
10 changes: 10 additions & 0 deletions packages/tiled/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -237,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'));
});
});
});

Expand Down

0 comments on commit 3181d39

Please sign in to comment.