plugin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * TinyMCE version 8.0.2 (2025-08-14)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. /* eslint-disable @typescript-eslint/no-wrapper-object-types */
  8. const hasProto = (v, constructor, predicate) => {
  9. var _a;
  10. if (predicate(v, constructor.prototype)) {
  11. return true;
  12. }
  13. else {
  14. // String-based fallback time
  15. return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
  16. }
  17. };
  18. const typeOf = (x) => {
  19. const t = typeof x;
  20. if (x === null) {
  21. return 'null';
  22. }
  23. else if (t === 'object' && Array.isArray(x)) {
  24. return 'array';
  25. }
  26. else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
  27. return 'string';
  28. }
  29. else {
  30. return t;
  31. }
  32. };
  33. const isType = (type) => (value) => typeOf(value) === type;
  34. const isSimpleType = (type) => (value) => typeof value === type;
  35. const isString = isType('string');
  36. const isObject = isType('object');
  37. const isArray = isType('array');
  38. const isFunction = isSimpleType('function');
  39. const nativeSlice = Array.prototype.slice;
  40. const nativePush = Array.prototype.push;
  41. const map = (xs, f) => {
  42. // pre-allocating array size when it's guaranteed to be known
  43. // http://jsperf.com/push-allocated-vs-dynamic/22
  44. const len = xs.length;
  45. const r = new Array(len);
  46. for (let i = 0; i < len; i++) {
  47. const x = xs[i];
  48. r[i] = f(x, i);
  49. }
  50. return r;
  51. };
  52. const flatten = (xs) => {
  53. // Note, this is possible because push supports multiple arguments:
  54. // http://jsperf.com/concat-push/6
  55. // Note that in the past, concat() would silently work (very slowly) for array-like objects.
  56. // With this change it will throw an error.
  57. const r = [];
  58. for (let i = 0, len = xs.length; i < len; ++i) {
  59. // Ensure that each value is an array itself
  60. if (!isArray(xs[i])) {
  61. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  62. }
  63. nativePush.apply(r, xs[i]);
  64. }
  65. return r;
  66. };
  67. const bind = (xs, f) => flatten(map(xs, f));
  68. isFunction(Array.from) ? Array.from : (x) => nativeSlice.call(x);
  69. var global$3 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  70. var global$2 = tinymce.util.Tools.resolve('tinymce.EditorManager');
  71. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  72. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  73. const option = (name) => (editor) => editor.options.get(name);
  74. const register = (editor) => {
  75. const registerOption = editor.options.register;
  76. const filterProcessor = (value) => isString(value) || isFunction(value) || isObject(value);
  77. registerOption('importcss_merge_classes', {
  78. processor: 'boolean',
  79. default: true
  80. });
  81. registerOption('importcss_exclusive', {
  82. processor: 'boolean',
  83. default: true
  84. });
  85. registerOption('importcss_selector_converter', {
  86. processor: 'function'
  87. });
  88. registerOption('importcss_selector_filter', {
  89. processor: filterProcessor
  90. });
  91. registerOption('importcss_file_filter', {
  92. processor: filterProcessor
  93. });
  94. registerOption('importcss_groups', {
  95. processor: 'object[]'
  96. });
  97. registerOption('importcss_append', {
  98. processor: 'boolean',
  99. default: false
  100. });
  101. };
  102. const shouldMergeClasses = option('importcss_merge_classes');
  103. const shouldImportExclusive = option('importcss_exclusive');
  104. const getSelectorConverter = option('importcss_selector_converter');
  105. const getSelectorFilter = option('importcss_selector_filter');
  106. const getCssGroups = option('importcss_groups');
  107. const shouldAppend = option('importcss_append');
  108. const getFileFilter = option('importcss_file_filter');
  109. const getSkin = option('skin');
  110. const getSkinUrl = option('skin_url');
  111. const generate = () => {
  112. const ungroupedOrder = [];
  113. const groupOrder = [];
  114. const groups = {};
  115. const addItemToGroup = (groupTitle, itemInfo) => {
  116. if (groups[groupTitle]) {
  117. groups[groupTitle].push(itemInfo);
  118. }
  119. else {
  120. groupOrder.push(groupTitle);
  121. groups[groupTitle] = [itemInfo];
  122. }
  123. };
  124. const addItem = (itemInfo) => {
  125. ungroupedOrder.push(itemInfo);
  126. };
  127. const toFormats = () => {
  128. const groupItems = bind(groupOrder, (g) => {
  129. const items = groups[g];
  130. return items.length === 0 ? [] : [{
  131. title: g,
  132. items
  133. }];
  134. });
  135. return groupItems.concat(ungroupedOrder);
  136. };
  137. return {
  138. addItemToGroup,
  139. addItem,
  140. toFormats
  141. };
  142. };
  143. const internalEditorStyle = /^\.(?:ephox|tiny-pageembed|mce)(?:[.-]+\w+)+$/;
  144. const removeCacheSuffix = (url) => {
  145. const cacheSuffix = global$1.cacheSuffix;
  146. if (isString(url)) {
  147. url = url.replace('?' + cacheSuffix, '').replace('&' + cacheSuffix, '');
  148. }
  149. return url;
  150. };
  151. const isSkinContentCss = (editor, href) => {
  152. const skin = getSkin(editor);
  153. if (skin) {
  154. const skinUrlBase = getSkinUrl(editor);
  155. const skinUrl = skinUrlBase ? editor.documentBaseURI.toAbsolute(skinUrlBase) : global$2.baseURL + '/skins/ui/' + skin;
  156. const contentSkinUrlPart = global$2.baseURL + '/skins/content/';
  157. const suffix = editor.editorManager.suffix;
  158. return href === skinUrl + '/content' + (editor.inline ? '.inline' : '') + `${suffix}.css` || href.indexOf(contentSkinUrlPart) !== -1;
  159. }
  160. return false;
  161. };
  162. const compileFilter = (filter) => {
  163. if (isString(filter)) {
  164. return (value) => {
  165. return value.indexOf(filter) !== -1;
  166. };
  167. }
  168. else if (filter instanceof RegExp) {
  169. return (value) => {
  170. return filter.test(value);
  171. };
  172. }
  173. return filter;
  174. };
  175. const isCssImportRule = (rule) => rule.styleSheet;
  176. const isCssPageRule = (rule) => rule.selectorText;
  177. const getSelectors = (editor, doc, fileFilter) => {
  178. const selectors = [];
  179. const contentCSSUrls = {};
  180. const append = (styleSheet, imported) => {
  181. let href = styleSheet.href;
  182. let rules;
  183. href = removeCacheSuffix(href);
  184. if (!href || fileFilter && !fileFilter(href, imported) || isSkinContentCss(editor, href)) {
  185. return;
  186. }
  187. // TODO: Is this still need as TypeScript/MDN says imports doesn't exist?
  188. global.each(styleSheet.imports, (styleSheet) => {
  189. append(styleSheet, true);
  190. });
  191. try {
  192. rules = styleSheet.cssRules || styleSheet.rules;
  193. }
  194. catch (_a) {
  195. // Firefox fails on rules to remote domain for example:
  196. // @import url(//fonts.googleapis.com/css?family=Pathway+Gothic+One);
  197. }
  198. global.each(rules, (cssRule) => {
  199. if (isCssImportRule(cssRule) && cssRule.styleSheet) {
  200. append(cssRule.styleSheet, true);
  201. }
  202. else if (isCssPageRule(cssRule)) {
  203. global.each(cssRule.selectorText.split(','), (selector) => {
  204. selectors.push(global.trim(selector));
  205. });
  206. }
  207. });
  208. };
  209. global.each(editor.contentCSS, (url) => {
  210. contentCSSUrls[url] = true;
  211. });
  212. if (!fileFilter) {
  213. fileFilter = (href, imported) => {
  214. return imported || contentCSSUrls[href];
  215. };
  216. }
  217. try {
  218. global.each(doc.styleSheets, (styleSheet) => {
  219. append(styleSheet);
  220. });
  221. }
  222. catch (_a) {
  223. // Ignore
  224. }
  225. return selectors;
  226. };
  227. const defaultConvertSelectorToFormat = (editor, selectorText) => {
  228. let format = {};
  229. // Parse simple element.class1, .class1
  230. const selector = /^(?:([a-z0-9\-_]+))?(\.[a-z0-9_\-\.]+)$/i.exec(selectorText);
  231. if (!selector) {
  232. return;
  233. }
  234. const elementName = selector[1];
  235. const classes = selector[2].substr(1).split('.').join(' ');
  236. const inlineSelectorElements = global.makeMap('a,img');
  237. // element.class - Produce block formats
  238. if (selector[1]) {
  239. format = {
  240. title: selectorText
  241. };
  242. if (editor.schema.getTextBlockElements()[elementName]) {
  243. // Text block format ex: h1.class1
  244. format.block = elementName;
  245. }
  246. else if (editor.schema.getBlockElements()[elementName] || inlineSelectorElements[elementName.toLowerCase()]) {
  247. // Block elements such as table.class and special inline elements such as a.class or img.class
  248. format.selector = elementName;
  249. }
  250. else {
  251. // Inline format strong.class1
  252. format.inline = elementName;
  253. }
  254. }
  255. else if (selector[2]) {
  256. // .class - Produce inline span with classes
  257. format = {
  258. inline: 'span',
  259. title: selectorText.substr(1),
  260. classes
  261. };
  262. }
  263. // Append to or override class attribute
  264. if (shouldMergeClasses(editor)) {
  265. format.classes = classes;
  266. }
  267. else {
  268. format.attributes = { class: classes };
  269. }
  270. return format;
  271. };
  272. const getGroupsBySelector = (groups, selector) => {
  273. return global.grep(groups, (group) => {
  274. return !group.filter || group.filter(selector);
  275. });
  276. };
  277. const compileUserDefinedGroups = (groups) => {
  278. return global.map(groups, (group) => {
  279. return global.extend({}, group, {
  280. original: group,
  281. selectors: {},
  282. filter: compileFilter(group.filter)
  283. });
  284. });
  285. };
  286. const isExclusiveMode = (editor, group) => {
  287. // Exclusive mode can only be disabled when there are groups allowing the same style to be present in multiple groups
  288. return group === null || shouldImportExclusive(editor);
  289. };
  290. const isUniqueSelector = (editor, selector, group, globallyUniqueSelectors) => {
  291. return !(isExclusiveMode(editor, group) ? selector in globallyUniqueSelectors : selector in group.selectors);
  292. };
  293. const markUniqueSelector = (editor, selector, group, globallyUniqueSelectors) => {
  294. if (isExclusiveMode(editor, group)) {
  295. globallyUniqueSelectors[selector] = true;
  296. }
  297. else {
  298. group.selectors[selector] = true;
  299. }
  300. };
  301. const convertSelectorToFormat = (editor, plugin, selector, group) => {
  302. let selectorConverter;
  303. const converter = getSelectorConverter(editor);
  304. if (group && group.selector_converter) {
  305. selectorConverter = group.selector_converter;
  306. }
  307. else if (converter) {
  308. selectorConverter = converter;
  309. }
  310. else {
  311. selectorConverter = () => {
  312. return defaultConvertSelectorToFormat(editor, selector);
  313. };
  314. }
  315. return selectorConverter.call(plugin, selector, group);
  316. };
  317. const setup = (editor) => {
  318. editor.on('init', () => {
  319. const model = generate();
  320. const globallyUniqueSelectors = {};
  321. const selectorFilter = compileFilter(getSelectorFilter(editor));
  322. const groups = compileUserDefinedGroups(getCssGroups(editor));
  323. const processSelector = (selector, group) => {
  324. if (isUniqueSelector(editor, selector, group, globallyUniqueSelectors)) {
  325. markUniqueSelector(editor, selector, group, globallyUniqueSelectors);
  326. const format = convertSelectorToFormat(editor, editor.plugins.importcss, selector, group);
  327. if (format) {
  328. const formatName = format.name || global$3.DOM.uniqueId();
  329. editor.formatter.register(formatName, format);
  330. return {
  331. title: format.title,
  332. format: formatName
  333. };
  334. }
  335. }
  336. return null;
  337. };
  338. global.each(getSelectors(editor, editor.getDoc(), compileFilter(getFileFilter(editor))), (selector) => {
  339. if (!internalEditorStyle.test(selector)) {
  340. if (!selectorFilter || selectorFilter(selector)) {
  341. const selectorGroups = getGroupsBySelector(groups, selector);
  342. if (selectorGroups.length > 0) {
  343. global.each(selectorGroups, (group) => {
  344. const menuItem = processSelector(selector, group);
  345. if (menuItem) {
  346. model.addItemToGroup(group.title, menuItem);
  347. }
  348. });
  349. }
  350. else {
  351. const menuItem = processSelector(selector, null);
  352. if (menuItem) {
  353. model.addItem(menuItem);
  354. }
  355. }
  356. }
  357. }
  358. });
  359. const items = model.toFormats();
  360. editor.dispatch('addStyleModifications', {
  361. items,
  362. replace: !shouldAppend(editor)
  363. });
  364. });
  365. };
  366. const get = (editor) => {
  367. const convertSelectorToFormat = (selectorText) => {
  368. return defaultConvertSelectorToFormat(editor, selectorText);
  369. };
  370. return {
  371. convertSelectorToFormat
  372. };
  373. };
  374. var Plugin = () => {
  375. global$4.add('importcss', (editor) => {
  376. register(editor);
  377. setup(editor);
  378. return get(editor);
  379. });
  380. };
  381. Plugin();
  382. /** *****
  383. * DO NOT EXPORT ANYTHING
  384. *
  385. * IF YOU DO ROLLUP WILL LEAVE A GLOBAL ON THE PAGE
  386. *******/
  387. })();