@master/css-server 2.0.0-rc.73 → 2.0.0-rc.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/create-server-css.d.ts +2 -2
- package/dist/create-server-css.js +5 -0
- package/dist/decode-html.d.ts +4 -1
- package/dist/decode-html.js +17 -0
- package/dist/default-manifest.d.ts +6 -2
- package/dist/default-manifest.js +8 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -0
- package/dist/parse-html.d.ts +11 -7
- package/dist/parse-html.js +43 -0
- package/dist/render-css.d.ts +7 -3
- package/dist/render-css.js +20 -0
- package/dist/render.d.ts +25 -21
- package/dist/render.js +165 -0
- package/package.json +66 -1
- package/dist/create-server-css.mjs +0 -2
- package/dist/decode-html.mjs +0 -27
- package/dist/default-manifest.mjs +0 -8
- package/dist/index.mjs +0 -4
- package/dist/parse-html.mjs +0 -46
- package/dist/render-css.mjs +0 -19
- package/dist/render.mjs +0 -206
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Aoyue Design LLC.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
</picture>
|
|
11
11
|
</a>
|
|
12
12
|
</p>
|
|
13
|
-
<p align="center">
|
|
13
|
+
<p align="center">Server-side HTML renderer for Master CSS</p>
|
|
14
14
|
|
|
15
15
|
<p align="center">
|
|
16
16
|
<a aria-label="GitHub release (latest by date including pre-releases)" href="https://github.com/master-co/css/releases">
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { createCSSWithNativeDeclarations } from
|
|
2
|
-
export default
|
|
1
|
+
import { createCSSWithNativeDeclarations } from "@master/css-validator";
|
|
2
|
+
export { createCSSWithNativeDeclarations as default };
|
package/dist/decode-html.d.ts
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/decode-html.ts
|
|
2
|
+
function decodeHTML(html) {
|
|
3
|
+
return html.replace(/&(amp|apos|#39|#x27|quot|lt|gt);/g, (match, entity) => {
|
|
4
|
+
switch (entity) {
|
|
5
|
+
case "amp": return "&";
|
|
6
|
+
case "apos":
|
|
7
|
+
case "#39":
|
|
8
|
+
case "#x27": return "'";
|
|
9
|
+
case "quot": return "\"";
|
|
10
|
+
case "lt": return "<";
|
|
11
|
+
case "gt": return ">";
|
|
12
|
+
default: return match;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { decodeHTML as default };
|
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { MasterCSSManifest } from "@master/css-schema/manifest";
|
|
2
|
+
|
|
3
|
+
//#region src/default-manifest.d.ts
|
|
4
|
+
declare function getDefaultManifest(): MasterCSSManifest;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { getDefaultManifest as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import defaultManifestJSON from "@master/css-preset/default-manifest.json" with { type: "json" };
|
|
2
|
+
//#region src/default-manifest.ts
|
|
3
|
+
const defaultManifest = defaultManifestJSON;
|
|
4
|
+
function getDefaultManifest() {
|
|
5
|
+
return defaultManifest;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
export { getDefaultManifest as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export {
|
|
1
|
+
import render, { RenderHydrationManifestOption, RenderOptions, RenderResult } from "./render.js";
|
|
2
|
+
import renderCSS from "./render-css.js";
|
|
3
|
+
import parseHTML from "./parse-html.js";
|
|
4
|
+
import createServerCSS from "./create-server-css.js";
|
|
5
|
+
export { type RenderHydrationManifestOption, type RenderOptions, type RenderResult, createServerCSS, parseHTML, render, renderCSS };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import create_server_css_default from "./create-server-css.js";
|
|
2
|
+
import parseHTML from "./parse-html.js";
|
|
3
|
+
import render from "./render.js";
|
|
4
|
+
import renderCSS from "./render-css.js";
|
|
5
|
+
export { create_server_css_default as createServerCSS, parseHTML, render, renderCSS };
|
package/dist/parse-html.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ChildNode, Element } from "domhandler";
|
|
2
|
+
|
|
3
|
+
//#region src/parse-html.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* @param html
|
|
4
6
|
* @returns {classes from html, root element}
|
|
5
7
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
declare function parseHTML(html: string): {
|
|
9
|
+
classes: string[];
|
|
10
|
+
nodes: ChildNode[];
|
|
11
|
+
htmlElement: Element | null;
|
|
12
|
+
headElement: Element | null;
|
|
13
|
+
styleElement: Element | null;
|
|
12
14
|
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { parseHTML as default };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import decodeHTML from "./decode-html.js";
|
|
2
|
+
import { MASTER_CSS_RUNTIME_STYLE_ID } from "@master/css-schema/runtime-style";
|
|
3
|
+
import { DomHandler, Parser } from "htmlparser2";
|
|
4
|
+
//#region src/parse-html.ts
|
|
5
|
+
/**
|
|
6
|
+
* @param html
|
|
7
|
+
* @returns {classes from html, root element}
|
|
8
|
+
*/
|
|
9
|
+
function parseHTML(html) {
|
|
10
|
+
const classes = [];
|
|
11
|
+
let htmlElement = null;
|
|
12
|
+
let headElement = null;
|
|
13
|
+
let styleElement = null;
|
|
14
|
+
const handler = new DomHandler(void 0, {}, (element) => {
|
|
15
|
+
if (element.type === "tag") switch (element.name) {
|
|
16
|
+
case "html":
|
|
17
|
+
htmlElement = element;
|
|
18
|
+
break;
|
|
19
|
+
case "head":
|
|
20
|
+
headElement = element;
|
|
21
|
+
break;
|
|
22
|
+
default: break;
|
|
23
|
+
}
|
|
24
|
+
else if (element.type === "style") {
|
|
25
|
+
if (element.attribs.id === MASTER_CSS_RUNTIME_STYLE_ID) styleElement = element;
|
|
26
|
+
}
|
|
27
|
+
if (element.attribs.class) element.attribs.class.trim().split(/\s+/).forEach((className) => {
|
|
28
|
+
if (!className) return;
|
|
29
|
+
className = decodeHTML(className);
|
|
30
|
+
if (!classes.includes(className)) classes.push(className);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
new Parser(handler, { decodeEntities: false }).end(html);
|
|
34
|
+
return {
|
|
35
|
+
classes,
|
|
36
|
+
nodes: handler.root.childNodes,
|
|
37
|
+
htmlElement,
|
|
38
|
+
headElement,
|
|
39
|
+
styleElement
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { parseHTML as default };
|
package/dist/render-css.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { MasterCSS } from
|
|
2
|
-
import
|
|
1
|
+
import { MasterCSS } from "@master/css";
|
|
2
|
+
import { MasterCSSManifest } from "@master/css-schema/manifest";
|
|
3
|
+
|
|
4
|
+
//#region src/render-css.d.ts
|
|
3
5
|
/**
|
|
4
6
|
* Renders the Master CSS string from HTML
|
|
5
7
|
* @param html
|
|
6
8
|
* @param manifest
|
|
7
9
|
* @returns MasterCSS
|
|
8
10
|
*/
|
|
9
|
-
|
|
11
|
+
declare function renderCSS(html: string, manifest?: MasterCSSManifest): MasterCSS | undefined;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { renderCSS as default };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import create_server_css_default from "./create-server-css.js";
|
|
2
|
+
import getDefaultManifest from "./default-manifest.js";
|
|
3
|
+
import parseHTML from "./parse-html.js";
|
|
4
|
+
//#region src/render-css.ts
|
|
5
|
+
/**
|
|
6
|
+
* Renders the Master CSS string from HTML
|
|
7
|
+
* @param html
|
|
8
|
+
* @param manifest
|
|
9
|
+
* @returns MasterCSS
|
|
10
|
+
*/
|
|
11
|
+
function renderCSS(html, manifest) {
|
|
12
|
+
if (!html) return;
|
|
13
|
+
const { classes } = parseHTML(html);
|
|
14
|
+
if (!classes.length) return;
|
|
15
|
+
const css = create_server_css_default(manifest || getDefaultManifest());
|
|
16
|
+
classes.forEach((eachClass) => css.ensureClassRules(eachClass));
|
|
17
|
+
return css;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { renderCSS as default };
|
package/dist/render.d.ts
CHANGED
|
@@ -1,29 +1,33 @@
|
|
|
1
|
-
import { MasterCSS } from
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { MasterCSS } from "@master/css";
|
|
2
|
+
import { MasterCSSHydrationManifest } from "@master/css-schema/hydration-manifest";
|
|
3
|
+
import { ChildNode, Element } from "domhandler";
|
|
4
|
+
import { MasterCSSManifest } from "@master/css-schema/manifest";
|
|
5
|
+
|
|
6
|
+
//#region src/render.d.ts
|
|
7
|
+
type RenderExternalHydrationManifestSource = string | ((json: string, hydrationManifest: MasterCSSHydrationManifest) => string);
|
|
8
|
+
interface RenderExternalHydrationManifestOption {
|
|
9
|
+
type: 'external';
|
|
10
|
+
src: RenderExternalHydrationManifestSource;
|
|
9
11
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
type RenderHydrationManifestOption = 'return' | 'inject' | false | RenderExternalHydrationManifestOption;
|
|
13
|
+
interface RenderOptions {
|
|
14
|
+
hydrationManifest?: RenderHydrationManifestOption;
|
|
13
15
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
interface RenderResult {
|
|
17
|
+
html: string;
|
|
18
|
+
css?: MasterCSS;
|
|
19
|
+
classes: string[];
|
|
20
|
+
nodes: ChildNode[];
|
|
21
|
+
htmlElement: Element | null;
|
|
22
|
+
headElement: Element | null;
|
|
23
|
+
styleElement: Element | null;
|
|
24
|
+
hydrationManifest?: MasterCSSHydrationManifest;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* Renders the page-required and sorted CSS text from HTML and injected it back into HTML
|
|
26
28
|
* @param html
|
|
27
29
|
* @param manifest
|
|
28
30
|
*/
|
|
29
|
-
|
|
31
|
+
declare function render(html: string, manifest?: MasterCSSManifest, options?: RenderOptions): RenderResult;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { RenderExternalHydrationManifestOption, RenderExternalHydrationManifestSource, RenderHydrationManifestOption, RenderOptions, RenderResult, render as default };
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import create_server_css_default from "./create-server-css.js";
|
|
2
|
+
import getDefaultManifest from "./default-manifest.js";
|
|
3
|
+
import parseHTML from "./parse-html.js";
|
|
4
|
+
import { createHydrationManifest } from "@master/css";
|
|
5
|
+
import { MASTER_CSS_HYDRATION_MANIFEST_ATTR, MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID, serializeMasterCSSHydrationManifest } from "@master/css-schema/hydration-manifest";
|
|
6
|
+
import { MASTER_CSS_RUNTIME_STYLE_ID } from "@master/css-schema/runtime-style";
|
|
7
|
+
import { Element, Text } from "domhandler";
|
|
8
|
+
import serialize from "dom-serializer";
|
|
9
|
+
//#region src/render.ts
|
|
10
|
+
function createHydrationManifestScript(hydrationManifest) {
|
|
11
|
+
return new Element("script", {
|
|
12
|
+
type: "application/json",
|
|
13
|
+
id: MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID
|
|
14
|
+
}, [new Text(serializeMasterCSSHydrationManifest(hydrationManifest))]);
|
|
15
|
+
}
|
|
16
|
+
function isHydrationManifestScript(node) {
|
|
17
|
+
return node.type === "script" && node.name === "script" && node.attribs.id === MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID;
|
|
18
|
+
}
|
|
19
|
+
function setHydrationManifestScript(element, hydrationManifest) {
|
|
20
|
+
element.attribs.type = "application/json";
|
|
21
|
+
element.attribs.id = MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID;
|
|
22
|
+
element.childNodes = [new Text(serializeMasterCSSHydrationManifest(hydrationManifest))];
|
|
23
|
+
}
|
|
24
|
+
function isExternalHydrationManifestOption(hydrationManifest) {
|
|
25
|
+
return typeof hydrationManifest === "object" && hydrationManifest?.type === "external";
|
|
26
|
+
}
|
|
27
|
+
function getExternalHydrationManifestSource(option, hydrationManifest) {
|
|
28
|
+
const json = serializeMasterCSSHydrationManifest(hydrationManifest);
|
|
29
|
+
return typeof option.src === "function" ? option.src(json, hydrationManifest) : option.src;
|
|
30
|
+
}
|
|
31
|
+
function findHydrationManifestScripts(childNodes, matches = []) {
|
|
32
|
+
for (let index = 0; index < childNodes.length; index++) {
|
|
33
|
+
const node = childNodes[index];
|
|
34
|
+
if (isHydrationManifestScript(node)) matches.push({
|
|
35
|
+
element: node,
|
|
36
|
+
childNodes,
|
|
37
|
+
index
|
|
38
|
+
});
|
|
39
|
+
if ("childNodes" in node) findHydrationManifestScripts(node.childNodes, matches);
|
|
40
|
+
}
|
|
41
|
+
return matches;
|
|
42
|
+
}
|
|
43
|
+
function removeNode(nodes, target) {
|
|
44
|
+
const index = nodes.indexOf(target);
|
|
45
|
+
if (index !== -1) {
|
|
46
|
+
nodes.splice(index, 1);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
for (const node of nodes) if ("childNodes" in node && removeNode(node.childNodes, target)) return true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
function removeHydrationManifestScripts(nodes) {
|
|
53
|
+
const existingScripts = findHydrationManifestScripts(nodes);
|
|
54
|
+
for (let index = existingScripts.length - 1; index >= 0; index--) {
|
|
55
|
+
const script = existingScripts[index];
|
|
56
|
+
script.childNodes.splice(script.index, 1);
|
|
57
|
+
}
|
|
58
|
+
return existingScripts.length > 0;
|
|
59
|
+
}
|
|
60
|
+
function removeExternalHydrationManifest(styleElement) {
|
|
61
|
+
if (styleElement) delete styleElement.attribs[MASTER_CSS_HYDRATION_MANIFEST_ATTR];
|
|
62
|
+
}
|
|
63
|
+
function injectExternalHydrationManifest(nodes, styleElement, src) {
|
|
64
|
+
removeHydrationManifestScripts(nodes);
|
|
65
|
+
if (styleElement) styleElement.attribs[MASTER_CSS_HYDRATION_MANIFEST_ATTR] = src;
|
|
66
|
+
}
|
|
67
|
+
function injectHydrationManifest(nodes, htmlElement, headElement, styleElement, hydrationManifest) {
|
|
68
|
+
removeExternalHydrationManifest(styleElement);
|
|
69
|
+
const existingScripts = findHydrationManifestScripts(nodes);
|
|
70
|
+
if (existingScripts.length) {
|
|
71
|
+
const [firstScript, ...duplicateScripts] = existingScripts;
|
|
72
|
+
setHydrationManifestScript(firstScript.element, hydrationManifest);
|
|
73
|
+
for (let index = duplicateScripts.length - 1; index >= 0; index--) {
|
|
74
|
+
const script = duplicateScripts[index];
|
|
75
|
+
script.childNodes.splice(script.index, 1);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const scriptElement = createHydrationManifestScript(hydrationManifest);
|
|
80
|
+
if (headElement) headElement.childNodes.push(scriptElement);
|
|
81
|
+
else if (htmlElement) {
|
|
82
|
+
headElement = new Element("head", {}, [scriptElement]);
|
|
83
|
+
htmlElement.childNodes.unshift(headElement);
|
|
84
|
+
} else {
|
|
85
|
+
const styleElementIndex = styleElement ? nodes.indexOf(styleElement) : -1;
|
|
86
|
+
if (styleElementIndex === -1) nodes.unshift(scriptElement);
|
|
87
|
+
else nodes.splice(styleElementIndex + 1, 0, scriptElement);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Renders the page-required and sorted CSS text from HTML and injected it back into HTML
|
|
92
|
+
* @param html
|
|
93
|
+
* @param manifest
|
|
94
|
+
*/
|
|
95
|
+
function render(html, manifest, options = {}) {
|
|
96
|
+
const context = parseHTML(html);
|
|
97
|
+
const { classes, nodes, htmlElement } = context;
|
|
98
|
+
let { headElement, styleElement } = context;
|
|
99
|
+
const externalHydrationManifestOption = isExternalHydrationManifestOption(options.hydrationManifest) ? options.hydrationManifest : void 0;
|
|
100
|
+
if (!classes.length && externalHydrationManifestOption) {
|
|
101
|
+
removeHydrationManifestScripts(nodes);
|
|
102
|
+
removeExternalHydrationManifest(styleElement);
|
|
103
|
+
}
|
|
104
|
+
if (!classes.length) return {
|
|
105
|
+
html: externalHydrationManifestOption ? serialize(nodes, {
|
|
106
|
+
decodeEntities: false,
|
|
107
|
+
encodeEntities: false
|
|
108
|
+
}) : html,
|
|
109
|
+
classes,
|
|
110
|
+
nodes,
|
|
111
|
+
htmlElement,
|
|
112
|
+
headElement,
|
|
113
|
+
styleElement
|
|
114
|
+
};
|
|
115
|
+
const css = create_server_css_default(manifest || getDefaultManifest());
|
|
116
|
+
classes.forEach((eachClass) => css.ensureClassRules(eachClass));
|
|
117
|
+
const hydrationManifest = options.hydrationManifest === false ? void 0 : createHydrationManifest(css);
|
|
118
|
+
if (!css.text) {
|
|
119
|
+
if ((options.hydrationManifest === "inject" || externalHydrationManifestOption) && styleElement) {
|
|
120
|
+
removeNode(nodes, styleElement);
|
|
121
|
+
styleElement = null;
|
|
122
|
+
}
|
|
123
|
+
if (externalHydrationManifestOption) removeHydrationManifestScripts(nodes);
|
|
124
|
+
return {
|
|
125
|
+
html: serialize(nodes, {
|
|
126
|
+
decodeEntities: false,
|
|
127
|
+
encodeEntities: false
|
|
128
|
+
}),
|
|
129
|
+
css,
|
|
130
|
+
classes,
|
|
131
|
+
hydrationManifest,
|
|
132
|
+
nodes,
|
|
133
|
+
htmlElement,
|
|
134
|
+
headElement,
|
|
135
|
+
styleElement
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (styleElement) styleElement.childNodes = [new Text(css.text)];
|
|
139
|
+
else {
|
|
140
|
+
styleElement = new Element("style", { id: MASTER_CSS_RUNTIME_STYLE_ID }, [new Text(css.text)]);
|
|
141
|
+
if (headElement) headElement.childNodes.push(styleElement);
|
|
142
|
+
else if (htmlElement) {
|
|
143
|
+
headElement = new Element("head", {}, [styleElement]);
|
|
144
|
+
htmlElement.childNodes.unshift(headElement);
|
|
145
|
+
} else nodes.unshift(styleElement);
|
|
146
|
+
}
|
|
147
|
+
if (options.hydrationManifest === "inject" && hydrationManifest?.rules.length) injectHydrationManifest(nodes, htmlElement, headElement, styleElement, hydrationManifest);
|
|
148
|
+
else if (externalHydrationManifestOption && hydrationManifest?.rules.length) injectExternalHydrationManifest(nodes, styleElement, getExternalHydrationManifestSource(externalHydrationManifestOption, hydrationManifest));
|
|
149
|
+
else removeExternalHydrationManifest(styleElement);
|
|
150
|
+
return {
|
|
151
|
+
html: serialize(nodes, {
|
|
152
|
+
decodeEntities: false,
|
|
153
|
+
encodeEntities: false
|
|
154
|
+
}),
|
|
155
|
+
css,
|
|
156
|
+
classes,
|
|
157
|
+
hydrationManifest,
|
|
158
|
+
nodes,
|
|
159
|
+
htmlElement,
|
|
160
|
+
headElement,
|
|
161
|
+
styleElement
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
export { render as default };
|
package/package.json
CHANGED
|
@@ -1 +1,66 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@master/css-server",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Server-side HTML renderer for Master CSS",
|
|
6
|
+
"author": "Aoyue Design LLC.",
|
|
7
|
+
"funding": "https://rc.css.master.co/sponsor",
|
|
8
|
+
"homepage": "https://css.master.co",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/master-co/css/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/master-co/css.git",
|
|
15
|
+
"directory": "packages/server"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"server",
|
|
19
|
+
"static",
|
|
20
|
+
"html",
|
|
21
|
+
"markup",
|
|
22
|
+
"dom",
|
|
23
|
+
"css",
|
|
24
|
+
"mastercss"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"jsnext:main": "./dist/index.js",
|
|
30
|
+
"esnext": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"dom-serializer": "^3.1.1",
|
|
48
|
+
"domhandler": "^6.0.1",
|
|
49
|
+
"htmlparser2": "^12.0.0",
|
|
50
|
+
"@master/css": "^2.0.0-rc.75",
|
|
51
|
+
"@master/css-validator": "^2.0.0-rc.75",
|
|
52
|
+
"@master/css-schema": "^2.0.0-rc.75",
|
|
53
|
+
"@master/css-preset": "^2.0.0-rc.75"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@master/css-compiler": "^2.0.0-rc.75"
|
|
57
|
+
},
|
|
58
|
+
"version": "2.0.0-rc.75",
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsdown --config ../../shared/tsdown-package.config.ts && node scripts/restore-json-import-attributes.js",
|
|
61
|
+
"dev": "tsdown --config ../../shared/tsdown-package.config.ts --watch",
|
|
62
|
+
"lint": "eslint",
|
|
63
|
+
"type-check": "tsc -b tsconfig.typecheck.json",
|
|
64
|
+
"test": "vitest"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/decode-html.mjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The framework will encode attribute values when rendering HTML to prevent XSS,
|
|
3
|
-
* so we must decode it for correct CSS selectors.
|
|
4
|
-
* https://github.com/facebook/react/issues/27836
|
|
5
|
-
*/ function decodeHTML(html) {
|
|
6
|
-
return html.replace(/&(amp|apos|#39|#x27|quot|lt|gt);/g, (match, entity)=>{
|
|
7
|
-
switch(entity){
|
|
8
|
-
case 'amp':
|
|
9
|
-
return '&';
|
|
10
|
-
case 'apos':
|
|
11
|
-
case '#39':
|
|
12
|
-
case '#x27':
|
|
13
|
-
return '\'';
|
|
14
|
-
case 'quot':
|
|
15
|
-
return '"';
|
|
16
|
-
case 'lt':
|
|
17
|
-
return '<';
|
|
18
|
-
case 'gt':
|
|
19
|
-
return '>';
|
|
20
|
-
default:
|
|
21
|
-
return match // 如果不是已知的實體,保留原樣
|
|
22
|
-
;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export { decodeHTML as default };
|
package/dist/index.mjs
DELETED
package/dist/parse-html.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { DomHandler, Parser } from 'htmlparser2';
|
|
2
|
-
import { MASTER_CSS_RUNTIME_STYLE_ID } from '@master/css-schema/runtime-style';
|
|
3
|
-
import decodeHTML from './decode-html.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param html
|
|
7
|
-
* @returns {classes from html, root element}
|
|
8
|
-
*/ function parseHTML(html) {
|
|
9
|
-
const classes = [];
|
|
10
|
-
let htmlElement = null;
|
|
11
|
-
let headElement = null;
|
|
12
|
-
let styleElement = null;
|
|
13
|
-
const handler = new DomHandler(undefined, {}, (element)=>{
|
|
14
|
-
if (element.type === 'tag') {
|
|
15
|
-
switch(element.name){
|
|
16
|
-
case 'html':
|
|
17
|
-
htmlElement = element;
|
|
18
|
-
break;
|
|
19
|
-
case 'head':
|
|
20
|
-
headElement = element;
|
|
21
|
-
break;
|
|
22
|
-
}
|
|
23
|
-
} else if (element.type === 'style') {
|
|
24
|
-
if (element.attribs.id === MASTER_CSS_RUNTIME_STYLE_ID) styleElement = element;
|
|
25
|
-
}
|
|
26
|
-
if (element.attribs.class) {
|
|
27
|
-
element.attribs.class.trim().split(/\s+/).forEach((className)=>{
|
|
28
|
-
if (!className) return;
|
|
29
|
-
className = decodeHTML(className);
|
|
30
|
-
if (!classes.includes(className)) classes.push(className);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
new Parser(handler, {
|
|
35
|
-
decodeEntities: false
|
|
36
|
-
}).end(html);
|
|
37
|
-
return {
|
|
38
|
-
classes,
|
|
39
|
-
nodes: handler.root.childNodes,
|
|
40
|
-
htmlElement,
|
|
41
|
-
headElement,
|
|
42
|
-
styleElement
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { parseHTML as default };
|
package/dist/render-css.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import parseHTML from './parse-html.mjs';
|
|
2
|
-
import getDefaultManifest from './default-manifest.mjs';
|
|
3
|
-
import { createCSSWithNativeDeclarations } from '@master/css-validator';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Renders the Master CSS string from HTML
|
|
7
|
-
* @param html
|
|
8
|
-
* @param manifest
|
|
9
|
-
* @returns MasterCSS
|
|
10
|
-
*/ function renderCSS(html, manifest) {
|
|
11
|
-
if (!html) return;
|
|
12
|
-
const { classes } = parseHTML(html);
|
|
13
|
-
if (!classes.length) return;
|
|
14
|
-
const css = createCSSWithNativeDeclarations(manifest || getDefaultManifest());
|
|
15
|
-
classes.forEach((eachClass)=>css.add(eachClass));
|
|
16
|
-
return css;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { renderCSS as default };
|
package/dist/render.mjs
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { createHydrationManifest } from '@master/css';
|
|
2
|
-
import { MASTER_CSS_HYDRATION_MANIFEST_ATTR, serializeMasterCSSHydrationManifest, MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID } from '@master/css-schema/hydration-manifest';
|
|
3
|
-
import { MASTER_CSS_RUNTIME_STYLE_ID } from '@master/css-schema/runtime-style';
|
|
4
|
-
import parseHTML from './parse-html.mjs';
|
|
5
|
-
import getDefaultManifest from './default-manifest.mjs';
|
|
6
|
-
import { createCSSWithNativeDeclarations } from '@master/css-validator';
|
|
7
|
-
import { Text, Element } from 'domhandler';
|
|
8
|
-
import serialize from 'dom-serializer';
|
|
9
|
-
|
|
10
|
-
function createHydrationManifestScript(hydrationManifest) {
|
|
11
|
-
return new Element('script', {
|
|
12
|
-
type: 'application/json',
|
|
13
|
-
id: MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID
|
|
14
|
-
}, [
|
|
15
|
-
new Text(serializeMasterCSSHydrationManifest(hydrationManifest))
|
|
16
|
-
]);
|
|
17
|
-
}
|
|
18
|
-
function isHydrationManifestScript(node) {
|
|
19
|
-
return node.type === 'script' && node.name === 'script' && node.attribs.id === MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID;
|
|
20
|
-
}
|
|
21
|
-
function setHydrationManifestScript(element, hydrationManifest) {
|
|
22
|
-
element.attribs.type = 'application/json';
|
|
23
|
-
element.attribs.id = MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID;
|
|
24
|
-
element.childNodes = [
|
|
25
|
-
new Text(serializeMasterCSSHydrationManifest(hydrationManifest))
|
|
26
|
-
];
|
|
27
|
-
}
|
|
28
|
-
function isExternalHydrationManifestOption(hydrationManifest) {
|
|
29
|
-
return typeof hydrationManifest === 'object' && hydrationManifest?.type === 'external';
|
|
30
|
-
}
|
|
31
|
-
function getExternalHydrationManifestSource(option, hydrationManifest) {
|
|
32
|
-
const json = serializeMasterCSSHydrationManifest(hydrationManifest);
|
|
33
|
-
return typeof option.src === 'function' ? option.src(json, hydrationManifest) : option.src;
|
|
34
|
-
}
|
|
35
|
-
function findHydrationManifestScripts(childNodes, matches = []) {
|
|
36
|
-
for(let index = 0; index < childNodes.length; index++){
|
|
37
|
-
const node = childNodes[index];
|
|
38
|
-
if (isHydrationManifestScript(node)) {
|
|
39
|
-
matches.push({
|
|
40
|
-
element: node,
|
|
41
|
-
childNodes,
|
|
42
|
-
index
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
if ('childNodes' in node) {
|
|
46
|
-
findHydrationManifestScripts(node.childNodes, matches);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return matches;
|
|
50
|
-
}
|
|
51
|
-
function removeNode(nodes, target) {
|
|
52
|
-
const index = nodes.indexOf(target);
|
|
53
|
-
if (index !== -1) {
|
|
54
|
-
nodes.splice(index, 1);
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
for (const node of nodes){
|
|
58
|
-
if ('childNodes' in node && removeNode(node.childNodes, target)) {
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
function removeHydrationManifestScripts(nodes) {
|
|
65
|
-
const existingScripts = findHydrationManifestScripts(nodes);
|
|
66
|
-
for(let index = existingScripts.length - 1; index >= 0; index--){
|
|
67
|
-
const script = existingScripts[index];
|
|
68
|
-
script.childNodes.splice(script.index, 1);
|
|
69
|
-
}
|
|
70
|
-
return existingScripts.length > 0;
|
|
71
|
-
}
|
|
72
|
-
function removeExternalHydrationManifest(styleElement) {
|
|
73
|
-
if (styleElement) {
|
|
74
|
-
delete styleElement.attribs[MASTER_CSS_HYDRATION_MANIFEST_ATTR];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
function injectExternalHydrationManifest(nodes, styleElement, src) {
|
|
78
|
-
removeHydrationManifestScripts(nodes);
|
|
79
|
-
if (styleElement) {
|
|
80
|
-
styleElement.attribs[MASTER_CSS_HYDRATION_MANIFEST_ATTR] = src;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function injectHydrationManifest(nodes, htmlElement, headElement, styleElement, hydrationManifest) {
|
|
84
|
-
removeExternalHydrationManifest(styleElement);
|
|
85
|
-
const existingScripts = findHydrationManifestScripts(nodes);
|
|
86
|
-
if (existingScripts.length) {
|
|
87
|
-
const [firstScript, ...duplicateScripts] = existingScripts;
|
|
88
|
-
setHydrationManifestScript(firstScript.element, hydrationManifest);
|
|
89
|
-
for(let index = duplicateScripts.length - 1; index >= 0; index--){
|
|
90
|
-
const script = duplicateScripts[index];
|
|
91
|
-
script.childNodes.splice(script.index, 1);
|
|
92
|
-
}
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
const scriptElement = createHydrationManifestScript(hydrationManifest);
|
|
96
|
-
if (headElement) {
|
|
97
|
-
headElement.childNodes.push(scriptElement);
|
|
98
|
-
} else if (htmlElement) {
|
|
99
|
-
headElement = new Element('head', {}, [
|
|
100
|
-
scriptElement
|
|
101
|
-
]);
|
|
102
|
-
htmlElement.childNodes.unshift(headElement);
|
|
103
|
-
} else {
|
|
104
|
-
const styleElementIndex = styleElement ? nodes.indexOf(styleElement) : -1;
|
|
105
|
-
if (styleElementIndex === -1) {
|
|
106
|
-
nodes.unshift(scriptElement);
|
|
107
|
-
} else {
|
|
108
|
-
nodes.splice(styleElementIndex + 1, 0, scriptElement);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Renders the page-required and sorted CSS text from HTML and injected it back into HTML
|
|
114
|
-
* @param html
|
|
115
|
-
* @param manifest
|
|
116
|
-
*/ function render(html, manifest, options = {}) {
|
|
117
|
-
const context = parseHTML(html);
|
|
118
|
-
const { classes, nodes, htmlElement } = context;
|
|
119
|
-
let { headElement, styleElement } = context;
|
|
120
|
-
const externalHydrationManifestOption = isExternalHydrationManifestOption(options.hydrationManifest) ? options.hydrationManifest : undefined;
|
|
121
|
-
if (!classes.length && externalHydrationManifestOption) {
|
|
122
|
-
removeHydrationManifestScripts(nodes);
|
|
123
|
-
removeExternalHydrationManifest(styleElement);
|
|
124
|
-
}
|
|
125
|
-
if (!classes.length) return {
|
|
126
|
-
html: externalHydrationManifestOption ? serialize(nodes, {
|
|
127
|
-
decodeEntities: false,
|
|
128
|
-
encodeEntities: false
|
|
129
|
-
}) : html,
|
|
130
|
-
classes,
|
|
131
|
-
nodes,
|
|
132
|
-
htmlElement,
|
|
133
|
-
headElement,
|
|
134
|
-
styleElement
|
|
135
|
-
};
|
|
136
|
-
const css = createCSSWithNativeDeclarations(manifest || getDefaultManifest());
|
|
137
|
-
classes.forEach((eachClass)=>css.add(eachClass));
|
|
138
|
-
const hydrationManifest = options.hydrationManifest === false ? undefined : createHydrationManifest(css);
|
|
139
|
-
if (!css.text) {
|
|
140
|
-
if ((options.hydrationManifest === 'inject' || externalHydrationManifestOption) && styleElement) {
|
|
141
|
-
removeNode(nodes, styleElement);
|
|
142
|
-
styleElement = null;
|
|
143
|
-
}
|
|
144
|
-
if (externalHydrationManifestOption) {
|
|
145
|
-
removeHydrationManifestScripts(nodes);
|
|
146
|
-
}
|
|
147
|
-
return {
|
|
148
|
-
html: serialize(nodes, {
|
|
149
|
-
decodeEntities: false,
|
|
150
|
-
encodeEntities: false
|
|
151
|
-
}),
|
|
152
|
-
css,
|
|
153
|
-
classes,
|
|
154
|
-
hydrationManifest,
|
|
155
|
-
nodes,
|
|
156
|
-
htmlElement,
|
|
157
|
-
headElement,
|
|
158
|
-
styleElement
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
if (styleElement) {
|
|
162
|
-
styleElement.childNodes = [
|
|
163
|
-
new Text(css.text)
|
|
164
|
-
];
|
|
165
|
-
} else {
|
|
166
|
-
styleElement = new Element('style', {
|
|
167
|
-
id: MASTER_CSS_RUNTIME_STYLE_ID
|
|
168
|
-
}, [
|
|
169
|
-
new Text(css.text)
|
|
170
|
-
]);
|
|
171
|
-
if (headElement) {
|
|
172
|
-
headElement.childNodes.push(styleElement);
|
|
173
|
-
} else {
|
|
174
|
-
if (htmlElement) {
|
|
175
|
-
headElement = new Element('head', {}, [
|
|
176
|
-
styleElement
|
|
177
|
-
]);
|
|
178
|
-
htmlElement.childNodes.unshift(headElement);
|
|
179
|
-
} else {
|
|
180
|
-
nodes.unshift(styleElement);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
if (options.hydrationManifest === 'inject' && hydrationManifest?.rules.length) {
|
|
185
|
-
injectHydrationManifest(nodes, htmlElement, headElement, styleElement, hydrationManifest);
|
|
186
|
-
} else if (externalHydrationManifestOption && hydrationManifest?.rules.length) {
|
|
187
|
-
injectExternalHydrationManifest(nodes, styleElement, getExternalHydrationManifestSource(externalHydrationManifestOption, hydrationManifest));
|
|
188
|
-
} else {
|
|
189
|
-
removeExternalHydrationManifest(styleElement);
|
|
190
|
-
}
|
|
191
|
-
return {
|
|
192
|
-
html: serialize(nodes, {
|
|
193
|
-
decodeEntities: false,
|
|
194
|
-
encodeEntities: false
|
|
195
|
-
}),
|
|
196
|
-
css,
|
|
197
|
-
classes,
|
|
198
|
-
hydrationManifest,
|
|
199
|
-
nodes,
|
|
200
|
-
htmlElement,
|
|
201
|
-
headElement,
|
|
202
|
-
styleElement
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export { render as default };
|