UNPKG

@meui-js/core

Version:
2,015 lines (1,816 loc) 73.1 kB
import { Box, BOX_STATE, Path2D as Path2D$1, CanvasGradient as CanvasGradient$1, Canvas, NativeMEUI } from 'meui-native'; import * as os from 'os'; import * as std from 'std'; class Node { constructor() { this.childNodes = []; this.parentNode = null; this._previousSibling = null; this._nextSibling = null; } get firstChild() { return this.childNodes[0]; } get lastChild() { return this.childNodes[this.childNodes.length - 1]; } get nextSibling() { return this._nextSibling; } get previousSibling() { return this._previousSibling; } appendChild(child) { this.insertBefore(child, null); } insertBefore(child, ref) { child.remove(); let index = this.childNodes.length; if (ref) { const i = this.childNodes.indexOf(ref); index = i == -1 ? index : i; } this.childNodes.splice(index, 0, child); child._previousSibling = this.childNodes[index - 1]; child._nextSibling = this.childNodes[index + 1]; child.parentNode = this; return child; } removeChild(child) { const index = this.childNodes.indexOf(child); if (index !== -1) { this.childNodes.splice(index, 1); child._previousSibling = null; child._nextSibling = null; child.parentNode = null; } } remove() { this.parentNode?.removeChild(this); } } var NodeType = /* @__PURE__ */ ((NodeType2) => { NodeType2[NodeType2["ELEMENT_NODE"] = 1] = "ELEMENT_NODE"; NodeType2[NodeType2["TEXT_NODE"] = 3] = "TEXT_NODE"; return NodeType2; })(NodeType || {}); class Text extends Node { constructor(text) { super(); this.nodeType = NodeType.TEXT_NODE; this.text = text; } set data(value) { this.text = value; this.parentNode?.updateText(); } } globalThis.document = { createElement(tagName) { return createElement(tagName); }, createTextNode(data) { return new Text(data); } }; const KeyMap = { space: " ", Tab: " ", Return: "\n", exclam: "!", numbersign: "#", percent: "%", dollar: "$", ampersand: "&", quotedbl: '"', apostrophe: "'", parenleft: "(", parenright: ")", asterisk: "*", equal: "=", plus: "+", comma: ",", minus: "-", period: ".", slash: "/", colon: ":", semicolon: ";", less: "<", greater: ">", question: "?", at: "@", bracketleft: "[", bracketright: "]", backslash: "\\", asciicircum: "^", underscore: "_", grave: "`", braceleft: "{", bar: "|", braceright: "}", asciitilde: "~" }; var colorString = {exports: {}}; var colorName = { "aliceblue": [240, 248, 255], "antiquewhite": [250, 235, 215], "aqua": [0, 255, 255], "aquamarine": [127, 255, 212], "azure": [240, 255, 255], "beige": [245, 245, 220], "bisque": [255, 228, 196], "black": [0, 0, 0], "blanchedalmond": [255, 235, 205], "blue": [0, 0, 255], "blueviolet": [138, 43, 226], "brown": [165, 42, 42], "burlywood": [222, 184, 135], "cadetblue": [95, 158, 160], "chartreuse": [127, 255, 0], "chocolate": [210, 105, 30], "coral": [255, 127, 80], "cornflowerblue": [100, 149, 237], "cornsilk": [255, 248, 220], "crimson": [220, 20, 60], "cyan": [0, 255, 255], "darkblue": [0, 0, 139], "darkcyan": [0, 139, 139], "darkgoldenrod": [184, 134, 11], "darkgray": [169, 169, 169], "darkgreen": [0, 100, 0], "darkgrey": [169, 169, 169], "darkkhaki": [189, 183, 107], "darkmagenta": [139, 0, 139], "darkolivegreen": [85, 107, 47], "darkorange": [255, 140, 0], "darkorchid": [153, 50, 204], "darkred": [139, 0, 0], "darksalmon": [233, 150, 122], "darkseagreen": [143, 188, 143], "darkslateblue": [72, 61, 139], "darkslategray": [47, 79, 79], "darkslategrey": [47, 79, 79], "darkturquoise": [0, 206, 209], "darkviolet": [148, 0, 211], "deeppink": [255, 20, 147], "deepskyblue": [0, 191, 255], "dimgray": [105, 105, 105], "dimgrey": [105, 105, 105], "dodgerblue": [30, 144, 255], "firebrick": [178, 34, 34], "floralwhite": [255, 250, 240], "forestgreen": [34, 139, 34], "fuchsia": [255, 0, 255], "gainsboro": [220, 220, 220], "ghostwhite": [248, 248, 255], "gold": [255, 215, 0], "goldenrod": [218, 165, 32], "gray": [128, 128, 128], "green": [0, 128, 0], "greenyellow": [173, 255, 47], "grey": [128, 128, 128], "honeydew": [240, 255, 240], "hotpink": [255, 105, 180], "indianred": [205, 92, 92], "indigo": [75, 0, 130], "ivory": [255, 255, 240], "khaki": [240, 230, 140], "lavender": [230, 230, 250], "lavenderblush": [255, 240, 245], "lawngreen": [124, 252, 0], "lemonchiffon": [255, 250, 205], "lightblue": [173, 216, 230], "lightcoral": [240, 128, 128], "lightcyan": [224, 255, 255], "lightgoldenrodyellow": [250, 250, 210], "lightgray": [211, 211, 211], "lightgreen": [144, 238, 144], "lightgrey": [211, 211, 211], "lightpink": [255, 182, 193], "lightsalmon": [255, 160, 122], "lightseagreen": [32, 178, 170], "lightskyblue": [135, 206, 250], "lightslategray": [119, 136, 153], "lightslategrey": [119, 136, 153], "lightsteelblue": [176, 196, 222], "lightyellow": [255, 255, 224], "lime": [0, 255, 0], "limegreen": [50, 205, 50], "linen": [250, 240, 230], "magenta": [255, 0, 255], "maroon": [128, 0, 0], "mediumaquamarine": [102, 205, 170], "mediumblue": [0, 0, 205], "mediumorchid": [186, 85, 211], "mediumpurple": [147, 112, 219], "mediumseagreen": [60, 179, 113], "mediumslateblue": [123, 104, 238], "mediumspringgreen": [0, 250, 154], "mediumturquoise": [72, 209, 204], "mediumvioletred": [199, 21, 133], "midnightblue": [25, 25, 112], "mintcream": [245, 255, 250], "mistyrose": [255, 228, 225], "moccasin": [255, 228, 181], "navajowhite": [255, 222, 173], "navy": [0, 0, 128], "oldlace": [253, 245, 230], "olive": [128, 128, 0], "olivedrab": [107, 142, 35], "orange": [255, 165, 0], "orangered": [255, 69, 0], "orchid": [218, 112, 214], "palegoldenrod": [238, 232, 170], "palegreen": [152, 251, 152], "paleturquoise": [175, 238, 238], "palevioletred": [219, 112, 147], "papayawhip": [255, 239, 213], "peachpuff": [255, 218, 185], "peru": [205, 133, 63], "pink": [255, 192, 203], "plum": [221, 160, 221], "powderblue": [176, 224, 230], "purple": [128, 0, 128], "rebeccapurple": [102, 51, 153], "red": [255, 0, 0], "rosybrown": [188, 143, 143], "royalblue": [65, 105, 225], "saddlebrown": [139, 69, 19], "salmon": [250, 128, 114], "sandybrown": [244, 164, 96], "seagreen": [46, 139, 87], "seashell": [255, 245, 238], "sienna": [160, 82, 45], "silver": [192, 192, 192], "skyblue": [135, 206, 235], "slateblue": [106, 90, 205], "slategray": [112, 128, 144], "slategrey": [112, 128, 144], "snow": [255, 250, 250], "springgreen": [0, 255, 127], "steelblue": [70, 130, 180], "tan": [210, 180, 140], "teal": [0, 128, 128], "thistle": [216, 191, 216], "tomato": [255, 99, 71], "turquoise": [64, 224, 208], "violet": [238, 130, 238], "wheat": [245, 222, 179], "white": [255, 255, 255], "whitesmoke": [245, 245, 245], "yellow": [255, 255, 0], "yellowgreen": [154, 205, 50] }; var simpleSwizzle = {exports: {}}; var isArrayish$1 = function isArrayish(obj) { if (!obj || typeof obj === 'string') { return false; } return obj instanceof Array || Array.isArray(obj) || (obj.length >= 0 && (obj.splice instanceof Function || (Object.getOwnPropertyDescriptor(obj, (obj.length - 1)) && obj.constructor.name !== 'String'))); }; var isArrayish = isArrayish$1; var concat = Array.prototype.concat; var slice = Array.prototype.slice; var swizzle$1 = simpleSwizzle.exports = function swizzle(args) { var results = []; for (var i = 0, len = args.length; i < len; i++) { var arg = args[i]; if (isArrayish(arg)) { // http://jsperf.com/javascript-array-concat-vs-push/98 results = concat.call(results, slice.call(arg)); } else { results.push(arg); } } return results; }; swizzle$1.wrap = function (fn) { return function () { return fn(swizzle$1(arguments)); }; }; /* MIT license */ var colorNames = colorName; var swizzle = simpleSwizzle.exports; var hasOwnProperty = Object.hasOwnProperty; var reverseNames = Object.create(null); // create a list of reverse color names for (var name in colorNames) { if (hasOwnProperty.call(colorNames, name)) { reverseNames[colorNames[name]] = name; } } var cs = colorString.exports = { to: {}, get: {} }; cs.get = function (string) { var prefix = string.substring(0, 3).toLowerCase(); var val; var model; switch (prefix) { case 'hsl': val = cs.get.hsl(string); model = 'hsl'; break; case 'hwb': val = cs.get.hwb(string); model = 'hwb'; break; default: val = cs.get.rgb(string); model = 'rgb'; break; } if (!val) { return null; } return {model: model, value: val}; }; cs.get.rgb = function (string) { if (!string) { return null; } var abbr = /^#([a-f0-9]{3,4})$/i; var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i; var rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var keyword = /^(\w+)$/; var rgb = [0, 0, 0, 1]; var match; var i; var hexAlpha; if (match = string.match(hex)) { hexAlpha = match[2]; match = match[1]; for (i = 0; i < 3; i++) { // https://jsperf.com/slice-vs-substr-vs-substring-methods-long-string/19 var i2 = i * 2; rgb[i] = parseInt(match.slice(i2, i2 + 2), 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha, 16) / 255; } } else if (match = string.match(abbr)) { match = match[1]; hexAlpha = match[3]; for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i] + match[i], 16); } if (hexAlpha) { rgb[3] = parseInt(hexAlpha + hexAlpha, 16) / 255; } } else if (match = string.match(rgba)) { for (i = 0; i < 3; i++) { rgb[i] = parseInt(match[i + 1], 0); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(per)) { for (i = 0; i < 3; i++) { rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55); } if (match[4]) { if (match[5]) { rgb[3] = parseFloat(match[4]) * 0.01; } else { rgb[3] = parseFloat(match[4]); } } } else if (match = string.match(keyword)) { if (match[1] === 'transparent') { return [0, 0, 0, 0]; } if (!hasOwnProperty.call(colorNames, match[1])) { return null; } rgb = colorNames[match[1]]; rgb[3] = 1; return rgb; } else { return null; } for (i = 0; i < 3; i++) { rgb[i] = clamp(rgb[i], 0, 255); } rgb[3] = clamp(rgb[3], 0, 1); return rgb; }; cs.get.hsl = function (string) { if (!string) { return null; } var hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d\.]+)%\s*,?\s*([+-]?[\d\.]+)%\s*(?:[,|\/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; var match = string.match(hsl); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var s = clamp(parseFloat(match[2]), 0, 100); var l = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, s, l, a]; } return null; }; cs.get.hwb = function (string) { if (!string) { return null; } var hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; var match = string.match(hwb); if (match) { var alpha = parseFloat(match[4]); var h = ((parseFloat(match[1]) % 360) + 360) % 360; var w = clamp(parseFloat(match[2]), 0, 100); var b = clamp(parseFloat(match[3]), 0, 100); var a = clamp(isNaN(alpha) ? 1 : alpha, 0, 1); return [h, w, b, a]; } return null; }; cs.to.hex = function () { var rgba = swizzle(arguments); return ( '#' + hexDouble(rgba[0]) + hexDouble(rgba[1]) + hexDouble(rgba[2]) + (rgba[3] < 1 ? (hexDouble(Math.round(rgba[3] * 255))) : '') ); }; cs.to.rgb = function () { var rgba = swizzle(arguments); return rgba.length < 4 || rgba[3] === 1 ? 'rgb(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ')' : 'rgba(' + Math.round(rgba[0]) + ', ' + Math.round(rgba[1]) + ', ' + Math.round(rgba[2]) + ', ' + rgba[3] + ')'; }; cs.to.rgb.percent = function () { var rgba = swizzle(arguments); var r = Math.round(rgba[0] / 255 * 100); var g = Math.round(rgba[1] / 255 * 100); var b = Math.round(rgba[2] / 255 * 100); return rgba.length < 4 || rgba[3] === 1 ? 'rgb(' + r + '%, ' + g + '%, ' + b + '%)' : 'rgba(' + r + '%, ' + g + '%, ' + b + '%, ' + rgba[3] + ')'; }; cs.to.hsl = function () { var hsla = swizzle(arguments); return hsla.length < 4 || hsla[3] === 1 ? 'hsl(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%)' : 'hsla(' + hsla[0] + ', ' + hsla[1] + '%, ' + hsla[2] + '%, ' + hsla[3] + ')'; }; // hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax // (hwb have alpha optional & 1 is default value) cs.to.hwb = function () { var hwba = swizzle(arguments); var a = ''; if (hwba.length >= 4 && hwba[3] !== 1) { a = ', ' + hwba[3]; } return 'hwb(' + hwba[0] + ', ' + hwba[1] + '%, ' + hwba[2] + '%' + a + ')'; }; cs.to.keyword = function (rgb) { return reverseNames[rgb.slice(0, 3)]; }; // helpers function clamp(num, min, max) { return Math.min(Math.max(min, num), max); } function hexDouble(num) { var str = Math.round(num).toString(16).toUpperCase(); return (str.length < 2) ? '0' + str : str; } var openParentheses = "(".charCodeAt(0); var closeParentheses = ")".charCodeAt(0); var singleQuote = "'".charCodeAt(0); var doubleQuote = '"'.charCodeAt(0); var backslash = "\\".charCodeAt(0); var slash = "/".charCodeAt(0); var comma = ",".charCodeAt(0); var colon = ":".charCodeAt(0); var star = "*".charCodeAt(0); var uLower = "u".charCodeAt(0); var uUpper = "U".charCodeAt(0); var plus$1 = "+".charCodeAt(0); var isUnicodeRange = /^[a-f0-9?-]+$/i; var parse$2 = function(input) { var tokens = []; var value = input; var next, quote, prev, token, escape, escapePos, whitespacePos, parenthesesOpenPos; var pos = 0; var code = value.charCodeAt(pos); var max = value.length; var stack = [{ nodes: tokens }]; var balanced = 0; var parent; var name = ""; var before = ""; var after = ""; while (pos < max) { // Whitespaces if (code <= 32) { next = pos; do { next += 1; code = value.charCodeAt(next); } while (code <= 32); token = value.slice(pos, next); prev = tokens[tokens.length - 1]; if (code === closeParentheses && balanced) { after = token; } else if (prev && prev.type === "div") { prev.after = token; prev.sourceEndIndex += token.length; } else if ( code === comma || code === colon || (code === slash && value.charCodeAt(next + 1) !== star && (!parent || (parent && parent.type === "function" && parent.value !== "calc"))) ) { before = token; } else { tokens.push({ type: "space", sourceIndex: pos, sourceEndIndex: next, value: token }); } pos = next; // Quotes } else if (code === singleQuote || code === doubleQuote) { next = pos; quote = code === singleQuote ? "'" : '"'; token = { type: "string", sourceIndex: pos, quote: quote }; do { escape = false; next = value.indexOf(quote, next + 1); if (~next) { escapePos = next; while (value.charCodeAt(escapePos - 1) === backslash) { escapePos -= 1; escape = !escape; } } else { value += quote; next = value.length - 1; token.unclosed = true; } } while (escape); token.value = value.slice(pos + 1, next); token.sourceEndIndex = token.unclosed ? next : next + 1; tokens.push(token); pos = next + 1; code = value.charCodeAt(pos); // Comments } else if (code === slash && value.charCodeAt(pos + 1) === star) { next = value.indexOf("*/", pos); token = { type: "comment", sourceIndex: pos, sourceEndIndex: next + 2 }; if (next === -1) { token.unclosed = true; next = value.length; token.sourceEndIndex = next; } token.value = value.slice(pos + 2, next); tokens.push(token); pos = next + 2; code = value.charCodeAt(pos); // Operation within calc } else if ( (code === slash || code === star) && parent && parent.type === "function" && parent.value === "calc" ) { token = value[pos]; tokens.push({ type: "word", sourceIndex: pos - before.length, sourceEndIndex: pos + token.length, value: token }); pos += 1; code = value.charCodeAt(pos); // Dividers } else if (code === slash || code === comma || code === colon) { token = value[pos]; tokens.push({ type: "div", sourceIndex: pos - before.length, sourceEndIndex: pos + token.length, value: token, before: before, after: "" }); before = ""; pos += 1; code = value.charCodeAt(pos); // Open parentheses } else if (openParentheses === code) { // Whitespaces after open parentheses next = pos; do { next += 1; code = value.charCodeAt(next); } while (code <= 32); parenthesesOpenPos = pos; token = { type: "function", sourceIndex: pos - name.length, value: name, before: value.slice(parenthesesOpenPos + 1, next) }; pos = next; if (name === "url" && code !== singleQuote && code !== doubleQuote) { next -= 1; do { escape = false; next = value.indexOf(")", next + 1); if (~next) { escapePos = next; while (value.charCodeAt(escapePos - 1) === backslash) { escapePos -= 1; escape = !escape; } } else { value += ")"; next = value.length - 1; token.unclosed = true; } } while (escape); // Whitespaces before closed whitespacePos = next; do { whitespacePos -= 1; code = value.charCodeAt(whitespacePos); } while (code <= 32); if (parenthesesOpenPos < whitespacePos) { if (pos !== whitespacePos + 1) { token.nodes = [ { type: "word", sourceIndex: pos, sourceEndIndex: whitespacePos + 1, value: value.slice(pos, whitespacePos + 1) } ]; } else { token.nodes = []; } if (token.unclosed && whitespacePos + 1 !== next) { token.after = ""; token.nodes.push({ type: "space", sourceIndex: whitespacePos + 1, sourceEndIndex: next, value: value.slice(whitespacePos + 1, next) }); } else { token.after = value.slice(whitespacePos + 1, next); token.sourceEndIndex = next; } } else { token.after = ""; token.nodes = []; } pos = next + 1; token.sourceEndIndex = token.unclosed ? next : pos; code = value.charCodeAt(pos); tokens.push(token); } else { balanced += 1; token.after = ""; token.sourceEndIndex = pos + 1; tokens.push(token); stack.push(token); tokens = token.nodes = []; parent = token; } name = ""; // Close parentheses } else if (closeParentheses === code && balanced) { pos += 1; code = value.charCodeAt(pos); parent.after = after; parent.sourceEndIndex += after.length; after = ""; balanced -= 1; stack[stack.length - 1].sourceEndIndex = pos; stack.pop(); parent = stack[balanced]; tokens = parent.nodes; // Words } else { next = pos; do { if (code === backslash) { next += 1; } next += 1; code = value.charCodeAt(next); } while ( next < max && !( code <= 32 || code === singleQuote || code === doubleQuote || code === comma || code === colon || code === slash || code === openParentheses || (code === star && parent && parent.type === "function" && parent.value === "calc") || (code === slash && parent.type === "function" && parent.value === "calc") || (code === closeParentheses && balanced) ) ); token = value.slice(pos, next); if (openParentheses === code) { name = token; } else if ( (uLower === token.charCodeAt(0) || uUpper === token.charCodeAt(0)) && plus$1 === token.charCodeAt(1) && isUnicodeRange.test(token.slice(2)) ) { tokens.push({ type: "unicode-range", sourceIndex: pos, sourceEndIndex: next, value: token }); } else { tokens.push({ type: "word", sourceIndex: pos, sourceEndIndex: next, value: token }); } pos = next; } } for (pos = stack.length - 1; pos; pos -= 1) { stack[pos].unclosed = true; stack[pos].sourceEndIndex = value.length; } return stack[0].nodes; }; var walk$1 = function walk(nodes, cb, bubble) { var i, max, node, result; for (i = 0, max = nodes.length; i < max; i += 1) { node = nodes[i]; if (!bubble) { result = cb(node, i, nodes); } if ( result !== false && node.type === "function" && Array.isArray(node.nodes) ) { walk(node.nodes, cb, bubble); } if (bubble) { cb(node, i, nodes); } } }; function stringifyNode(node, custom) { var type = node.type; var value = node.value; var buf; var customResult; if (custom && (customResult = custom(node)) !== undefined) { return customResult; } else if (type === "word" || type === "space") { return value; } else if (type === "string") { buf = node.quote || ""; return buf + value + (node.unclosed ? "" : buf); } else if (type === "comment") { return "/*" + value + (node.unclosed ? "" : "*/"); } else if (type === "div") { return (node.before || "") + value + (node.after || ""); } else if (Array.isArray(node.nodes)) { buf = stringify$1(node.nodes, custom); if (type !== "function") { return buf; } return ( value + "(" + (node.before || "") + buf + (node.after || "") + (node.unclosed ? "" : ")") ); } return value; } function stringify$1(nodes, custom) { var result, i; if (Array.isArray(nodes)) { result = ""; for (i = nodes.length - 1; ~i; i -= 1) { result = stringifyNode(nodes[i], custom) + result; } return result; } return stringifyNode(nodes, custom); } var stringify_1 = stringify$1; var minus = "-".charCodeAt(0); var plus = "+".charCodeAt(0); var dot = ".".charCodeAt(0); var exp = "e".charCodeAt(0); var EXP = "E".charCodeAt(0); // Check if three code points would start a number // https://www.w3.org/TR/css-syntax-3/#starts-with-a-number function likeNumber(value) { var code = value.charCodeAt(0); var nextCode; if (code === plus || code === minus) { nextCode = value.charCodeAt(1); if (nextCode >= 48 && nextCode <= 57) { return true; } var nextNextCode = value.charCodeAt(2); if (nextCode === dot && nextNextCode >= 48 && nextNextCode <= 57) { return true; } return false; } if (code === dot) { nextCode = value.charCodeAt(1); if (nextCode >= 48 && nextCode <= 57) { return true; } return false; } if (code >= 48 && code <= 57) { return true; } return false; } // Consume a number // https://www.w3.org/TR/css-syntax-3/#consume-number var unit = function(value) { var pos = 0; var length = value.length; var code; var nextCode; var nextNextCode; if (length === 0 || !likeNumber(value)) { return false; } code = value.charCodeAt(pos); if (code === plus || code === minus) { pos++; } while (pos < length) { code = value.charCodeAt(pos); if (code < 48 || code > 57) { break; } pos += 1; } code = value.charCodeAt(pos); nextCode = value.charCodeAt(pos + 1); if (code === dot && nextCode >= 48 && nextCode <= 57) { pos += 2; while (pos < length) { code = value.charCodeAt(pos); if (code < 48 || code > 57) { break; } pos += 1; } } code = value.charCodeAt(pos); nextCode = value.charCodeAt(pos + 1); nextNextCode = value.charCodeAt(pos + 2); if ( (code === exp || code === EXP) && ((nextCode >= 48 && nextCode <= 57) || ((nextCode === plus || nextCode === minus) && nextNextCode >= 48 && nextNextCode <= 57)) ) { pos += nextCode === plus || nextCode === minus ? 3 : 2; while (pos < length) { code = value.charCodeAt(pos); if (code < 48 || code > 57) { break; } pos += 1; } } return { number: value.slice(0, pos), unit: value.slice(pos) }; }; var parse$1 = parse$2; var walk = walk$1; var stringify = stringify_1; function ValueParser(value) { if (this instanceof ValueParser) { this.nodes = parse$1(value); return this; } return new ValueParser(value); } ValueParser.prototype.toString = function() { return Array.isArray(this.nodes) ? stringify(this.nodes) : ""; }; ValueParser.prototype.walk = function(cb, bubble) { walk(this.nodes, cb, bubble); return this; }; ValueParser.unit = unit; ValueParser.walk = walk; ValueParser.stringify = stringify; var lib = ValueParser; var cssfontparser$1 = {exports: {}}; // Inspriation from node-canvas (https://github.com/LearnBoost/node-canvas/) var cache = {}; // regex hoisted from http://stackoverflow.com/questions/10135697/regex-to-parse-any-css-font var fontRegex = new RegExp([ '^\\s*(?=(?:(?:[-a-z]+\\s*){0,2}(italic|oblique))?)', '(?=(?:(?:[-a-z]+\\s*){0,2}(small-caps))?)', '(?=(?:(?:[-a-z]+\\s*){0,2}(bold(?:er)?|lighter|[1-9]00 ))?)', '(?:(?:normal|\\1|\\2|\\3)\\s*){0,3}((?:xx?-)?', '(?:small|large)|medium|smaller|larger|[\\.\\d]+(?:\\%|in|[cem]m|ex|p[ctx]))', '(?:\\s*\\/\\s*(normal|[\\.\\d]+(?:\\%|in|[cem]m|ex|p[ctx])?))', '?\\s*([-,\\"\\\'\\sa-z]+?)\\s*$' ].join(''), 'i'); var mapping = [ 'style', 'variant', 'weight', 'size', 'lineHeight', 'family' ]; var unitMatch = /([\.0-9]+)(.*)/; var numeric = function(val, parent, dpi) { var matches = val.match(unitMatch); if (!matches) { return; } val = parseFloat(matches[1]); var units = matches[2].toLowerCase().trim(); var v = Math.round(val); if (v === val) { val = v; } switch (units) { case 'em': if (parent === null) { return; } return val * parent; case 'px': return val; case 'pt': return val / (72/dpi); case 'pc': return val / (6/dpi); case 'mm': return val * (dpi/25.4) case 'cm': return val * (dpi/2.54) case 'in': return val * dpi; case '%': if (parent === null) { return; } return parent * (val/100); } }; var op = { size: numeric, lineHeight: numeric }; var generics = { serif : 1, 'sans-serif': 1, cursive: 1, fantasy: 1, monospace: 1 }; var parse = cssfontparser$1.exports = function(str, existing, dpi) { var cacheKey = str + '-' + (existing || 'null') +'@' + dpi; dpi = dpi || 96.0; if (typeof cache[cacheKey] !== 'undefined') { return cache[cacheKey]; } if (existing) { existing = parse(existing, null, dpi); } if (str === 'inherit') { return existing; } var matches = fontRegex.exec(str); if (!matches) { cache[cacheKey] = null; return; } matches.shift(); var collected = {}; for (var i=0; i<matches.length; i++) { var key = mapping[i]; var val = matches[i]; if (op[key] && val) { var existingVal = (existing) ? existing[key] || null : null; var v = op[key](val, existingVal, dpi); if (typeof v === 'undefined' && key === 'lineHeight' && val) { val = collected.size * parseFloat(val); } else { val = v; } } if (!val || val === 'normal') { continue; } else if (val === 'inherit') { if (!existing) { return; } val = existing[key]; } if (val.trim) { val = val.trim(); } collected[key] = val; } if (!Object.keys(collected).length) { collected = null; } cache[cacheKey] = collected; var out = []; if (collected.style) { out.push(collected.style); } if (collected.variant) { out.push(collected.variant); } if (collected.weight && collected.weight !== '400' && collected.weight !== 'normal') { out.push(collected.weight); } out.push(collected.size + 'px'); if (collected.lineHeight) { out[out.length-1] += '/' + collected.lineHeight + 'px'; } var family = collected.family.split(','); collected.family = family.map(function(a) { a = a.trim(); if (generics[a.toLowerCase()]) { a = a.toLowerCase(); } return a; }); out.push(collected.family); Object.defineProperty(collected, 'toString', { value: function() { return out.map(function(val) { if (Array.isArray(val)) { return val.map(function(a) { if (a.indexOf(' ') > -1) { return '"' + a.replace(/["']/g, '') + '"'; } else { return a; } }).join(', '); } else { return val; } }).join(' '); } }); return collected; }; cssfontparser$1.exports.generics = generics; var cssfontparser = cssfontparser$1.exports; function matrix_multiply(_a, _b) { let a, b; { const [m002, m102, m012, m112, m022, m122] = _a; a = { m00: m002, m10: m102, m01: m012, m11: m112, m02: m022, m12: m122 }; } { const [m002, m102, m012, m112, m022, m122] = _b; b = { m00: m002, m10: m102, m01: m012, m11: m112, m02: m022, m12: m122 }; } const m00 = a.m00 * b.m00 + a.m10 * b.m01; const m10 = a.m00 * b.m10 + a.m10 * b.m11; const m01 = a.m01 * b.m00 + a.m11 * b.m01; const m11 = a.m01 * b.m10 + a.m11 * b.m11; const m02 = a.m02 * b.m00 + a.m12 * b.m01 + b.m02; const m12 = a.m02 * b.m10 + a.m12 * b.m11 + b.m12; return [m00, m10, m01, m11, m02, m12]; } const Transform = { identity() { return [1, 0, 0, 1, 0, 0]; }, translate(m, x, y) { return matrix_multiply(m, [1, 0, 0, 1, x, y]); }, translateX(m, x) { return matrix_multiply(m, [1, 0, 0, 1, x, 0]); }, translateY(m, y) { return matrix_multiply(m, [1, 0, 0, 1, 0, y]); }, rotate(m, radians) { const c = Math.cos(radians); const s = Math.sin(radians); return matrix_multiply(m, [c, s, -s, c, 0, 0]); }, scale(m, x, y) { return matrix_multiply(m, [x, 0, 0, y, 0, 0]); }, scaleX(m, x) { return matrix_multiply(m, [x, 0, 0, 1, 0, 0]); }, scaleY(m, y) { return matrix_multiply(m, [1, 0, 0, y, 0, 0]); }, skew(m, x, y) { return matrix_multiply(m, [ 1, Math.tan(y), Math.tan(x), 1, 0, 0 ]); }, skewX(m, x) { return matrix_multiply(m, [1, 0, Math.tan(x), 1, 0, 0]); }, skewY(m, y) { return matrix_multiply(m, [1, Math.tan(y), 0, 1, 0, 0]); } }; const lengthReg = /^(-?\d*\.?\d+)([a-zA-Z]*)?$/; function parseLength(value) { const found = lengthReg.exec(value); if (!found) throw new Error("Invalid value"); const num = parseFloat(found[1]); const unit = found[2]; if (!unit) { return num; } else if (unit === "px") { return num; } throw new Error(`Unexcepted unit [${unit}]`); } const degReg = /^(-?\d*\.?\d+)(deg|grad|rad|turn)?$/; function parseAngle(value) { const found = degReg.exec(value); if (!found) throw new Error("Invalid value"); const num = parseFloat(found[1]); const unit = found[2]; if (!unit) { const num2 = parseFloat(value); if (num2 !== 0) { throw new Error("should be zero"); } return num2; } else if (unit === "deg") { return num * (Math.PI / 180); } else if (unit === "grad") { return num * (Math.PI / 200); } else if (unit === "rad") { return num; } else if (unit === "turn") { return num * (2 * Math.PI); } throw new Error(`Unexcepted unit [${unit}]`); } function parseTransform(value) { return lib(value).nodes.filter((node) => node.type === "function").reduce((previousValue, currentValue) => { switch (currentValue.value) { case "matrix": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = valueNode.map( (node) => parseFloat(node.value) ); return matrix_multiply(previousValue, v); } case "scale": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); if (valueNode.length === 1) { const v = parseFloat(valueNode[0].value); return Transform.scale(previousValue, v, v); } else { return Transform.scale( previousValue, parseFloat(valueNode[0].value), parseFloat(valueNode[1].value) ); } } case "scaleX": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseFloat(valueNode[0].value); return Transform.scaleX(previousValue, v); } case "scaleY": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseFloat(valueNode[0].value); return Transform.scaleY(previousValue, v); } case "rotate": case "rotateZ": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseAngle(valueNode[0].value); return Transform.rotate(previousValue, v); } case "skew": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v0 = parseAngle(valueNode[0].value); const v1 = parseAngle(valueNode[1].value); return Transform.skew(previousValue, v0, v1); } case "skewX": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseAngle(valueNode[0].value); return Transform.skewX(previousValue, v); } case "skewY": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseAngle(valueNode[0].value); return Transform.skewY(previousValue, v); } case "translate": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v0 = parseLength(valueNode[0].value); const v1 = parseLength(valueNode[1].value); return Transform.translate(previousValue, v0, v1); } case "translateX": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseLength(valueNode[0].value); return Transform.translateX(previousValue, v); } case "translateY": { const valueNode = currentValue.nodes.filter( (node) => node.type === "word" ); const v = parseLength(valueNode[0].value); return Transform.translateY(previousValue, v); } } return previousValue; }, Transform.identity()); } function parseFont(value) { const { style, variant, weight, size, lineHeight, family } = cssfontparser( value ); return { fontSize: size, fontFamily: family }; } class MeuiEvent { constructor(type, eventInitDict = {}) { this.type = type; this.bubbles = eventInitDict.bubbles ?? false; this.cancelable = eventInitDict.cancelable ?? false; this.target = null; this.currentTarget = null; } stopPropagation() { } } class MeuiMouseEvent extends MeuiEvent { constructor(type, eventInitDict = {}) { super(type); this.button = eventInitDict.button ?? 0; this.buttons = eventInitDict.buttons ?? 0; this.clientX = eventInitDict.clientX ?? 0; this.clientY = eventInitDict.clientY ?? 0; this.screenX = eventInitDict.screenX ?? 0; this.screenY = eventInitDict.screenY ?? 0; this.x = eventInitDict.clientX ?? 0; this.y = eventInitDict.clientY ?? 0; this.offsetX = 0; this.offsetY = 0; } } class MeuiWheelEvent extends MeuiMouseEvent { constructor(type, eventInitDict = {}) { super(type, eventInitDict); this.deltaX = eventInitDict.deltaX ?? 0; this.deltaY = eventInitDict.deltaY ?? 0; this.deltaZ = eventInitDict.deltaZ ?? 0; } } class MeuiKeyboardEvent extends MeuiEvent { constructor(type, eventInitDict = {}) { super(type); this.DOM_KEY_LOCATION_LEFT = 0; this.DOM_KEY_LOCATION_NUMPAD = 1; this.DOM_KEY_LOCATION_RIGHT = 2; this.DOM_KEY_LOCATION_STANDARD = 3; this.altKey = eventInitDict.altKey ?? false; this.ctrlKey = eventInitDict.ctrlKey ?? false; this.shiftKey = eventInitDict.shiftKey ?? false; this.key = eventInitDict.key ?? ""; this.keyCode = eventInitDict.keyCode ?? 0; } } class MeuiFocusEvent extends MeuiEvent { constructor(type, eventInitDict = {}) { super(type); this.relatedTarget = eventInitDict.relatedTarget ?? null; } } class CustomEvent extends MeuiEvent { constructor(type) { super(type); } } const styleHandler = { set: (target, prop, value) => { if (typeof value === "string" && value.endsWith("px")) { value = parseLength(value); } target.setStyle({ [prop]: value }); return true; } }; class Element extends Node { constructor(type = "View", style) { super(); this.focusable = false; this.nodeType = NodeType.ELEMENT_NODE; this.nativeBox = new Box(type); this.children = []; this.eventListeners = {}; if (style) this.setStyle(style); this.style = new Proxy(this, styleHandler); } get onmousedown() { return null; } get onmouseup() { return null; } get onmousemove() { return null; } get onmouseout() { return null; } get onmouseover() { return null; } get onmousewheel() { return null; } get onkeydown() { return null; } get onkeyup() { return null; } get onclick() { return null; } get onfocusin() { return null; } get onfocusout() { return null; } getStyle(state) { return this.nativeBox.getStyle(state); } _setStyle(style, state) { const nativeStyle = style; if (style.borderColor) { const [r, g, b, a] = colorString.exports.get.rgb(style.borderColor) ?? [ 0, 0, 0, 0 ]; nativeStyle.borderColor = [r / 255, g / 255, b / 255, a]; } if (style.backgroundColor) { const [r, g, b, a] = colorString.exports.get.rgb(style.backgroundColor) ?? [ 0, 0, 0, 0 ]; nativeStyle.backgroundColor = [r / 255, g / 255, b / 255, a]; } if (style.fontColor) { const [r, g, b, a] = colorString.exports.get.rgb(style.fontColor) ?? [ 0, 0, 0, 0 ]; nativeStyle.fontColor = [r / 255, g / 255, b / 255, a]; } if (style.transform) { nativeStyle.transform = parseTransform(style.transform); } if (style.borderRadius) { const v = style.borderRadius; if (typeof style.borderRadius === "number") { nativeStyle.borderRadius = [v, v, v, v]; } } if (style.margin) { const v = style.margin; if (typeof style.margin === "number") { nativeStyle.margin = [v, v, v, v]; } } if (style.border) { const v = style.border; if (typeof style.border === "number") { nativeStyle.border = [v, v, v, v]; } } if (style.padding) { const v = style.padding; if (typeof style.padding === "number") { nativeStyle.padding = [v, v, v, v]; } } this.nativeBox.setStyle(nativeStyle, state); } setStyle(style) { const defaultStyle = {}; for (const [k, v] of Object.entries(style)) { if (k in BOX_STATE) { const otherStyle = {}; for (const [key, val] of Object.entries(v)) { otherStyle[key] = val ?? null; } this._setStyle( otherStyle, BOX_STATE[k] ); } else { defaultStyle[k] = v ?? null; } } this._setStyle(defaultStyle, BOX_STATE.DEFAULT); } checkConsistency() { if (this.childNodes.length !== this.nativeBox.getChildCount()) { throw new Error("Failed to pass consistency check"); } } insertChildElement(child, index) { this.children.splice(index, 0, child); this.nativeBox.insertChild(child.nativeBox, index); this.checkConsistency(); } updateText() { const text = this.childNodes.filter((child) => child.nodeType === NodeType.TEXT_NODE).map((child) => child.text ?? "").join(""); this.textContent = text; } removeChild(child) { super.removeChild(child); if (child.nodeType === NodeType.ELEMENT_NODE) { const index = this.children.indexOf(child); if (index !== -1) { this.children.splice(index, 1); this.nativeBox.removeChild(child.nativeBox); this.checkConsistency(); } } else if (child.nodeType === NodeType.TEXT_NODE) { this.updateText(); } } insertBefore(child, beforeChild) { super.insertBefore(child, beforeChild); if (beforeChild && child.nodeType !== beforeChild.nodeType) { throw new Error("Child nodeType must be same"); } if (child.nodeType === NodeType.ELEMENT_NODE) { const index = beforeChild ? this.children.indexOf(beforeChild) : this.children.length; this.insertChildElement(child, index); } else if (child.nodeType === NodeType.TEXT_NODE) { this.updateText(); } return child; } getState() { return this.nativeBox.getState(); } setState(state) { this.nativeBox.setState(state); } hit(x, y) { return this.nativeBox.hit(x, y); } search(x, y) { return this.nativeBox.search(x, y); } getNativeObject() { return this.nativeBox; } addEventListener(type, listener, useCapture = false) { if (!listener) return; const listenerArray = this.eventListeners[type] ?? (this.eventListeners[type] = []); listenerArray.push({ listener, useCapture }); } removeEventListener(type, listener, useCapture = false) { if (!listener || !type) return; if (!this.eventListeners[type]) return; const listenerArray = this.eventListeners[type]; if (!listenerArray) return; const index = listenerArray.findIndex( (value) => value.listener === listener && value.useCapture === useCapture ); if (index !== -1) { listenerArray.splice(index, 1); return; } } toClient(x, y) { return this.nativeBox.toClient(x, y); } toOffset(x, y) { return this.nativeBox.toOffset(x, y); } tryHandleEvent(event, capture) { const listenerArray = this.eventListeners[event.type]; if (!listenerArray) return; for (const { listener, useCapture } of listenerArray) { if (["mouseup", "mousedown", "mousemove"].includes(event.type)) { const mouseEvent = event; const [clientX, clientY] = this.toClient( mouseEvent.screenX, mouseEvent.screenY ); Object.defineProperty(mouseEvent, "clientX", { ...Object.getOwnPropertyDescriptor(mouseEvent, "clientX"), value: clientX }); Object.defineProperty(mouseEvent, "clientY", { ...Object.getOwnPropertyDescriptor(mouseEvent, "clientY"), value: clientY }); const [offsetX, offsetY] = this.toOffset( mouseEvent.screenX, mouseEvent.screenY ); Object.defineProperty(mouseEvent, "offsetX", { ...Object.getOwnPropertyDescriptor(mouseEvent, "offsetX"), value: offsetX }); Object.defineProperty(mouseEvent, "offsetY", { ...Object.getOwnPropertyDescriptor(mouseEvent, "offsetY"), value: offsetY }); } if (capture == useCapture) { listener.call(this, event); } } } capturingPhase(node, event) { if (node.parentNode) this.capturingPhase(node.parentNode, event); event.currentTarget = node; node.tryHandleEvent(event, true); } bubblingPhase(node, event) { while (true) { event.currentTarget = node; node.tryHandleEvent(event, false); if (!node.parentNode) break; node = node.parentNode; } } dispatchEvent(event) { event.target = this; this.capturingPhase(this, event); this.bubblingPhase(this, event); } getPath() { const path = []; let box = this; while (box) { path.unshift(box); box = box.parentNode; } return path; } get textContent() { return this.nativeBox.textContent; } set textContent(val) { this.nativeBox.textContent = val; } get scrollWidth() { return this.nativeBox.scrollWidth; } get scrollHeight() { return this.nativeBox.scrollHeight; } get clientWidth() { return this.nativeBox.clientWidth; } get clientHeight() { return this.nativeBox.clientHeight; } get scrollTopMax() { return this.nativeBox.scrollHeight - this.nativeBox.clientHeight; } get scrollLeftMax() { return this.nativeBox.scrollWidth - this.nativeBox.clientWidth; } get scrollTop() { return this.nativeBox.scrollTop; } set scrollTop(val) { this.nativeBox.scrollTop = val; } get scrollLeft() { return this.nativeBox.scrollLeft; } set scrollLeft(val) { this.nativeBox.scrollLeft = val; } } class DivElement extends Element { constructor(style) { super("div", style); } } class StackElement extends Element { constructor(style) { super("stack", style); } } class Path2D { constructor(path) { if (!path) { this.native = new Path2D$1(); } else if (path instanceof Path2D) { this.native = new Path2D$1(path.native); } else if (typeof path === "string") { this.native = new Path2D$1(path); } else { throw new Error("Invalid paramter type"); } } arc(x, y, radius, startAngle, endAngle, counterclockwise) { this.native.arc(x, y, radius, startAngle, endAngle, counterclockwise); } arcTo(x1, y1, x2, y2, radius) { this.native.arcTo(x1, y1, x2, y2, radius); } bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) { this.native.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y); } closePath() { this.native.closePath(); } ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) { this.native.ellipse( x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise ); } lineTo(x, y) { this.native.lineTo(x, y); } moveTo(x, y) { this.native.moveTo(x, y); } quadraticCurveTo(cpx, cpy, x, y) { this.native.quadraticCurveTo(cpx, cpy, x, y); } rect(x, y, w, h) { this.native.rect(x, y, w, h); } addPath(path, transform) { this.native.addPath(path, transform); } } class CanvasGradient { constructor(...args) { if (args.length === 3) { throw new Error("Not support ConicGradient"); } else if (args.length === 4) { const [x0, y0, x1, y1] = args; this.native = new CanvasGradient$1("linear", x0, y0, x1, y1); } else if (args.length === 6) { const [x0, y0, r0, x1, y1, r1] = args; this.native = new CanvasGradient$1( "radial", x0, y0, r0, x1, y1, r1 ); } else { throw new Error("Invalid arguments count"); } } addColorStop(offset, color) { const [r, g, b, a] = colorString.exports.get.rgb(color); this.native.addColorStop(offset, r / 255, g / 255, b / 255, a); } } var COLOR_FORMAT = /* @__PURE__ */ ((COLOR_FORMAT2) => { COLOR_FORMAT2[COLOR_FORMAT2["COLOR_RGBA"] = 0] = "COLOR_RGBA"; COLOR_FORMAT2[COLOR_FORMAT2["COLOR_RGB"] = 1] = "COLOR_RGB"; COLOR_FORMAT2[COLOR_FORMAT2["COLOR_BGRA"] = 2] = "COLOR_BGRA"; COLOR_FORMAT2[COLOR_FORMAT2["COLOR_BGR"] = 3] = "COLOR_BGR"; return COLOR_FORMAT2; })(COLOR_FORMAT || {}); class ImageData { constructor(...args) { if (args.length < 2) { throw new TypeError(` Failed to construct 'ImageData': 2 arguments required, but only ${args.length} present. `); } if (args.length > 2 && typeof args[0] !== "number") { this.data = args.shift(); if (!(this.data instanceof Uint8ClampedArray)) {