From 782b2f4d00116068947ba31ce77f299aa5772206 Mon Sep 17 00:00:00 2001 From: willbean <182769979@qq.com> Date: Sat, 17 Jun 2023 18:39:30 +0800 Subject: [PATCH] fix: Improve the getAttributes method implementation --- src/types/AbstractType.js | 15 ++++++++++++--- src/types/YText.js | 6 ++++-- src/types/YXmlElement.js | 8 +++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/types/AbstractType.js b/src/types/AbstractType.js index 144cfc3c..dfd0ef1d 100644 --- a/src/types/AbstractType.js +++ b/src/types/AbstractType.js @@ -877,19 +877,28 @@ export const typeMapGet = (parent, key) => { /** * @param {AbstractType} parent + * @param {Snapshot} [snapshot] * @return {Object|number|null|Array|string|Uint8Array|AbstractType|undefined>} * * @private * @function */ -export const typeMapGetAll = (parent) => { +export const typeMapGetAll = (parent, snapshot) => { /** * @type {Object} */ const res = {} + const isItemExist = parent._item && isVisible(parent._item, snapshot) parent._map.forEach((value, key) => { - if (!value.deleted) { - res[key] = value.content.getContent()[value.length - 1] + /** + * @type {Item|null} + */ + let item = value + while (isItemExist && item && !isVisible(item, snapshot)) { + item = item.left + } + if (item) { + res[key] = item.content.getContent()[item.length - 1] } }) return res diff --git a/src/types/YText.js b/src/types/YText.js index 79430c1a..378db0b9 100644 --- a/src/types/YText.js +++ b/src/types/YText.js @@ -1259,12 +1259,14 @@ export class YText extends AbstractType { * * @note Xml-Text nodes don't have attributes. You can use this feature to assign properties to complete text-blocks. * + * @param {Snapshot} [snapshot] + * * @return {Object} A JSON Object that describes the attributes. * * @public */ - getAttributes () { - return typeMapGetAll(this) + getAttributes (snapshot) { + return typeMapGetAll(this, snapshot) } /** diff --git a/src/types/YXmlElement.js b/src/types/YXmlElement.js index 92088cdd..bea880b8 100644 --- a/src/types/YXmlElement.js +++ b/src/types/YXmlElement.js @@ -10,7 +10,7 @@ import { typeMapGetAll, typeListForEach, YXmlElementRefID, - YXmlText, ContentType, AbstractType, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Item // eslint-disable-line + YXmlText, ContentType, AbstractType, UpdateDecoderV1, UpdateDecoderV2, UpdateEncoderV1, UpdateEncoderV2, Doc, Item, Snapshot // eslint-disable-line } from '../internals.js' /** @@ -192,12 +192,14 @@ export class YXmlElement extends YXmlFragment { /** * Returns all attribute name/value pairs in a JSON Object. * + * @param {Snapshot} [snapshot] + * * @return {{ [Key in Extract]?: KV[Key]}} A JSON Object that describes the attributes. * * @public */ - getAttributes () { - return /** @type {any} */ (typeMapGetAll(this)) + getAttributes (snapshot) { + return /** @type {any} */ (typeMapGetAll(this, snapshot)) } /**