UNPKG

nehan

Version:

Html layout engine for paged-media written in Typescript

112 lines 3.87 kB
import { CssLength, LogicalBorderStyle, LogicalBorderWidth, LogicalBorderColor, LogicalBorderRadius, NativeStyleMap, CssText, } from "./public-api"; export class LogicalBorder { constructor(values) { this.style = values.style; this.width = values.width; this.color = values.color; this.radius = values.radius; } clone() { return new LogicalBorder({ style: this.style.clone(), width: this.width.clone(), color: this.color.clone(), radius: this.radius.clone() }); } static load(element) { return new LogicalBorder({ style: LogicalBorderStyle.load(element), width: LogicalBorderWidth.load(element), color: LogicalBorderColor.load(element), radius: LogicalBorderRadius.load(element) }); } static get none() { return new LogicalBorder({ style: LogicalBorderStyle.none, width: LogicalBorderWidth.none, color: LogicalBorderColor.none, radius: LogicalBorderRadius.none }); } acceptCssEvaluator(visitor) { const css = new NativeStyleMap(); visitor.visitLogicalBorderWidth(this.width).mergeTo(css); visitor.visitLogicalBorderStyle(this.style).mergeTo(css); visitor.visitLogicalBorderColor(this.color).mergeTo(css); visitor.visitLogicalBorderRadius(this.radius, this.width).mergeTo(css); return css; } clearBefore() { this.width.clearBefore(); } clearEnd() { this.width.clearEnd(); } clearAfter() { this.width.clearAfter(); } clearStart() { this.width.clearStart(); } get beforeWidth() { return this.width.before; } get endWidth() { return this.width.end; } get afterWidth() { return this.width.after; } get startWidth() { return this.width.start; } static expand(declrs) { return declrs.reduce((acm, item) => { switch (item.prop) { case "width": return acm.concat(LogicalBorderWidth.parseShorthand(new CssText(item))); case "style": return acm.concat(LogicalBorderStyle.parseShorthand(new CssText(item))); case "color": return acm.concat(LogicalBorderColor.parseShorthand(new CssText(item))); } console.error(`undefined prop for logical-border:${item.prop}`); return acm; }, []); } static inferPropType(value) { if (LogicalBorderStyle.values.indexOf(value) >= 0) { return "style"; } if (LogicalBorderWidth.keywords.indexOf(value) >= 0) { return "width"; } if (CssLength.hasUnit(value)) { return "width"; } return "color"; } static parseShorthandWidthStyleColor(css_text) { let declrs = { width: "medium", style: "none", color: "currentcolor" }; let vals = (css_text.value === "none") ? [] : css_text.split().slice(0, 3); vals.forEach(value => { let prop = LogicalBorder.inferPropType(value); declrs[prop] = value; }, declrs); return Object.keys(declrs).map(key => { return { prop: key, value: declrs[key] }; }); } static parseShorthand(css_text) { let sh_declrs = LogicalBorder.parseShorthandWidthStyleColor(css_text); return LogicalBorder.expand(sh_declrs); } static parseShorthandEach(css_text, direction) { return LogicalBorder.parseShorthandWidthStyleColor(css_text).map(item => { return { prop: `border-${direction}-${item.prop}`, value: item.value }; }); } } //# sourceMappingURL=logical-border.js.map