@master/css-server 2.0.0-rc.8 → 2.0.0-rc.81
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 +49 -7
- package/dist/create-server-css.d.ts +2 -0
- package/dist/create-server-css.js +5 -0
- package/dist/decode-html.d.ts +4 -1
- package/dist/decode-html.js +17 -1
- package/dist/default-manifest.d.ts +6 -0
- package/dist/default-manifest.js +8 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -1
- package/dist/parse-html.d.ts +11 -7
- package/dist/parse-html.js +43 -1
- package/dist/render-css.d.ts +8 -3
- package/dist/render-css.js +20 -1
- package/dist/render.d.ts +29 -12
- package/dist/render.js +165 -1
- package/package.json +66 -1
- package/dist/decode-html.mjs +0 -1
- package/dist/index.mjs +0 -1
- package/dist/parse-html.mjs +0 -1
- package/dist/render-css.mjs +0 -1
- package/dist/render.mjs +0 -1
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">
|
|
@@ -41,16 +41,58 @@
|
|
|
41
41
|
<img alt="Follow @mastercorg" src="https://img.shields.io/static/v1?label=%20&message=twitter&color=f6f7f8&logo=twitter&style=for-the-badge">
|
|
42
42
|
</picture>
|
|
43
43
|
</a>
|
|
44
|
-
<a aria-label="Github Actions" href="https://github.com/
|
|
44
|
+
<a aria-label="Github Actions" href="https://github.com/master-co/css/actions/workflows/ci-release.yml">
|
|
45
45
|
<picture>
|
|
46
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/actions/workflow/status/master-co/css/release.yml?branch=rc&label=%20&message=twitter&color=212022&logo=githubactions&style=for-the-badge">
|
|
47
|
-
<source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/github/actions/workflow/status/master-co/css/release.yml?branch=rc&label=%20&message=twitter&color=f6f7f8&logo=githubactions&style=for-the-badge&logoColor=%23000">
|
|
48
|
-
<img alt="Github release actions" src="https://img.shields.io/github/actions/workflow/status/master-co/css/release.yml?branch=rc&label=%20&message=twitter&color=f6f7f8&logo=githubactions&style=for-the-badge&logoColor=%23000">
|
|
46
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://img.shields.io/github/actions/workflow/status/master-co/css/ci-release.yml?branch=rc&label=%20&message=twitter&color=212022&logo=githubactions&style=for-the-badge">
|
|
47
|
+
<source media="(prefers-color-scheme: light)" srcset="https://img.shields.io/github/actions/workflow/status/master-co/css/ci-release.yml?branch=rc&label=%20&message=twitter&color=f6f7f8&logo=githubactions&style=for-the-badge&logoColor=%23000">
|
|
48
|
+
<img alt="Github release actions" src="https://img.shields.io/github/actions/workflow/status/master-co/css/ci-release.yml?branch=rc&label=%20&message=twitter&color=f6f7f8&logo=githubactions&style=for-the-badge&logoColor=%23000">
|
|
49
49
|
</picture>
|
|
50
50
|
</a>
|
|
51
51
|
</p>
|
|
52
52
|
|
|
53
53
|
</div>
|
|
54
54
|
|
|
55
|
-
##
|
|
56
|
-
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install @master/css-server
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`@master/css-server` renders required Master CSS from HTML. It parses HTML, extracts class names, creates a `MasterCSS` instance, and injects or updates `<style id="master-css">`.
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
### `render()`
|
|
66
|
+
|
|
67
|
+
Render HTML and inject generated CSS.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { render } from '@master/css-server'
|
|
71
|
+
|
|
72
|
+
const result = render('<div class="text:center"></div>', manifest)
|
|
73
|
+
|
|
74
|
+
console.log(result.html)
|
|
75
|
+
console.log(result.css.text)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `renderCSS()`
|
|
79
|
+
|
|
80
|
+
Render only the CSS needed by an HTML string.
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { renderCSS } from '@master/css-server'
|
|
84
|
+
|
|
85
|
+
const css = renderCSS('<div class="text:center"></div>', manifest)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### `parseHTML()`
|
|
89
|
+
|
|
90
|
+
Parse HTML into the lightweight DOM representation used by the server renderer.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { parseHTML } from '@master/css-server'
|
|
94
|
+
|
|
95
|
+
const document = parseHTML('<html><head></head><body></body></html>')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Use official framework integrations when possible; they compose this package with manifest loading, source scanning, and runtime hydration.
|
package/dist/decode-html.d.ts
CHANGED
package/dist/decode-html.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
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 };
|
|
@@ -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,3 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
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 };
|
package/dist/parse-html.js
CHANGED
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
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,8 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MasterCSS } from "@master/css";
|
|
2
|
+
import { MasterCSSManifest } from "@master/css-schema/manifest";
|
|
3
|
+
|
|
4
|
+
//#region src/render-css.d.ts
|
|
2
5
|
/**
|
|
3
6
|
* Renders the Master CSS string from HTML
|
|
4
7
|
* @param html
|
|
5
|
-
* @param
|
|
8
|
+
* @param manifest
|
|
6
9
|
* @returns MasterCSS
|
|
7
10
|
*/
|
|
8
|
-
|
|
11
|
+
declare function renderCSS(html: string, manifest?: MasterCSSManifest): MasterCSS | undefined;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { renderCSS as default };
|
package/dist/render-css.js
CHANGED
|
@@ -1 +1,20 @@
|
|
|
1
|
-
|
|
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,16 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
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;
|
|
11
|
+
}
|
|
12
|
+
type RenderHydrationManifestOption = 'return' | 'inject' | false | RenderExternalHydrationManifestOption;
|
|
13
|
+
interface RenderOptions {
|
|
14
|
+
hydrationManifest?: RenderHydrationManifestOption;
|
|
15
|
+
}
|
|
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;
|
|
25
|
+
}
|
|
3
26
|
/**
|
|
4
27
|
* Renders the page-required and sorted CSS text from HTML and injected it back into HTML
|
|
5
28
|
* @param html
|
|
6
|
-
* @param
|
|
29
|
+
* @param manifest
|
|
7
30
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
classes: string[];
|
|
12
|
-
nodes: ChildNode[];
|
|
13
|
-
htmlElement: Element | null;
|
|
14
|
-
headElement: Element | null;
|
|
15
|
-
styleElement: Element | null;
|
|
16
|
-
};
|
|
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
CHANGED
|
@@ -1 +1,165 @@
|
|
|
1
|
-
|
|
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.81",
|
|
51
|
+
"@master/css-preset": "^2.0.0-rc.81",
|
|
52
|
+
"@master/css-schema": "^2.0.0-rc.81",
|
|
53
|
+
"@master/css-validator": "^2.0.0-rc.81"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@master/css-compiler": "^2.0.0-rc.81"
|
|
57
|
+
},
|
|
58
|
+
"version": "2.0.0-rc.81",
|
|
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 +0,0 @@
|
|
|
1
|
-
function a(e){return e.replace(/&(amp|apos|#39|#x27|quot|lt|gt);/g,(t,r)=>{switch(r){case"amp":return"&";case"apos":case"#39":case"#x27":return"'";case"quot":return'"';case"lt":return"<";case"gt":return">";default:return t}})}export{a as default};
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{default as a}from"./render.mjs";import{default as o}from"./render-css.mjs";import{default as d}from"./parse-html.mjs";export{d as parseHTML,a as render,o as renderCSS};
|
package/dist/parse-html.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{DomHandler as o,Parser as a}from"htmlparser2";import u from"./decode-html.mjs";function m(i){const t=[];let n=null,s=null,r=null;const d=new o(void 0,{},e=>{if(e.type==="tag")switch(e.name){case"html":n=e;break;case"head":s=e;break;default:break}else e.type==="style"&&e.attribs.id==="master"&&(r=e);e.attribs.class&&e.attribs.class.split(" ").forEach(l=>{l=u(l),t.includes(l)||t.push(l)})});return new a(d,{decodeEntities:!1}).end(i),{classes:t,nodes:d.root.childNodes,htmlElement:n,headElement:s,styleElement:r}}export{m as default};
|
package/dist/render-css.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import s from"./parse-html.mjs";import f from"@master/css";function i(r,t){if(!r)return;const{classes:e}=s(r);if(!e.length)return;const n=new f(t);return e.forEach(o=>n.add(o)),n}export{i as default};
|
package/dist/render.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import a from"@master/css";import c from"./parse-html.mjs";import{Element as d,Text as m}from"domhandler";import E from"dom-serializer";function u(o,f){const r=c(o),{classes:n,nodes:s,htmlElement:l}=r;let{headElement:t,styleElement:e}=r;if(!n.length)return{html:o,classes:n,nodes:s,htmlElement:l,headElement:t,styleElement:e};const i=new a(f);return n.forEach(h=>i.add(h)),e?e.childNodes=[new m(i.text)]:(e=new d("style",{id:"master"},[new m(i.text)]),t?t.childNodes.push(e):l?(t=new d("head",{},[e]),l.childNodes.unshift(t)):s.unshift(e)),{html:E(s,{decodeEntities:!1,encodeEntities:!1}),css:i,classes:n,nodes:s,htmlElement:l,headElement:t,styleElement:e}}export{u as default};
|