Skip to content

Commit

Permalink
fixing background parse
Browse files Browse the repository at this point in the history
  • Loading branch information
hsllany committed May 17, 2017
1 parent 951c6c6 commit 814f332
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void create() throws Exception {

String s2 = "url(http://n.sinaimg.cn/news/crawl/20170302/18ey-fycaahm6004808.jpg)";
Background r2 = Background.createOrChange("background-image", s2, null);
Assert.assertTrue(r2.getUrl().equals("http://n.sinaimg" +
Assert.assertTrue(r2.getUrl().equals("http://n.sinaimg" + "" +
".cn/news/crawl/20170302/18ey-fycaahm6004808.jpg"));

String s3 = "left center";
Expand All @@ -37,6 +37,9 @@ public void create() throws Exception {
Assert.assertTrue(r5.getY() == 0.5f);
Assert.assertTrue(r5.getColor() == Color.RED);
Assert.assertTrue(r5.getUrl().equals("http://www.baidu.com"));

String s6 = "red url(http://www.baidu.com) left center / auto auto";
Background r6 = Background.createOrChange("background", s6, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
public class ParserTest {
@Test
public void parseStyleSingle() throws Exception {
testBackground("url(http://n.sinaimg.cn/news/crawl/20170302/18ey-fycaahm6004808.jpg) #fff", "http://baidu.com", Color.WHITE);
testBackground("url(http://n.sinaimg.cn/news/crawl/20170302/18ey-fycaahm6004808.jpg) " +
"#fff", "http://baidu.com", Color.WHITE);
testBackground("url(http://baidu.com) white", "http://baidu.com", Color.WHITE);
testBackground("#fff url(http://baidu.com) ", "http://baidu.com", Color.WHITE);

Expand Down Expand Up @@ -69,7 +70,7 @@ private void parserDebugger(String code) throws HNSyntaxError {
try {
HNSegment rootTree = parser.process();
debug("\ntree is :");
debug(rootTree.mDom.wholeTreeToString());
debug(rootTree.getDom().wholeTreeToString());

debug("\nfunction is :");
debug(rootTree.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Color;
import android.graphics.Matrix;
import android.text.TextUtils;
import android.util.Log;

import com.mozz.htmlnative.utils.ParametersUtils;
Expand All @@ -28,7 +29,8 @@ public class Background {
private static final int LK_REPEAT = 2;
private static final int LK_X = 3;
private static final int LK_Y = 4;

private static final int LK_WIDTH = 5;
private static final int LK_HEIGHT = 6;

private String url = "";
private int color = Color.TRANSPARENT;
Expand Down Expand Up @@ -57,15 +59,13 @@ public String toString() {
width + "");
String heightStr = heightMode == AUTO ? "auto" : (heightMode == PERCENTAGE ? height + "%"
: height + "");
String widthColorStr = colorWidthMode == PERCENTAGE ? colorWidth + "%" : colorWidth + "";
String heightColorStr = colorHeightMode == PERCENTAGE ? colorHeight + "%" : colorHeight +
"";
String xStr = xMode == PERCENTAGE ? (x * 100) + "%" : x + "";
String yStr = yMode == PERCENTAGE ? (y * 100) + "%" : y + "";
String urlStr = !TextUtils.isEmpty(url) ? " url(" + url + ")" : "";

return "background:" + color + " url(" + url + ") repeat=" + repeat + " x=" + xStr + " y=" +
yStr + ", width=" + widthStr + ", height=" + heightStr + ", colorWidth=" +
widthColorStr + ", colorHeight" + heightColorStr;
return "background:" + ParametersUtils.toHtmlColorString(color) + urlStr +
" " + xStr + " " +
yStr + " / " + widthStr + " " + heightStr + " " + repeatToString(repeat);
}

public void setColor(int color) {
Expand Down Expand Up @@ -152,20 +152,22 @@ public static Background createOrChange(String param, String val, Object oldOne)
int lk = LK_COLOR;
for (String item : subStrings) {

if (item.equals("/")) {
lk = LK_WIDTH;
continue;
}

if (item.startsWith("url(") && lk <= LK_URL) {
style.setUrl(item.substring(item.indexOf('(') + 1, item.lastIndexOf(')'))
.trim());
lk = LK_URL + 1;

} else if (item.startsWith("#") && lk == LK_COLOR) {
} else if (item.startsWith("#") || ParametersUtils.isHtmlColorString(item)) {
try {
style.setColor(ParametersUtils.toColor(item));
} catch (IllegalArgumentException ignored) {

}

lk = LK_COLOR + 1;

} else if (item.equals("no-repeat") || item.equals("repeat-x") || item.equals
("repeat-y") || item.equals("repeat") && lk <= LK_REPEAT) {
switch (item) {
Expand All @@ -186,7 +188,7 @@ public static Background createOrChange(String param, String val, Object oldOne)
}
lk = LK_REPEAT + 1;

} else if (lk == LK_X) {
} else if (lk >= LK_URL) {
try {
if (item.endsWith("%")) {
style.setX(ParametersUtils.getPercent(item));
Expand Down Expand Up @@ -234,11 +236,31 @@ public static Background createOrChange(String param, String val, Object oldOne)

lk++;

} else if (lk == LK_COLOR) {
try {
style.setColor(ParametersUtils.toColor(item));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} else if (lk == LK_WIDTH) {
String width = item;

if (width.endsWith("%")) {
style.width = ParametersUtils.getPercent(width);
style.widthMode = PERCENTAGE;
} else if (width.equals("auto")) {
style.width = 0;
style.widthMode = AUTO;
} else {
style.width = (float) ParametersUtils.toPixel(width).getPxValue();
style.widthMode = LENGTH;
}

lk++;
} else if (lk == LK_HEIGHT) {
String height = subStrings[1];

if (height.endsWith("%")) {
style.colorHeight = ParametersUtils.getPercent(height);
style.colorHeightMode = PERCENTAGE;
} else {
style.colorHeight = (float) ParametersUtils.toPixel(height)
.getPxValue();
style.colorHeightMode = LENGTH;
}

lk++;
Expand Down Expand Up @@ -427,4 +449,19 @@ public float getColorWidth() {
public float getColorHeight() {
return colorHeight;
}

private static String repeatToString(int repeat) {
switch (repeat) {
case REPEAT:
return "repeat";
case REPEAT_X:
return "repeat-x";
case REPEAT_Y:
return "repeat-y";
case NO_REPEAT:
default:
return "no-repeat";

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import com.mozz.htmlnative.HNSandBoxContext;
import com.mozz.htmlnative.InheritStyleStack;
import com.mozz.htmlnative.css.Styles;
import com.mozz.htmlnative.css.stylehandler.LayoutStyleHandler;
import com.mozz.htmlnative.css.stylehandler.StyleHandler;
import com.mozz.htmlnative.css.stylehandler.StyleHandlerFactory;
import com.mozz.htmlnative.dom.AttachedElement;
import com.mozz.htmlnative.dom.DomElement;
import com.mozz.htmlnative.exception.AttrApplyException;
import com.mozz.htmlnative.parser.CssParser;
import com.mozz.htmlnative.css.stylehandler.LayoutStyleHandler;
import com.mozz.htmlnative.css.stylehandler.StyleHandler;
import com.mozz.htmlnative.css.stylehandler.StyleHandlerFactory;
import com.mozz.htmlnative.utils.MainHandlerUtils;
import com.mozz.htmlnative.view.LayoutParamsLazyCreator;

Expand Down Expand Up @@ -56,10 +56,7 @@ class LView extends LuaTable {
}

/**
* Only used
*
* @param v
* @param context
* Used only by {@link LFindViewById}, which will look up view in existing view tree.
*/
LView(final View v, final HNSandBoxContext context) {
this((DomElement) v.getTag(), null, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.mozz.htmlnative.common.PixelValue;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* @author Yang Tao, 17/2/24.
Expand All @@ -18,6 +20,36 @@ public final class ParametersUtils {

private static final String TAG = ParametersUtils.class.getSimpleName();

private static final Set<String> sColorNameMap;

static {
sColorNameMap = new HashSet<>();
sColorNameMap.add("black");
sColorNameMap.add("darkgray");
sColorNameMap.add("gray");
sColorNameMap.add("lightgray");
sColorNameMap.add("white");
sColorNameMap.add("red");
sColorNameMap.add("green");
sColorNameMap.add("blue");
sColorNameMap.add("yellow");
sColorNameMap.add("cyan");
sColorNameMap.add("magenta");
sColorNameMap.add("aqua");
sColorNameMap.add("fuchsia");
sColorNameMap.add("darkgrey");
sColorNameMap.add("grey");
sColorNameMap.add("lightgrey");
sColorNameMap.add("lime");
sColorNameMap.add("maroon");
sColorNameMap.add("navy");
sColorNameMap.add("olive");
sColorNameMap.add("purple");
sColorNameMap.add("silver");
sColorNameMap.add("teal");

}

private ParametersUtils() {
}

Expand Down Expand Up @@ -180,6 +212,14 @@ public static int toColor(@NonNull Object colorObj) throws IllegalArgumentExcept
}
}

public static String toHtmlColorString(int color) {
return "#" + Integer.toHexString(color & 0x00ffffff);
}

public static boolean isHtmlColorString(String string) {
return sColorNameMap.contains(string);
}


public static Map<String, String> parseStyle(@NonNull String styleString) {
Map<String, String> pas = new HashMap<>();
Expand Down

0 comments on commit 814f332

Please sign in to comment.