Skip to content

Commit 02719e8

Browse files
authored
Merge pull request #650 from danfickle/649_multiple_bg_images
Fixes #649 multiple background images
2 parents 67913a4 + 661f0e0 commit 02719e8

20 files changed

+778
-476
lines changed

openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/constants/CSSName.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.openhtmltopdf.css.parser.CSSParser;
3030
import com.openhtmltopdf.css.parser.PropertyValue;
3131
import com.openhtmltopdf.css.parser.property.BackgroundPropertyBuilder;
32+
import com.openhtmltopdf.css.parser.property.PrimitiveBackgroundPropertyBuilders;
3233
import com.openhtmltopdf.css.parser.property.BorderPropertyBuilders;
3334
import com.openhtmltopdf.css.parser.property.BorderSpacingPropertyBuilder;
3435
import com.openhtmltopdf.css.parser.property.ContentPropertyBuilder;
@@ -146,7 +147,7 @@ public final class CSSName implements Comparable<CSSName> {
146147
PRIMITIVE,
147148
"transparent",
148149
NOT_INHERITED,
149-
new PrimitivePropertyBuilders.BackgroundColor()
150+
new PrimitiveBackgroundPropertyBuilders.BackgroundColor()
150151
);
151152

152153
/**
@@ -158,7 +159,7 @@ public final class CSSName implements Comparable<CSSName> {
158159
PRIMITIVE,
159160
"none",
160161
NOT_INHERITED,
161-
new PrimitivePropertyBuilders.BackgroundImage()
162+
new PrimitiveBackgroundPropertyBuilders.BackgroundImage()
162163
);
163164

164165
/**
@@ -170,7 +171,7 @@ public final class CSSName implements Comparable<CSSName> {
170171
PRIMITIVE,
171172
"repeat",
172173
NOT_INHERITED,
173-
new PrimitivePropertyBuilders.BackgroundRepeat()
174+
new PrimitiveBackgroundPropertyBuilders.BackgroundRepeat()
174175
);
175176

176177
/**
@@ -182,7 +183,7 @@ public final class CSSName implements Comparable<CSSName> {
182183
PRIMITIVE,
183184
"scroll",
184185
NOT_INHERITED,
185-
new PrimitivePropertyBuilders.BackgroundAttachment()
186+
new PrimitiveBackgroundPropertyBuilders.BackgroundAttachment()
186187
);
187188

188189
/**
@@ -194,7 +195,7 @@ public final class CSSName implements Comparable<CSSName> {
194195
PRIMITIVE,
195196
"0% 0%",
196197
NOT_INHERITED,
197-
new PrimitivePropertyBuilders.BackgroundPosition()
198+
new PrimitiveBackgroundPropertyBuilders.BackgroundPosition()
198199
);
199200

200201
public final static CSSName BACKGROUND_SIZE =
@@ -203,7 +204,7 @@ public final class CSSName implements Comparable<CSSName> {
203204
PRIMITIVE,
204205
"auto auto",
205206
NOT_INHERITED,
206-
new PrimitivePropertyBuilders.BackgroundSize()
207+
new PrimitiveBackgroundPropertyBuilders.BackgroundSize()
207208
);
208209

209210
/**

openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/AbstractPropertyBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ protected void checkInheritAllowed(CSSPrimitiveValue value, boolean inheritAllow
196196
}
197197
}
198198

199+
protected void checkForbidInherit(CSSPrimitiveValue value) {
200+
checkInheritAllowed(value, false);
201+
}
202+
199203
protected List<PropertyDeclaration> checkInheritAll(CSSName[] all, List<PropertyValue> values, int origin, boolean important, boolean inheritAllowed) {
200204
if (values.size() == 1) {
201205
CSSPrimitiveValue value = values.get(0);

openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/BackgroundPropertyBuilder.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.openhtmltopdf.css.parser.property;
2121

2222
import java.util.ArrayList;
23+
import java.util.Arrays;
24+
import java.util.Collections;
2325
import java.util.List;
2426

2527
import com.openhtmltopdf.css.constants.CSSName;
@@ -29,7 +31,10 @@
2931
import com.openhtmltopdf.css.parser.FSRGBColor;
3032
import com.openhtmltopdf.css.parser.PropertyValue;
3133
import com.openhtmltopdf.css.sheet.PropertyDeclaration;
34+
import com.openhtmltopdf.util.WebDoc;
35+
import com.openhtmltopdf.util.WebDocLocations;
3236

37+
@WebDoc(WebDocLocations.CSS_BACKGROUND_PROPERTIES)
3338
public class BackgroundPropertyBuilder extends AbstractPropertyBuilder {
3439
// [<'background-color'> || <'background-image'> || <'background-repeat'> ||
3540
// <'background-attachment'> || <'background-position'>] | inherit
@@ -93,18 +98,18 @@ public List<PropertyDeclaration> buildDeclarations(
9398
}
9499

95100
backgroundRepeat = new PropertyDeclaration(
96-
CSSName.BACKGROUND_REPEAT, value, important, origin);
101+
CSSName.BACKGROUND_REPEAT, new PropertyValue(Collections.singletonList(value)), important, origin);
97102
}
98103

99104
if (PrimitivePropertyBuilders.BACKGROUND_ATTACHMENTS.get(ident.FS_ID)) {
100105
if (backgroundAttachment != null) {
101106
throw new CSSParseException("A background-attachment value cannot be set twice", -1);
102107
}
103-
108+
104109
backgroundAttachment = new PropertyDeclaration(
105-
CSSName.BACKGROUND_ATTACHMENT, value, important, origin);
110+
CSSName.BACKGROUND_ATTACHMENT, new PropertyValue(Collections.singletonList(value)), important, origin);
106111
}
107-
112+
108113
if (ident == IdentValue.TRANSPARENT) {
109114
if (backgroundColor != null) {
110115
throw new CSSParseException("A background-color value cannot be set twice", -1);
@@ -118,11 +123,13 @@ public List<PropertyDeclaration> buildDeclarations(
118123
if (backgroundImage != null) {
119124
throw new CSSParseException("A background-image value cannot be set twice", -1);
120125
}
121-
126+
127+
List<PropertyValue> bgImages = Collections.singletonList(value);
128+
122129
backgroundImage = new PropertyDeclaration(
123-
CSSName.BACKGROUND_IMAGE, value, important, origin);
130+
CSSName.BACKGROUND_IMAGE, new PropertyValue(bgImages), important, origin);
124131
}
125-
132+
126133
if (PrimitivePropertyBuilders.BACKGROUND_POSITIONS.get(ident.FS_ID)) {
127134
processingBackgroundPosition = true;
128135
}
@@ -137,9 +144,11 @@ public List<PropertyDeclaration> buildDeclarations(
137144
if (backgroundImage != null) {
138145
throw new CSSParseException("A background-image value cannot be set twice", -1);
139146
}
140-
147+
148+
List<PropertyValue> bgImages = Collections.singletonList(value);
149+
141150
backgroundImage = new PropertyDeclaration(
142-
CSSName.BACKGROUND_IMAGE, value, important, origin);
151+
CSSName.BACKGROUND_IMAGE, new PropertyValue(bgImages), important, origin);
143152
}
144153

145154
if (processingBackgroundPosition || isLength(value) || type == CSSPrimitiveValue.CSS_PERCENTAGE) {
@@ -167,38 +176,34 @@ public List<PropertyDeclaration> buildDeclarations(
167176
backgroundColor = new PropertyDeclaration(
168177
CSSName.BACKGROUND_COLOR, new PropertyValue(IdentValue.TRANSPARENT), important, origin);
169178
}
170-
179+
171180
if (backgroundImage == null) {
181+
List<PropertyValue> bgImages = Collections.singletonList(new PropertyValue(IdentValue.NONE));
182+
172183
backgroundImage = new PropertyDeclaration(
173-
CSSName.BACKGROUND_IMAGE, new PropertyValue(IdentValue.NONE), important, origin);
184+
CSSName.BACKGROUND_IMAGE, new PropertyValue(bgImages), important, origin);
174185
}
175-
186+
176187
if (backgroundRepeat == null) {
177188
backgroundRepeat = new PropertyDeclaration(
178-
CSSName.BACKGROUND_REPEAT, new PropertyValue(IdentValue.REPEAT), important, origin);
189+
CSSName.BACKGROUND_REPEAT, new PropertyValue(Collections.singletonList(new PropertyValue(IdentValue.REPEAT))), important, origin);
179190
}
180-
191+
181192
if (backgroundAttachment == null) {
182193
backgroundAttachment = new PropertyDeclaration(
183-
CSSName.BACKGROUND_ATTACHMENT, new PropertyValue(IdentValue.SCROLL), important, origin);
184-
194+
CSSName.BACKGROUND_ATTACHMENT, new PropertyValue(Collections.singletonList(new PropertyValue(IdentValue.SCROLL))), important, origin);
185195
}
186-
196+
187197
if (backgroundPosition == null) {
188198
List<PropertyValue> v = new ArrayList<>(2);
189199
v.add(new PropertyValue(CSSPrimitiveValue.CSS_PERCENTAGE, 0.0f, "0%"));
190200
v.add(new PropertyValue(CSSPrimitiveValue.CSS_PERCENTAGE, 0.0f, "0%"));
191201
backgroundPosition = new PropertyDeclaration(
192202
CSSName.BACKGROUND_POSITION, new PropertyValue(v), important, origin);
193203
}
194-
195-
result = new ArrayList<>(5);
196-
result.add(backgroundColor);
197-
result.add(backgroundImage);
198-
result.add(backgroundRepeat);
199-
result.add(backgroundAttachment);
200-
result.add(backgroundPosition);
201-
202-
return result;
204+
205+
return Arrays.asList(
206+
backgroundColor, backgroundImage, backgroundRepeat,
207+
backgroundAttachment, backgroundPosition);
203208
}
204209
}

0 commit comments

Comments
 (0)