1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
|
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
'use strict';
/**
* Block style type.
*
* Read more in the {@link CKEDITOR.style} class documentation.
*
* @readonly
* @property {Number} [=1]
* @member CKEDITOR
*/
CKEDITOR.STYLE_BLOCK = 1;
/**
* Inline style type.
*
* Read more in the {@link CKEDITOR.style} class documentation.
*
* @readonly
* @property {Number} [=2]
* @member CKEDITOR
*/
CKEDITOR.STYLE_INLINE = 2;
/**
* Object style type.
*
* Read more in the {@link CKEDITOR.style} class documentation.
*
* @readonly
* @property {Number} [=3]
* @member CKEDITOR
*/
CKEDITOR.STYLE_OBJECT = 3;
( function() {
var blockElements = {
address: 1, div: 1, h1: 1, h2: 1, h3: 1, h4: 1, h5: 1, h6: 1, p: 1,
pre: 1, section: 1, header: 1, footer: 1, nav: 1, article: 1, aside: 1, figure: 1,
dialog: 1, hgroup: 1, time: 1, meter: 1, menu: 1, command: 1, keygen: 1, output: 1,
progress: 1, details: 1, datagrid: 1, datalist: 1
},
objectElements = {
a: 1, blockquote: 1, embed: 1, hr: 1, img: 1, li: 1, object: 1, ol: 1, table: 1, td: 1,
tr: 1, th: 1, ul: 1, dl: 1, dt: 1, dd: 1, form: 1, audio: 1, video: 1
};
var semicolonFixRegex = /\s*(?:;\s*|$)/,
varRegex = /#\((.+?)\)/g;
var notBookmark = CKEDITOR.dom.walker.bookmark( 0, 1 ),
nonWhitespaces = CKEDITOR.dom.walker.whitespaces( 1 );
/**
* A class representing a style instance for the specific style definition.
* In this approach, a style is a set of properties, like attributes and styles,
* which can be applied to and removed from a {@link CKEDITOR.dom.selection selection} through
* {@link CKEDITOR.editor editor} methods: {@link CKEDITOR.editor#applyStyle} and {@link CKEDITOR.editor#removeStyle},
* respectively.
*
* Three default style types are available: {@link CKEDITOR#STYLE_BLOCK STYLE_BLOCK}, {@link CKEDITOR#STYLE_INLINE STYLE_INLINE},
* and {@link CKEDITOR#STYLE_OBJECT STYLE_OBJECT}. Based on its type, a style heavily changes its behavior.
* You can read more about style types in the {@glink features/styles#style-types Style Types section of the Styles guide}.
*
* It is possible to define a custom style type by subclassing this class by using the {@link #addCustomHandler} method.
* However, because of great complexity of the styles handling job, it is only possible in very specific cases.
*
* ### Usage
*
* Basic usage:
*
* // Define a block style.
* var style = new CKEDITOR.style( { element: 'h1' } );
*
* // Considering the following selection:
* // <p>Foo</p><p>Bar^</p>
* // Executing:
* editor.applyStyle( style );
* // Will give:
* // <p>Foo</p><h1>Bar^</h1>
* style.checkActive( editor.elementPath(), editor ); // -> true
*
* editor.removeStyle( style );
* // Will give:
* // <p>Foo</p><p>Bar^</p>
*
* style.checkActive( editor.elementPath(), editor ); // -> false
*
* Object style:
*
* // Define an object style.
* var style = new CKEDITOR.style( { element: 'img', attributes: { 'class': 'foo' } } );
*
* // Considering the following selection:
* // <p><img src="bar.png" alt="" />Foo^</p>
* // Executing:
* editor.applyStyle( style );
* // Will not apply the style, because the image is not selected.
* // You can check if a style can be applied on the current selection with:
* style.checkApplicable( editor.elementPath(), editor ); // -> false
*
* // Considering the following selection:
* // <p>[<img src="bar.png" alt="" />]Foo</p>
* // Executing
* editor.applyStyle( style );
* // Will give:
* // <p>[<img src="bar.png" alt="" class="foo" />]Foo</p>
*
* ### API changes introduced in CKEditor 4.4.0
*
* Before CKEditor 4.4.0 all style instances had no access at all to the {@link CKEDITOR.editor editor instance}
* within which the style is used. Neither the style constructor, nor style methods were requiring
* passing the editor instance which made styles independent of the editor and hence its settings and state.
* This design decision came from CKEditor 3; it started causing problems and became an unsolvable obstacle for
* the {@link CKEDITOR.style.customHandlers.widget widget style handler} which we introduced in CKEditor 4.4.
*
* There were two possible solutions. Passing an editor instance to the style constructor or to every method.
* The first approach would be clean, however, having in mind the backward compatibility, we did not decide
* to go for it. It would bind the style to one editor instance, making it unusable with other editor instances.
* That could break many implementations reusing styles between editors. Therefore, we decided to take the longer
* but safer path — the editor instance became an argument for nearly all style methods, however,
* for backward compatibility reasons, all these methods will work without it. Even the newly
* implemented {@link CKEDITOR.style.customHandlers.widget widget style handler}'s methods will not fail,
* although they will also not work by aborting at an early stage.
*
* Therefore, you can safely upgrade to CKEditor 4.4.0 even if you use style methods without providing
* the editor instance. You must only align your code if your implementation should handle widget styles
* or any other custom style handler. Of course, we recommend doing this in any case to avoid potential
* problems in the future.
*
* @class
* @constructor Creates a style class instance.
* @param {CKEDITOR.style.definition} styleDefinition
* @param variablesValues
*/
CKEDITOR.style = function( styleDefinition, variablesValues ) {
if ( typeof styleDefinition.type == 'string' )
return new CKEDITOR.style.customHandlers[ styleDefinition.type ]( styleDefinition );
// Inline style text as attribute should be converted
// to styles object.
var attrs = styleDefinition.attributes;
if ( attrs && attrs.style ) {
styleDefinition.styles = CKEDITOR.tools.extend( {},
styleDefinition.styles, CKEDITOR.tools.parseCssText( attrs.style ) );
delete attrs.style;
}
if ( variablesValues ) {
styleDefinition = CKEDITOR.tools.clone( styleDefinition );
replaceVariables( styleDefinition.attributes, variablesValues );
replaceVariables( styleDefinition.styles, variablesValues );
}
var element = this.element = styleDefinition.element ?
(
typeof styleDefinition.element == 'string' ?
styleDefinition.element.toLowerCase() : styleDefinition.element
) : '*';
this.type = styleDefinition.type ||
(
blockElements[ element ] ? CKEDITOR.STYLE_BLOCK :
objectElements[ element ] ? CKEDITOR.STYLE_OBJECT :
CKEDITOR.STYLE_INLINE
);
// If the 'element' property is an object with a set of possible element, it will be applied like an object style: only to existing elements
if ( typeof this.element == 'object' )
this.type = CKEDITOR.STYLE_OBJECT;
this._ = {
definition: styleDefinition
};
};
CKEDITOR.style.prototype = {
/**
* Applies the style on the editor's current selection.
*
* Before the style is applied, the method checks if the {@link #checkApplicable style is applicable}.
*
* **Note:** The recommended way of applying the style is by using the
* {@link CKEDITOR.editor#applyStyle} method, which is a shorthand for this method.
*
* @param {CKEDITOR.editor/CKEDITOR.dom.document} editor The editor instance in which
* the style will be applied.
* A {@link CKEDITOR.dom.document} instance is accepted for backward compatibility
* reasons, although since CKEditor 4.4.0 this type of argument is deprecated. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
*/
apply: function( editor ) {
// Backward compatibility.
if ( editor instanceof CKEDITOR.dom.document )
return applyStyleOnSelection.call( this, editor.getSelection() );
if ( this.checkApplicable( editor.elementPath(), editor ) ) {
var initialEnterMode = this._.enterMode;
// See comment in removeStyle.
if ( !initialEnterMode )
this._.enterMode = editor.activeEnterMode;
applyStyleOnSelection.call( this, editor.getSelection(), 0, editor );
this._.enterMode = initialEnterMode;
}
},
/**
* Removes the style from the editor's current selection.
*
* Before the style is applied, the method checks if {@link #checkApplicable style could be applied}.
*
* **Note:** The recommended way of removing the style is by using the
* {@link CKEDITOR.editor#removeStyle} method, which is a shorthand for this method.
*
* @param {CKEDITOR.editor/CKEDITOR.dom.document} editor The editor instance in which
* the style will be removed.
* A {@link CKEDITOR.dom.document} instance is accepted for backward compatibility
* reasons, although since CKEditor 4.4.0 this type of argument is deprecated. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
*/
remove: function( editor ) {
// Backward compatibility.
if ( editor instanceof CKEDITOR.dom.document )
return applyStyleOnSelection.call( this, editor.getSelection(), 1 );
if ( this.checkApplicable( editor.elementPath(), editor ) ) {
var initialEnterMode = this._.enterMode;
// Before CKEditor 4.4.0 style knew nothing about editor, so in order to provide enterMode
// which should be used developers were forced to hack the style object (see https://dev.ckeditor.com/ticket/10190).
// Since CKEditor 4.4.0 style knows about editor (at least when it's being applied/removed), but we
// use _.enterMode for backward compatibility with those hacks.
// Note: we should not change style's enter mode if it was already set.
if ( !initialEnterMode )
this._.enterMode = editor.activeEnterMode;
applyStyleOnSelection.call( this, editor.getSelection(), 1, editor );
this._.enterMode = initialEnterMode;
}
},
/**
* Applies the style on the provided range. Unlike {@link #apply} this
* method does not take care of setting the selection, however, the range
* is updated to the correct place.
*
* **Note:** If you want to apply the style on the editor selection,
* you probably want to use {@link CKEDITOR.editor#applyStyle}.
*
* @param {CKEDITOR.dom.range} range
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
*/
applyToRange: function( range ) {
this.applyToRange =
this.type == CKEDITOR.STYLE_INLINE ? applyInlineStyle :
this.type == CKEDITOR.STYLE_BLOCK ? applyBlockStyle :
this.type == CKEDITOR.STYLE_OBJECT ? applyObjectStyle :
null;
return this.applyToRange( range );
},
/**
* Removes the style from the provided range. Unlike {@link #remove} this
* method does not take care of setting the selection, however, the range
* is updated to the correct place.
*
* **Note:** If you want to remove the style from the editor selection,
* you probably want to use {@link CKEDITOR.editor#removeStyle}.
*
* @param {CKEDITOR.dom.range} range
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
*/
removeFromRange: function( range ) {
this.removeFromRange =
this.type == CKEDITOR.STYLE_INLINE ? removeInlineStyle :
this.type == CKEDITOR.STYLE_BLOCK ? removeBlockStyle :
this.type == CKEDITOR.STYLE_OBJECT ? removeObjectStyle :
null;
return this.removeFromRange( range );
},
/**
* Applies the style to the element. This method bypasses all checks
* and applies the style attributes directly on the provided element. Use with caution.
*
* See {@link CKEDITOR.editor#applyStyle}.
*
* @param {CKEDITOR.dom.element} element
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
*/
applyToObject: function( element ) {
setupElement( element, this );
},
/**
* Gets the style state inside the elements path.
*
* @param {CKEDITOR.dom.elementPath} elementPath
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
* @returns {Boolean} `true` if the element is active in the elements path.
*/
checkActive: function( elementPath, editor ) {
switch ( this.type ) {
case CKEDITOR.STYLE_BLOCK:
return this.checkElementRemovable( elementPath.block || elementPath.blockLimit, true, editor );
case CKEDITOR.STYLE_OBJECT:
case CKEDITOR.STYLE_INLINE:
var elements = elementPath.elements;
for ( var i = 0, element; i < elements.length; i++ ) {
element = elements[ i ];
if ( this.type == CKEDITOR.STYLE_INLINE && ( element == elementPath.block || element == elementPath.blockLimit ) )
continue;
if ( this.type == CKEDITOR.STYLE_OBJECT ) {
var name = element.getName();
if ( !( typeof this.element == 'string' ? name == this.element : name in this.element ) )
continue;
}
if ( this.checkElementRemovable( element, true, editor ) )
return true;
}
}
return false;
},
/**
* Whether this style can be applied at the specified elements path.
*
* @param {CKEDITOR.dom.elementPath} elementPath The elements path to
* check the style against.
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
* @param {CKEDITOR.filter} [filter] If defined, the style will be
* checked against this filter as well.
* @returns {Boolean} `true` if this style can be applied at the elements path.
*/
checkApplicable: function( elementPath, editor, filter ) {
// Backward compatibility.
if ( editor && editor instanceof CKEDITOR.filter )
filter = editor;
if ( filter && !filter.check( this ) )
return false;
switch ( this.type ) {
case CKEDITOR.STYLE_OBJECT:
return !!elementPath.contains( this.element );
case CKEDITOR.STYLE_BLOCK:
return !!elementPath.blockLimit.getDtd()[ this.element ];
}
return true;
},
/**
* Checks if the element matches the current style definition.
*
* @param {CKEDITOR.dom.element} element
* @param {Boolean} fullMatch
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
* @returns {Boolean}
*/
checkElementMatch: function( element, fullMatch ) {
var def = this._.definition;
if ( !element || !def.ignoreReadonly && element.isReadOnly() )
return false;
var attribs,
name = element.getName();
// If the element name is the same as the style name.
if ( typeof this.element == 'string' ? name == this.element : name in this.element ) {
// If no attributes are defined in the element.
if ( !fullMatch && !element.hasAttributes() )
return true;
attribs = getAttributesForComparison( def );
if ( attribs._length ) {
for ( var attName in attribs ) {
if ( attName == '_length' )
continue;
var elementAttr = element.getAttribute( attName ) || '';
// Special treatment for 'style' attribute is required.
if ( attName == 'style' ? compareCssText( attribs[ attName ], elementAttr ) : attribs[ attName ] == elementAttr ) {
if ( !fullMatch )
return true;
} else if ( fullMatch ) {
return false;
}
}
if ( fullMatch )
return true;
} else {
return true;
}
}
return false;
},
/**
* Checks if an element, or any of its attributes, is removable by the
* current style definition.
*
* @param {CKEDITOR.dom.element} element
* @param {Boolean} fullMatch
* @param {CKEDITOR.editor} editor The editor instance. Required argument since
* CKEditor 4.4. The style system will work without it, but it is highly
* recommended to provide it for integration with all features. Read more about
* the signature change in the {@link CKEDITOR.style} documentation.
* @returns {Boolean}
*/
checkElementRemovable: function( element, fullMatch, editor ) {
// Check element matches the style itself.
if ( this.checkElementMatch( element, fullMatch, editor ) )
return true;
// Check if the element matches the style overrides.
var override = getOverrides( this )[ element.getName() ];
if ( override ) {
var attribs, attName;
// If no attributes have been defined, remove the element.
if ( !( attribs = override.attributes ) )
return true;
for ( var i = 0; i < attribs.length; i++ ) {
attName = attribs[ i ][ 0 ];
var actualAttrValue = element.getAttribute( attName );
if ( actualAttrValue ) {
var attValue = attribs[ i ][ 1 ];
// Remove the attribute if:
// - The override definition value is null;
// - The override definition value is a string that
// matches the attribute value exactly.
// - The override definition value is a regex that
// has matches in the attribute value.
if ( attValue === null )
return true;
if ( typeof attValue == 'string' ) {
if ( actualAttrValue == attValue )
return true;
} else if ( attValue.test( actualAttrValue ) ) {
return true;
}
}
}
}
return false;
},
/**
* Builds the preview HTML based on the styles definition.
*
* @param {String} [label] The label used in the style preview.
* @return {String} The HTML of preview.
*/
buildPreview: function( label ) {
var styleDefinition = this._.definition,
html = [],
elementName = styleDefinition.element;
// Avoid <bdo> in the preview.
if ( elementName == 'bdo' )
elementName = 'span';
html = [ '<', elementName ];
// Assign all defined attributes.
var attribs = styleDefinition.attributes;
if ( attribs ) {
for ( var att in attribs )
html.push( ' ', att, '="', attribs[ att ], '"' );
}
// Assign the style attribute.
var cssStyle = CKEDITOR.style.getStyleText( styleDefinition );
if ( cssStyle )
html.push( ' style="', cssStyle, '"' );
html.push( '>', ( label || styleDefinition.name ), '</', elementName, '>' );
return html.join( '' );
},
/**
* Returns the style definition.
*
* @since 4.1.0
* @returns {Object}
*/
getDefinition: function() {
return this._.definition;
}
/**
* If defined (for example by {@link CKEDITOR.style#addCustomHandler custom style handler}), it returns
* the {@link CKEDITOR.filter.allowedContentRules allowed content rules} which should be added to the
* {@link CKEDITOR.filter} when enabling this style.
*
* **Note:** This method is not defined in the {@link CKEDITOR.style} class.
*
* @since 4.4.0
* @method toAllowedContentRules
* @param {CKEDITOR.editor} [editor] The editor instance.
* @returns {CKEDITOR.filter.allowedContentRules} The rules that should represent this style in the {@link CKEDITOR.filter}.
*/
};
/**
* Builds the inline style text based on the style definition.
*
* @static
* @param styleDefinition
* @returns {String} Inline style text.
*/
CKEDITOR.style.getStyleText = function( styleDefinition ) {
// If we have already computed it, just return it.
var stylesDef = styleDefinition._ST;
if ( stylesDef )
return stylesDef;
stylesDef = styleDefinition.styles;
// Builds the StyleText.
var stylesText = ( styleDefinition.attributes && styleDefinition.attributes.style ) || '',
specialStylesText = '';
if ( stylesText.length )
stylesText = stylesText.replace( semicolonFixRegex, ';' );
for ( var style in stylesDef ) {
var styleVal = stylesDef[ style ],
text = ( style + ':' + styleVal ).replace( semicolonFixRegex, ';' );
// Some browsers don't support 'inherit' property value, leave them intact. (https://dev.ckeditor.com/ticket/5242)
if ( styleVal == 'inherit' )
specialStylesText += text;
else
stylesText += text;
}
// Browsers make some changes to the style when applying them. So, here
// we normalize it to the browser format.
if ( stylesText.length )
stylesText = CKEDITOR.tools.normalizeCssText( stylesText, true );
stylesText += specialStylesText;
// Return it, saving it to the next request.
return ( styleDefinition._ST = stylesText );
};
/**
* Namespace containing custom style handlers added with {@link CKEDITOR.style#addCustomHandler}.
*
* @since 4.4.0
* @class
* @singleton
*/
CKEDITOR.style.customHandlers = {};
/**
* List of all elements that are ignored during styling.
*
* @since 4.15.0
* @property {String[]} [unstylableElements=[]]
* @member CKEDITOR.style
*/
CKEDITOR.style.unstylableElements = [];
/**
* Creates a {@link CKEDITOR.style} subclass and registers it in the style system.
* Registered class will be used as a handler for a style of this type. This allows
* to extend the styles system, which by default uses only the {@link CKEDITOR.style}, with
* new functionality. Registered classes are accessible in the {@link CKEDITOR.style.customHandlers}.
*
* ### The Style Class Definition
*
* The definition object is used to override properties in a prototype inherited
* from the {@link CKEDITOR.style} class. It must contain a `type` property which is
* a name of the new type and therefore it must be unique. The default style types
* ({@link CKEDITOR#STYLE_BLOCK STYLE_BLOCK}, {@link CKEDITOR#STYLE_INLINE STYLE_INLINE},
* and {@link CKEDITOR#STYLE_OBJECT STYLE_OBJECT}) are integers, but for easier identification
* it is recommended to use strings as custom type names.
*
* Besides `type`, the definition may contain two more special properties:
*
* * `setup {Function}` – An optional callback executed when a style instance is created.
* Like the style constructor, it is executed in style context and with the style definition as an argument.
* * `assignedTo {Number}` – Can be set to one of the default style types. Some editor
* features like the Styles drop-down assign styles to one of the default groups based on
* the style type. By using this property it is possible to notify them to which group this
* custom style should be assigned. It defaults to the {@link CKEDITOR#STYLE_OBJECT}.
*
* Other properties of the definition object will just be used to extend the prototype inherited
* from the {@link CKEDITOR.style} class. So if the definition contains an `apply` method, it will
* override the {@link CKEDITOR.style#apply} method.
*
* ### Usage
*
* Registering a basic handler:
*
* var styleClass = CKEDITOR.style.addCustomHandler( {
* type: 'custom'
* } );
*
* var style = new styleClass( { ... } );
* style instanceof styleClass; // -> true
* style instanceof CKEDITOR.style; // -> true
* style.type; // -> 'custom'
*
* The {@link CKEDITOR.style} constructor used as a factory:
*
* var styleClass = CKEDITOR.style.addCustomHandler( {
* type: 'custom'
* } );
*
* // Style constructor accepts style definition (do not confuse with style class definition).
* var style = new CKEDITOR.style( { type: 'custom', attributes: ... } );
* style instanceof styleClass; // -> true
*
* Thanks to that, integration code using styles does not need to know
* which style handler it should use. It is determined by the {@link CKEDITOR.style} constructor.
*
* Overriding existing {@link CKEDITOR.style} methods:
*
* var styleClass = CKEDITOR.style.addCustomHandler( {
* type: 'custom',
* apply: function( editor ) {
* console.log( 'apply' );
* },
* remove: function( editor ) {
* console.log( 'remove' );
* }
* } );
*
* var style = new CKEDITOR.style( { type: 'custom', attributes: ... } );
* editor.applyStyle( style ); // logged 'apply'
*
* style = new CKEDITOR.style( { element: 'img', attributes: { 'class': 'foo' } } );
* editor.applyStyle( style ); // style is really applied if image was selected
*
* ### Practical Recommendations
*
* The style handling job, which includes such tasks as applying, removing, checking state, and
* checking if a style can be applied, is very complex. Therefore without deep knowledge
* about DOM and especially {@link CKEDITOR.dom.range ranges} and {@link CKEDITOR.dom.walker DOM walker} it is impossible
* to implement a completely custom style handler able to handle block, inline, and object type styles.
* However, it is possible to customize the default implementation by overriding default methods and
* reusing them.
*
* The only style handler which can be implemented from scratch without huge effort is a style
* applicable to objects ({@glink features/styles#style-types read more about types}).
* Such style can only be applied when a specific object is selected. An example implementation can
* be found in the [widget plugin](https://github.com/ckeditor/ckeditor4/blob/master/plugins/widget/plugin.js).
*
* When implementing a style handler from scratch at least the following methods must be defined:
*
* * {@link CKEDITOR.style#apply apply} and {@link CKEDITOR.style#remove remove},
* * {@link CKEDITOR.style#checkElementRemovable checkElementRemovable} and
* {@link CKEDITOR.style#checkElementMatch checkElementMatch} – Note that both methods reuse the same logic,
* * {@link CKEDITOR.style#checkActive checkActive} – Reuses
* {@link CKEDITOR.style#checkElementMatch checkElementMatch},
* * {@link CKEDITOR.style#toAllowedContentRules toAllowedContentRules} – Not required, but very useful in
* case of a custom style that has to notify the {@link CKEDITOR.filter} which rules it allows when registered.
*
* @since 4.4.0
* @static
* @member CKEDITOR.style
* @param definition The style class definition.
* @returns {CKEDITOR.style} The new style class created for the provided definition.
*/
CKEDITOR.style.addCustomHandler = function( definition ) {
var styleClass = function( styleDefinition ) {
this._ = {
definition: styleDefinition
};
if ( this.setup )
this.setup( styleDefinition );
};
styleClass.prototype = CKEDITOR.tools.extend(
// Prototype of CKEDITOR.style.
CKEDITOR.tools.prototypedCopy( CKEDITOR.style.prototype ),
// Defaults.
{
assignedTo: CKEDITOR.STYLE_OBJECT
},
// Passed definition - overrides.
definition,
true
);
this.customHandlers[ definition.type ] = styleClass;
return styleClass;
};
// Gets the parent element which blocks the styling for an element. This
// can be done through read-only elements (contenteditable=false) or
// elements with the "data-nostyle" attribute.
function getUnstylableParent( element, root ) {
var unstylable, editable;
while ( ( element = element.getParent() ) ) {
if ( element.equals( root ) )
break;
if ( element.getAttribute( 'data-nostyle' ) )
unstylable = element;
else if ( !editable ) {
var contentEditable = element.getAttribute( 'contentEditable' );
if ( contentEditable == 'false' )
unstylable = element;
else if ( contentEditable == 'true' )
editable = 1;
}
}
return unstylable;
}
var posPrecedingIdenticalContained =
CKEDITOR.POSITION_PRECEDING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED,
posFollowingIdenticalContained =
CKEDITOR.POSITION_FOLLOWING | CKEDITOR.POSITION_IDENTICAL | CKEDITOR.POSITION_IS_CONTAINED;
// Checks if the current node can be a child of the style element.
function checkIfNodeCanBeChildOfStyle( def, currentNode, lastNode, nodeName, dtd, nodeIsNoStyle, nodeIsReadonly, includeReadonly ) {
// Style can be applied to text node.
if ( !nodeName )
return 1;
// Style definitely cannot be applied if DTD or data-nostyle do not allow.
if ( !dtd[ nodeName ] || nodeIsNoStyle )
return 0;
// Non-editable element cannot be styled is we shouldn't include readonly elements.
if ( nodeIsReadonly && !includeReadonly )
return 0;
// Check that we haven't passed lastNode yet and that style's childRule allows this style on current element.
return checkPositionAndRule( currentNode, lastNode, def, posPrecedingIdenticalContained );
}
// Check if the style element can be a child of the current
// node parent or if the element is not defined in the DTD.
function checkIfStyleCanBeChildOf( def, currentParent, elementName, isUnknownElement ) {
return currentParent &&
( ( currentParent.getDtd() || CKEDITOR.dtd.span )[ elementName ] || isUnknownElement ) &&
( !def.parentRule || def.parentRule( currentParent ) );
}
function checkIfStartsRange( nodeName, currentNode, lastNode ) {
return (
!nodeName || !CKEDITOR.dtd.$removeEmpty[ nodeName ] ||
( currentNode.getPosition( lastNode ) | posPrecedingIdenticalContained ) == posPrecedingIdenticalContained
);
}
function checkIfTextOrReadonlyOrEmptyElement( currentNode, nodeIsReadonly ) {
var nodeType = currentNode.type;
return nodeType == CKEDITOR.NODE_TEXT || nodeIsReadonly || ( nodeType == CKEDITOR.NODE_ELEMENT && !currentNode.getChildCount() );
}
// Checks if position is a subset of posBitFlags and that nodeA fulfills style def rule.
function checkPositionAndRule( nodeA, nodeB, def, posBitFlags ) {
return ( nodeA.getPosition( nodeB ) | posBitFlags ) == posBitFlags &&
( !def.childRule || def.childRule( nodeA ) );
}
function applyInlineStyle( range ) {
var document = range.document;
if ( range.collapsed ) {
// Create the element to be inserted in the DOM.
var collapsedElement = getElement( this, document );
// Insert the empty element into the DOM at the range position.
range.insertNode( collapsedElement );
// Place the selection right inside the empty element.
range.moveToPosition( collapsedElement, CKEDITOR.POSITION_BEFORE_END );
return;
}
var elementName = this.element,
def = this._.definition,
isUnknownElement;
// Indicates that fully selected read-only elements are to be included in the styling range.
var ignoreReadonly = def.ignoreReadonly,
includeReadonly = ignoreReadonly || def.includeReadonly;
// If the read-only inclusion is not available in the definition, try
// to get it from the root data (most often it's the editable).
if ( includeReadonly == null )
includeReadonly = range.root.getCustomData( 'cke_includeReadonly' );
// Get the DTD definition for the element. Defaults to "span".
var dtd = CKEDITOR.dtd[ elementName ];
if ( !dtd ) {
isUnknownElement = true;
dtd = CKEDITOR.dtd.span;
}
// Expand the range.
range.enlarge( CKEDITOR.ENLARGE_INLINE, 1 );
range.trim();
// Get the first node to be processed and the last, which concludes the processing.
var boundaryNodes = range.createBookmark(),
firstNode = boundaryNodes.startNode,
lastNode = boundaryNodes.endNode,
currentNode = firstNode,
styleRange;
if ( !ignoreReadonly ) {
// Check if the boundaries are inside non stylable elements.
var root = range.getCommonAncestor(),
firstUnstylable = getUnstylableParent( firstNode, root ),
lastUnstylable = getUnstylableParent( lastNode, root );
// If the first element can't be styled, we'll start processing right
// after its unstylable root.
if ( firstUnstylable )
currentNode = firstUnstylable.getNextSourceNode( true );
// If the last element can't be styled, we'll stop processing on its
// unstylable root.
if ( lastUnstylable )
lastNode = lastUnstylable;
}
// Do nothing if the current node now follows the last node to be processed.
if ( currentNode.getPosition( lastNode ) == CKEDITOR.POSITION_FOLLOWING )
currentNode = 0;
while ( currentNode ) {
var applyStyle = false;
if ( currentNode.equals( lastNode ) ) {
currentNode = null;
applyStyle = true;
} else {
var nodeName = currentNode.type == CKEDITOR.NODE_ELEMENT ? currentNode.getName() : null,
nodeIsReadonly = nodeName && ( currentNode.getAttribute( 'contentEditable' ) == 'false' ),
nodeIsUnstylable = nodeName &&
CKEDITOR.tools.array.indexOf( CKEDITOR.style.unstylableElements, nodeName ) !== -1,
nodeIsNoStyle = nodeName && ( currentNode.getAttribute( 'data-nostyle' ) || nodeIsUnstylable );
// Skip bookmarks or comments.
if ( ( nodeName && currentNode.data( 'cke-bookmark' ) ) || currentNode.type === CKEDITOR.NODE_COMMENT ) {
currentNode = currentNode.getNextSourceNode( true );
continue;
}
// Find all nested editables of a non-editable block and apply this style inside them.
if ( nodeIsReadonly && includeReadonly && CKEDITOR.dtd.$block[ nodeName ] )
applyStyleOnNestedEditables.call( this, currentNode );
// Check if the current node can be a child of the style element.
if ( checkIfNodeCanBeChildOfStyle( def, currentNode, lastNode, nodeName, dtd, nodeIsNoStyle, nodeIsReadonly, includeReadonly ) ) {
var currentParent = currentNode.getParent();
// Check if the style element can be a child of the current
// node parent or if the element is not defined in the DTD.
if ( checkIfStyleCanBeChildOf( def, currentParent, elementName, isUnknownElement ) ) {
// This node will be part of our range, so if it has not
// been started, place its start right before the node.
// In the case of an element node, it will be included
// only if it is entirely inside the range.
if ( !styleRange && checkIfStartsRange( nodeName, currentNode, lastNode ) ) {
styleRange = range.clone();
styleRange.setStartBefore( currentNode );
}
// Non element nodes, readonly elements, or empty
// elements can be added completely to the range.
if ( checkIfTextOrReadonlyOrEmptyElement( currentNode, nodeIsReadonly ) ) {
var includedNode = currentNode;
var parentNode;
// This node is about to be included completely, but,
// if this is the last node in its parent, we must also
// check if the parent itself can be added completely
// to the range, otherwise apply the style immediately.
while (
( applyStyle = !includedNode.getNext( notBookmark ) ) &&
( parentNode = includedNode.getParent(), dtd[ parentNode.getName() ] ) &&
checkPositionAndRule( parentNode, firstNode, def, posFollowingIdenticalContained )
) {
includedNode = parentNode;
}
styleRange.setEndAfter( includedNode );
}
} else {
applyStyle = true;
}
}
// Style isn't applicable to current element, so apply style to
// range ending at previously chosen position, or nowhere if we haven't
// yet started styleRange.
else {
applyStyle = true;
}
// Get the next node to be processed.
// If we're currently on a non-editable element or non-styleable element,
// then we'll be moved to current node's sibling (or even further), so we'll
// avoid messing up its content.
currentNode = currentNode.getNextSourceNode( nodeIsNoStyle || nodeIsReadonly );
}
// Apply the style if we have something to which apply it.
if ( applyStyle && styleRange && !styleRange.collapsed ) {
// Build the style element, based on the style object definition.
var styleNode = getElement( this, document ),
styleHasAttrs = styleNode.hasAttributes();
// Get the element that holds the entire range.
var parent = styleRange.getCommonAncestor();
var removeList = {
styles: {},
attrs: {},
// Styles cannot be removed.
blockedStyles: {},
// Attrs cannot be removed.
blockedAttrs: {}
};
var attName, styleName, value;
// Loop through the parents, removing the redundant attributes
// from the element to be applied.
while ( styleNode && parent ) {
if ( parent.getName() == elementName ) {
for ( attName in def.attributes ) {
if ( removeList.blockedAttrs[ attName ] || !( value = parent.getAttribute( styleName ) ) )
continue;
if ( styleNode.getAttribute( attName ) == value )
removeList.attrs[ attName ] = 1;
else
removeList.blockedAttrs[ attName ] = 1;
}
for ( styleName in def.styles ) {
if ( removeList.blockedStyles[ styleName ] || !( value = parent.getStyle( styleName ) ) )
continue;
if ( styleNode.getStyle( styleName ) == value )
removeList.styles[ styleName ] = 1;
else
removeList.blockedStyles[ styleName ] = 1;
}
}
parent = parent.getParent();
}
for ( attName in removeList.attrs )
styleNode.removeAttribute( attName );
for ( styleName in removeList.styles )
styleNode.removeStyle( styleName );
if ( styleHasAttrs && !styleNode.hasAttributes() )
styleNode = null;
if ( styleNode ) {
// Move the contents of the range to the style element.
styleRange.extractContents().appendTo( styleNode );
// Insert it into the range position (it is collapsed after
// extractContents.
styleRange.insertNode( styleNode );
// Here we do some cleanup, removing all duplicated
// elements from the style element.
removeFromInsideElement.call( this, styleNode );
// Let's merge our new style with its neighbors, if possible.
styleNode.mergeSiblings();
// As the style system breaks text nodes constantly, let's normalize
// things for performance.
// With IE, some paragraphs get broken when calling normalize()
// repeatedly. Also, for IE, we must normalize body, not documentElement.
// IE is also known for having a "crash effect" with normalize().
// We should try to normalize with IE too in some way, somewhere.
if ( !CKEDITOR.env.ie )
styleNode.$.normalize();
}
// Style already inherit from parents, left just to clear up any internal overrides. (https://dev.ckeditor.com/ticket/5931)
else {
styleNode = new CKEDITOR.dom.element( 'span' );
styleRange.extractContents().appendTo( styleNode );
styleRange.insertNode( styleNode );
removeFromInsideElement.call( this, styleNode );
styleNode.remove( true );
}
// Style applied, let's release the range, so it gets
// re-initialization in the next loop.
styleRange = null;
}
}
// Remove the bookmark nodes.
range.moveToBookmark( boundaryNodes );
// Minimize the result range to exclude empty text nodes. (https://dev.ckeditor.com/ticket/5374)
range.shrink( CKEDITOR.SHRINK_TEXT );
// Get inside the remaining element if range.shrink( TEXT ) has failed because of non-editable elements inside.
// E.g. range.shrink( TEXT ) will not get inside:
// [<b><i contenteditable="false">x</i></b>]
// but range.shrink( ELEMENT ) will.
range.shrink( CKEDITOR.NODE_ELEMENT, true );
}
function removeInlineStyle( range ) {
// Make sure our range has included all "collpased" parent inline nodes so
// that our operation logic can be simpler.
range.enlarge( CKEDITOR.ENLARGE_INLINE, 1 );
var bookmark = range.createBookmark(),
startNode = bookmark.startNode,
alwaysRemoveElement = this._.definition.alwaysRemoveElement;
if ( range.collapsed ) {
var startPath = new CKEDITOR.dom.elementPath( startNode.getParent(), range.root ),
// The topmost element in elements path which we should jump out of.
boundaryElement;
for ( var i = 0, element; i < startPath.elements.length && ( element = startPath.elements[ i ] ); i++ ) {
// 1. If it's collaped inside text nodes, try to remove the style from the whole element.
//
// 2. Otherwise if it's collapsed on element boundaries, moving the selection
// outside the styles instead of removing the whole tag,
// also make sure other inner styles were well preserved.(https://dev.ckeditor.com/ticket/3309)
//
// 3. Force removing the element even if it's an boundary element when alwaysRemoveElement is true.
// Without it, the links won't be unlinked if the cursor is placed right before/after it. (https://dev.ckeditor.com/ticket/13062)
if ( element == startPath.block || element == startPath.blockLimit ) {
break;
}
if ( this.checkElementRemovable( element ) ) {
var isStart;
if ( !alwaysRemoveElement && range.collapsed && ( range.checkBoundaryOfElement( element, CKEDITOR.END ) || ( isStart = range.checkBoundaryOfElement( element, CKEDITOR.START ) ) ) ) {
boundaryElement = element;
boundaryElement.match = isStart ? 'start' : 'end';
} else {
// Before removing the style node, there may be a sibling to the style node
// that's exactly the same to the one to be removed. To the user, it makes
// no difference that they're separate entities in the DOM tree. So, merge
// them before removal.
element.mergeSiblings();
if ( element.is( this.element ) ) {
removeFromElement.call( this, element );
} else {
removeOverrides( element, getOverrides( this )[ element.getName() ] );
}
}
}
}
// Re-create the style tree after/before the boundary element,
// the replication start from bookmark start node to define the
// new range.
if ( boundaryElement ) {
var clonedElement = startNode;
for ( i = 0; ; i++ ) {
var newElement = startPath.elements[ i ];
if ( newElement.equals( boundaryElement ) )
break;
// Avoid copying any matched element.
else if ( newElement.match )
continue;
else
newElement = newElement.clone();
newElement.append( clonedElement );
clonedElement = newElement;
}
clonedElement[ boundaryElement.match == 'start' ? 'insertBefore' : 'insertAfter' ]( boundaryElement );
}
} else {
// Now our range isn't collapsed. Lets walk from the start node to the end
// node via DFS and remove the styles one-by-one.
var endNode = bookmark.endNode,
me = this;
breakNodes();
// Now, do the DFS walk.
var currentNode = startNode;
while ( !currentNode.equals( endNode ) ) {
// Need to get the next node first because removeFromElement() can remove
// the current node from DOM tree.
var nextNode = currentNode.getNextSourceNode();
if ( currentNode.type == CKEDITOR.NODE_ELEMENT && this.checkElementRemovable( currentNode ) ) {
// Remove style from element or overriding element.
if ( currentNode.getName() == this.element )
removeFromElement.call( this, currentNode );
else
removeOverrides( currentNode, getOverrides( this )[ currentNode.getName() ] );
// removeFromElement() may have merged the next node with something before
// the startNode via mergeSiblings(). In that case, the nextNode would
// contain startNode and we'll have to call breakNodes() again and also
// reassign the nextNode to something after startNode.
if ( nextNode.type == CKEDITOR.NODE_ELEMENT && nextNode.contains( startNode ) ) {
breakNodes();
nextNode = startNode.getNext();
}
}
currentNode = nextNode;
}
}
range.moveToBookmark( bookmark );
// See the comment for range.shrink in applyInlineStyle.
range.shrink( CKEDITOR.NODE_ELEMENT, true );
// Find out the style ancestor that needs to be broken down at startNode
// and endNode.
function breakNodes() {
var startPath = new CKEDITOR.dom.elementPath( startNode.getParent() ),
endPath = new CKEDITOR.dom.elementPath( endNode.getParent() ),
breakStart = null,
breakEnd = null;
for ( var i = 0; i < startPath.elements.length; i++ ) {
var element = startPath.elements[ i ];
if ( element == startPath.block || element == startPath.blockLimit )
break;
if ( me.checkElementRemovable( element, true ) )
breakStart = element;
}
for ( i = 0; i < endPath.elements.length; i++ ) {
element = endPath.elements[ i ];
if ( element == endPath.block || element == endPath.blockLimit )
break;
if ( me.checkElementRemovable( element, true ) )
breakEnd = element;
}
if ( breakEnd )
endNode.breakParent( breakEnd );
if ( breakStart )
startNode.breakParent( breakStart );
}
}
// Apply style to nested editables inside editablesContainer.
// @param {CKEDITOR.dom.element} editablesContainer
// @context CKEDITOR.style
function applyStyleOnNestedEditables( editablesContainer ) {
var editables = findNestedEditables( editablesContainer ),
editable,
l = editables.length,
i = 0,
range = l && new CKEDITOR.dom.range( editablesContainer.getDocument() );
for ( ; i < l; ++i ) {
editable = editables[ i ];
// Check if style is allowed by this editable's ACF.
if ( checkIfAllowedInEditable( editable, this ) ) {
range.selectNodeContents( editable );
applyInlineStyle.call( this, range );
}
}
}
// Finds nested editables within container. Does not return
// editables nested in another editable (twice).
function findNestedEditables( container ) {
var editables = [];
container.forEach( function( element ) {
if ( element.getAttribute( 'contenteditable' ) == 'true' ) {
editables.push( element );
return false; // Skip children.
}
}, CKEDITOR.NODE_ELEMENT, true );
return editables;
}
// Checks if style is allowed in this editable.
function checkIfAllowedInEditable( editable, style ) {
var filter = CKEDITOR.filter.instances[ editable.data( 'cke-filter' ) ];
return filter ? filter.check( style ) : 1;
}
// Checks if style is allowed by iterator's active filter.
function checkIfAllowedByIterator( iterator, style ) {
return iterator.activeFilter ? iterator.activeFilter.check( style ) : 1;
}
function applyObjectStyle( range ) {
// Selected or parent element. (https://dev.ckeditor.com/ticket/9651)
var start = range.getEnclosedNode() || range.getCommonAncestor( false, true ),
element = new CKEDITOR.dom.elementPath( start, range.root ).contains( this.element, 1 );
element && !element.isReadOnly() && setupElement( element, this );
}
function removeObjectStyle( range ) {
var parent = range.getCommonAncestor( true, true ),
element = new CKEDITOR.dom.elementPath( parent, range.root ).contains( this.element, 1 );
if ( !element )
return;
var style = this,
def = style._.definition,
attributes = def.attributes;
// Remove all defined attributes.
if ( attributes ) {
for ( var att in attributes )
element.removeAttribute( att, attributes[ att ] );
}
// Assign all defined styles.
if ( def.styles ) {
for ( var i in def.styles ) {
if ( def.styles.hasOwnProperty( i ) )
element.removeStyle( i );
}
}
}
function applyBlockStyle( range ) {
// Serializible bookmarks is needed here since
// elements may be merged.
var bookmark = range.createBookmark( true );
var iterator = range.createIterator();
iterator.enforceRealBlocks = true;
// make recognize <br /> tag as a separator in ENTER_BR mode (https://dev.ckeditor.com/ticket/5121)
if ( this._.enterMode )
iterator.enlargeBr = ( this._.enterMode != CKEDITOR.ENTER_BR );
var block,
doc = range.document,
newBlock;
while ( ( block = iterator.getNextParagraph() ) ) {
if ( !block.isReadOnly() && checkIfAllowedByIterator( iterator, this ) ) {
newBlock = getElement( this, doc, block );
replaceBlock( block, newBlock );
}
}
range.moveToBookmark( bookmark );
}
function removeBlockStyle( range ) {
// Serializible bookmarks is needed here since
// elements may be merged.
var bookmark = range.createBookmark( 1 );
var iterator = range.createIterator();
iterator.enforceRealBlocks = true;
iterator.enlargeBr = this._.enterMode != CKEDITOR.ENTER_BR;
var block,
newBlock;
while ( ( block = iterator.getNextParagraph() ) ) {
if ( this.checkElementRemovable( block ) ) {
// <pre> get special treatment.
if ( block.is( 'pre' ) ) {
newBlock = this._.enterMode == CKEDITOR.ENTER_BR ? null :
range.document.createElement( this._.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );
newBlock && block.copyAttributes( newBlock );
replaceBlock( block, newBlock );
} else {
removeFromElement.call( this, block );
}
}
}
range.moveToBookmark( bookmark );
}
// Replace the original block with new one, with special treatment
// for <pre> blocks to make sure content format is well preserved, and merging/splitting adjacent
// when necessary. (https://dev.ckeditor.com/ticket/3188)
function replaceBlock( block, newBlock ) {
// Block is to be removed, create a temp element to
// save contents.
var removeBlock = !newBlock;
if ( removeBlock ) {
newBlock = block.getDocument().createElement( 'div' );
block.copyAttributes( newBlock );
}
var newBlockIsPre = newBlock && newBlock.is( 'pre' ),
blockIsPre = block.is( 'pre' ),
isToPre = newBlockIsPre && !blockIsPre,
isFromPre = !newBlockIsPre && blockIsPre;
if ( isToPre )
newBlock = toPre( block, newBlock );
else if ( isFromPre )
// Split big <pre> into pieces before start to convert.
newBlock = fromPres( removeBlock ? [ block.getHtml() ] : splitIntoPres( block ), newBlock );
else
block.moveChildren( newBlock );
newBlock.replace( block );
if ( newBlockIsPre ) {
// Merge previous <pre> blocks.
mergePre( newBlock );
} else if ( removeBlock ) {
removeNoAttribsElement( newBlock );
}
}
// Merge a <pre> block with a previous sibling if available.
function mergePre( preBlock ) {
var previousBlock;
if ( !( ( previousBlock = preBlock.getPrevious( nonWhitespaces ) ) && previousBlock.type == CKEDITOR.NODE_ELEMENT && previousBlock.is( 'pre' ) ) )
return;
// Merge the previous <pre> block contents into the current <pre>
// block.
//
// Another thing to be careful here is that currentBlock might contain
// a '\n' at the beginning, and previousBlock might contain a '\n'
// towards the end. These new lines are not normally displayed but they
// become visible after merging.
var mergedHtml = replace( previousBlock.getHtml(), /\n$/, '' ) + '\n\n' +
replace( preBlock.getHtml(), /^\n/, '' );
// Krugle: IE normalizes innerHTML from <pre>, breaking whitespaces.
if ( CKEDITOR.env.ie )
preBlock.$.outerHTML = '<pre>' + mergedHtml + '</pre>';
else
preBlock.setHtml( mergedHtml );
previousBlock.remove();
}
// Split into multiple <pre> blocks separated by double line-break.
function splitIntoPres( preBlock ) {
// Exclude the ones at header OR at tail,
// and ignore bookmark content between them.
var duoBrRegex = /(\S\s*)\n(?:\s|(<span[^>]+data-cke-bookmark.*?\/span>))*\n(?!$)/gi,
pres = [],
splitedHtml = replace( preBlock.getOuterHtml(), duoBrRegex, function( match, charBefore, bookmark ) {
return charBefore + '</pre>' + bookmark + '<pre>';
} );
splitedHtml.replace( /<pre\b.*?>([\s\S]*?)<\/pre>/gi, function( match, preContent ) {
pres.push( preContent );
} );
return pres;
}
// Wrapper function of String::replace without considering of head/tail bookmarks nodes.
function replace( str, regexp, replacement ) {
var headBookmark = '',
tailBookmark = '';
str = str.replace( /(^<span[^>]+data-cke-bookmark.*?\/span>)|(<span[^>]+data-cke-bookmark.*?\/span>$)/gi, function( str, m1, m2 ) {
m1 && ( headBookmark = m1 );
m2 && ( tailBookmark = m2 );
return '';
} );
return headBookmark + str.replace( regexp, replacement ) + tailBookmark;
}
// Converting a list of <pre> into blocks with format well preserved.
function fromPres( preHtmls, newBlock ) {
var docFrag;
if ( preHtmls.length > 1 )
docFrag = new CKEDITOR.dom.documentFragment( newBlock.getDocument() );
for ( var i = 0; i < preHtmls.length; i++ ) {
var blockHtml = preHtmls[ i ];
// 1. Trim the first and last line-breaks immediately after and before <pre>,
// they're not visible.
blockHtml = blockHtml.replace( /(\r\n|\r)/g, '\n' );
blockHtml = replace( blockHtml, /^[ \t]*\n/, '' );
blockHtml = replace( blockHtml, /\n$/, '' );
// 2. Convert spaces or tabs at the beginning or at the end to
blockHtml = replace( blockHtml, /^[ \t]+|[ \t]+$/g, function( match, offset ) {
if ( match.length == 1 ) // one space, preserve it
return ' ';
else if ( !offset ) // beginning of block
return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
else // end of block
return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
} );
// 3. Convert \n to <BR>.
// 4. Convert contiguous (i.e. non-singular) spaces or tabs to
blockHtml = blockHtml.replace( /\n/g, '<br>' );
blockHtml = blockHtml.replace( /[ \t]{2,}/g, function( match ) {
return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
} );
if ( docFrag ) {
var newBlockClone = newBlock.clone();
newBlockClone.setHtml( blockHtml );
docFrag.append( newBlockClone );
} else {
newBlock.setHtml( blockHtml );
}
}
return docFrag || newBlock;
}
// Converting from a non-PRE block to a PRE block in formatting operations.
function toPre( block, newBlock ) {
var bogus = block.getBogus();
bogus && bogus.remove();
// First trim the block content.
var preHtml = block.getHtml();
// 1. Trim head/tail spaces, they're not visible.
preHtml = replace( preHtml, /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, '' );
// 2. Delete ANSI whitespaces immediately before and after <BR> because
// they are not visible.
preHtml = preHtml.replace( /[ \t\r\n]*(<br[^>]*>)[ \t\r\n]*/gi, '$1' );
// 3. Compress other ANSI whitespaces since they're only visible as one
// single space previously.
// 4. Convert to spaces since is no longer needed in <PRE>.
preHtml = preHtml.replace( /([ \t\n\r]+| )/g, ' ' );
// 5. Convert any <BR /> to \n. This must not be done earlier because
// the \n would then get compressed.
preHtml = preHtml.replace( /<br\b[^>]*>/gi, '\n' );
// Krugle: IE normalizes innerHTML to <pre>, breaking whitespaces.
if ( CKEDITOR.env.ie ) {
var temp = block.getDocument().createElement( 'div' );
temp.append( newBlock );
newBlock.$.outerHTML = '<pre>' + preHtml + '</pre>';
newBlock.copyAttributes( temp.getFirst() );
newBlock = temp.getFirst().remove();
} else {
newBlock.setHtml( preHtml );
}
return newBlock;
}
// Removes a style from an element itself, don't care about its subtree.
function removeFromElement( element, keepDataAttrs ) {
var def = this._.definition,
attributes = def.attributes,
styles = def.styles,
overrides = getOverrides( this )[ element.getName() ],
// If the style is only about the element itself, we have to remove the element.
removeEmpty = CKEDITOR.tools.isEmpty( attributes ) && CKEDITOR.tools.isEmpty( styles );
// Remove definition attributes/style from the elemnt.
for ( var attName in attributes ) {
// The 'class' element value must match (https://dev.ckeditor.com/ticket/1318).
if ( ( attName == 'class' || this._.definition.fullMatch ) && element.getAttribute( attName ) != normalizeProperty( attName, attributes[ attName ] ) )
continue;
// Do not touch data-* attributes (https://dev.ckeditor.com/ticket/11011) (https://dev.ckeditor.com/ticket/11258).
if ( keepDataAttrs && attName.slice( 0, 5 ) == 'data-' )
continue;
removeEmpty = element.hasAttribute( attName );
element.removeAttribute( attName );
}
for ( var styleName in styles ) {
// Full match style insist on having fully equivalence. (https://dev.ckeditor.com/ticket/5018)
if ( this._.definition.fullMatch && element.getStyle( styleName ) != normalizeProperty( styleName, styles[ styleName ], true ) )
continue;
removeEmpty = removeEmpty || !!element.getStyle( styleName );
element.removeStyle( styleName );
}
// Remove overrides, but don't remove the element if it's a block element
removeOverrides( element, overrides, blockElements[ element.getName() ] );
if ( removeEmpty ) {
if ( this._.definition.alwaysRemoveElement )
removeNoAttribsElement( element, 1 );
else {
if ( !CKEDITOR.dtd.$block[ element.getName() ] || this._.enterMode == CKEDITOR.ENTER_BR && !element.hasAttributes() )
removeNoAttribsElement( element );
else
element.renameNode( this._.enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' );
}
}
}
// Removes a style from inside an element. Called on applyStyle to make cleanup
// before apply. During clean up this function keep data-* attribute in contrast
// to removeFromElement.
function removeFromInsideElement( element ) {
var overrides = getOverrides( this ),
innerElements = element.getElementsByTag( this.element ),
innerElement;
for ( var i = innerElements.count(); --i >= 0; ) {
innerElement = innerElements.getItem( i );
// Do not remove elements which are read only (e.g. duplicates inside widgets).
if ( !innerElement.isReadOnly() )
removeFromElement.call( this, innerElement, true );
}
// Now remove any other element with different name that is
// defined to be overriden.
for ( var overrideElement in overrides ) {
if ( overrideElement != this.element ) {
innerElements = element.getElementsByTag( overrideElement );
for ( i = innerElements.count() - 1; i >= 0; i-- ) {
innerElement = innerElements.getItem( i );
// Do not remove elements which are read only (e.g. duplicates inside widgets).
if ( !innerElement.isReadOnly() )
removeOverrides( innerElement, overrides[ overrideElement ] );
}
}
}
}
// Remove overriding styles/attributes from the specific element.
// Note: Remove the element if no attributes remain.
// @param {Object} element
// @param {Object} overrides
// @param {Boolean} Don't remove the element
function removeOverrides( element, overrides, dontRemove ) {
var attributes = overrides && overrides.attributes;
if ( attributes ) {
for ( var i = 0; i < attributes.length; i++ ) {
var attName = attributes[ i ][ 0 ],
actualAttrValue;
if ( ( actualAttrValue = element.getAttribute( attName ) ) ) {
var attValue = attributes[ i ][ 1 ];
// Remove the attribute if:
// - The override definition value is null ;
// - The override definition valie is a string that
// matches the attribute value exactly.
// - The override definition value is a regex that
// has matches in the attribute value.
if ( attValue === null || ( attValue.test && attValue.test( actualAttrValue ) ) || ( typeof attValue == 'string' && actualAttrValue == attValue ) )
element.removeAttribute( attName );
}
}
}
if ( !dontRemove )
removeNoAttribsElement( element );
}
// If the element has no more attributes, remove it.
function removeNoAttribsElement( element, forceRemove ) {
// If no more attributes remained in the element, remove it,
// leaving its children.
if ( !element.hasAttributes() || forceRemove ) {
if ( CKEDITOR.dtd.$block[ element.getName() ] ) {
var previous = element.getPrevious( nonWhitespaces ),
next = element.getNext( nonWhitespaces );
if ( previous && ( previous.type == CKEDITOR.NODE_TEXT || !previous.isBlockBoundary( { br: 1 } ) ) )
element.append( 'br', 1 );
if ( next && ( next.type == CKEDITOR.NODE_TEXT || !next.isBlockBoundary( { br: 1 } ) ) )
element.append( 'br' );
element.remove( true );
} else {
// Removing elements may open points where merging is possible,
// so let's cache the first and last nodes for later checking.
var firstChild = element.getFirst();
var lastChild = element.getLast();
element.remove( true );
if ( firstChild ) {
// Check the cached nodes for merging.
firstChild.type == CKEDITOR.NODE_ELEMENT && firstChild.mergeSiblings();
if ( lastChild && !firstChild.equals( lastChild ) && lastChild.type == CKEDITOR.NODE_ELEMENT )
lastChild.mergeSiblings();
}
}
}
}
function getElement( style, targetDocument, element ) {
var el,
elementName = style.element;
// The "*" element name will always be a span for this function.
if ( elementName == '*' )
elementName = 'span';
// Create the element.
el = new CKEDITOR.dom.element( elementName, targetDocument );
// https://dev.ckeditor.com/ticket/6226: attributes should be copied before the new ones are applied
if ( element )
element.copyAttributes( el );
el = setupElement( el, style );
// Avoid ID duplication.
if ( targetDocument.getCustomData( 'doc_processing_style' ) && el.hasAttribute( 'id' ) )
el.removeAttribute( 'id' );
else
targetDocument.setCustomData( 'doc_processing_style', 1 );
return el;
}
function setupElement( el, style ) {
var def = style._.definition,
attributes = def.attributes,
styles = CKEDITOR.style.getStyleText( def );
// Assign all defined attributes.
if ( attributes ) {
for ( var att in attributes )
el.setAttribute( att, attributes[ att ] );
}
// Assign all defined styles.
if ( styles )
el.setAttribute( 'style', styles );
el.getDocument().removeCustomData( 'doc_processing_style' );
return el;
}
function replaceVariables( list, variablesValues ) {
for ( var item in list ) {
list[ item ] = list[ item ].replace( varRegex, function( match, varName ) {
return variablesValues[ varName ];
} );
}
}
// Returns an object that can be used for style matching comparison.
// Attributes names and values are all lowercased, and the styles get
// merged with the style attribute.
function getAttributesForComparison( styleDefinition ) {
// If we have already computed it, just return it.
var attribs = styleDefinition._AC;
if ( attribs )
return attribs;
attribs = {};
var length = 0;
// Loop through all defined attributes.
var styleAttribs = styleDefinition.attributes;
if ( styleAttribs ) {
for ( var styleAtt in styleAttribs ) {
length++;
attribs[ styleAtt ] = styleAttribs[ styleAtt ];
}
}
// Includes the style definitions.
var styleText = CKEDITOR.style.getStyleText( styleDefinition );
if ( styleText ) {
if ( !attribs.style )
length++;
attribs.style = styleText;
}
// Appends the "length" information to the object.
attribs._length = length;
// Return it, saving it to the next request.
return ( styleDefinition._AC = attribs );
}
// Get the the collection used to compare the elements and attributes,
// defined in this style overrides, with other element. All information in
// it is lowercased.
// @param {CKEDITOR.style} style
function getOverrides( style ) {
if ( style._.overrides )
return style._.overrides;
var overrides = ( style._.overrides = {} ),
definition = style._.definition.overrides;
if ( definition ) {
// The override description can be a string, object or array.
// Internally, well handle arrays only, so transform it if needed.
if ( !CKEDITOR.tools.isArray( definition ) )
definition = [ definition ];
// Loop through all override definitions.
for ( var i = 0; i < definition.length; i++ ) {
var override = definition[ i ],
elementName,
overrideEl,
attrs;
// If can be a string with the element name.
if ( typeof override == 'string' )
elementName = override.toLowerCase();
// Or an object.
else {
elementName = override.element ? override.element.toLowerCase() : style.element;
attrs = override.attributes;
}
// We can have more than one override definition for the same
// element name, so we attempt to simply append information to
// it if it already exists.
overrideEl = overrides[ elementName ] || ( overrides[ elementName ] = {} );
if ( attrs ) {
// The returning attributes list is an array, because we
// could have different override definitions for the same
// attribute name.
var overrideAttrs = ( overrideEl.attributes = overrideEl.attributes || [] );
for ( var attName in attrs ) {
// Each item in the attributes array is also an array,
// where [0] is the attribute name and [1] is the
// override value.
overrideAttrs.push( [ attName.toLowerCase(), attrs[ attName ] ] );
}
}
}
}
return overrides;
}
// Make the comparison of attribute value easier by standardizing it.
function normalizeProperty( name, value, isStyle ) {
var temp = new CKEDITOR.dom.element( 'span' );
temp[ isStyle ? 'setStyle' : 'setAttribute' ]( name, value );
return temp[ isStyle ? 'getStyle' : 'getAttribute' ]( name );
}
// Compare two bunch of styles, with the speciality that value 'inherit'
// is treated as a wildcard which will match any value.
// @param {Object/String} source
// @param {Object/String} target
// @returns {Boolean}
function compareCssText( source, target ) {
function filter( string, propertyName ) {
// In case of font-families we'll skip quotes. (https://dev.ckeditor.com/ticket/10750)
return propertyName.toLowerCase() == 'font-family' ? string.replace( /["']/g, '' ) : string;
}
if ( typeof source == 'string' )
source = CKEDITOR.tools.parseCssText( source );
if ( typeof target == 'string' )
target = CKEDITOR.tools.parseCssText( target, true );
for ( var name in source ) {
if ( !( name in target ) ) {
return false;
}
if ( !( filter( target[ name ], name ) == filter( source[ name ], name ) ||
source[ name ] == 'inherit' ||
target[ name ] == 'inherit' ) ) {
return false;
}
}
return true;
}
function applyStyleOnSelection( selection, remove, editor ) {
var ranges = selection.getRanges(),
func = remove ? this.removeFromRange : this.applyToRange,
range;
var iterator = ranges.createIterator();
while ( ( range = iterator.getNextRange() ) )
func.call( this, range, editor );
selection.selectRanges( ranges );
}
} )();
/**
* Generic style command. It applies a specific style when executed.
*
* var boldStyle = new CKEDITOR.style( { element: 'strong' } );
* // Register the "bold" command, which applies the bold style.
* editor.addCommand( 'bold', new CKEDITOR.styleCommand( boldStyle ) );
*
* @class
* @constructor Creates a styleCommand class instance.
* @extends CKEDITOR.commandDefinition
* @param {CKEDITOR.style} style The style to be applied when command is executed.
* @param {Object} [ext] Additional command definition's properties.
*/
CKEDITOR.styleCommand = function( style, ext ) {
this.style = style;
this.allowedContent = style;
this.requiredContent = style;
CKEDITOR.tools.extend( this, ext, true );
};
/**
* @param {CKEDITOR.editor} editor
* @todo
*/
CKEDITOR.styleCommand.prototype.exec = function( editor ) {
editor.focus();
if ( this.state == CKEDITOR.TRISTATE_OFF )
editor.applyStyle( this.style );
else if ( this.state == CKEDITOR.TRISTATE_ON )
editor.removeStyle( this.style );
};
/**
* Manages styles registration and loading. See also {@link CKEDITOR.config#stylesSet}.
*
* **Note** This object is an instance of {@link CKEDITOR.resourceManager}.
*
* // The set of styles for the <b>Styles</b> drop-down list.
* CKEDITOR.stylesSet.add( 'default', [
* // Block Styles
* { name: 'Blue Title', element: 'h3', styles: { 'color': 'Blue' } },
* { name: 'Red Title', element: 'h3', styles: { 'color': 'Red' } },
*
* // Inline Styles
* { name: 'Marker: Yellow', element: 'span', styles: { 'background-color': 'Yellow' } },
* { name: 'Marker: Green', element: 'span', styles: { 'background-color': 'Lime' } },
*
* // Object Styles
* {
* name: 'Image on Left',
* element: 'img',
* attributes: {
* style: 'padding: 5px; margin-right: 5px',
* border: '2',
* align: 'left'
* }
* }
* ] );
*
* @since 3.2.0
* @class
* @singleton
* @extends CKEDITOR.resourceManager
*/
CKEDITOR.stylesSet = new CKEDITOR.resourceManager( '', 'stylesSet' );
// Backward compatibility (https://dev.ckeditor.com/ticket/5025).
CKEDITOR.addStylesSet = CKEDITOR.tools.bind( CKEDITOR.stylesSet.add, CKEDITOR.stylesSet );
CKEDITOR.loadStylesSet = function( name, url, callback ) {
CKEDITOR.stylesSet.addExternal( name, url, '' );
CKEDITOR.stylesSet.load( name, callback );
};
CKEDITOR.tools.extend( CKEDITOR.editor.prototype, {
/**
* Registers a function to be called whenever the selection position changes in the
* editing area. The current state is passed to the function. The possible
* states are {@link CKEDITOR#TRISTATE_ON} and {@link CKEDITOR#TRISTATE_OFF}.
*
* // Create a style object for the <b> element.
* var style = new CKEDITOR.style( { element: 'b' } );
* var editor = CKEDITOR.instances.editor1;
* editor.attachStyleStateChange( style, function( state ) {
* if ( state == CKEDITOR.TRISTATE_ON )
* alert( 'The current state for the B element is ON' );
* else
* alert( 'The current state for the B element is OFF' );
* } );
*
* @member CKEDITOR.editor
* @param {CKEDITOR.style} style The style to be watched.
* @param {Function} callback The function to be called.
*/
attachStyleStateChange: function( style, callback ) {
// Try to get the list of attached callbacks.
var styleStateChangeCallbacks = this._.styleStateChangeCallbacks;
// If it doesn't exist, it means this is the first call. So, let's create
// all the structure to manage the style checks and the callback calls.
if ( !styleStateChangeCallbacks ) {
// Create the callbacks array.
styleStateChangeCallbacks = this._.styleStateChangeCallbacks = [];
// Attach to the selectionChange event, so we can check the styles at
// that point.
this.on( 'selectionChange', function( ev ) {
// Loop throw all registered callbacks.
for ( var i = 0; i < styleStateChangeCallbacks.length; i++ ) {
var callback = styleStateChangeCallbacks[ i ];
// Check the current state for the style defined for that callback.
var currentState = callback.style.checkActive( ev.data.path, this ) ?
CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;
// Call the callback function, passing the current state to it.
callback.fn.call( this, currentState );
}
} );
}
// Save the callback info, so it can be checked on the next occurrence of
// selectionChange.
styleStateChangeCallbacks.push( { style: style, fn: callback } );
},
/**
* Applies the style upon the editor's current selection. Shorthand for
* {@link CKEDITOR.style#apply}.
*
* @member CKEDITOR.editor
* @param {CKEDITOR.style} style
*/
applyStyle: function( style ) {
style.apply( this );
},
/**
* Removes the style from the editor's current selection. Shorthand for
* {@link CKEDITOR.style#remove}.
*
* @member CKEDITOR.editor
* @param {CKEDITOR.style} style
*/
removeStyle: function( style ) {
style.remove( this );
},
/**
* Gets the current `stylesSet` for this instance.
*
* editor.getStylesSet( function( stylesDefinitions ) {} );
*
* See also {@link CKEDITOR.editor#stylesSet} event.
*
* @member CKEDITOR.editor
* @param {Function} callback The function to be called with the styles data.
*/
getStylesSet: function( callback ) {
if ( !this._.stylesDefinitions ) {
var editor = this,
// Respect the backwards compatible definition entry
configStyleSet = editor.config.stylesCombo_stylesSet || editor.config.stylesSet;
// The false value means that none styles should be loaded.
if ( configStyleSet === false ) {
callback( null );
return;
}
// https://dev.ckeditor.com/ticket/5352 Allow to define the styles directly in the config object
if ( configStyleSet instanceof Array ) {
editor._.stylesDefinitions = configStyleSet;
callback( configStyleSet );
return;
}
// Default value is 'default'.
if ( !configStyleSet )
configStyleSet = 'default';
var partsStylesSet = configStyleSet.split( ':' ),
styleSetName = partsStylesSet[ 0 ],
externalPath = partsStylesSet[ 1 ];
CKEDITOR.stylesSet.addExternal( styleSetName, externalPath ? partsStylesSet.slice( 1 ).join( ':' ) : CKEDITOR.getUrl( 'styles.js' ), '' );
CKEDITOR.stylesSet.load( styleSetName, function( stylesSet ) {
editor._.stylesDefinitions = stylesSet[ styleSetName ];
callback( editor._.stylesDefinitions );
} );
} else {
callback( this._.stylesDefinitions );
}
}
} );
/**
* Indicates that fully selected read-only elements will be included when
* applying the style (for inline styles only).
*
* @since 3.5.0
* @property {Boolean} [includeReadonly=false]
* @member CKEDITOR.style
*/
/**
* Indicates that any matches element of this style will be eventually removed
* when calling {@link CKEDITOR.editor#removeStyle}.
*
* @since 4.0.0
* @property {Boolean} [alwaysRemoveElement=false]
* @member CKEDITOR.style
*/
/**
* Disables inline styling on read-only elements.
*
* @since 3.5.0
* @cfg {Boolean} [disableReadonlyStyling=false]
* @member CKEDITOR.config
*/
/**
* The "styles definition set" to use in the editor. They will be used in the
* styles combo and the style selector of the div container.
*
* The styles may be defined in the page containing the editor, or can be
* loaded on demand from an external file. In the second case, if this setting
* contains only a name, the `styles.js` file will be loaded from the
* CKEditor root folder (what ensures backward compatibility with CKEditor 4.0).
*
* Otherwise, this setting has the `name:url` syntax, making it
* possible to set the URL from which the styles file will be loaded.
* Note that the `name` has to be equal to the name used in
* {@link CKEDITOR.stylesSet#add} while registering the styles set.
*
* **Note**: Since 4.1.0 it is possible to set `stylesSet` to `false`
* to prevent loading any styles set.
*
* Read more in the {@glink features/styles documentation}
* and see the {@glink examples/styles example}.
*
* // Do not load any file. The styles set is empty.
* config.stylesSet = false;
*
* // Load the 'mystyles' styles set from the styles.js file.
* config.stylesSet = 'mystyles';
*
* // Load the 'mystyles' styles set from a relative URL.
* config.stylesSet = 'mystyles:/editorstyles/styles.js';
*
* // Load the 'mystyles' styles set from a full URL.
* config.stylesSet = 'mystyles:http://www.example.com/editorstyles/styles.js';
*
* // Load from a list of definitions.
* config.stylesSet = [
* { name: 'Strong Emphasis', element: 'strong' },
* { name: 'Emphasis', element: 'em' },
* ...
* ];
*
* @since 3.3.0
* @cfg {String/Array/Boolean} [stylesSet='default']
* @member CKEDITOR.config
*/
/**
* Abstract class describing the definition of a style.
*
* This virtual class illustrates the properties that developers can use to define and create
* style definitions.
*
* A style definition object represents a style as a set of properties defining the element structure, its attributes and CSS styles.
* The {@link CKEDITOR.style} based on such definition can be applied to and removed from the selection
* through various {@link CKEDITOR.style} methods.
*
* ```javascript
* {
* name: 'Special Title',
* element: 'h1',
* attributes: { class: 'my_class' },
* styles: { color: 'red', 'font-size': '16px', 'font-width': 'bold' }
* }
* ```
*
* Refer to the {@glink guide/dev_howtos_styles Styles guide} for more information about how editor content styles are handled.
*
* @class CKEDITOR.style.definition
* @abstract
*/
/**
* Defines the style type.
*
* There are three standard style types:
*
* * {@link CKEDITOR#STYLE_INLINE},
* * {@link CKEDITOR#STYLE_BLOCK},
* * {@link CKEDITOR#STYLE_OBJECT}.
*
* Each type is related to the element used in the style rule and the types of elements to which a specific style can be applied.
*
* Plugins may define {@link CKEDITOR.style.customHandlers special style handlers} to customize style operations.
* To use a special style handler, the `type` property should be set to the name of the style handler, e.g. `widget`.
*
* Refer to the {@glink features/styles#style-types Style Types section of the Applying Styles to Editor Content guide} for more information about style types.
*
* ```javascript
* { type: CKEDITOR.STYLE_INLINE }
* ```
*
* @property {String/Number} type=CKEDITOR.STYLE_INLINE
*/
/**
* A unique style definition name. It can be used to differentiate style definitions, like in the {@glink features/styles Styles Combo} plugin
* drop-down where it represents item labels.
*
* ```javascript
* { name: 'Special title' }
* ```
*
* @property {String} name
*/
/**
* A set of properties specifying attributes of the HTML style element.
* If the `style` attribute is present, it will be merged with the existing {@link CKEDITOR.style.definition#styles} property.
*
* ```javascript
* {
* attributes: {
* style: 'color: red',
* class: 'link'
* }
* }
* ```
*
* @property {Object.<String, String>} attributes
*/
/**
* An element type that will be applied to the selection when applying a style. It should be a valid HTML element, for example `span`.
*
* ```javascript
* { element: 'h1' }
* ```
*
* @property {String} element
*/
/**
* A set of properties specifying CSS style rules of the HTML style element.
*
* ```javascript
* {
* styles: {
* color: 'red',
* 'font-size': '12px'
* 'font-weight': 'bold'
* }
* }
* ```
*
* @property {Object.<String, String>} styles
*/
|