Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Dec 26, 2024
2 parents 6b89e08 + 4473283 commit 17387fd
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,119 @@ public virtual void MatchesEmptySelectorItemSpaceTest() {
NUnit.Framework.Assert.IsFalse(item.Matches(divNode));
}

[NUnit.Framework.Test]
public virtual void MatchesNthChildFixSelectorItemTest() {
CssPseudoClassNthChildSelectorItem item = new CssPseudoClassNthChildSelectorItem("2");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><p>Second</p><p>Third</p><p>Fourth</p>");
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[1];
INode third = bodyNode.ChildNodes()[2];
INode fourth = bodyNode.ChildNodes()[3];
NUnit.Framework.Assert.IsFalse(item.Matches(first), "First paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(second), "Second paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(third), "Third paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsFalse(item.Matches(fourth), "Fourth paragraph should NOT be matched, but matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthChildEvenSelectorItemTest() {
CssPseudoClassNthChildSelectorItem item = new CssPseudoClassNthChildSelectorItem("2n");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><p>Second</p><p>Third</p><p>Fourth</p>");
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[1];
INode third = bodyNode.ChildNodes()[2];
INode fourth = bodyNode.ChildNodes()[3];
NUnit.Framework.Assert.IsFalse(item.Matches(first), "First paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(second), "Second paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(third), "Third paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(fourth), "Fourth paragraph should be be matched, but WAS NOT matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthChildOddSelectorItemTest() {
CssPseudoClassNthChildSelectorItem item = new CssPseudoClassNthChildSelectorItem("2n-1");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><p>Second</p><p>Third</p><p>Fourth</p>");
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[1];
INode third = bodyNode.ChildNodes()[2];
INode fourth = bodyNode.ChildNodes()[3];
NUnit.Framework.Assert.IsTrue(item.Matches(first), "First paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(second), "Second paragraph should NOT be matched, but matched!"
);
NUnit.Framework.Assert.IsTrue(item.Matches(third), "Third paragraph should be be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(fourth), "Fourth paragraph should NOT be matched, but matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthLastChildFixSelectorItemTest() {
CssPseudoClassNthLastChildSelectorItem item = new CssPseudoClassNthLastChildSelectorItem("2");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><p>Second</p><p>Third</p><p>Fourth</p>");
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[1];
INode third = bodyNode.ChildNodes()[2];
INode fourth = bodyNode.ChildNodes()[3];
NUnit.Framework.Assert.IsFalse(item.Matches(first), "First paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsFalse(item.Matches(second), "Second paragraph should NOT be matched, but matched!"
);
NUnit.Framework.Assert.IsTrue(item.Matches(third), "Third paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(fourth), "Fourth paragraph should NOT be matched, but matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthLastChildEvenSelectorItemTest() {
CssPseudoClassNthLastChildSelectorItem item = new CssPseudoClassNthLastChildSelectorItem("2n");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><p>Second</p><p>Third</p><p>Fourth</p>");
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[1];
INode third = bodyNode.ChildNodes()[2];
INode fourth = bodyNode.ChildNodes()[3];
NUnit.Framework.Assert.IsTrue(item.Matches(first), "First paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(second), "Second paragraph should NOT be matched, but matched!"
);
NUnit.Framework.Assert.IsTrue(item.Matches(third), "Third paragraph should be be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(fourth), "Fourth paragraph should NOT be matched, but matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthLastChildOddSelectorItemTest() {
CssPseudoClassNthLastChildSelectorItem item = new CssPseudoClassNthLastChildSelectorItem("2n-1");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><p>Second</p><p>Third</p><p>Fourth</p>");
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[1];
INode third = bodyNode.ChildNodes()[2];
INode fourth = bodyNode.ChildNodes()[3];
NUnit.Framework.Assert.IsFalse(item.Matches(first), "First paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(second), "Second paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(third), "Third paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(fourth), "Fourth paragraph should be be matched, but WAS NOT matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesFirstOfTypeSelectorItemTest() {
CssPseudoClassFirstOfTypeSelectorItem item = CssPseudoClassFirstOfTypeSelectorItem.GetInstance();
Expand Down Expand Up @@ -88,6 +201,66 @@ public virtual void MatchesLastOfTypeSelectorItemTest() {
NUnit.Framework.Assert.IsTrue(item.Matches(divNode));
}

[NUnit.Framework.Test]
public virtual void MatchesNthLastOfTypeFixSelectorItemTest() {
CssPseudoClassNthLastOfTypeSelectorItem item = new CssPseudoClassNthLastOfTypeSelectorItem("2");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><h1>Headline</h1><p>Second</p><h1>Headline</h1><p>Third</p><h1>Headline</h1><p>Fourth</p>"
);
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[2];
INode third = bodyNode.ChildNodes()[4];
INode fourth = bodyNode.ChildNodes()[6];
NUnit.Framework.Assert.IsFalse(item.Matches(first), "First paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsFalse(item.Matches(second), "Second paragraph should NOT be matched, but matched!"
);
NUnit.Framework.Assert.IsTrue(item.Matches(third), "Third paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(fourth), "Fourth paragraph should NOT be be matched, but matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthLastOfTypeEvenSelectorItemTest() {
CssPseudoClassNthLastOfTypeSelectorItem item = new CssPseudoClassNthLastOfTypeSelectorItem("2n");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><h1>Headline</h1><p>Second</p><h1>Headline</h1><p>Third</p><h1>Headline</h1><p>Fourth</p>"
);
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[2];
INode third = bodyNode.ChildNodes()[4];
INode fourth = bodyNode.ChildNodes()[6];
NUnit.Framework.Assert.IsTrue(item.Matches(first), "First paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(second), "Second paragraph should NOT be matched, but matched!"
);
NUnit.Framework.Assert.IsTrue(item.Matches(third), "Third paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(fourth), "Fourth paragraph should NOT be be matched, but matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesNthLastOfTypeOddSelectorItemTest() {
CssPseudoClassNthLastOfTypeSelectorItem item = new CssPseudoClassNthLastOfTypeSelectorItem("2n-1");
IXmlParser htmlParser = new JsoupHtmlParser();
IDocumentNode documentNode = htmlParser.Parse("<p>First</p><h1>Headline</h1><p>Second</p><h1>Headline</h1><p>Third</p><h1>Headline</h1><p>Fourth</p>"
);
INode bodyNode = documentNode.ChildNodes()[0].ChildNodes()[1];
INode first = bodyNode.ChildNodes()[0];
INode second = bodyNode.ChildNodes()[2];
INode third = bodyNode.ChildNodes()[4];
INode fourth = bodyNode.ChildNodes()[6];
NUnit.Framework.Assert.IsFalse(item.Matches(first), "First paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(second), "Second paragraph should be matched, but WAS NOT matched!"
);
NUnit.Framework.Assert.IsFalse(item.Matches(third), "Third paragraph should NOT be matched, but matched!");
NUnit.Framework.Assert.IsTrue(item.Matches(fourth), "Fourth paragraph should be be matched, but WAS NOT matched!"
);
}

[NUnit.Framework.Test]
public virtual void MatchesLastOfTypeSelectorItemTestNotTaggedText() {
CssPseudoClassLastOfTypeSelectorItem item = CssPseudoClassLastOfTypeSelectorItem.GetInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ public virtual void Test22() {
));
}

[NUnit.Framework.Test]
public virtual void Test23() {
NUnit.Framework.Assert.AreEqual(CssSpecificityConstants.CLASS_SPECIFICITY * 2, GetSpecificity(".class_name:nth-last-child(2n - 3)"
));
}

[NUnit.Framework.Test]
public virtual void Test24() {
NUnit.Framework.Assert.AreEqual(CssSpecificityConstants.CLASS_SPECIFICITY * 2, GetSpecificity(".class_name:nth-of-type(2n - 3)"
));
}

[NUnit.Framework.Test]
public virtual void Test25() {
NUnit.Framework.Assert.AreEqual(CssSpecificityConstants.CLASS_SPECIFICITY * 2, GetSpecificity(".class_name:nth-last-of-type(2n - 3)"
));
}

[NUnit.Framework.Test]
public virtual void PageTest01() {
NUnit.Framework.Assert.AreEqual(CssSpecificityConstants.ID_SPECIFICITY, GetPageSelectorSpecificity("customPageName"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using iText.Commons.Utils;
using iText.StyledXmlParser.Css;
using iText.StyledXmlParser.Node;

namespace iText.StyledXmlParser.Css.Selector.Item {
//\cond DO_NOT_DOCUMENT
internal class CssPseudoClassNthLastChildSelectorItem : CssPseudoClassNthSelectorItem {
//\cond DO_NOT_DOCUMENT
internal CssPseudoClassNthLastChildSelectorItem(String arguments)
: base(CommonCssConstants.NTH_LAST_CHILD, arguments) {
}
//\endcond

protected internal override bool ResolveNth(INode node, IList<INode> children) {
IList<INode> reversedChildren = new List<INode>(children);
JavaCollectionsUtil.Reverse(reversedChildren);
return base.ResolveNth(node, reversedChildren);
}
}
//\endcond
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using iText.Commons.Utils;
using iText.StyledXmlParser.Css;
using iText.StyledXmlParser.Node;

namespace iText.StyledXmlParser.Css.Selector.Item {
//\cond DO_NOT_DOCUMENT
internal class CssPseudoClassNthLastOfTypeSelectorItem : CssPseudoClassNthSelectorItem {
//\cond DO_NOT_DOCUMENT
internal CssPseudoClassNthLastOfTypeSelectorItem(String arguments)
: base(CommonCssConstants.NTH_LAST_OF_TYPE, arguments) {
}
//\endcond

public override bool Matches(INode node) {
IList<INode> children = GetAllSiblingsOfNodeType(node);
return !children.IsEmpty() && ResolveNth(node, children);
}

protected internal override bool ResolveNth(INode node, IList<INode> children) {
IList<INode> reversedChildren = new List<INode>(children);
JavaCollectionsUtil.Reverse(reversedChildren);
return base.ResolveNth(node, reversedChildren);
}
}
//\endcond
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ You should have received a copy of the GNU Affero General Public License
namespace iText.StyledXmlParser.Css.Selector.Item {
//\cond DO_NOT_DOCUMENT
internal class CssPseudoClassNthOfTypeSelectorItem : CssPseudoClassNthSelectorItem {
public CssPseudoClassNthOfTypeSelectorItem(String arguments)
//\cond DO_NOT_DOCUMENT
internal CssPseudoClassNthOfTypeSelectorItem(String arguments)
: base(CommonCssConstants.NTH_OF_TYPE, arguments) {
}
//\endcond

public override bool Matches(INode node) {
if (!(node is IElementNode) || node is ICustomElementNode || node is IDocumentNode) {
return false;
}
IList<INode> children = GetAllSiblingsOfNodeType(node);
return !children.IsEmpty() && ResolveNth(node, children);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ internal CssPseudoClassNthSelectorItem(String pseudoClass, String arguments)
//\endcond

public override bool Matches(INode node) {
if (!(node is IElementNode) || node is ICustomElementNode || node is IDocumentNode) {
return false;
}
IList<INode> children = GetAllSiblings(node);
return !children.IsEmpty() && ResolveNth(node, children);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,18 @@ public static iText.StyledXmlParser.Css.Selector.Item.CssPseudoClassSelectorItem
return new CssPseudoClassNthChildSelectorItem(arguments);
}

case CommonCssConstants.NTH_LAST_CHILD: {
return new CssPseudoClassNthLastChildSelectorItem(arguments);
}

case CommonCssConstants.NTH_OF_TYPE: {
return new CssPseudoClassNthOfTypeSelectorItem(arguments);
}

case CommonCssConstants.NTH_LAST_OF_TYPE: {
return new CssPseudoClassNthLastOfTypeSelectorItem(arguments);
}

case CommonCssConstants.NOT: {
CssSelector selector = new CssSelector(arguments);
foreach (ICssSelectorItem item in selector.GetSelectorItems()) {
Expand Down Expand Up @@ -136,8 +144,6 @@ public static iText.StyledXmlParser.Css.Selector.Item.CssPseudoClassSelectorItem
//case CommonCssConstants.IN_RANGE:
//case CommonCssConstants.INVALID:
//case CommonCssConstants.LANG:
//case CommonCssConstants.NTH_LAST_CHILD:
//case CommonCssConstants.NTH_LAST_OF_TYPE:
//case CommonCssConstants.ONLY_OF_TYPE:
//case CommonCssConstants.ONLY_CHILD:
//case CommonCssConstants.OPTIONAL:
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8dd0cafd0d2fb3671f49eb5c633291df2f862b8f
4cd6065d57c660f788a331f31b96ffbdeea6a13a

0 comments on commit 17387fd

Please sign in to comment.