Skip to content

Commit dd6c2f2

Browse files
Egor MartsynkovskyiText-CI
authored andcommitted
Add support of attr() with type for url and string
Add support of attr() with fallback for url and string Add support of attr() in target-counter Add tests DEVSIX-2994 Autoported commit. Original commit hash: [ca4c3bbc0]
1 parent 6bd0123 commit dd6c2f2

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed

itext.tests/itext.styledxmlparser.tests/itext/styledxmlparser/css/util/CssUtilsTest.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ source product.
4646
using iText.Layout.Properties;
4747
using iText.StyledXmlParser;
4848
using iText.StyledXmlParser.Css;
49+
using iText.StyledXmlParser.Css.Pseudo;
4950
using iText.StyledXmlParser.Exceptions;
51+
using iText.StyledXmlParser.Node;
5052
using iText.StyledXmlParser.Node.Impl.Jsoup.Node;
5153
using iText.Test;
5254
using iText.Test.Attributes;
@@ -460,5 +462,130 @@ public virtual void IsNegativeValueTest() {
460462
NUnit.Framework.Assert.IsTrue(CssUtils.IsNegativeValue("-0.123"));
461463
NUnit.Framework.Assert.IsTrue(CssUtils.IsNegativeValue("-.34"));
462464
}
465+
466+
[NUnit.Framework.Test]
467+
public virtual void TestWrongAttrTest01() {
468+
String strToParse = "attr((href))";
469+
String result = CssUtils.ExtractAttributeValue(strToParse, null);
470+
NUnit.Framework.Assert.IsNull(result);
471+
}
472+
473+
[NUnit.Framework.Test]
474+
public virtual void TestWrongAttrTest02() {
475+
String strToParse = "attr('href')";
476+
String result = CssUtils.ExtractAttributeValue(strToParse, null);
477+
NUnit.Framework.Assert.IsNull(result);
478+
}
479+
480+
[NUnit.Framework.Test]
481+
public virtual void TestWrongAttrTest03() {
482+
String strToParse = "attrrname)";
483+
String result = CssUtils.ExtractAttributeValue(strToParse, null);
484+
NUnit.Framework.Assert.IsNull(result);
485+
}
486+
487+
[NUnit.Framework.Test]
488+
public virtual void TestExtractingAttrTest01() {
489+
IElementNode iNode = new CssPseudoElementNode(null, "url");
490+
String strToParse = "attr(url)";
491+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
492+
NUnit.Framework.Assert.AreEqual("", result);
493+
}
494+
495+
[NUnit.Framework.Test]
496+
public virtual void TestExtractingAttrTest02() {
497+
IElementNode iNode = new CssPseudoElementNode(null, "test");
498+
String strToParse = "attr(url url)";
499+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
500+
NUnit.Framework.Assert.IsNull(result);
501+
}
502+
503+
[NUnit.Framework.Test]
504+
public virtual void TestExtractingAttrTest03() {
505+
IElementNode iNode = new CssPseudoElementNode(null, "test");
506+
String strToParse = "attr(url url,#one)";
507+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
508+
NUnit.Framework.Assert.AreEqual("#one", result);
509+
}
510+
511+
[NUnit.Framework.Test]
512+
public virtual void TestExtractingAttrTest04() {
513+
IElementNode iNode = new CssPseudoElementNode(null, "test");
514+
String strToParse = "attr()";
515+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
516+
NUnit.Framework.Assert.IsNull(result);
517+
}
518+
519+
[NUnit.Framework.Test]
520+
public virtual void TestExtractingAttrTest05() {
521+
IElementNode iNode = new CssPseudoElementNode(null, "test");
522+
String strToParse = "attr('\')";
523+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
524+
NUnit.Framework.Assert.IsNull(result);
525+
}
526+
527+
[NUnit.Framework.Test]
528+
public virtual void TestExtractingAttrTest06() {
529+
IElementNode iNode = new CssPseudoElementNode(null, "test");
530+
String strToParse = "attr(str,\"hey\")";
531+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
532+
NUnit.Framework.Assert.AreEqual("hey", result);
533+
}
534+
535+
[NUnit.Framework.Test]
536+
public virtual void TestExtractingAttrTest07() {
537+
IElementNode iNode = new CssPseudoElementNode(null, "test");
538+
String strToParse = "attr(str string)";
539+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
540+
NUnit.Framework.Assert.AreEqual("", result);
541+
}
542+
543+
[NUnit.Framework.Test]
544+
public virtual void TestExtractingAttrTest08() {
545+
IElementNode iNode = new CssPseudoElementNode(null, "test");
546+
String strToParse = "attr(str string,\"value\")";
547+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
548+
NUnit.Framework.Assert.AreEqual("value", result);
549+
}
550+
551+
[NUnit.Framework.Test]
552+
public virtual void TestExtractingAttrTest09() {
553+
IElementNode iNode = new CssPseudoElementNode(null, "test");
554+
String strToParse = "attr(str string,\"val,ue\")";
555+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
556+
NUnit.Framework.Assert.AreEqual("val,ue", result);
557+
}
558+
559+
[NUnit.Framework.Test]
560+
public virtual void TestExtractingAttrTest10() {
561+
IElementNode iNode = new CssPseudoElementNode(null, "test");
562+
String strToParse = "attr(str string,'val,ue')";
563+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
564+
NUnit.Framework.Assert.AreEqual("val,ue", result);
565+
}
566+
567+
[NUnit.Framework.Test]
568+
public virtual void TestExtractingAttrTest11() {
569+
IElementNode iNode = new CssPseudoElementNode(null, "test");
570+
String strToParse = "attr(name, \"value\", \"value\", \"value\")";
571+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
572+
NUnit.Framework.Assert.IsNull(result);
573+
}
574+
575+
[NUnit.Framework.Test]
576+
public virtual void WrongAttributeTypeTest() {
577+
IElementNode iNode = new CssPseudoElementNode(null, "test");
578+
String strToParse = "attr(str mem)";
579+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
580+
NUnit.Framework.Assert.IsNull(result);
581+
}
582+
583+
[NUnit.Framework.Test]
584+
public virtual void WrongParamsInAttrFunctionTest() {
585+
IElementNode iNode = new CssPseudoElementNode(null, "test");
586+
String strToParse = "attr(str mem lol)";
587+
String result = CssUtils.ExtractAttributeValue(strToParse, iNode);
588+
NUnit.Framework.Assert.IsNull(result);
589+
}
463590
}
464591
}

itext/itext.styledxmlparser/itext/styledxmlparser/css/CommonCssConstants.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ static CommonCssConstants() {
7272
/// <summary>The Constant ALIGN_SELF.</summary>
7373
public const String ALIGN_SELF = "align-self";
7474

75+
/// <summary>The constant ATTRIBUTE.</summary>
76+
public const String ATTRIBUTE = "attr";
77+
7578
/// <summary>The Constant BACKGROUND.</summary>
7679
public const String BACKGROUND = "background";
7780

@@ -883,6 +886,9 @@ static CommonCssConstants() {
883886
/// <summary>The Constant STRETCH.</summary>
884887
public const String STRETCH = "stretch";
885888

889+
/// <summary>The Constant STRING.</summary>
890+
public const String STRING = "string";
891+
886892
/// <summary>The Constant THICK.</summary>
887893
public const String THICK = "thick";
888894

@@ -1088,6 +1094,9 @@ static CommonCssConstants() {
10881094
/// <summary>The Constant TARGET.</summary>
10891095
public const String TARGET = "target";
10901096

1097+
/// <summary>The Constant URL.</summary>
1098+
public const String URL = "url";
1099+
10911100
/// <summary>The Constant VALID.</summary>
10921101
public const String VALID = "valid";
10931102

itext/itext.styledxmlparser/itext/styledxmlparser/css/util/CssUtils.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class CssUtils {
6565
private static readonly ILog logger = LogManager.GetLogger(typeof(iText.StyledXmlParser.Css.Util.CssUtils)
6666
);
6767

68+
private const int QUANTITY_OF_PARAMS_WITH_FALLBACK_OR_TYPE = 2;
69+
6870
/// <summary>
6971
/// Creates a new
7072
/// <see cref="CssUtils"/>
@@ -647,6 +649,45 @@ public static String ExtractUrl(String url) {
647649
return str;
648650
}
649651

652+
/// <summary>Parses string and return attribute value.</summary>
653+
/// <param name="attrStr">the string contains attr() to extract attribute value</param>
654+
/// <param name="element">the parentNode from which we extract information</param>
655+
/// <returns>the value of attribute</returns>
656+
public static String ExtractAttributeValue(String attrStr, IElementNode element) {
657+
String attrValue = null;
658+
if (attrStr.StartsWith(CommonCssConstants.ATTRIBUTE + '(') && attrStr.Length > CommonCssConstants.ATTRIBUTE
659+
.Length + 2 && attrStr.EndsWith(")")) {
660+
String fallback = null;
661+
String typeOfAttribute = null;
662+
String stringToSplit = attrStr.JSubstring(5, attrStr.Length - 1);
663+
IList<String> paramsWithFallback = SplitString(stringToSplit, ',', new EscapeGroup('\"'), new EscapeGroup(
664+
'\''));
665+
if (paramsWithFallback.Count > QUANTITY_OF_PARAMS_WITH_FALLBACK_OR_TYPE) {
666+
return null;
667+
}
668+
if (paramsWithFallback.Count == QUANTITY_OF_PARAMS_WITH_FALLBACK_OR_TYPE) {
669+
fallback = ExtractFallback(paramsWithFallback[1]);
670+
}
671+
IList<String> @params = SplitString(paramsWithFallback[0], ' ');
672+
if (@params.Count > QUANTITY_OF_PARAMS_WITH_FALLBACK_OR_TYPE) {
673+
return null;
674+
}
675+
else {
676+
if (@params.Count == QUANTITY_OF_PARAMS_WITH_FALLBACK_OR_TYPE) {
677+
typeOfAttribute = ExtractTypeOfAttribute(@params[1]);
678+
if (typeOfAttribute == null) {
679+
return null;
680+
}
681+
}
682+
}
683+
String attributeName = @params[0];
684+
if (IsAttributeNameValid(attributeName)) {
685+
attrValue = GetAttributeValue(attributeName, typeOfAttribute, fallback, element);
686+
}
687+
}
688+
return attrValue;
689+
}
690+
650691
/// <summary>Checks if a data is base 64 encoded.</summary>
651692
/// <param name="data">the data</param>
652693
/// <returns>true, if the data is base 64 encoded</returns>
@@ -808,5 +849,44 @@ private static bool AddRange(RangeBuilder builder, String left, String right) {
808849
builder.AddRange(l, r);
809850
return true;
810851
}
852+
853+
private static bool IsAttributeNameValid(String attributeName) {
854+
return !(attributeName.Contains("'") || attributeName.Contains("\"") || attributeName.Contains("(") || attributeName
855+
.Contains(")"));
856+
}
857+
858+
private static String ExtractFallback(String fallbackString) {
859+
String tmpString;
860+
if ((fallbackString.StartsWith("'") && fallbackString.EndsWith("'")) || (fallbackString.StartsWith("\"") &&
861+
fallbackString.EndsWith("\""))) {
862+
tmpString = fallbackString.JSubstring(1, fallbackString.Length - 1);
863+
}
864+
else {
865+
tmpString = fallbackString;
866+
}
867+
return ExtractUrl(tmpString);
868+
}
869+
870+
private static String ExtractTypeOfAttribute(String typeString) {
871+
if (typeString.Equals(CommonCssConstants.URL) || typeString.Equals(CommonCssConstants.STRING)) {
872+
return typeString;
873+
}
874+
return null;
875+
}
876+
877+
private static String GetAttributeValue(String attributeName, String typeOfAttribute, String fallback, IElementNode
878+
elementNode) {
879+
String returnString = elementNode.GetAttribute(attributeName);
880+
if (CommonCssConstants.URL.Equals(typeOfAttribute)) {
881+
returnString = returnString == null ? null : ExtractUrl(returnString);
882+
}
883+
else {
884+
returnString = returnString == null ? "" : returnString;
885+
}
886+
if (fallback != null && (returnString == null || String.IsNullOrEmpty(returnString))) {
887+
returnString = fallback;
888+
}
889+
return returnString;
890+
}
811891
}
812892
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e6d8afaeaac5fd9d2f0e22faddce5bdbb5e65541
1+
ca4c3bbc07a624e2d7f7d95a8b71b7dc164e3f77

0 commit comments

Comments
 (0)