epiphany/node_modules/postcss-logical/dist/index.cjs
2023-12-09 22:48:07 -08:00

1 line
12 KiB
JavaScript

"use strict";var r,e,n=require("postcss-value-parser");!function(r){r.Block="block",r.Inline="inline"}(r||(r={})),function(r){r.Start="start",r.End="end"}(e||(e={}));const o={BlockStart:"block-start",BlockEnd:"block-end",InlineStart:"inline-start",InlineEnd:"inline-end"};var t,i;function directionFlowToAxes(r){switch(r){case t.TopToBottom:return[i.Top,i.Bottom];case t.BottomToTop:return[i.Bottom,i.Top];case t.RightToLeft:return[i.Right,i.Left];case t.LeftToRight:return[i.Left,i.Right]}}function cloneDeclaration(r,e,n){return r.parent&&r.parent.some((r=>"decl"==r.type&&r.prop===n&&r.value===e))?[]:[r.clone({value:e,prop:n})]}function transformTransition(r,e,o){const{prop:t,value:i}=r,l=n(i),a=[];let s=[];l.nodes.forEach((r=>{if("div"===r.type&&","===r.value)return a.push(s),void(s=[]);s.push(r)})),a.push(s);for(let r=0;r<a.length;r++){const n=a[r];for(let t=0;t<n.length;t++){const i=n[t];if("word"!==i.type)continue;const l=i.value.toLowerCase(),s=o[l];if(!s)continue;const d=s(e.decl({prop:l,value:"initial"}));if(0!==d.length){for(let e=d.length-1;e>=0;e--){const o=JSON.parse(JSON.stringify(n));o[t].value=d[e].prop,a.splice(r+1,0,o)}a.splice(r,1),r++}}}const d=[];for(let r=0;r<a.length;r++){const e=a[r];d.push(...e),r!==a.length-1&&d.push({type:"div",value:",",sourceIndex:0,sourceEndIndex:0,before:"",after:" "})}const c=n.stringify(d);return c!==i?cloneDeclaration(r,c,t):[]}function parseValueCouple(r){const e=n(r.value).nodes.filter((r=>"space"!==r.type));if(e.length>2||0===e.length){const n=`[postcss-logical] Invalid number of values for ${r.prop}. Found ${e.length} values, expected 1 or 2.`;throw r.error(n)}let o,t;return 1===e.length?(o=n.stringify(e[0]),t=o):(o=n.stringify(e[0]),t=n.stringify(e[1])),[o,t]}function transformBorder(r,e){return n=>cloneDeclaration(n,n.value,`border-${e}-${r}`)}function transformBorderProperty(r,e){return n=>{const[o,t]=e,[i,l]=parseValueCouple(n);return[...cloneDeclaration(n,i,`border-${o}-${r}`),...cloneDeclaration(n,l,`border-${t}-${r}`)]}}function transformBorderShorthand(r){return e=>r.flatMap((r=>cloneDeclaration(e,e.value,`border-${r}`)))}function transformBorderRadius(r){return e=>{let n;switch(e.prop.toLowerCase()){case"border-start-start-radius":n=`border-${r.inlineIsHorizontal?`${r.block[0]}-${r.inline[0]}`:`${r.inline[0]}-${r.block[0]}`}-radius`;break;case"border-start-end-radius":n=`border-${r.inlineIsHorizontal?`${r.block[0]}-${r.inline[1]}`:`${r.inline[1]}-${r.block[0]}`}-radius`;break;case"border-end-start-radius":n=`border-${r.inlineIsHorizontal?`${r.block[1]}-${r.inline[0]}`:`${r.inline[0]}-${r.block[1]}`}-radius`;break;case"border-end-end-radius":n=`border-${r.inlineIsHorizontal?`${r.block[1]}-${r.inline[1]}`:`${r.inline[1]}-${r.block[1]}`}-radius`;break}return n?cloneDeclaration(e,e.value,n):[]}}function transformLogicalSize(r){return e=>{const{value:n}=e,o=r.inlineIsHorizontal?"width":"height",t=r.inlineIsHorizontal?"height":"width",i=e.prop.toLowerCase().replace("inline-size",o).replace("block-size",t);return cloneDeclaration(e,n,i)}}function transformOffset(r){return e=>cloneDeclaration(e,e.value,r)}function transformOffsetShorthand(r){return e=>{const[n,o]=r,[t,i]=parseValueCouple(e);return[...cloneDeclaration(e,t,n),...cloneDeclaration(e,i,o)]}}function transformSide(r,e){return n=>cloneDeclaration(n,n.value,`${r}-${e}`)}function transformSideShorthand(r,e){return n=>{const[o,t]=e,[i,l]=parseValueCouple(n);return[...cloneDeclaration(n,i,`${r}-${o}`),...cloneDeclaration(n,l,`${r}-${t}`)]}}function logicalToPhysical(r,e){const[n,t]=e.block,[i,l]=e.inline;switch(r){case o.BlockStart:return n;case o.BlockEnd:return t;case o.InlineStart:return i;case o.InlineEnd:return l;default:throw new Error("Unsupported logical direction")}}function doTransform(r,e,o){const{prop:t,value:i}=r,l=n(i);l.nodes.forEach((r=>{if("word"===r.type){const n=r.value.toLowerCase();e.includes(n)&&(r.value=logicalToPhysical(n,o))}}));const a=l.toString();return a!==i?cloneDeclaration(r,a,t):[]}function prepareTransforms(r,t,i,l,a){return{"caption-side":(d=r,r=>doTransform(r,Object.values(o),d)),"text-align":r.inlineIsHorizontal?(s=r.inline,r=>{const{prop:o,value:t}=r,i=n(t),[l,a]=s;i.nodes.forEach((r=>{if("word"===r.type){const n=r.value.toLowerCase();if(n===e.End)return void(r.value=a);n===e.Start&&(r.value=l)}}));const d=i.toString();return d!==t?cloneDeclaration(r,d,o):[]}):null,"block-size":transformLogicalSize(r),"inline-size":transformLogicalSize(r),"min-block-size":transformLogicalSize(r),"max-block-size":transformLogicalSize(r),"min-inline-size":transformLogicalSize(r),"max-inline-size":transformLogicalSize(r),"margin-block-start":transformSide("margin",t),"margin-block-end":transformSide("margin",i),"margin-inline-start":transformSide("margin",l),"margin-inline-end":transformSide("margin",a),"margin-block":transformSideShorthand("margin",r.block),"margin-inline":transformSideShorthand("margin",r.inline),"inset-block":transformOffsetShorthand(r.block),"inset-block-start":transformOffset(t),"inset-block-end":transformOffset(i),"inset-inline":transformOffsetShorthand(r.inline),"inset-inline-start":transformOffset(l),"inset-inline-end":transformOffset(a),inset:r=>{const e=n(r.value).nodes.filter((r=>"space"!==r.type&&"comment"!==r.type));if(e.length>4){const n=`[postcss-logical] Invalid number of values for ${r.prop}. Found ${e.length} values, expected up to 4 values.`;throw r.error(n)}let o,t,i,l;if(1===e.length)o=n.stringify(e[0]),t=o,i=o,l=o;else if(2===e.length)o=n.stringify(e[0]),t=n.stringify(e[1]),i=o,l=t;else if(3===e.length)o=n.stringify(e[0]),t=n.stringify(e[1]),l=t,i=n.stringify(e[2]);else{if(4!==e.length)return[];o=n.stringify(e[0]),t=n.stringify(e[1]),i=n.stringify(e[2]),l=n.stringify(e[3])}return[...cloneDeclaration(r,o,"top"),...cloneDeclaration(r,t,"right"),...cloneDeclaration(r,i,"bottom"),...cloneDeclaration(r,l,"left")]},"padding-block-start":transformSide("padding",t),"padding-block-end":transformSide("padding",i),"padding-inline-start":transformSide("padding",l),"padding-inline-end":transformSide("padding",a),"padding-block":transformSideShorthand("padding",r.block),"padding-inline":transformSideShorthand("padding",r.inline),"border-block-start-width":transformBorder("width",t),"border-block-end-width":transformBorder("width",i),"border-inline-start-width":transformBorder("width",l),"border-inline-end-width":transformBorder("width",a),"border-block-width":transformBorderProperty("width",r.block),"border-inline-width":transformBorderProperty("width",r.inline),"border-block-start-style":transformBorder("style",t),"border-block-end-style":transformBorder("style",i),"border-inline-start-style":transformBorder("style",l),"border-inline-end-style":transformBorder("style",a),"border-block-style":transformBorderProperty("style",r.block),"border-inline-style":transformBorderProperty("style",r.inline),"border-block-start-color":transformBorder("color",t),"border-block-end-color":transformBorder("color",i),"border-inline-start-color":transformBorder("color",l),"border-inline-end-color":transformBorder("color",a),"border-block-color":transformBorderProperty("color",r.block),"border-inline-color":transformBorderProperty("color",r.inline),"border-block":transformBorderShorthand(r.block),"border-block-start":transformBorderShorthand([t]),"border-block-end":transformBorderShorthand([i]),"border-inline":transformBorderShorthand(r.inline),"border-inline-start":transformBorderShorthand([l]),"border-inline-end":transformBorderShorthand([a]),"border-start-start-radius":transformBorderRadius(r),"border-start-end-radius":transformBorderRadius(r),"border-end-start-radius":transformBorderRadius(r),"border-end-end-radius":transformBorderRadius(r)};var s,d}!function(r){r.TopToBottom="top-to-bottom",r.BottomToTop="bottom-to-top",r.RightToLeft="right-to-left",r.LeftToRight="left-to-right"}(t||(t={})),function(r){r.Top="top",r.Right="right",r.Bottom="bottom",r.Left="left"}(i||(i={}));const creator=r=>{const e=Object.assign({blockDirection:t.TopToBottom,inlineDirection:t.LeftToRight},r),n=Object.values(t);if(!n.includes(e.blockDirection))throw new Error(`[postcss-logical] "blockDirection" must be one of ${n.join(", ")}`);if(!n.includes(e.inlineDirection))throw new Error(`[postcss-logical] "inlineDirection" must be one of ${n.join(", ")}`);const[o,l]=directionFlowToAxes(e.blockDirection),[a,s]=directionFlowToAxes(e.inlineDirection);if(!Object.values(i).every((r=>[o,l,a,s].includes(r))))throw new Error('[postcss-logical] "blockDirection" and "inlineDirection" must be on separate axes');const makeTransform=r=>(e,{result:n})=>{var o;if(!r)return;let t=[];try{t=r(e)}catch(r){return void e.warn(n,r.message)}null!=(o=t)&&o.length&&(t.forEach((r=>{e.cloneBefore(r)})),e.remove())},d=prepareTransforms({block:[o,l],inline:[a,s],inlineIsHorizontal:[t.LeftToRight,t.RightToLeft].includes(e.inlineDirection)},o,l,a,s);return{postcssPlugin:"postcss-logical",Declaration:{"caption-side":makeTransform(d["caption-side"]),"text-align":makeTransform(d["text-align"]),"block-size":makeTransform(d["block-size"]),"inline-size":makeTransform(d["inline-size"]),"min-block-size":makeTransform(d["min-block-size"]),"max-block-size":makeTransform(d["max-block-size"]),"min-inline-size":makeTransform(d["min-inline-size"]),"max-inline-size":makeTransform(d["max-inline-size"]),"margin-block-start":makeTransform(d["margin-block-start"]),"margin-block-end":makeTransform(d["margin-block-end"]),"margin-inline-start":makeTransform(d["margin-inline-start"]),"margin-inline-end":makeTransform(d["margin-inline-end"]),"margin-block":makeTransform(d["margin-block"]),"margin-inline":makeTransform(d["margin-inline"]),"inset-block":makeTransform(d["inset-block"]),"inset-block-start":makeTransform(d["inset-block-start"]),"inset-block-end":makeTransform(d["inset-block-end"]),"inset-inline":makeTransform(d["inset-inline"]),"inset-inline-start":makeTransform(d["inset-inline-start"]),"inset-inline-end":makeTransform(d["inset-inline-end"]),inset:makeTransform(d.inset),"padding-block-start":makeTransform(d["padding-block-start"]),"padding-block-end":makeTransform(d["padding-block-end"]),"padding-inline-start":makeTransform(d["padding-inline-start"]),"padding-inline-end":makeTransform(d["padding-inline-end"]),"padding-block":makeTransform(d["padding-block"]),"padding-inline":makeTransform(d["padding-inline"]),"border-block-start-width":makeTransform(d["border-block-start-width"]),"border-block-end-width":makeTransform(d["border-block-end-width"]),"border-inline-start-width":makeTransform(d["border-inline-start-width"]),"border-inline-end-width":makeTransform(d["border-inline-end-width"]),"border-block-width":makeTransform(d["border-block-width"]),"border-inline-width":makeTransform(d["border-inline-width"]),"border-block-start-style":makeTransform(d["border-block-start-style"]),"border-block-end-style":makeTransform(d["border-block-end-style"]),"border-inline-start-style":makeTransform(d["border-inline-start-style"]),"border-inline-end-style":makeTransform(d["border-inline-end-style"]),"border-block-style":makeTransform(d["border-block-style"]),"border-inline-style":makeTransform(d["border-inline-style"]),"border-block-start-color":makeTransform(d["border-block-start-color"]),"border-block-end-color":makeTransform(d["border-block-end-color"]),"border-inline-start-color":makeTransform(d["border-inline-start-color"]),"border-inline-end-color":makeTransform(d["border-inline-end-color"]),"border-block-color":makeTransform(d["border-block-color"]),"border-inline-color":makeTransform(d["border-inline-color"]),"border-block":makeTransform(d["border-block"]),"border-block-start":makeTransform(d["border-block-start"]),"border-block-end":makeTransform(d["border-block-end"]),"border-inline":makeTransform(d["border-inline"]),"border-inline-start":makeTransform(d["border-inline-start"]),"border-inline-end":makeTransform(d["border-inline-end"]),"border-start-start-radius":makeTransform(d["border-start-start-radius"]),"border-start-end-radius":makeTransform(d["border-start-end-radius"]),"border-end-start-radius":makeTransform(d["border-end-start-radius"]),"border-end-end-radius":makeTransform(d["border-end-end-radius"]),transition:(r,{result:e,postcss:n})=>{var o;let t=[];try{t=transformTransition(r,n,d)}catch(n){return void r.warn(e,n.message)}null!=(o=t)&&o.length&&(t.forEach((e=>{r.cloneBefore(e)})),r.remove())}}}};creator.postcss=!0,module.exports=creator;