1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
import r from"postcss-value-parser";var e,n;!function(r){r.Block="block",r.Inline="inline"}(e||(e={})),function(r){r.Start="start",r.End="end"}(n||(n={}));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(e,n,o){const{prop:t,value:i}=e,l=r(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 e=a[r];for(let t=0;t<e.length;t++){const i=e[t];if("word"!==i.type)continue;const l=i.value.toLowerCase(),s=o[l];if(!s)continue;const d=s(n.decl({prop:l,value:"initial"}));if(0!==d.length){for(let n=d.length-1;n>=0;n--){const o=JSON.parse(JSON.stringify(e));o[t].value=d[n].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=r.stringify(d);return c!==i?cloneDeclaration(e,c,t):[]}function parseValueCouple(e){const n=r(e.value).nodes.filter((r=>"space"!==r.type));if(n.length>2||0===n.length){const r=`[postcss-logical] Invalid number of values for ${e.prop}. Found ${n.length} values, expected 1 or 2.`;throw e.error(r)}let o,t;return 1===n.length?(o=r.stringify(n[0]),t=o):(o=r.stringify(n[0]),t=r.stringify(n[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(e,n,o){const{prop:t,value:i}=e,l=r(i);l.nodes.forEach((r=>{if("word"===r.type){const e=r.value.toLowerCase();n.includes(e)&&(r.value=logicalToPhysical(e,o))}}));const a=l.toString();return a!==i?cloneDeclaration(e,a,t):[]}function prepareTransforms(e,t,i,l,a){return{"caption-side":(d=e,r=>doTransform(r,Object.values(o),d)),"text-align":e.inlineIsHorizontal?(s=e.inline,e=>{const{prop:o,value:t}=e,i=r(t),[l,a]=s;i.nodes.forEach((r=>{if("word"===r.type){const e=r.value.toLowerCase();if(e===n.End)return void(r.value=a);e===n.Start&&(r.value=l)}}));const d=i.toString();return d!==t?cloneDeclaration(e,d,o):[]}):null,"block-size":transformLogicalSize(e),"inline-size":transformLogicalSize(e),"min-block-size":transformLogicalSize(e),"max-block-size":transformLogicalSize(e),"min-inline-size":transformLogicalSize(e),"max-inline-size":transformLogicalSize(e),"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",e.block),"margin-inline":transformSideShorthand("margin",e.inline),"inset-block":transformOffsetShorthand(e.block),"inset-block-start":transformOffset(t),"inset-block-end":transformOffset(i),"inset-inline":transformOffsetShorthand(e.inline),"inset-inline-start":transformOffset(l),"inset-inline-end":transformOffset(a),inset:e=>{const n=r(e.value).nodes.filter((r=>"space"!==r.type&&"comment"!==r.type));if(n.length>4){const r=`[postcss-logical] Invalid number of values for ${e.prop}. Found ${n.length} values, expected up to 4 values.`;throw e.error(r)}let o,t,i,l;if(1===n.length)o=r.stringify(n[0]),t=o,i=o,l=o;else if(2===n.length)o=r.stringify(n[0]),t=r.stringify(n[1]),i=o,l=t;else if(3===n.length)o=r.stringify(n[0]),t=r.stringify(n[1]),l=t,i=r.stringify(n[2]);else{if(4!==n.length)return[];o=r.stringify(n[0]),t=r.stringify(n[1]),i=r.stringify(n[2]),l=r.stringify(n[3])}return[...cloneDeclaration(e,o,"top"),...cloneDeclaration(e,t,"right"),...cloneDeclaration(e,i,"bottom"),...cloneDeclaration(e,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",e.block),"padding-inline":transformSideShorthand("padding",e.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",e.block),"border-inline-width":transformBorderProperty("width",e.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",e.block),"border-inline-style":transformBorderProperty("style",e.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",e.block),"border-inline-color":transformBorderProperty("color",e.inline),"border-block":transformBorderShorthand(e.block),"border-block-start":transformBorderShorthand([t]),"border-block-end":transformBorderShorthand([i]),"border-inline":transformBorderShorthand(e.inline),"border-inline-start":transformBorderShorthand([l]),"border-inline-end":transformBorderShorthand([a]),"border-start-start-radius":transformBorderRadius(e),"border-start-end-radius":transformBorderRadius(e),"border-end-start-radius":transformBorderRadius(e),"border-end-end-radius":transformBorderRadius(e)};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;export{creator as default};
|