@tiptap/core 2.3.0 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3550,28 +3550,30 @@ class NodePos {
3550
3550
  }
3551
3551
  querySelectorAll(selector, attributes = {}, firstItemOnly = false) {
3552
3552
  let nodes = [];
3553
- // iterate through children recursively finding all nodes which match the selector with the node name
3554
3553
  if (!this.children || this.children.length === 0) {
3555
3554
  return nodes;
3556
3555
  }
3556
+ const attrKeys = Object.keys(attributes);
3557
+ /**
3558
+ * Finds all children recursively that match the selector and attributes
3559
+ * If firstItemOnly is true, it will return the first item found
3560
+ */
3557
3561
  this.children.forEach(childPos => {
3562
+ // If we already found a node and we only want the first item, we dont need to keep going
3563
+ if (firstItemOnly && nodes.length > 0) {
3564
+ return;
3565
+ }
3558
3566
  if (childPos.node.type.name === selector) {
3559
- if (Object.keys(attributes).length > 0) {
3560
- const nodeAttributes = childPos.node.attrs;
3561
- const attrKeys = Object.keys(attributes);
3562
- for (let index = 0; index < attrKeys.length; index += 1) {
3563
- const key = attrKeys[index];
3564
- if (nodeAttributes[key] !== attributes[key]) {
3565
- return;
3566
- }
3567
- }
3568
- }
3569
- nodes.push(childPos);
3570
- if (firstItemOnly) {
3571
- return;
3567
+ const doesAllAttributesMatch = attrKeys.every(key => attributes[key] === childPos.node.attrs[key]);
3568
+ if (doesAllAttributesMatch) {
3569
+ nodes.push(childPos);
3572
3570
  }
3573
3571
  }
3574
- nodes = nodes.concat(childPos.querySelectorAll(selector));
3572
+ // If we already found a node and we only want the first item, we can stop here and skip the recursion
3573
+ if (firstItemOnly && nodes.length > 0) {
3574
+ return;
3575
+ }
3576
+ nodes = nodes.concat(childPos.querySelectorAll(selector, attributes, firstItemOnly));
3575
3577
  });
3576
3578
  return nodes;
3577
3579
  }