Skip to content

Commit 910e3e9

Browse files
authored
Merge pull request #1076 from Patternslib/improvements-from-modernize-markdown
Improvements from modernize markdown
2 parents af69e64 + 8bec57a commit 910e3e9

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/core/utils.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ const _MS_PER_DAY = 1000 * 60 * 60 * 24; // Milliseconds per day.
55

66
$.fn.safeClone = function () {
77
var $clone = this.clone();
8-
// IE BUG : Placeholder text becomes actual value after deep clone on textarea
8+
// IE 9-11 BUG : Placeholder text becomes actual value after deep clone on textarea
99
// https://connect.microsoft.com/IE/feedback/details/781612/placeholder-text-becomes-actual-value-after-deep-clone-on-textarea
10+
// Ref:
11+
// https://github.com/Patternslib/Patterns/issues/412
12+
// https://github.com/Patternslib/Patterns/pull/410
1013
if (window.document.documentMode) {
1114
$clone.findInclusive(":input[placeholder]").each(function (i, item) {
1215
var $item = $(item);
@@ -589,7 +592,9 @@ const localized_isodate = (date) => {
589592
* Replace HTML reserved characters with html entities to add HTML for user
590593
* editing to e.g. a textarea or a contenteditable.
591594
*
592-
* See: https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
595+
* See:
596+
* https://stackoverflow.com/a/22706073/1337474
597+
* https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
593598
*
594599
* @param {string} html - The HTML string to encode.
595600
*
@@ -600,17 +605,21 @@ const localized_isodate = (date) => {
600605
* ``"`` will be replaced with ``"``.
601606
*/
602607
const escape_html = (html) => {
603-
return (html || "")
604-
.replace(/&/g, "&") // needs to be first!
605-
.replace(/</g, "&lt;")
606-
.replace(/>/g, "&gt;")
607-
.replace(/"/g, "&quot;");
608+
if (!html) {
609+
return "";
610+
}
611+
const el = document.createElement("div");
612+
el.appendChild(document.createTextNode(html));
613+
// Return escaped html and also replace quotes.
614+
return el.innerHTML.replace(/"/g, "&quot;");
608615
};
609616

610617
/**
611618
* Return unescaped, raw HTML from an escaped HTML string.
612619
*
613-
* See: https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
620+
* See:
621+
* https://stackoverflow.com/a/34064434/1337474
622+
* https://developer.mozilla.org/en-US/docs/Glossary/Entity#reserved_characters
614623
*
615624
* @param {string} escaped_html - The HTML string to decode.
616625
*
@@ -621,11 +630,12 @@ const escape_html = (html) => {
621630
* ``&quot;`` will be replaced with ``"``.
622631
*/
623632
const unescape_html = (escaped_html) => {
624-
return (escaped_html || "")
625-
.replace(/&amp;/g, "&")
626-
.replace(/&lt;/g, "<")
627-
.replace(/&gt;/g, ">")
628-
.replace(/&quot;/g, '"');
633+
if (!escaped_html) {
634+
return "";
635+
}
636+
const doc = new DOMParser().parseFromString(escaped_html, "text/html");
637+
// Return unescaped html and also unescape quote named entities.
638+
return doc.documentElement.textContent.replace(/&quot;/g, '"');
629639
};
630640

631641
/**

src/patterns.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Import base
66
import "./globals";
77
import registry from "./core/registry";
8-
import "modernizr";
98

109
// Import all used patterns for the bundle to be generated
1110
import "./core/push_kit";
@@ -74,4 +73,12 @@ import "@patternslib/pat-upload";
7473
// Set to ``true`` to include core styles via JavaScript
7574
//window.__patternslib_import_styles = false;
7675

76+
// Include modernizr per default.
77+
// Most of our styles depend on it.
78+
// You might want to disable it for your project by setting:
79+
// window.__patternslib_disable_modernizr = true;
80+
if (!window.__patternslib_disable_modernizr) {
81+
import("modernizr");
82+
}
83+
7784
registry.init();

0 commit comments

Comments
 (0)