kokopu-react 1.5.4 → 1.7.0

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.
Files changed (114) hide show
  1. package/.nycrc.yml +1 -1
  2. package/CHANGELOG.md +18 -0
  3. package/README.md +2 -2
  4. package/dist/lib/ArrowMarkerIcon.js +2 -2
  5. package/dist/lib/Chessboard.js +23 -15
  6. package/dist/lib/Movetext.js +41 -82
  7. package/dist/lib/formatmove.js +119 -0
  8. package/dist/lib/impl/HtmlSanitizer.js +8 -3
  9. package/dist/lib/impl/colorsets.js +44 -44
  10. package/dist/lib/impl/util.js +1 -1
  11. package/dist/lib/index.js +14 -0
  12. package/dist/lib/markers.js +7 -7
  13. package/doc_src/demo/PageChessboardInteraction.js +4 -3
  14. package/doc_src/demo/PageChessboardMove.js +36 -5
  15. package/doc_src/demo/demo.css +4 -0
  16. package/{graphic_test_app → graphic_test_src}/01_marker_icons.js +0 -0
  17. package/{graphic_test_app → graphic_test_src}/02_chessboard_simple.js +0 -0
  18. package/{graphic_test_app → graphic_test_src}/03_chessboard_flipped.js +0 -0
  19. package/{graphic_test_app → graphic_test_src}/04_chessboard_annotations.js +8 -8
  20. package/{graphic_test_app → graphic_test_src}/05_chessboard_move.js +4 -4
  21. package/{graphic_test_app → graphic_test_src}/06_chessboard_theme.js +2 -2
  22. package/{graphic_test_app → graphic_test_src}/07_chessboard_click_squares.js +0 -0
  23. package/{graphic_test_app → graphic_test_src}/08_chessboard_move_pieces.js +1 -1
  24. package/{graphic_test_app → graphic_test_src}/09_chessboard_edit_arrows.js +0 -0
  25. package/{graphic_test_app → graphic_test_src}/10_chessboard_play_moves.js +1 -1
  26. package/{graphic_test_app → graphic_test_src}/11_chessboard_play_promotions.js +0 -0
  27. package/{graphic_test_app → graphic_test_src}/12_movetext_simple.js +0 -0
  28. package/{graphic_test_app → graphic_test_src}/13_movetext_error.js +0 -0
  29. package/{graphic_test_app → graphic_test_src}/14_movetext_html.js +0 -0
  30. package/{graphic_test_app → graphic_test_src}/15_movetext_options.js +0 -4
  31. package/{graphic_test_app → graphic_test_src}/16_movetext_interaction.js +0 -0
  32. package/{graphic_test_app → graphic_test_src}/common/dummy.pgn +0 -0
  33. package/{graphic_test_app → graphic_test_src}/common/games.pgn +11 -5
  34. package/{graphic_test_app → graphic_test_src}/common/heartbeat.txt +0 -0
  35. package/graphic_test_src/common/smiley.png +0 -0
  36. package/{graphic_test_app → graphic_test_src}/common/test_app.css +0 -0
  37. package/{graphic_test_app → graphic_test_src}/common/test_app.js +3 -2
  38. package/graphics/chess_sprites/cburnett/bb.svg +45 -0
  39. package/graphics/chess_sprites/cburnett/bk.svg +47 -0
  40. package/graphics/chess_sprites/cburnett/bn.svg +37 -0
  41. package/graphics/chess_sprites/cburnett/bp.svg +19 -0
  42. package/graphics/chess_sprites/cburnett/bq.svg +58 -0
  43. package/graphics/chess_sprites/cburnett/br.svg +60 -0
  44. package/graphics/chess_sprites/cburnett/bx.svg +56 -0
  45. package/graphics/chess_sprites/cburnett/wb.svg +48 -0
  46. package/graphics/chess_sprites/cburnett/wk.svg +26 -0
  47. package/graphics/chess_sprites/cburnett/wn.svg +37 -0
  48. package/graphics/chess_sprites/cburnett/wp.svg +19 -0
  49. package/graphics/chess_sprites/cburnett/wq.svg +60 -0
  50. package/graphics/chess_sprites/cburnett/wr.svg +44 -0
  51. package/graphics/chess_sprites/cburnett/wx.svg +56 -0
  52. package/graphics/chess_sprites/generate_sprites.sh +135 -0
  53. package/graphics/chess_sprites/mmonge/celtic-bx.svg +112 -0
  54. package/graphics/chess_sprites/mmonge/celtic-wx.svg +111 -0
  55. package/graphics/chess_sprites/mmonge/celtic.svg +1576 -0
  56. package/graphics/chess_sprites/mmonge/eyes-spatial-bx.svg +101 -0
  57. package/graphics/chess_sprites/mmonge/eyes-spatial-wx.svg +100 -0
  58. package/graphics/chess_sprites/mmonge/eyes.svg +2538 -0
  59. package/graphics/chess_sprites/mmonge/fantasy-bx.svg +112 -0
  60. package/graphics/chess_sprites/mmonge/fantasy-wx.svg +111 -0
  61. package/graphics/chess_sprites/mmonge/fantasy.svg +5497 -0
  62. package/graphics/chess_sprites/mmonge/fantasy_alt.svg +3742 -0
  63. package/graphics/chess_sprites/mmonge/freak.svg +3479 -0
  64. package/graphics/chess_sprites/mmonge/prmi.svg +6776 -0
  65. package/graphics/chess_sprites/mmonge/skulls-bx.svg +124 -0
  66. package/graphics/chess_sprites/mmonge/skulls-wx.svg +111 -0
  67. package/graphics/chess_sprites/mmonge/skulls.svg +12978 -0
  68. package/graphics/chess_sprites/mmonge/spatial.svg +1223 -0
  69. package/package.json +28 -27
  70. package/scripts/{test_graphic/clean-graphic-output.js → clean-graphic-output.js} +1 -1
  71. package/scripts/{test_graphic/docker-compose.yml → docker-compose.yml} +3 -3
  72. package/scripts/{test_graphic/webpack-config.js → test-graphic.webpack.config.js} +9 -5
  73. package/scripts/{test_headless.webpack-config.js → test-headless.webpack.config.js} +2 -1
  74. package/src/Chessboard.js +21 -13
  75. package/src/Movetext.js +37 -59
  76. package/src/formatmove.js +91 -0
  77. package/src/impl/HtmlSanitizer.js +8 -2
  78. package/src/impl/colorsets.js +44 -44
  79. package/src/impl/util.js +1 -1
  80. package/src/index.js +1 -0
  81. package/src/markers.js +7 -7
  82. package/test/1_markers.js +14 -14
  83. package/test/common/graphic.js +2 -2
  84. package/test/references/04_chessboard_annotations/0.png +0 -0
  85. package/test/references/04_chessboard_annotations/1.png +0 -0
  86. package/test/references/04_chessboard_annotations/2.png +0 -0
  87. package/test/references/04_chessboard_annotations/3.png +0 -0
  88. package/test/references/04_chessboard_annotations/4.png +0 -0
  89. package/test/references/04_chessboard_annotations/5.png +0 -0
  90. package/test/references/05_chessboard_move/4.png +0 -0
  91. package/test/references/05_chessboard_move/5.png +0 -0
  92. package/test/references/05_chessboard_move/6.png +0 -0
  93. package/test/references/06_chessboard_theme/0.png +0 -0
  94. package/test/references/06_chessboard_theme/1.png +0 -0
  95. package/test/references/06_chessboard_theme/2.png +0 -0
  96. package/test/references/06_chessboard_theme/3.png +0 -0
  97. package/test/references/06_chessboard_theme/4.png +0 -0
  98. package/test/references/06_chessboard_theme/5.png +0 -0
  99. package/test/references/06_chessboard_theme/6.png +0 -0
  100. package/test/references/06_chessboard_theme/7.png +0 -0
  101. package/test/references/06_chessboard_theme/8.png +0 -0
  102. package/test/references/08_chessboard_move_pieces/over_arrow_marker.png +0 -0
  103. package/test/references/08_chessboard_move_pieces/over_square_marker.png +0 -0
  104. package/test/references/08_chessboard_move_pieces/over_text_marker.png +0 -0
  105. package/test/references/09_chessboard_edit_arrows/base_1.png +0 -0
  106. package/test/references/09_chessboard_edit_arrows/base_2.png +0 -0
  107. package/test/references/09_chessboard_edit_arrows/null_vector.png +0 -0
  108. package/test/references/09_chessboard_edit_arrows/over_arrow_marker.png +0 -0
  109. package/test/references/09_chessboard_edit_arrows/over_square_marker.png +0 -0
  110. package/test/references/10_chessboard_play_moves/castling_move.png +0 -0
  111. package/test/references/10_chessboard_play_moves/regular_move_2.png +0 -0
  112. package/test/references/14_movetext_html/0.png +0 -0
  113. package/test/references/14_movetext_html/1.png +0 -0
  114. package/test/references/14_movetext_html/2.png +0 -0
package/.nycrc.yml CHANGED
@@ -9,5 +9,5 @@ check-coverage: true
9
9
 
10
10
  exclude:
11
11
  - build/**
12
- - graphic_test_app/**
12
+ - graphic_test_src/**
13
13
  - test/**
package/CHANGELOG.md CHANGED
@@ -1,6 +1,24 @@
1
1
  ChangeLog
2
2
  =========
3
3
 
4
+ 1.7.0 (June 22, 2022)
5
+ ---------------------
6
+ * Upgrade React to React 18.
7
+ * Add support for color blue for markers.
8
+ * Allow customization of the move arrow color in the `Chessboard` component.
9
+ * Change the property names used to define the marker colors in the objects returned by `Chessboard.colorsets()`:
10
+ `g`, `r`, `y` and `highlight` have become respectively `cg`, `cr`, `cy` and `cb`.
11
+
12
+ 1.6.0 (June 1, 2022)
13
+ --------------------
14
+ * Allow block-formatting HTML tags in PGN comments.
15
+ * Provide `formatMove` and `moveFormatter`.
16
+
17
+ 1.5.5 (May 18, 2022)
18
+ --------------------
19
+ * Upgrade dependencies.
20
+ * Remove dependency with security issue.
21
+
4
22
  1.5.4 (May 1, 2022)
5
23
  -------------------
6
24
  * Provide method `Chessboard#size(..)` to retrieve the width/height of a `Chessboard` component instance.
package/README.md CHANGED
@@ -27,10 +27,10 @@ Example
27
27
 
28
28
  ```javascript
29
29
  import React from 'react';
30
- import ReactDOM from 'react-dom';
30
+ import { createRoot } from 'react-dom/client';
31
31
  import { Chessboard } from 'kokopu-react';
32
32
 
33
- ReactDOM.render(<Chessboard />, document.body);
33
+ createRoot(document.body).render(<Chessboard />);
34
34
  ```
35
35
 
36
36
  ![Chessboard component](test/references/02_chessboard_simple/0.png)
@@ -27,7 +27,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
27
27
 
28
28
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
29
29
 
30
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
30
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
31
 
32
32
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
33
33
 
@@ -37,7 +37,7 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
37
37
 
38
38
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
39
39
 
40
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
40
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
41
41
 
42
42
  var ARROW_THICKNESS_FACTOR = 0.2;
43
43
  /**
@@ -59,7 +59,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
59
59
 
60
60
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
61
61
 
62
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
62
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
63
63
 
64
64
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
65
65
 
@@ -69,7 +69,7 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
69
69
 
70
70
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
71
71
 
72
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
72
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
73
73
 
74
74
  var TURN_FLAG_SPACING_FACTOR = 0.1;
75
75
  var RANK_COORDINATE_WIDTH_FACTOR = 1;
@@ -188,7 +188,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
188
188
  viewBox: viewBox,
189
189
  width: xmax - xmin,
190
190
  height: ymax - ymin
191
- }, /*#__PURE__*/_react["default"].createElement("defs", null, this.renderArrowTip(colorset, 'g'), this.renderArrowTip(colorset, 'r'), this.renderArrowTip(colorset, 'y'), this.renderArrowTip(colorset, 'highlight')), squares, rankCoordinates, fileCoordinates, this.renderBoardContent(position, positionBefore, move, squareSize, colorset, pieceset));
191
+ }, /*#__PURE__*/_react["default"].createElement("defs", null, this.renderArrowTip(colorset, 'b'), this.renderArrowTip(colorset, 'g'), this.renderArrowTip(colorset, 'r'), this.renderArrowTip(colorset, 'y')), squares, rankCoordinates, fileCoordinates, this.renderBoardContent(position, positionBefore, move, squareSize, colorset, pieceset));
192
192
  }
193
193
  }, {
194
194
  key: "renderBoardContent",
@@ -286,13 +286,14 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
286
286
 
287
287
  var thickness = Math.max(2, Math.round(HOVER_MARKER_THICKNESS_FACTOR * squareSize));
288
288
  var size = squareSize - thickness;
289
+ var color = this.props.interactionMode === 'editArrows' ? this.props.editedArrowColor : this.props.moveArrowColor;
289
290
  return /*#__PURE__*/_react["default"].createElement("rect", {
290
291
  className: "kokopu-hoveredSquare",
291
292
  x: x + thickness / 2,
292
293
  y: y + thickness / 2,
293
294
  width: size,
294
295
  height: size,
295
- stroke: colorset.highlight,
296
+ stroke: colorset['c' + color],
296
297
  strokeWidth: thickness
297
298
  });
298
299
  }
@@ -407,7 +408,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
407
408
  y1: yFrom,
408
409
  x2: xTo,
409
410
  y2: yTo,
410
- stroke: colorset[this.props.editedArrowColor],
411
+ stroke: colorset['c' + this.props.editedArrowColor],
411
412
  strokeWidth: strokeWidth,
412
413
  markerEnd: "url(#".concat(arrowTipId, ")")
413
414
  });
@@ -543,7 +544,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
543
544
  y: y,
544
545
  width: squareSize,
545
546
  height: squareSize,
546
- fill: colorset[color]
547
+ fill: colorset['c' + color]
547
548
  }));
548
549
  });
549
550
  return result;
@@ -573,7 +574,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
573
574
  y: y,
574
575
  size: squareSize,
575
576
  symbol: value.symbol,
576
- color: colorset[value.color]
577
+ color: colorset['c' + value.color]
577
578
  })));
578
579
  });
579
580
  return result;
@@ -621,7 +622,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
621
622
  y1: yFrom,
622
623
  x2: xTo,
623
624
  y2: yTo,
624
- stroke: colorset[color],
625
+ stroke: colorset['c' + color],
625
626
  strokeWidth: strokeWidth,
626
627
  markerEnd: "url(#".concat(arrowTipId, ")")
627
628
  }));
@@ -652,15 +653,16 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
652
653
  yTo += Math.sign(yFrom - yTo) * ARROW_TIP_OFFSET_FACTOR * squareSize;
653
654
  var x = xTo * alpha + xFrom * (1 - alpha);
654
655
  var y = yTo * alpha + yFrom * (1 - alpha);
656
+ var color = this.props.moveArrowColor;
655
657
  return /*#__PURE__*/_react["default"].createElement("line", {
656
658
  className: "kokopu-annotation kokopu-arrow",
657
659
  x1: xFrom,
658
660
  y1: yFrom,
659
661
  x2: x,
660
662
  y2: y,
661
- stroke: colorset['highlight'],
663
+ stroke: colorset['c' + color],
662
664
  strokeWidth: squareSize * STROKE_THICKNESS_FACTOR,
663
- markerEnd: "url(#".concat(this.getArrowTipId('highlight'), ")")
665
+ markerEnd: "url(#".concat(this.getArrowTipId(color), ")")
664
666
  });
665
667
  }
666
668
  }, {
@@ -668,7 +670,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
668
670
  value: function renderArrowTip(colorset, color) {
669
671
  return /*#__PURE__*/_react["default"].createElement(_ArrowTip["default"], {
670
672
  id: this.getArrowTipId(color),
671
- color: colorset[color]
673
+ color: colorset['c' + color]
672
674
  });
673
675
  }
674
676
  }, {
@@ -1114,7 +1116,7 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
1114
1116
  /**
1115
1117
  * Available colorsets for theming.
1116
1118
  *
1117
- * @returns {Object.<string, {w: string, b: string, g: string, r: string, y: string, highlight: string}>}
1119
+ * @returns {Object.<string, { w: string, b: string, cb: string, cg: string, cr: string, cy: string }>}
1118
1120
  * @public
1119
1121
  */
1120
1122
 
@@ -1126,8 +1128,8 @@ var Chessboard = /*#__PURE__*/function (_React$Component) {
1126
1128
  /**
1127
1129
  * Available piecesets for theming.
1128
1130
  *
1129
- * @returns {Object.<string, {bb: string, bk: string, bn: string, bp: string, bq: string, br: string, bx: string,
1130
- * wb: string, wk: string, wn: string, wp: string, wq: string, wr: string, wx: string}>}
1131
+ * @returns {Object.<string, { bb: string, bk: string, bn: string, bp: string, bq: string, br: string, bx: string,
1132
+ * wb: string, wk: string, wn: string, wp: string, wq: string, wr: string, wx: string }>}
1131
1133
  * @public
1132
1134
  */
1133
1135
 
@@ -1205,6 +1207,11 @@ Chessboard.propTypes = {
1205
1207
  */
1206
1208
  moveArrowVisible: _propTypes["default"].bool,
1207
1209
 
1210
+ /**
1211
+ * Color of the move arrow.
1212
+ */
1213
+ moveArrowColor: _propTypes["default"].oneOf(['b', 'g', 'r', 'y']),
1214
+
1208
1215
  /**
1209
1216
  * Whether moves are animated or not.
1210
1217
  */
@@ -1252,7 +1259,7 @@ Chessboard.propTypes = {
1252
1259
  /**
1253
1260
  * Color of the edited arrow (only used if `interactionMode` is set to `'editArrows'`).
1254
1261
  */
1255
- editedArrowColor: _propTypes["default"].oneOf(['g', 'r', 'y']),
1262
+ editedArrowColor: _propTypes["default"].oneOf(['b', 'g', 'r', 'y']),
1256
1263
 
1257
1264
  /**
1258
1265
  * Callback invoked when a piece is moved through drag&drop (only if `interactionMode` is set to `'movePieces'`).
@@ -1290,6 +1297,7 @@ Chessboard.defaultProps = {
1290
1297
  squareSize: 40,
1291
1298
  coordinateVisible: true,
1292
1299
  moveArrowVisible: true,
1300
+ moveArrowColor: 'b',
1293
1301
  animated: false,
1294
1302
  colorset: 'original',
1295
1303
  pieceset: 'cburnett'
@@ -19,9 +19,9 @@ var _Chessboard = _interopRequireDefault(require("./Chessboard"));
19
19
 
20
20
  var _ErrorBox = _interopRequireDefault(require("./ErrorBox"));
21
21
 
22
- var _i18n = _interopRequireDefault(require("./i18n"));
22
+ var _formatmove = require("./formatmove");
23
23
 
24
- require("./css/fonts.css");
24
+ var _i18n = _interopRequireDefault(require("./i18n"));
25
25
 
26
26
  require("./css/movetext.css");
27
27
 
@@ -49,7 +49,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
49
49
 
50
50
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
51
51
 
52
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
52
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
53
53
 
54
54
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
55
55
 
@@ -59,7 +59,7 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
59
59
 
60
60
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
61
61
 
62
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
62
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
63
63
 
64
64
  /**
65
65
  * Display a chess game, i.e. the headers (name of the players, event, etc.), the moves, and all the related annotations if any (comments, variations, NAGs...).
@@ -254,7 +254,8 @@ var Movetext = /*#__PURE__*/function (_React$Component) {
254
254
  }, {
255
255
  key: "renderBody",
256
256
  value: function renderBody(game) {
257
- return this.renderVariation(this.getNotationTextBuilder(), game.mainVariation(), 'main-variation', true, game.result());
257
+ var notationTextBuilder = (0, _formatmove.moveFormatter)(this.props.pieceSymbols);
258
+ return this.renderVariation(notationTextBuilder, game.mainVariation(), 'main-variation', true, game.result());
258
259
  }
259
260
  /**
260
261
  * Render the given variation and its sub-variations, recursively.
@@ -441,10 +442,10 @@ var Movetext = /*#__PURE__*/function (_React$Component) {
441
442
  key: "renderComment",
442
443
  value: function renderComment(node, comment, isVariation) {
443
444
  var content;
445
+ var sanitizer = createSanitizer(true);
444
446
 
445
447
  if (this.props.diagramVisible && comment.includes('[#]')) {
446
448
  content = [];
447
- var sanitizer = createSanitizer();
448
449
  var isFirstPart = true;
449
450
 
450
451
  var _iterator2 = _createForOfIteratorHelper(comment.split('[#]').entries()),
@@ -491,7 +492,7 @@ var Movetext = /*#__PURE__*/function (_React$Component) {
491
492
  _iterator2.f();
492
493
  }
493
494
  } else {
494
- content = sanitizeHtml(comment);
495
+ content = sanitizeHtml(comment, sanitizer);
495
496
  }
496
497
 
497
498
  var key = isVariation ? 'initial-comment' : node.fullMoveNumber() + node.moveColor() + '-comment';
@@ -564,40 +565,6 @@ var Movetext = /*#__PURE__*/function (_React$Component) {
564
565
  this.props.onMoveSelected(nodeId === this.props.selection ? undefined : nodeId, 'click');
565
566
  }
566
567
  }
567
- /**
568
- * Return the square at the given location.
569
- */
570
-
571
- }, {
572
- key: "getNotationTextBuilder",
573
- value: function getNotationTextBuilder() {
574
- var pieceSymbols = this.props.pieceSymbols;
575
-
576
- if (pieceSymbols === 'localized') {
577
- var mapping = _i18n["default"].PIECE_SYMBOLS;
578
- return function (notation) {
579
- return notation.replace(/[KQRBNP]/g, function (match) {
580
- return mapping[match];
581
- });
582
- };
583
- } else if (pieceSymbols === 'figurines') {
584
- return function (notation) {
585
- return figurineNotation(notation, 'alpha');
586
- };
587
- } else if (pieceSymbols !== 'native' && pieceSymbols && ['K', 'Q', 'R', 'B', 'N', 'P'].every(function (p) {
588
- return typeof pieceSymbols[p] === 'string';
589
- })) {
590
- return function (notation) {
591
- return notation.replace(/[KQRBNP]/g, function (match) {
592
- return pieceSymbols[match];
593
- });
594
- };
595
- } else {
596
- return function (notation) {
597
- return notation;
598
- };
599
- }
600
- }
601
568
  /**
602
569
  * Set the focus to the current component.
603
570
  *
@@ -846,58 +813,50 @@ function formatResult(result) {
846
813
  }
847
814
  }
848
815
 
849
- function createSanitizer() {
816
+ function createSanitizer(withBlockTags) {
817
+ var allowedTags = ['span', // general purpose
818
+ 'a', // links
819
+ 'b', 'strong', 'i', 'em', // bold, italic
820
+ 'del', 'ins', 's', 'u', // underline, strikethrough
821
+ 'sub', 'sup', // subscript, superscript
822
+ 'abbr', // acronym (typically associated to a title=".." tooltip)
823
+ 'q', 'cite', // quotation and title of a work
824
+ 'mark', // highlighted text
825
+ 'small' // smaller text
826
+ ];
827
+ var allowedAttributes = {
828
+ '*': ['class', 'id', 'title'],
829
+ 'a': ['href', 'target']
830
+ };
831
+
832
+ if (withBlockTags) {
833
+ allowedTags = allowedTags.concat(['div', // general purpose
834
+ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', // headings
835
+ 'p', 'br', // paragraph and line break
836
+ 'blockquote', // long quotation
837
+ 'img', // image
838
+ 'pre', // preformatted text
839
+ 'ul', 'ol', 'li' // lists
840
+ ]);
841
+ Object.assign(allowedAttributes, {
842
+ 'img': ['alt', 'src', 'height', 'width']
843
+ });
844
+ }
845
+
850
846
  return new _HtmlSanitizer["default"]({
851
- allowedTags: ['a', 'span', 'b', 'strong', 'i', 'em', 'mark', 'small', 'del', 'ins', 'sub', 'sup'],
852
- allowedAttributes: {
853
- '*': ['class', 'id', 'title'],
854
- 'a': ['href', 'target']
855
- }
847
+ allowedTags: allowedTags,
848
+ allowedAttributes: allowedAttributes
856
849
  });
857
850
  }
858
851
 
859
852
  function sanitizeHtml(text, sanitizer) {
860
853
  if (!sanitizer) {
861
- sanitizer = createSanitizer();
854
+ sanitizer = createSanitizer(false);
862
855
  }
863
856
 
864
857
  var result = sanitizer.parse(text);
865
858
  return result !== null && result !== void 0 ? result : text;
866
859
  }
867
- /**
868
- * Decompose the given string into piece symbol characters and sections of non piece symbol characters, and transform the piece symbols into
869
- * React objects represented with the given chess font.
870
- */
871
-
872
-
873
- function figurineNotation(text, fontName) {
874
- var result = [];
875
- var beginOfText = 0;
876
- var pieceSymbolIndex = 0;
877
-
878
- for (var pos = 0; pos < text.length; ++pos) {
879
- var currentChar = text.charAt(pos);
880
-
881
- if (currentChar === 'K' || currentChar === 'Q' || currentChar === 'R' || currentChar === 'B' || currentChar === 'N' || currentChar === 'P') {
882
- if (pos > beginOfText) {
883
- result.push(text.substring(beginOfText, pos));
884
- }
885
-
886
- beginOfText = pos + 1;
887
- var key = 'symbol-' + pieceSymbolIndex++;
888
- result.push( /*#__PURE__*/_react["default"].createElement("span", {
889
- className: 'kokopu-font-' + fontName,
890
- key: key
891
- }, currentChar));
892
- }
893
- }
894
-
895
- if (beginOfText < text.length) {
896
- result.push(text.substring(beginOfText));
897
- }
898
-
899
- return result;
900
- }
901
860
  /**
902
861
  * Try to interpret the given object as a chess game.
903
862
  */
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.formatMove = formatMove;
7
+ exports.moveFormatter = moveFormatter;
8
+
9
+ var _react = _interopRequireDefault(require("react"));
10
+
11
+ var _i18n = _interopRequireDefault(require("./i18n"));
12
+
13
+ require("./css/fonts.css");
14
+
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
16
+
17
+ /******************************************************************************
18
+ * *
19
+ * This file is part of Kokopu-React, a JavaScript chess library. *
20
+ * Copyright (C) 2021-2022 Yoann Le Montagner <yo35 -at- melix.net> *
21
+ * *
22
+ * This program is free software: you can redistribute it and/or *
23
+ * modify it under the terms of the GNU Lesser General Public License *
24
+ * as published by the Free Software Foundation, either version 3 of *
25
+ * the License, or (at your option) any later version. *
26
+ * *
27
+ * This program is distributed in the hope that it will be useful, *
28
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30
+ * GNU Lesser General Public License for more details. *
31
+ * *
32
+ * You should have received a copy of the GNU Lesser General *
33
+ * Public License along with this program. If not, see *
34
+ * <http://www.gnu.org/licenses/>. *
35
+ * *
36
+ ******************************************************************************/
37
+
38
+ /**
39
+ * Render the given SAN notation according to the given piece symbol style.
40
+ *
41
+ * @param {string|object} pieceSymbols See {@link moveFormatter}.
42
+ * @param {string} notation SAN notation.
43
+ * @returns {string|React.ReactFragment}
44
+ */
45
+ function formatMove(pieceSymbols, notation) {
46
+ var formatter = moveFormatter(pieceSymbols);
47
+ return formatter(notation);
48
+ }
49
+ /**
50
+ * Return a callback capable of rendering a SAN notation using the given piece symbol style.
51
+ *
52
+ * @param {string|object} pieceSymbols `'native'`, `'localized'`, `'figurines'`, or an object defining a string-valued properties
53
+ * for each English piece symbol.
54
+ * @returns {function(string)} Callback returning either a string or a React fragment.
55
+ */
56
+
57
+
58
+ function moveFormatter(pieceSymbols) {
59
+ if (pieceSymbols === 'localized') {
60
+ var mapping = _i18n["default"].PIECE_SYMBOLS;
61
+ return function (notation) {
62
+ return notation.replace(/[KQRBNP]/g, function (match) {
63
+ return mapping[match];
64
+ });
65
+ };
66
+ } else if (pieceSymbols === 'figurines') {
67
+ return function (notation) {
68
+ return figurineNotation('alpha', notation);
69
+ };
70
+ } else if (pieceSymbols !== 'native' && pieceSymbols && ['K', 'Q', 'R', 'B', 'N', 'P'].every(function (p) {
71
+ return typeof pieceSymbols[p] === 'string';
72
+ })) {
73
+ return function (notation) {
74
+ return notation.replace(/[KQRBNP]/g, function (match) {
75
+ return pieceSymbols[match];
76
+ });
77
+ };
78
+ } else {
79
+ return function (notation) {
80
+ return notation;
81
+ };
82
+ }
83
+ }
84
+ /**
85
+ * Decompose the given string into piece symbol characters and sections of non piece symbol characters, and transform the piece symbols into
86
+ * React objects represented with the given chess font.
87
+ *
88
+ * @ignore
89
+ */
90
+
91
+
92
+ function figurineNotation(fontName, text) {
93
+ var result = [];
94
+ var beginOfText = 0;
95
+ var pieceSymbolIndex = 0;
96
+
97
+ for (var pos = 0; pos < text.length; ++pos) {
98
+ var currentChar = text.charAt(pos);
99
+
100
+ if (currentChar === 'K' || currentChar === 'Q' || currentChar === 'R' || currentChar === 'B' || currentChar === 'N' || currentChar === 'P') {
101
+ if (pos > beginOfText) {
102
+ result.push(text.substring(beginOfText, pos));
103
+ }
104
+
105
+ beginOfText = pos + 1;
106
+ var key = 'symbol-' + pieceSymbolIndex++;
107
+ result.push( /*#__PURE__*/_react["default"].createElement("span", {
108
+ className: 'kokopu-font-' + fontName,
109
+ key: key
110
+ }, currentChar));
111
+ }
112
+ }
113
+
114
+ if (beginOfText < text.length) {
115
+ result.push(text.substring(beginOfText));
116
+ }
117
+
118
+ return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, result);
119
+ }
@@ -35,9 +35,14 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
35
35
 
36
36
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
37
37
 
38
+ /**
39
+ * Those tags cannot have any children.
40
+ */
41
+ var NO_CHILDREN_TAGS = new Set(['br', 'hr', 'img']);
38
42
  /**
39
43
  * HTML sanitizer: parse HTML string, keeping only the allowed HTML nodes, and the corresponding React object.
40
44
  */
45
+
41
46
  var HtmlSanitizer = /*#__PURE__*/function () {
42
47
  function HtmlSanitizer(_ref) {
43
48
  var allowedTags = _ref.allowedTags,
@@ -101,7 +106,7 @@ var HtmlSanitizer = /*#__PURE__*/function () {
101
106
  if (node.type === 'text') {
102
107
  return node.data;
103
108
  } else if (node.type === 'tag') {
104
- var children = node.children.map(function (child) {
109
+ var children = NO_CHILDREN_TAGS.has(node.name) ? undefined : node.children.map(function (child) {
105
110
  return _this2._processNode(child);
106
111
  });
107
112
 
@@ -111,9 +116,9 @@ var HtmlSanitizer = /*#__PURE__*/function () {
111
116
  attributes['key'] = this._allocateKey();
112
117
  return /*#__PURE__*/_react["default"].createElement(node.name, attributes, children);
113
118
  } else {
114
- return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, {
119
+ return children ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, {
115
120
  key: this._allocateKey()
116
- }, children);
121
+ }, children) : undefined;
117
122
  }
118
123
  } else {
119
124
  return undefined;