nokogiri 1.18.4-java → 1.18.5-java

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7ce8e9024b1c99a7791442f4878b8dbf32c496d64df67a7cab74a301f684e57
4
- data.tar.gz: 76dedc95796c27a5d391e0b7747a912c62384d9c4289315ffd8c2f8d8d0a5fc9
3
+ metadata.gz: 469a3a4e6c64c4d29b75c44c8f861888947c199ce76911e9183d5aa7bb6f2c75
4
+ data.tar.gz: bf7b60ee2236e95fc1304a35dcc06ca769d55c7d7e42be3fa56a971268c7e902
5
5
  SHA512:
6
- metadata.gz: 115830e2fb42509bd48eef3a0c0841a82355a41f569f6f15cbe37d43fab988dad27dbe54a7d52dbccc7129c83fee97cba44e6ace43fa44a719daba5c2a2d81b4
7
- data.tar.gz: afc5d8bcb1114d10f7fa7bae4cd0137756be0d395a58411c821c0c78809d4e0b6b2691dacb8eb762d8c4101cb1738b3adebdfec9da2b760267249f560e91c068
6
+ metadata.gz: 59daf1552e1d775824ece1a776923603cd662b5b625ca94180731cf51089ffef4bd84b6484287aaa391bf1b0d2fc19df8de30f9b769a7232f8b3725f2ce03c6b
7
+ data.tar.gz: ca81c5c6d87ce3ac0cc9efd394eb9a35bf64ea9d6204a85cb80efbf9c763f5d3042ccc1c7a4a170b7debd4fae6317005923277c25d6d55949a47f2447cdb93ab
@@ -58,6 +58,7 @@ public class SaveContextVisitor
58
58
  private final Deque<Attr[]> c14nNamespaceStack;
59
59
  private final Deque<Attr[]> c14nAttrStack;
60
60
  //private List<String> c14nExclusiveInclusivePrefixes = null;
61
+ private final Stack<Map<String, String>> xmlnsNamespaceStack;
61
62
 
62
63
  /*
63
64
  * U can't touch this.
@@ -117,6 +118,7 @@ public class SaveContextVisitor
117
118
  if ((asBuilder && indent == null) || (asBuilder && indent.length() == 0)) { indent = " "; } // default, two spaces
118
119
  indentString = indent;
119
120
  if (!asXml && !asHtml && !asXhtml && !asBuilder) { asXml = true; }
121
+ xmlnsNamespaceStack = asXml ? new Stack<Map<String, String>>() : null;
120
122
  }
121
123
 
122
124
  @Override
@@ -432,19 +434,24 @@ public class SaveContextVisitor
432
434
  public boolean
433
435
  enter(Element element)
434
436
  {
437
+ pushXmlnsNamespaceStack();
438
+
435
439
  if (canonical) {
436
440
  c14nNodeList.add(element);
437
441
  if (element == element.getOwnerDocument().getDocumentElement()) {
438
442
  c14nNodeList.add(element.getOwnerDocument());
439
443
  }
440
444
  }
445
+
441
446
  String current = indentation.peek();
442
447
  buffer.append(current);
443
448
  if (needIndent(element)) {
444
449
  indentation.push(current + indentString);
445
450
  }
451
+
446
452
  String name = element.getTagName();
447
453
  buffer.append('<').append(name);
454
+
448
455
  Attr[] attrs = getAttrsAndNamespaces(element);
449
456
  for (Attr attr : attrs) {
450
457
  if (attr.getSpecified()) {
@@ -453,11 +460,13 @@ public class SaveContextVisitor
453
460
  leave(attr);
454
461
  }
455
462
  }
463
+
456
464
  if (element.hasChildNodes()) {
457
465
  buffer.append('>');
458
466
  if (needBreakInOpening(element)) { buffer.append('\n'); }
459
467
  return true;
460
468
  }
469
+
461
470
  // no child
462
471
  if (asHtml) {
463
472
  buffer.append('>');
@@ -472,12 +481,38 @@ public class SaveContextVisitor
472
481
  } else {
473
482
  buffer.append("/>");
474
483
  }
484
+
475
485
  if (needBreakInOpening(element)) {
476
486
  buffer.append('\n');
477
487
  }
478
488
  return true;
479
489
  }
480
490
 
491
+ private Map<String, String>
492
+ pushXmlnsNamespaceStack() {
493
+ if (!asXml || xmlnsNamespaceStack == null) { return null; }
494
+ Map<String, String> newContext;
495
+ if (xmlnsNamespaceStack.isEmpty()) {
496
+ newContext = new HashMap<String, String>();
497
+ } else {
498
+ Map<String, String> parentContext = xmlnsNamespaceStack.peek();
499
+ newContext = new HashMap<String, String>(parentContext);
500
+ }
501
+ return xmlnsNamespaceStack.push(newContext);
502
+ }
503
+
504
+ private Map<String, String>
505
+ popXmlnsNamespaceStack() {
506
+ if (!asXml || xmlnsNamespaceStack == null || xmlnsNamespaceStack.isEmpty()) { return null; }
507
+ return xmlnsNamespaceStack.pop();
508
+ }
509
+
510
+ private Map<String, String>
511
+ peekXmlnsNamespaceStack() {
512
+ if (!asXml || xmlnsNamespaceStack == null || xmlnsNamespaceStack.isEmpty()) { return null; }
513
+ return xmlnsNamespaceStack.peek();
514
+ }
515
+
481
516
  private boolean
482
517
  needIndent(Element element)
483
518
  {
@@ -511,11 +546,15 @@ public class SaveContextVisitor
511
546
  NamedNodeMap attrs = element.getAttributes();
512
547
  if (!canonical) {
513
548
  if (attrs == null || attrs.getLength() == 0) { return new Attr[0]; }
514
- Attr[] attrsAndNamespaces = new Attr[attrs.getLength()];
549
+ Map<String, String> xmlnsContext = peekXmlnsNamespaceStack();
550
+ List<Attr> filteredAttrsAndNamespaces = new ArrayList<Attr>();
515
551
  for (int i = 0; i < attrs.getLength(); i++) {
516
- attrsAndNamespaces[i] = (Attr) attrs.item(i);
552
+ Attr attr = (Attr) attrs.item(i);
553
+ if (!findOrAddRedundantNamespaceAttr(xmlnsContext, attr)) {
554
+ filteredAttrsAndNamespaces.add(attr);
555
+ }
517
556
  }
518
- return attrsAndNamespaces;
557
+ return filteredAttrsAndNamespaces.toArray(new Attr[0]);
519
558
  } else {
520
559
  List<Attr> namespaces = new ArrayList<Attr>();
521
560
  List<Attr> attributes = new ArrayList<Attr>();
@@ -544,7 +583,45 @@ public class SaveContextVisitor
544
583
  c14nAttrStack.push(attributeArray);
545
584
  return allAttrs;
546
585
  }
586
+ }
587
+
588
+ /**
589
+ * Detects whether a given attribute is a redundant xmlns namespace
590
+ * already present within xmlnsContext.
591
+ *
592
+ * As a side-effect, if the attribute is a non-redundant namespace,
593
+ * it is added to the xmlnsContext, so that it will be considered redundant
594
+ * for subsequent checks.
595
+ *
596
+ * @param xmlnsContext The namespace context, which should be the top object
597
+ * of xmlnsNamespaceStack.
598
+ * @param attr The attribute to check.
599
+ * @return True if the object is redundant, false otherwise.
600
+ */
601
+ private boolean
602
+ findOrAddRedundantNamespaceAttr(Map<String, String> xmlnsContext, Attr attr) {
603
+ if (xmlnsContext == null || !attr.getSpecified()) { return false; }
604
+
605
+ String xmlnsPrefix;
606
+ String attrName = attr.getNodeName();
607
+ if (attrName.equals("xmlns")) {
608
+ xmlnsPrefix = "";
609
+ } else if (attrName.startsWith("xmlns:")) {
610
+ xmlnsPrefix = attrName.substring(6);
611
+ } else {
612
+ // Not a namespace attribute
613
+ return false;
614
+ }
547
615
 
616
+ String xmlnsUri = attr.getNodeValue();
617
+ if (xmlnsContext.containsKey(xmlnsPrefix) && xmlnsUri.equals(xmlnsContext.get(xmlnsPrefix))) {
618
+ // Redundant namespace detected
619
+ return true;
620
+ } else {
621
+ // Add non-redundant namespace to the top of xmlnsNamespaceStack
622
+ xmlnsContext.put(xmlnsPrefix, xmlnsUri);
623
+ return false;
624
+ }
548
625
  }
549
626
 
550
627
  private void
@@ -653,6 +730,8 @@ public class SaveContextVisitor
653
730
  public void
654
731
  leave(Element element)
655
732
  {
733
+ popXmlnsNamespaceStack();
734
+
656
735
  if (canonical) {
657
736
  c14nNamespaceStack.poll();
658
737
  c14nAttrStack.poll();
Binary file
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Nokogiri
4
4
  # The version of Nokogiri you are using
5
- VERSION = "1.18.4"
5
+ VERSION = "1.18.5"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.18.4
4
+ version: 1.18.5
5
5
  platform: java
6
6
  authors:
7
7
  - Mike Dalessio
@@ -20,7 +20,7 @@ authors:
20
20
  autorequire:
21
21
  bindir: bin
22
22
  cert_chain: []
23
- date: 2025-03-14 00:00:00.000000000 Z
23
+ date: 2025-03-19 00:00:00.000000000 Z
24
24
  dependencies:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jar-dependencies