formacao
This commit is contained in:
+28
-17
@@ -2,10 +2,21 @@
|
||||
import { raw } from "../helper/html/index.js";
|
||||
import { escapeToBuffer, resolveCallbackSync, stringBufferToString } from "../utils/html.js";
|
||||
import { DOM_RENDERER, DOM_MEMO } from "./constants.js";
|
||||
import { createContext, globalContexts, useContext } from "./context.js";
|
||||
import {
|
||||
captureRenderContext,
|
||||
createContext,
|
||||
globalContexts,
|
||||
runWithRenderContext,
|
||||
useContext
|
||||
} from "./context.js";
|
||||
import { domRenderers } from "./intrinsic-element/common.js";
|
||||
import * as intrinsicElementTags from "./intrinsic-element/components.js";
|
||||
import { normalizeIntrinsicElementKey, styleObjectForEach } from "./utils.js";
|
||||
import {
|
||||
isValidAttributeName,
|
||||
isValidTagName,
|
||||
normalizeIntrinsicElementKey,
|
||||
styleObjectForEach
|
||||
} from "./utils.js";
|
||||
var nameSpaceContext = void 0;
|
||||
var getNameSpaceContext = () => nameSpaceContext;
|
||||
var toSVGAttributeName = (key) => /[A-Z]/.test(key) && // Presentation attributes are findable in style object. "clip-path", "font-size", "stroke-width", etc.
|
||||
@@ -83,8 +94,11 @@ var JSXNode = class {
|
||||
key;
|
||||
children;
|
||||
isEscaped = true;
|
||||
localContexts;
|
||||
suspendedContext;
|
||||
constructor(tag, props, children) {
|
||||
if (typeof tag !== "function" && !isValidTagName(tag)) {
|
||||
throw new Error(`Invalid JSX tag name: ${tag}`);
|
||||
}
|
||||
this.tag = tag;
|
||||
this.props = props;
|
||||
this.children = children;
|
||||
@@ -98,27 +112,24 @@ var JSXNode = class {
|
||||
return this.props.ref || null;
|
||||
}
|
||||
toString() {
|
||||
const buffer = [""];
|
||||
this.localContexts?.forEach(([context, value]) => {
|
||||
context.values.push(value);
|
||||
});
|
||||
try {
|
||||
const render = () => {
|
||||
const buffer = [""];
|
||||
this.toStringToBuffer(buffer);
|
||||
} finally {
|
||||
this.localContexts?.forEach(([context]) => {
|
||||
context.values.pop();
|
||||
});
|
||||
}
|
||||
return buffer.length === 1 ? "callbacks" in buffer ? resolveCallbackSync(raw(buffer[0], buffer.callbacks)).toString() : buffer[0] : stringBufferToString(buffer, buffer.callbacks);
|
||||
return buffer.length === 1 ? "callbacks" in buffer ? resolveCallbackSync(raw(buffer[0], buffer.callbacks)).toString() : buffer[0] : stringBufferToString(buffer, buffer.callbacks);
|
||||
};
|
||||
return this.suspendedContext ? this.suspendedContext(render) : runWithRenderContext(render);
|
||||
}
|
||||
toStringToBuffer(buffer) {
|
||||
const tag = this.tag;
|
||||
const props = this.props;
|
||||
let { children } = this;
|
||||
buffer[0] += `<${tag}`;
|
||||
const normalizeKey = nameSpaceContext && useContext(nameSpaceContext) === "svg" ? (key) => toSVGAttributeName(normalizeIntrinsicElementKey(key)) : (key) => normalizeIntrinsicElementKey(key);
|
||||
const normalizeKey = tag === "svg" || nameSpaceContext && useContext(nameSpaceContext) === "svg" ? (key) => toSVGAttributeName(normalizeIntrinsicElementKey(key)) : (key) => normalizeIntrinsicElementKey(key);
|
||||
for (let [key, v] of Object.entries(props)) {
|
||||
key = normalizeKey(key);
|
||||
if (!isValidAttributeName(key)) {
|
||||
continue;
|
||||
}
|
||||
if (key === "children") {
|
||||
} else if (key === "style" && typeof v === "object") {
|
||||
let styleStr = "";
|
||||
@@ -182,12 +193,12 @@ var JSXFunctionNode = class extends JSXNode {
|
||||
if (globalContexts.length === 0) {
|
||||
buffer.unshift("", res);
|
||||
} else {
|
||||
const currentContexts = globalContexts.map((c) => [c, c.values.at(-1)]);
|
||||
const suspendedContext = captureRenderContext();
|
||||
buffer.unshift(
|
||||
"",
|
||||
res.then((childRes) => {
|
||||
if (childRes instanceof JSXNode) {
|
||||
childRes.localContexts = currentContexts;
|
||||
childRes.suspendedContext = suspendedContext;
|
||||
}
|
||||
return childRes;
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user