Skip to content

Commit

Permalink
Merge pull request #650 from danfickle/649_multiple_bg_images
Browse files Browse the repository at this point in the history
Fixes #649 multiple background images
  • Loading branch information
danfickle authored Feb 17, 2021
2 parents 67913a4 + 661f0e0 commit 02719e8
Show file tree
Hide file tree
Showing 20 changed files with 778 additions and 476 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.openhtmltopdf.css.parser.CSSParser;
import com.openhtmltopdf.css.parser.PropertyValue;
import com.openhtmltopdf.css.parser.property.BackgroundPropertyBuilder;
import com.openhtmltopdf.css.parser.property.PrimitiveBackgroundPropertyBuilders;
import com.openhtmltopdf.css.parser.property.BorderPropertyBuilders;
import com.openhtmltopdf.css.parser.property.BorderSpacingPropertyBuilder;
import com.openhtmltopdf.css.parser.property.ContentPropertyBuilder;
Expand Down Expand Up @@ -146,7 +147,7 @@ public final class CSSName implements Comparable<CSSName> {
PRIMITIVE,
"transparent",
NOT_INHERITED,
new PrimitivePropertyBuilders.BackgroundColor()
new PrimitiveBackgroundPropertyBuilders.BackgroundColor()
);

/**
Expand All @@ -158,7 +159,7 @@ public final class CSSName implements Comparable<CSSName> {
PRIMITIVE,
"none",
NOT_INHERITED,
new PrimitivePropertyBuilders.BackgroundImage()
new PrimitiveBackgroundPropertyBuilders.BackgroundImage()
);

/**
Expand All @@ -170,7 +171,7 @@ public final class CSSName implements Comparable<CSSName> {
PRIMITIVE,
"repeat",
NOT_INHERITED,
new PrimitivePropertyBuilders.BackgroundRepeat()
new PrimitiveBackgroundPropertyBuilders.BackgroundRepeat()
);

/**
Expand All @@ -182,7 +183,7 @@ public final class CSSName implements Comparable<CSSName> {
PRIMITIVE,
"scroll",
NOT_INHERITED,
new PrimitivePropertyBuilders.BackgroundAttachment()
new PrimitiveBackgroundPropertyBuilders.BackgroundAttachment()
);

/**
Expand All @@ -194,7 +195,7 @@ public final class CSSName implements Comparable<CSSName> {
PRIMITIVE,
"0% 0%",
NOT_INHERITED,
new PrimitivePropertyBuilders.BackgroundPosition()
new PrimitiveBackgroundPropertyBuilders.BackgroundPosition()
);

public final static CSSName BACKGROUND_SIZE =
Expand All @@ -203,7 +204,7 @@ public final class CSSName implements Comparable<CSSName> {
PRIMITIVE,
"auto auto",
NOT_INHERITED,
new PrimitivePropertyBuilders.BackgroundSize()
new PrimitiveBackgroundPropertyBuilders.BackgroundSize()
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ protected void checkInheritAllowed(CSSPrimitiveValue value, boolean inheritAllow
}
}

protected void checkForbidInherit(CSSPrimitiveValue value) {
checkInheritAllowed(value, false);
}

protected List<PropertyDeclaration> checkInheritAll(CSSName[] all, List<PropertyValue> values, int origin, boolean important, boolean inheritAllowed) {
if (values.size() == 1) {
CSSPrimitiveValue value = values.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.openhtmltopdf.css.parser.property;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.openhtmltopdf.css.constants.CSSName;
Expand All @@ -29,7 +31,10 @@
import com.openhtmltopdf.css.parser.FSRGBColor;
import com.openhtmltopdf.css.parser.PropertyValue;
import com.openhtmltopdf.css.sheet.PropertyDeclaration;
import com.openhtmltopdf.util.WebDoc;
import com.openhtmltopdf.util.WebDocLocations;

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

backgroundRepeat = new PropertyDeclaration(
CSSName.BACKGROUND_REPEAT, value, important, origin);
CSSName.BACKGROUND_REPEAT, new PropertyValue(Collections.singletonList(value)), important, origin);
}

if (PrimitivePropertyBuilders.BACKGROUND_ATTACHMENTS.get(ident.FS_ID)) {
if (backgroundAttachment != null) {
throw new CSSParseException("A background-attachment value cannot be set twice", -1);
}

backgroundAttachment = new PropertyDeclaration(
CSSName.BACKGROUND_ATTACHMENT, value, important, origin);
CSSName.BACKGROUND_ATTACHMENT, new PropertyValue(Collections.singletonList(value)), important, origin);
}

if (ident == IdentValue.TRANSPARENT) {
if (backgroundColor != null) {
throw new CSSParseException("A background-color value cannot be set twice", -1);
Expand All @@ -118,11 +123,13 @@ public List<PropertyDeclaration> buildDeclarations(
if (backgroundImage != null) {
throw new CSSParseException("A background-image value cannot be set twice", -1);
}


List<PropertyValue> bgImages = Collections.singletonList(value);

backgroundImage = new PropertyDeclaration(
CSSName.BACKGROUND_IMAGE, value, important, origin);
CSSName.BACKGROUND_IMAGE, new PropertyValue(bgImages), important, origin);
}

if (PrimitivePropertyBuilders.BACKGROUND_POSITIONS.get(ident.FS_ID)) {
processingBackgroundPosition = true;
}
Expand All @@ -137,9 +144,11 @@ public List<PropertyDeclaration> buildDeclarations(
if (backgroundImage != null) {
throw new CSSParseException("A background-image value cannot be set twice", -1);
}


List<PropertyValue> bgImages = Collections.singletonList(value);

backgroundImage = new PropertyDeclaration(
CSSName.BACKGROUND_IMAGE, value, important, origin);
CSSName.BACKGROUND_IMAGE, new PropertyValue(bgImages), important, origin);
}

if (processingBackgroundPosition || isLength(value) || type == CSSPrimitiveValue.CSS_PERCENTAGE) {
Expand Down Expand Up @@ -167,38 +176,34 @@ public List<PropertyDeclaration> buildDeclarations(
backgroundColor = new PropertyDeclaration(
CSSName.BACKGROUND_COLOR, new PropertyValue(IdentValue.TRANSPARENT), important, origin);
}

if (backgroundImage == null) {
List<PropertyValue> bgImages = Collections.singletonList(new PropertyValue(IdentValue.NONE));

backgroundImage = new PropertyDeclaration(
CSSName.BACKGROUND_IMAGE, new PropertyValue(IdentValue.NONE), important, origin);
CSSName.BACKGROUND_IMAGE, new PropertyValue(bgImages), important, origin);
}

if (backgroundRepeat == null) {
backgroundRepeat = new PropertyDeclaration(
CSSName.BACKGROUND_REPEAT, new PropertyValue(IdentValue.REPEAT), important, origin);
CSSName.BACKGROUND_REPEAT, new PropertyValue(Collections.singletonList(new PropertyValue(IdentValue.REPEAT))), important, origin);
}

if (backgroundAttachment == null) {
backgroundAttachment = new PropertyDeclaration(
CSSName.BACKGROUND_ATTACHMENT, new PropertyValue(IdentValue.SCROLL), important, origin);

CSSName.BACKGROUND_ATTACHMENT, new PropertyValue(Collections.singletonList(new PropertyValue(IdentValue.SCROLL))), important, origin);
}

if (backgroundPosition == null) {
List<PropertyValue> v = new ArrayList<>(2);
v.add(new PropertyValue(CSSPrimitiveValue.CSS_PERCENTAGE, 0.0f, "0%"));
v.add(new PropertyValue(CSSPrimitiveValue.CSS_PERCENTAGE, 0.0f, "0%"));
backgroundPosition = new PropertyDeclaration(
CSSName.BACKGROUND_POSITION, new PropertyValue(v), important, origin);
}

result = new ArrayList<>(5);
result.add(backgroundColor);
result.add(backgroundImage);
result.add(backgroundRepeat);
result.add(backgroundAttachment);
result.add(backgroundPosition);

return result;

return Arrays.asList(
backgroundColor, backgroundImage, backgroundRepeat,
backgroundAttachment, backgroundPosition);
}
}
Loading

0 comments on commit 02719e8

Please sign in to comment.