1
1
'use strict' ;
2
2
3
+ var _ = require ( 'lodash' ) ;
4
+
3
5
/**
4
6
* Generates attributes in HTML.
5
7
* @param {Object } attributes
@@ -403,6 +405,45 @@ function _getObjectProperty(sourceObject, path) {
403
405
} , sourceObject ) ;
404
406
}
405
407
408
+ /**
409
+ * Clean up duplicate classes in HTML string.
410
+ * @param {string } value - HTML string that might contain class attributes
411
+ * @returns {string } - HTML string with unique classes
412
+ * @private
413
+ */
414
+ function _cleanDuplicateClasses ( value ) {
415
+ if ( ! value . includes ( 'class="' ) ) {
416
+ return value ;
417
+ }
418
+ return value . replace ( / c l a s s = " ( [ ^ " ] * ) " / g, function ( match , classString ) {
419
+ var uniqueClasses = [ ...new Set ( classString . split ( / \s + / ) . filter ( Boolean ) ) ] ;
420
+ return 'class="' + uniqueClasses . join ( ' ' ) + '"' ;
421
+ } ) ;
422
+ }
423
+
424
+ /**
425
+ * Escape HTML special characters.
426
+ * @param {string } value - String to escape
427
+ * @returns {string } - Escaped string
428
+ * @private
429
+ */
430
+ function _escapeHTML ( value ) {
431
+ return _ . escape ( value ) ;
432
+ }
433
+
434
+ /**
435
+ * Format HTML string value by cleaning classes and escaping
436
+ * @param {string } value - String to process
437
+ * @returns {string } - Processed string
438
+ * @private
439
+ */
440
+ function _formatHTMLString ( value ) {
441
+ if ( value . includes ( 'clickable-value' ) ) {
442
+ return value ;
443
+ }
444
+ return _escapeHTML ( _cleanDuplicateClasses ( value ) ) ;
445
+ }
446
+
406
447
module . exports = {
407
448
addArrow : _addArrow ,
408
449
addToolsButtons : _addToolsButtons ,
@@ -426,5 +467,6 @@ module.exports = {
426
467
wrapInSelectTag : _wrapInSelectTag ,
427
468
wrapInCheckBox : _wrapInCheckBox ,
428
469
valueNeedsQuotes : _valueNeedsQuotes ,
429
- createDefaultSpan : _createDefaultSpan
470
+ createDefaultSpan : _createDefaultSpan ,
471
+ formatHTMLString : _formatHTMLString
430
472
} ;
0 commit comments