From 30d0ca141e31bdf2ad0ee11e0455e5fc7e84fe26 Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Tue, 20 Aug 2024 21:31:46 +0330 Subject: [PATCH] Fix offset for line wrapped in block --- packages/parchment/lib/src/document/attributes.dart | 8 ++++---- packages/parchment/lib/src/document/node.dart | 10 +++------- packages/parchment/test/document/line_test.dart | 9 ++++++++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/packages/parchment/lib/src/document/attributes.dart b/packages/parchment/lib/src/document/attributes.dart index 2e9cc650..120ee711 100644 --- a/packages/parchment/lib/src/document/attributes.dart +++ b/packages/parchment/lib/src/document/attributes.dart @@ -1,7 +1,6 @@ import 'dart:math' as math; import 'package:collection/collection.dart'; -import 'package:quiver/core.dart'; /// Scope of a style attribute, defines context in which an attribute can be /// applied. @@ -254,7 +253,7 @@ class ParchmentAttribute implements ParchmentAttributeBuilder { } @override - int get hashCode => hash3(key, scope, value); + int get hashCode => Object.hash(key, scope, value); @override String toString() => '$key: $value'; @@ -397,8 +396,9 @@ class ParchmentStyle { @override int get hashCode { - final hashes = _data.entries.map((entry) => hash2(entry.key, entry.value)); - return hashObjects(hashes); + final hashes = + _data.entries.map((entry) => Object.hash(entry.key, entry.value)); + return Object.hashAll(hashes); } @override diff --git a/packages/parchment/lib/src/document/node.dart b/packages/parchment/lib/src/document/node.dart index 91083473..47848b45 100644 --- a/packages/parchment/lib/src/document/node.dart +++ b/packages/parchment/lib/src/document/node.dart @@ -112,8 +112,8 @@ abstract base class Node extends LinkedListEntry { void unlink() { assert(_parent != null); final oldParent = _parent; - _parent = null; final oldNext = next; + _parent = null; super.unlink(); oldNext?.invalidateOffset(); oldParent?.invalidateLength(); @@ -192,7 +192,7 @@ abstract base class ContainerNode extends Node { assert(node._parent == null); node._parent = this; _children.add(node); - node.next?.invalidateOffset(); + node.invalidateOffset(); invalidateLength(); } @@ -201,7 +201,7 @@ abstract base class ContainerNode extends Node { assert(node._parent == null); node._parent = this; _children.addFirst(node); - node.next?.invalidateOffset(); + node.invalidateOffset(); invalidateLength(); } @@ -227,8 +227,6 @@ abstract base class ContainerNode extends Node { newParent.add(child); } - invalidateLength(); - /// In case [newParent] already had children we need to make sure /// combined list is optimized. if (toBeOptimized != null) toBeOptimized.optimize(); @@ -283,7 +281,6 @@ abstract base class ContainerNode extends Node { final result = lookup(index); result.node!.insert(result.offset, data, style); } - invalidateLength(); } @override @@ -298,7 +295,6 @@ abstract base class ContainerNode extends Node { assert(isNotEmpty); final res = lookup(index); res.node!.delete(res.offset, length); - invalidateLength(); } @override diff --git a/packages/parchment/test/document/line_test.dart b/packages/parchment/test/document/line_test.dart index b3eed1b5..778e19ce 100644 --- a/packages/parchment/test/document/line_test.dart +++ b/packages/parchment/test/document/line_test.dart @@ -127,10 +127,17 @@ void main() { }); test('format line', () { - root.insert(0, 'Hello world', null); + root.insert(0, 'Hello world\n', null); + root.insert(12, 'Second headline\n', null); root.retain(11, 1, h1Style); root.retain(11, 1, rightStyle); + final secondHeadline = root.first.next!; + expect(secondHeadline.offset, 12); + + root.retain(27, 1, ParchmentStyle().merge(ParchmentAttribute.cl)); + expect(secondHeadline.offset, 0); + final line = root.first as LineNode; expect(line, hasLength(12));