@master/css-server 2.0.0-rc.8 → 2.0.0-rc.80

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 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">Generate Master CSS text from markup on the server</p>
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/1aron/repo/actions/workflows/release.yml">
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
- ## Documentation
56
- Check out the official [documentation](https://rc.css.master.co/docs/server).
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.
@@ -0,0 +1,2 @@
1
+ import { createCSSWithNativeDeclarations } from "@master/css-validator";
2
+ export { createCSSWithNativeDeclarations as default };
@@ -0,0 +1,5 @@
1
+ import { createCSSWithNativeDeclarations } from "@master/css-validator";
2
+ //#region src/create-server-css.ts
3
+ var create_server_css_default = createCSSWithNativeDeclarations;
4
+ //#endregion
5
+ export { create_server_css_default as default };
@@ -1 +1,4 @@
1
- export default function decodeHTML(html: string): string;
1
+ //#region src/decode-html.d.ts
2
+ declare function decodeHTML(html: string): string;
3
+ //#endregion
4
+ export { decodeHTML as default };
@@ -1 +1,17 @@
1
- "use strict";var u=Object.defineProperty;var n=Object.getOwnPropertyDescriptor;var o=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var l=(t,e)=>{for(var r in e)u(t,r,{get:e[r],enumerable:!0})},d=(t,e,r,c)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of o(e))!p.call(t,a)&&a!==r&&u(t,a,{get:()=>e[a],enumerable:!(c=n(e,a))||c.enumerable});return t};var g=t=>d(u({},"__esModule",{value:!0}),t);var f={};l(f,{default:()=>s});module.exports=g(f);function s(t){return t.replace(/&(amp|apos|#39|#x27|quot|lt|gt);/g,(e,r)=>{switch(r){case"amp":return"&";case"apos":case"#39":case"#x27":return"'";case"quot":return'"';case"lt":return"<";case"gt":return">";default:return e}})}
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,6 @@
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,3 +1,5 @@
1
- export { default as render } from './render';
2
- export { default as renderCSS } from './render-css';
3
- export { default as parseHTML } from './parse-html';
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
- "use strict";var u=Object.create;var o=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var n=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,C=Object.prototype.hasOwnProperty;var H=(e,r)=>{for(var a in r)o(e,a,{get:r[a],enumerable:!0})},p=(e,r,a,d)=>{if(r&&typeof r=="object"||typeof r=="function")for(let f of n(r))!C.call(e,f)&&f!==a&&o(e,f,{get:()=>r[f],enumerable:!(d=x(r,f))||d.enumerable});return e};var t=(e,r,a)=>(a=e!=null?u(S(e)):{},p(r||!e||!e.__esModule?o(a,"default",{value:e,enumerable:!0}):a,e)),L=e=>p(o({},"__esModule",{value:!0}),e);var M={};H(M,{parseHTML:()=>m.default,render:()=>s.default,renderCSS:()=>l.default});module.exports=L(M);var s=t(require("./render")),l=t(require("./render-css")),m=t(require("./parse-html"));0&&(module.exports={parseHTML,render,renderCSS});
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 };
@@ -1,12 +1,16 @@
1
- import type { ChildNode, Element } from 'domhandler';
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
- export default function parseHTML(html: string): {
7
- classes: string[];
8
- nodes: ChildNode[];
9
- htmlElement: Element | null;
10
- headElement: Element | null;
11
- styleElement: Element | null;
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 };
@@ -1 +1,43 @@
1
- "use strict";var f=Object.create;var i=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var p=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty;var y=(e,l)=>{for(var t in l)i(e,t,{get:l[t],enumerable:!0})},u=(e,l,t,r)=>{if(l&&typeof l=="object"||typeof l=="function")for(let s of c(l))!b.call(e,s)&&s!==t&&i(e,s,{get:()=>l[s],enumerable:!(r=h(l,s))||r.enumerable});return e};var g=(e,l,t)=>(t=e!=null?f(p(e)):{},u(l||!e||!e.__esModule?i(t,"default",{value:e,enumerable:!0}):t,e)),k=e=>u(i({},"__esModule",{value:!0}),e);var w={};y(w,{default:()=>E});module.exports=k(w);var o=require("htmlparser2"),m=g(require("./decode-html"));function E(e){const l=[];let t=null,r=null,s=null;const a=new o.DomHandler(void 0,{},n=>{if(n.type==="tag")switch(n.name){case"html":t=n;break;case"head":r=n;break;default:break}else n.type==="style"&&n.attribs.id==="master"&&(s=n);n.attribs.class&&n.attribs.class.split(" ").forEach(d=>{d=(0,m.default)(d),l.includes(d)||l.push(d)})});return new o.Parser(a,{decodeEntities:!1}).end(e),{classes:l,nodes:a.root.childNodes,htmlElement:t,headElement:r,styleElement:s}}
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 };
@@ -1,8 +1,13 @@
1
- import MasterCSS, { Config } from '@master/css';
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 config
8
+ * @param manifest
6
9
  * @returns MasterCSS
7
10
  */
8
- export default function renderCSS(html: string, config?: Config): MasterCSS | undefined;
11
+ declare function renderCSS(html: string, manifest?: MasterCSSManifest): MasterCSS | undefined;
12
+ //#endregion
13
+ export { renderCSS as default };
@@ -1 +1,20 @@
1
- "use strict";var u=Object.create;var s=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var g=Object.getPrototypeOf,p=Object.prototype.hasOwnProperty;var m=(r,e)=>{for(var n in e)s(r,n,{get:e[n],enumerable:!0})},f=(r,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of C(e))!p.call(r,t)&&t!==n&&s(r,t,{get:()=>e[t],enumerable:!(o=S(e,t))||o.enumerable});return r};var i=(r,e,n)=>(n=r!=null?u(g(r)):{},f(e||!r||!r.__esModule?s(n,"default",{value:r,enumerable:!0}):n,r)),M=r=>f(s({},"__esModule",{value:!0}),r);var l={};m(l,{default:()=>d});module.exports=M(l);var a=i(require("./parse-html")),c=i(require("@master/css"));function d(r,e){if(!r)return;const{classes:n}=(0,a.default)(r);if(!n.length)return;const o=new c.default(e);return n.forEach(t=>o.add(t)),o}
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 MasterCSS, { Config } from '@master/css';
2
- import { Element, ChildNode } from 'domhandler';
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 config
29
+ * @param manifest
7
30
  */
8
- export default function render(html: string, config?: Config): {
9
- html: string;
10
- css?: MasterCSS;
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
- "use strict";var C=Object.create;var f=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var y=(e,t)=>{for(var n in t)f(e,n,{get:t[n],enumerable:!0})},a=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of x(t))!S.call(e,s)&&s!==n&&f(e,s,{get:()=>t[s],enumerable:!(i=w(t,s))||i.enumerable});return e};var h=(e,t,n)=>(n=e!=null?C(N(e)):{},a(t||!e||!e.__esModule?f(n,"default",{value:e,enumerable:!0}):n,e)),M=e=>a(f({},"__esModule",{value:!0}),e);var T={};y(T,{default:()=>p});module.exports=M(T);var c=h(require("@master/css")),E=h(require("./parse-html")),r=require("domhandler"),u=h(require("dom-serializer"));function p(e,t){const n=(0,E.default)(e),{classes:i,nodes:s,htmlElement:d}=n;let{headElement:o,styleElement:l}=n;if(!i.length)return{html:e,classes:i,nodes:s,htmlElement:d,headElement:o,styleElement:l};const m=new c.default(t);return i.forEach(g=>m.add(g)),l?l.childNodes=[new r.Text(m.text)]:(l=new r.Element("style",{id:"master"},[new r.Text(m.text)]),o?o.childNodes.push(l):d?(o=new r.Element("head",{},[l]),d.childNodes.unshift(o)):s.unshift(l)),{html:(0,u.default)(s,{decodeEntities:!1,encodeEntities:!1}),css:m,classes:i,nodes:s,htmlElement:d,headElement:o,styleElement:l}}
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
- {"name":"@master/css-server","scripts":{"build":"techor pack \"./src/**/*.ts\" --platform node --declare","dev":"pnpm run build --watch","lint":"eslint ./src","type-check":"tsc --noEmit","test":"jest"},"license":"MIT","description":"Generate Master CSS text from markup on the server","author":"Aoyue Design LLC.","funding":"https://css.master.co/docs/donate","homepage":"https://css.master.co","bugs":{"url":"https://github.com/master-co/css/issues"},"repository":{"type":"git","url":"https://github.com/master-co/css.git","directory":"packages/server"},"keywords":["server","static","html","markup","dom","css","mastercss"],"sideEffects":false,"main":"./dist/index.js","module":"./dist/index.mjs","jsnext:main":"./dist/index.mjs","esnext":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.js","types":"./dist/index.d.ts"}},"files":["dist"],"publishConfig":{"access":"public"},"dependencies":{"@master/css":"^2.0.0-rc.8","htmlparser2":"^9.0.0"},"version":"2.0.0-rc.8"}
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.80",
51
+ "@master/css-preset": "^2.0.0-rc.80",
52
+ "@master/css-schema": "^2.0.0-rc.80",
53
+ "@master/css-validator": "^2.0.0-rc.80"
54
+ },
55
+ "devDependencies": {
56
+ "@master/css-compiler": "^2.0.0-rc.80"
57
+ },
58
+ "version": "2.0.0-rc.80",
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
+ }
@@ -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};
@@ -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};
@@ -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};