react-form-builder-more-flexibility 2.0.0 → 2.0.1

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.
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports["default"] = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _myxss = _interopRequireDefault(require("./myxss"));
10
+ var _contrastBacking = require("../utils/contrast-backing");
10
11
  var ComponentLabel = function ComponentLabel(props) {
11
12
  var hasRequiredLabel = props.data.hasOwnProperty('required') && props.data.required === true && !props.read_only;
12
13
  var labelText = _myxss["default"].process(props.data.label);
@@ -27,7 +28,7 @@ var ComponentLabel = function ComponentLabel(props) {
27
28
  }
28
29
  }, /*#__PURE__*/_react["default"].createElement("span", {
29
30
  dangerouslySetInnerHTML: {
30
- __html: labelText
31
+ __html: (0, _contrastBacking.canvasHtml)(props, labelText)
31
32
  }
32
33
  }), hasRequiredLabel && !props.hide_required_alert && /*#__PURE__*/_react["default"].createElement("span", {
33
34
  className: "label-required badge badge-danger"
@@ -25,6 +25,7 @@ var _datePicker = _interopRequireDefault(require("./date-picker"));
25
25
  var _componentHeader = _interopRequireDefault(require("./component-header"));
26
26
  var _componentLabel = _interopRequireDefault(require("./component-label"));
27
27
  var _myxss = _interopRequireDefault(require("./myxss"));
28
+ var _contrastBacking = require("../utils/contrast-backing");
28
29
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
29
30
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
30
31
  function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
@@ -79,7 +80,7 @@ var Header = /*#__PURE__*/function (_React$Component) {
79
80
  }, /*#__PURE__*/_react["default"].createElement(_componentHeader["default"], this.props), /*#__PURE__*/_react["default"].createElement("h3", {
80
81
  className: classNames,
81
82
  dangerouslySetInnerHTML: {
82
- __html: _myxss["default"].process(this.props.data.content)
83
+ __html: (0, _contrastBacking.canvasHtml)(this.props, _myxss["default"].process(this.props.data.content))
83
84
  }
84
85
  }));
85
86
  }
@@ -111,7 +112,7 @@ var Paragraph = /*#__PURE__*/function (_React$Component2) {
111
112
  }, /*#__PURE__*/_react["default"].createElement(_componentHeader["default"], this.props), /*#__PURE__*/_react["default"].createElement("p", {
112
113
  className: classNames,
113
114
  dangerouslySetInnerHTML: {
114
- __html: _myxss["default"].process(this.props.data.content)
115
+ __html: (0, _contrastBacking.canvasHtml)(this.props, _myxss["default"].process(this.props.data.content))
115
116
  }
116
117
  }));
117
118
  }
@@ -143,7 +144,7 @@ var Label = /*#__PURE__*/function (_React$Component3) {
143
144
  }, /*#__PURE__*/_react["default"].createElement(_componentHeader["default"], this.props), /*#__PURE__*/_react["default"].createElement("label", {
144
145
  className: "".concat(classNames, " form-label"),
145
146
  dangerouslySetInnerHTML: {
146
- __html: _myxss["default"].process(this.props.data.content)
147
+ __html: (0, _contrastBacking.canvasHtml)(this.props, _myxss["default"].process(this.props.data.content))
147
148
  }
148
149
  }));
149
150
  }
@@ -663,7 +664,7 @@ var Checkbox = /*#__PURE__*/function (_React$Component12) {
663
664
  className: "form-check-label",
664
665
  onClick: this.handleLabelClick,
665
666
  dangerouslySetInnerHTML: {
666
- __html: this.props.data.boxLabel
667
+ __html: (0, _contrastBacking.canvasHtml)(this.props, this.props.data.boxLabel)
667
668
  }
668
669
  }))));
669
670
  }
@@ -814,7 +815,7 @@ var HyperLink = /*#__PURE__*/function (_React$Component16) {
814
815
  target: "_blank",
815
816
  href: this.props.data.href,
816
817
  dangerouslySetInnerHTML: {
817
- __html: _myxss["default"].process(this.props.data.content)
818
+ __html: (0, _contrastBacking.canvasHtml)(this.props, _myxss["default"].process(this.props.data.content))
818
819
  }
819
820
  }))));
820
821
  }
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.MIN_READABLE_CONTRAST = exports.CANVAS_BACKGROUND = exports.BACKING_COLOR = void 0;
8
+ exports.backHardToReadText = backHardToReadText;
9
+ exports.canvasHtml = canvasHtml;
10
+ exports.contrastRatio = contrastRatio;
11
+ exports.parseCssColorToRgb = parseCssColorToRgb;
12
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
14
+ // A LABEL THE OPERATOR COLOURED WHITE MUST STILL BE READABLE ON THE BUILDER CANVAS (2.0.1).
15
+ //
16
+ // The canvas draws every field's saved label / header / paragraph on a white card, so text the
17
+ // operator coloured white (for a dark page) is a blank row in the builder. `canvasHtml` draws
18
+ // such text on a dark backing — ON THE CANVAS ONLY.
19
+ //
20
+ // "Only" is the load-bearing word. The same element classes render the GENERATED form
21
+ // (ReactFormGenerator), which hosts show to their own users on their own background; a backing
22
+ // there would be a dark chip on every user's form. The canvas renders elements with
23
+ // `mutable={false}`, the generator with `mutable={true}`: the backing is applied on a literal
24
+ // `false`, so any other caller gets its HTML back untouched.
25
+ //
26
+ // The backing is added to the HTML handed to React for display. It is never written to the
27
+ // element's data, so it is never saved.
28
+ //
29
+ // The three numbers below match the SQWAD tenant admin's rich-text editor
30
+ // (src/components/utils/lightTextColor.js there), so a label looks the same in the editor and on
31
+ // the canvas. Nothing breaks if they drift; the two would only stop matching.
32
+
33
+ var CANVAS_BACKGROUND = exports.CANVAS_BACKGROUND = '#ffffff';
34
+ var BACKING_COLOR = exports.BACKING_COLOR = '#2a2a2a';
35
+ var MIN_READABLE_CONTRAST = exports.MIN_READABLE_CONTRAST = 3;
36
+ var BACKING_ATTRIBUTE = 'data-rfb-contrast-backing';
37
+ var BACKING_TITLE = 'Shown on a dark backing so you can read it here. Your form shows this text on its own background.';
38
+ var WHITE = [255, 255, 255];
39
+ var NAMED = {
40
+ black: [0, 0, 0],
41
+ white: WHITE,
42
+ snow: WHITE,
43
+ ivory: WHITE,
44
+ ghostwhite: WHITE,
45
+ whitesmoke: WHITE,
46
+ floralwhite: WHITE,
47
+ mintcream: WHITE,
48
+ azure: WHITE,
49
+ aliceblue: WHITE,
50
+ seashell: WHITE,
51
+ honeydew: WHITE,
52
+ lightyellow: WHITE,
53
+ lightcyan: WHITE,
54
+ lavenderblush: WHITE,
55
+ oldlace: WHITE,
56
+ linen: WHITE,
57
+ cornsilk: WHITE,
58
+ beige: WHITE
59
+ };
60
+ var channel = function channel(token) {
61
+ var n = parseFloat(token);
62
+ if (!Number.isFinite(n)) return null;
63
+ return token.trim().endsWith('%') ? n / 100 * 255 : n;
64
+ };
65
+ var hslToRgb = function hslToRgb(h, s, l) {
66
+ var hue = (h % 360 + 360) % 360 / 360;
67
+ if (s === 0) return [l, l, l].map(function (v) {
68
+ return Math.round(v * 255);
69
+ });
70
+ var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
71
+ var p = 2 * l - q;
72
+ var part = function part(t) {
73
+ var x = t;
74
+ if (x < 0) x += 1;
75
+ if (x > 1) x -= 1;
76
+ if (x < 1 / 6) return p + (q - p) * 6 * x;
77
+ if (x < 1 / 2) return q;
78
+ if (x < 2 / 3) return p + (q - p) * (2 / 3 - x) * 6;
79
+ return p;
80
+ };
81
+ return [part(hue + 1 / 3), part(hue), part(hue - 1 / 3)].map(function (v) {
82
+ return Math.round(v * 255);
83
+ });
84
+ };
85
+ function parseCssColorToRgb(value) {
86
+ if (typeof value !== 'string') return null;
87
+ var v = value.trim().toLowerCase();
88
+ if (!v) return null;
89
+ if (NAMED[v]) return (0, _toConsumableArray2["default"])(NAMED[v]);
90
+ var hex = /^#([0-9a-f]{3,8})$/.exec(v);
91
+ if (hex) {
92
+ var h = hex[1];
93
+ if (h.length === 3 || h.length === 4) return [0, 1, 2].map(function (i) {
94
+ return parseInt(h[i] + h[i], 16);
95
+ });
96
+ if (h.length === 6 || h.length === 8) return [0, 2, 4].map(function (i) {
97
+ return parseInt(h.slice(i, i + 2), 16);
98
+ });
99
+ return null;
100
+ }
101
+ var rgb = /^rgba?\(([^)]*)\)$/.exec(v);
102
+ if (rgb) {
103
+ var parts = rgb[1].split(/[\s,/]+/).filter(Boolean);
104
+ if (parts.length < 3) return null;
105
+ var channels = parts.slice(0, 3).map(channel);
106
+ return channels.some(function (c) {
107
+ return c === null;
108
+ }) ? null : channels;
109
+ }
110
+ var hsl = /^hsla?\(([^)]*)\)$/.exec(v);
111
+ if (hsl) {
112
+ var _parts = hsl[1].split(/[\s,/]+/).filter(Boolean);
113
+ if (_parts.length < 3) return null;
114
+ var _h = parseFloat(_parts[0]);
115
+ var s = parseFloat(_parts[1]);
116
+ var l = parseFloat(_parts[2]);
117
+ if (![_h, s, l].every(Number.isFinite)) return null;
118
+ return hslToRgb(_h, Math.min(100, Math.max(0, s)) / 100, Math.min(100, Math.max(0, l)) / 100);
119
+ }
120
+ return null;
121
+ }
122
+ var relativeLuminance = function relativeLuminance(rgb) {
123
+ var _rgb$map = rgb.map(function (c) {
124
+ var x = Math.min(255, Math.max(0, c)) / 255;
125
+ return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
126
+ }),
127
+ _rgb$map2 = (0, _slicedToArray2["default"])(_rgb$map, 3),
128
+ r = _rgb$map2[0],
129
+ g = _rgb$map2[1],
130
+ b = _rgb$map2[2];
131
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
132
+ };
133
+
134
+ // WCAG contrast ratio of two CSS colours, 1–21. null when either cannot be read.
135
+ function contrastRatio(a, b) {
136
+ var ra = parseCssColorToRgb(a);
137
+ var rb = parseCssColorToRgb(b);
138
+ if (!ra || !rb) return null;
139
+ var la = relativeLuminance(ra);
140
+ var lb = relativeLuminance(rb);
141
+ return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05);
142
+ }
143
+
144
+ // The colour an inline `style` attribute sets, as written. Read from the attribute text rather
145
+ // than `element.style`, which not every DOM implementation fills for every colour form.
146
+ var inlineDeclaration = function inlineDeclaration(el, property) {
147
+ var style = el.getAttribute && el.getAttribute('style');
148
+ if (!style) return null;
149
+ var found = null;
150
+ style.split(';').forEach(function (declaration) {
151
+ var at = declaration.indexOf(':');
152
+ if (at === -1) return;
153
+ if (declaration.slice(0, at).trim().toLowerCase() === property) found = declaration.slice(at + 1).trim();
154
+ });
155
+ return found;
156
+ };
157
+ var hasOwnBackground = function hasOwnBackground(el) {
158
+ return !!(inlineDeclaration(el, 'background-color') || inlineDeclaration(el, 'background'));
159
+ };
160
+
161
+ // Returns `html` with a dark backing on every element whose own inline text colour is hard to
162
+ // read on the white canvas. Returns the SAME string when nothing needs one (the ordinary case),
163
+ // and whenever it cannot parse — it never throws and never drops content.
164
+ function backHardToReadText(html) {
165
+ if (typeof html !== 'string' || !html || html.indexOf('color') === -1) return html;
166
+ // No DOM (a server render): nothing to draw on, and the canvas never renders there.
167
+ if (typeof window === 'undefined' || typeof window.DOMParser === 'undefined') return html;
168
+ var body;
169
+ try {
170
+ // An inert document: nothing in it loads or runs.
171
+ body = new window.DOMParser().parseFromString("<!doctype html><body>".concat(html), 'text/html').body;
172
+ } catch (e) {
173
+ return html;
174
+ }
175
+ if (!body) return html;
176
+ var changed = false;
177
+ body.querySelectorAll('[style]').forEach(function (el) {
178
+ var color = inlineDeclaration(el, 'color');
179
+ if (!color) return;
180
+ var ratio = contrastRatio(color, CANVAS_BACKGROUND);
181
+ // A colour we cannot read is left alone: a backing drawn on a guess is noise.
182
+ if (ratio === null || ratio >= MIN_READABLE_CONTRAST) return;
183
+ // Text that already carries a background — its own or an ancestor's — is the operator's design.
184
+ for (var node = el; node && node !== body; node = node.parentElement) {
185
+ if (hasOwnBackground(node)) return;
186
+ }
187
+ var style = (el.getAttribute('style') || '').trim().replace(/;$/, '');
188
+ el.setAttribute('style', "".concat(style, "; background-color: ").concat(BACKING_COLOR, "; border-radius: 3px; box-shadow: 0 0 0 2px ").concat(BACKING_COLOR, ";"));
189
+ el.setAttribute(BACKING_ATTRIBUTE, 'dark');
190
+ el.setAttribute('title', BACKING_TITLE);
191
+ changed = true;
192
+ });
193
+ return changed ? body.innerHTML : html;
194
+ }
195
+
196
+ // What an element draws for a piece of saved HTML: backed on the builder canvas
197
+ // (`mutable === false`), untouched everywhere else.
198
+ function canvasHtml(props, html) {
199
+ return props && props.mutable === false ? backHardToReadText(html) : html;
200
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-form-builder-more-flexibility",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A complete form builder for react.",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",