|
|
@@ -11,6 +11,7 @@
|
|
|
*/
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
+#include <stdio.h>
|
|
|
#include <string.h>
|
|
|
#include <math.h>
|
|
|
#include <ucmd_heap.h>
|
|
|
@@ -233,6 +234,7 @@ static unsigned int s_len(const char *s);
|
|
|
static int s_eq(const char *a, const char *b);
|
|
|
static void form_key(int k);
|
|
|
static void form_click(unsigned int node);
|
|
|
+static void style_for_node(unsigned int node, struct br_style *out);
|
|
|
|
|
|
static int form_disabled(unsigned int node)
|
|
|
{
|
|
|
@@ -774,6 +776,297 @@ static int js_host_form_reset(unsigned int form)
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+static void css_computed_name(const char *p, char *out, int cap)
|
|
|
+{
|
|
|
+ int n = 0;
|
|
|
+
|
|
|
+ for (; p && *p && n + 2 < cap; p++) {
|
|
|
+ char c = *p;
|
|
|
+
|
|
|
+ if (c >= 'A' && c <= 'Z') {
|
|
|
+ out[n++] = '-';
|
|
|
+ c = (char)(c - 'A' + 'a');
|
|
|
+ } else if (c == '_') {
|
|
|
+ c = '-';
|
|
|
+ } else if (c >= 'a' && c <= 'z') {
|
|
|
+ /* keep */
|
|
|
+ }
|
|
|
+ out[n++] = c;
|
|
|
+ }
|
|
|
+ out[n] = '\0';
|
|
|
+ if (!strcmp(out, "css-float"))
|
|
|
+ strcpy(out, "float");
|
|
|
+}
|
|
|
+
|
|
|
+static void css_rgb(unsigned short c, char *buf, int cap)
|
|
|
+{
|
|
|
+ int r = ((c >> 11) & 31) * 255 / 31;
|
|
|
+ int g = ((c >> 5) & 63) * 255 / 63;
|
|
|
+ int b = (c & 31) * 255 / 31;
|
|
|
+
|
|
|
+ snprintf(buf, cap, "rgb(%d, %d, %d)", r, g, b);
|
|
|
+}
|
|
|
+
|
|
|
+static void css_mp_value(char *buf, int cap, int raw, int unit, int px)
|
|
|
+{
|
|
|
+ if (unit == 4) {
|
|
|
+ snprintf(buf, cap, "auto");
|
|
|
+ } else if (unit == 0) {
|
|
|
+ snprintf(buf, cap, "%dpx", raw);
|
|
|
+ } else if (unit == 1) {
|
|
|
+ snprintf(buf, cap, "%d.%02d%%", raw / 100, raw % 100);
|
|
|
+ } else if (unit == 2) {
|
|
|
+ snprintf(buf, cap, "%d.%02dem", raw / 100, raw % 100);
|
|
|
+ } else if (unit == 3) {
|
|
|
+ snprintf(buf, cap, "%d.%02drem", raw / 100, raw % 100);
|
|
|
+ } else {
|
|
|
+ snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
|
|
|
+ raw / 100, raw % 100, px);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int js_host_computed(unsigned int node, const char *property,
|
|
|
+ char *buf, int cap)
|
|
|
+{
|
|
|
+ struct br_style st;
|
|
|
+ char name[40];
|
|
|
+
|
|
|
+ if (!buf || cap <= 0 || node >= doc.nnode || node == 0)
|
|
|
+ return 0;
|
|
|
+ buf[0] = '\0';
|
|
|
+ css_computed_name(property, name, sizeof(name));
|
|
|
+ style_for_node(node, &st);
|
|
|
+
|
|
|
+ if (!strcmp(name, "display")) {
|
|
|
+ const char *v = "inline";
|
|
|
+
|
|
|
+ if (st.display == BR_DISPLAY_BLOCK)
|
|
|
+ v = "block";
|
|
|
+ else if (st.display == BR_DISPLAY_NONE)
|
|
|
+ v = "none";
|
|
|
+ else if (st.display == BR_DISPLAY_FLEX)
|
|
|
+ v = "flex";
|
|
|
+ else if (st.display == BR_DISPLAY_GRID)
|
|
|
+ v = "grid";
|
|
|
+ else if (st.display == BR_DISPLAY_INLINE_BLOCK)
|
|
|
+ v = "inline-block";
|
|
|
+ snprintf(buf, cap, "%s", v);
|
|
|
+ } else if (!strcmp(name, "position")) {
|
|
|
+ static const char *pos[] = {
|
|
|
+ "static", "absolute", "fixed", "relative", "sticky"
|
|
|
+ };
|
|
|
+ snprintf(buf, cap, "%s", pos[st.pos < 5 ? st.pos : 0]);
|
|
|
+ } else if (!strcmp(name, "width") || !strcmp(name, "height")) {
|
|
|
+ int is_w = name[0] == 'w';
|
|
|
+ int kind = is_w ? st.width_kind : st.height_kind;
|
|
|
+ int val = is_w ? st.width : st.height;
|
|
|
+
|
|
|
+ if (kind == 3) {
|
|
|
+ if (is_w)
|
|
|
+ snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
|
|
|
+ st.width_calc_pct / 100,
|
|
|
+ st.width_calc_pct % 100,
|
|
|
+ st.width_calc_px);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "calc(%d.%02d%% + %dpx)",
|
|
|
+ st.height_calc_pct / 100,
|
|
|
+ st.height_calc_pct % 100,
|
|
|
+ st.height_calc_px);
|
|
|
+ } else if (kind == 1) {
|
|
|
+ snprintf(buf, cap, "%dpx", val);
|
|
|
+ } else if (kind == 2) {
|
|
|
+ snprintf(buf, cap, "%d.%02d%%", val / 100, val % 100);
|
|
|
+ } else {
|
|
|
+ snprintf(buf, cap, "auto");
|
|
|
+ }
|
|
|
+ } else if (!strcmp(name, "color") ||
|
|
|
+ !strcmp(name, "background-color")) {
|
|
|
+ if (!strcmp(name, "background-color") && !st.has_bg)
|
|
|
+ snprintf(buf, cap, "rgba(0, 0, 0, 0)");
|
|
|
+ else
|
|
|
+ css_rgb(name[0] == 'c' ? st.color : st.bg, buf, cap);
|
|
|
+ } else if (!strcmp(name, "font-size")) {
|
|
|
+ snprintf(buf, cap, "%dpx", st.font_px);
|
|
|
+ } else if (!strcmp(name, "font-weight")) {
|
|
|
+ snprintf(buf, cap, "%s", st.bold ? "700" : "400");
|
|
|
+ } else if (!strcmp(name, "font-style")) {
|
|
|
+ snprintf(buf, cap, "%s", st.italic ? "italic" : "normal");
|
|
|
+ } else if (!strcmp(name, "font-family")) {
|
|
|
+ if (st.font_face > 0 && st.font_face <= BR_MAX_FACES &&
|
|
|
+ br_font_face_family(st.font_face)[0])
|
|
|
+ snprintf(buf, cap, "%s",
|
|
|
+ br_font_face_family(st.font_face));
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "%s",
|
|
|
+ st.mono ? "monospace" : "sans-serif");
|
|
|
+ } else if (!strcmp(name, "text-align")) {
|
|
|
+ static const char *al[] = { "left", "center", "right", "justify" };
|
|
|
+ snprintf(buf, cap, "%s", al[st.align < 4 ? st.align : 0]);
|
|
|
+ } else if (!strcmp(name, "float")) {
|
|
|
+ snprintf(buf, cap, "%s", st.float_left == 1 ? "left" :
|
|
|
+ st.float_left == 2 ? "right" : "none");
|
|
|
+ } else if (!strcmp(name, "clear")) {
|
|
|
+ static const char *cl[] = { "none", "left", "right", "both" };
|
|
|
+ snprintf(buf, cap, "%s", cl[st.clear < 4 ? st.clear : 0]);
|
|
|
+ } else if (!strcmp(name, "opacity")) {
|
|
|
+ snprintf(buf, cap, "%s", st.opacity_hidden ? "0" : "1");
|
|
|
+ } else if (!strcmp(name, "visibility")) {
|
|
|
+ snprintf(buf, cap, "%s", st.vis_hidden ? "hidden" : "visible");
|
|
|
+ } else if (!strcmp(name, "overflow")) {
|
|
|
+ snprintf(buf, cap, "%s",
|
|
|
+ st.overflow_hidden ? "hidden" : "visible");
|
|
|
+ } else if (!strcmp(name, "white-space")) {
|
|
|
+ snprintf(buf, cap, "%s", st.nowrap ? "nowrap" : "normal");
|
|
|
+ } else if (!strcmp(name, "text-transform")) {
|
|
|
+ snprintf(buf, cap, "%s", st.uppercase ? "uppercase" : "none");
|
|
|
+ } else if (!strcmp(name, "box-sizing")) {
|
|
|
+ snprintf(buf, cap, "%s",
|
|
|
+ st.box_sizing ? "border-box" : "content-box");
|
|
|
+ } else if (!strcmp(name, "line-height")) {
|
|
|
+ if (st.line_h_px)
|
|
|
+ snprintf(buf, cap, "%dpx", st.line_h_px);
|
|
|
+ else if (st.line_h_pct)
|
|
|
+ snprintf(buf, cap, "%d%%", st.line_h_pct);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "normal");
|
|
|
+ } else if (!strcmp(name, "z-index")) {
|
|
|
+ if (st.z_set)
|
|
|
+ snprintf(buf, cap, "%d", st.z_index);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "auto");
|
|
|
+ } else if (!strcmp(name, "top") || !strcmp(name, "left") ||
|
|
|
+ !strcmp(name, "right") || !strcmp(name, "bottom")) {
|
|
|
+ int set = name[0] == 't' ? st.top_set :
|
|
|
+ name[0] == 'l' ? st.left_set :
|
|
|
+ name[0] == 'r' ? st.right_set : st.bottom_set;
|
|
|
+ int v = name[0] == 't' ? st.top :
|
|
|
+ name[0] == 'l' ? st.left :
|
|
|
+ name[0] == 'r' ? st.right : st.bottom;
|
|
|
+
|
|
|
+ if (set)
|
|
|
+ snprintf(buf, cap, "%dpx", v);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "auto");
|
|
|
+ } else if (!strcmp(name, "margin-top") || !strcmp(name, "margin-right") ||
|
|
|
+ !strcmp(name, "margin-bottom") ||
|
|
|
+ !strcmp(name, "margin-left") ||
|
|
|
+ !strcmp(name, "padding-top") ||
|
|
|
+ !strcmp(name, "padding-right") ||
|
|
|
+ !strcmp(name, "padding-bottom") ||
|
|
|
+ !strcmp(name, "padding-left")) {
|
|
|
+ int is_pad = name[0] == 'p';
|
|
|
+ int side = name[strlen(name) - 1] == 'p' ? 0 :
|
|
|
+ name[strlen(name) - 1] == 't' ? 1 :
|
|
|
+ name[strlen(name) - 1] == 'm' ? 2 : 3;
|
|
|
+
|
|
|
+ /* last letter: top p, right t, bottom m, left t (ambiguity). */
|
|
|
+ if (!strcmp(name, "margin-right") ||
|
|
|
+ !strcmp(name, "padding-right"))
|
|
|
+ side = 1;
|
|
|
+ else if (!strcmp(name, "margin-bottom") ||
|
|
|
+ !strcmp(name, "padding-bottom"))
|
|
|
+ side = 2;
|
|
|
+ else if (!strcmp(name, "margin-left") ||
|
|
|
+ !strcmp(name, "padding-left"))
|
|
|
+ side = 3;
|
|
|
+ else
|
|
|
+ side = 0;
|
|
|
+ side += is_pad ? 4 : 0;
|
|
|
+ css_mp_value(buf, cap,
|
|
|
+ side == 0 ? st.mt : side == 1 ? st.mr :
|
|
|
+ side == 2 ? st.mb : side == 3 ? st.ml :
|
|
|
+ side == 4 ? st.pt : side == 5 ? st.pr :
|
|
|
+ side == 6 ? st.pb : st.pl,
|
|
|
+ st.mp_unit[side], st.mp_calc_px[side]);
|
|
|
+ } else if (!strcmp(name, "border-top-width") ||
|
|
|
+ !strcmp(name, "border-right-width") ||
|
|
|
+ !strcmp(name, "border-bottom-width") ||
|
|
|
+ !strcmp(name, "border-left-width")) {
|
|
|
+ int side = name[7] == 't' ? 0 : name[7] == 'r' ? 1 :
|
|
|
+ name[7] == 'b' ? 2 : 3;
|
|
|
+ snprintf(buf, cap, "%dpx", st.border_w[side]);
|
|
|
+ } else if (!strcmp(name, "border-top-color") ||
|
|
|
+ !strcmp(name, "border-right-color") ||
|
|
|
+ !strcmp(name, "border-bottom-color") ||
|
|
|
+ !strcmp(name, "border-left-color")) {
|
|
|
+ int side = name[7] == 't' ? 0 : name[7] == 'r' ? 1 :
|
|
|
+ name[7] == 'b' ? 2 : 3;
|
|
|
+ if (st.border_w[side])
|
|
|
+ css_rgb(st.border_c[side], buf, cap);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "rgba(0, 0, 0, 0)");
|
|
|
+ } else if (!strcmp(name, "background-image")) {
|
|
|
+ if (st.bg_image)
|
|
|
+ snprintf(buf, cap, "url(\"%s\")",
|
|
|
+ br_bg_image_url(st.bg_image - 1));
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "none");
|
|
|
+ } else if (!strcmp(name, "background-repeat")) {
|
|
|
+ static const char *rp[] = {
|
|
|
+ "repeat", "no-repeat", "repeat-x", "repeat-y"
|
|
|
+ };
|
|
|
+ snprintf(buf, cap, "%s", rp[st.bg_repeat < 4 ?
|
|
|
+ st.bg_repeat : 0]);
|
|
|
+ } else if (!strcmp(name, "background-position")) {
|
|
|
+ if (st.bg_pos_x_kind == 1)
|
|
|
+ snprintf(buf, cap, "%d%% %d%%", st.bg_pos_x,
|
|
|
+ st.bg_pos_y);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "%dpx %dpx", st.bg_pos_x,
|
|
|
+ st.bg_pos_y);
|
|
|
+ } else if (!strcmp(name, "background-size")) {
|
|
|
+ if (st.bg_size_kind == 3)
|
|
|
+ snprintf(buf, cap, "cover");
|
|
|
+ else if (st.bg_size_kind == 4)
|
|
|
+ snprintf(buf, cap, "contain");
|
|
|
+ else if (st.bg_size_kind == 1)
|
|
|
+ snprintf(buf, cap, "%dpx %dpx", st.bg_size_w,
|
|
|
+ st.bg_size_h);
|
|
|
+ else if (st.bg_size_kind == 2)
|
|
|
+ snprintf(buf, cap, "%d%% %d%%", st.bg_size_w,
|
|
|
+ st.bg_size_h);
|
|
|
+ else
|
|
|
+ snprintf(buf, cap, "auto");
|
|
|
+ } else if (!strcmp(name, "transform")) {
|
|
|
+ char tmp[128];
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ if (!st.transform_set) {
|
|
|
+ snprintf(buf, cap, "none");
|
|
|
+ } else {
|
|
|
+ tmp[0] = '\0';
|
|
|
+ if (st.tx || st.ty) {
|
|
|
+ snprintf(tmp + n, sizeof(tmp) - n,
|
|
|
+ "translate(%dpx, %dpx)", st.tx, st.ty);
|
|
|
+ n = (unsigned int)strlen(tmp);
|
|
|
+ }
|
|
|
+ if (st.transform_scale) {
|
|
|
+ snprintf(tmp + n, sizeof(tmp) - n,
|
|
|
+ "%sscale(%d.%02d, %d.%02d)",
|
|
|
+ n ? " " : "", st.scale_x / 100,
|
|
|
+ st.scale_x % 100, st.scale_y / 100,
|
|
|
+ st.scale_y % 100);
|
|
|
+ n = (unsigned int)strlen(tmp);
|
|
|
+ if (st.rotate_x100) {
|
|
|
+ snprintf(tmp + n, sizeof(tmp) - n,
|
|
|
+ " rotate(%d.%02ddeg)",
|
|
|
+ st.rotate_x100 / 100,
|
|
|
+ st.rotate_x100 % 100);
|
|
|
+ }
|
|
|
+ } else if (st.rotate_x100) {
|
|
|
+ snprintf(tmp + n, sizeof(tmp) - n,
|
|
|
+ "%srotate(%d.%02ddeg)",
|
|
|
+ n ? " " : "", st.rotate_x100 / 100,
|
|
|
+ st.rotate_x100 % 100);
|
|
|
+ }
|
|
|
+ snprintf(buf, cap, "%s", tmp[0] ? tmp : "none");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return buf[0] != '\0';
|
|
|
+}
|
|
|
+
|
|
|
static int js_host_click(unsigned int node)
|
|
|
{
|
|
|
char url[BR_URL_MAX];
|
|
|
@@ -840,6 +1133,7 @@ static void run_page_scripts(void)
|
|
|
host.form_submit = js_host_form_submit;
|
|
|
host.form_reset = js_host_form_reset;
|
|
|
host.click = js_host_click;
|
|
|
+ host.computed = js_host_computed;
|
|
|
host.page_url = page_url;
|
|
|
host.width = ui.w;
|
|
|
host.height = ui.h;
|