wysihtml5-parser.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /**
  2. * Full HTML5 compatibility rule set
  3. * These rules define which tags and CSS classes are supported and which tags should be specially treated.
  4. *
  5. * Examples based on this rule set:
  6. *
  7. * <a href="http://foobar.com">foo</a>
  8. * ... becomes ...
  9. * <a href="http://foobar.com" target="_blank" rel="nofollow">foo</a>
  10. *
  11. * <img align="left" src="http://foobar.com/image.png">
  12. * ... becomes ...
  13. * <img class="wysiwyg-float-left" src="http://foobar.com/image.png" alt="">
  14. *
  15. * <div>foo<script>alert(document.cookie);</script></div>
  16. * ... becomes ...
  17. * <div>foo</div>
  18. *
  19. * <marquee>foo</marquee>
  20. * ... becomes ...
  21. * <span>foo</span>
  22. *
  23. * foo <br clear="both"> bar
  24. * ... becomes ...
  25. * foo <br class="wysiwyg-clear-both"> bar
  26. *
  27. * <div>hello <iframe src="http://google.com"></iframe></div>
  28. * ... becomes ...
  29. * <div>hello </div>
  30. *
  31. * <center>hello</center>
  32. * ... becomes ...
  33. * <div class="wysiwyg-text-align-center">hello</div>
  34. */
  35. var wysihtml5ParserRules = {
  36. /**
  37. * CSS Class white-list
  38. * Following CSS classes won't be removed when parsed by the wysihtml5 HTML parser
  39. * If all classes should pass "any" as classes value. Ex: "classes": "any"
  40. */
  41. "classes": {
  42. "wysiwyg-clear-both": 1,
  43. "wysiwyg-clear-left": 1,
  44. "wysiwyg-clear-right": 1,
  45. "wysiwyg-color-aqua": 1,
  46. "wysiwyg-color-black": 1,
  47. "wysiwyg-color-blue": 1,
  48. "wysiwyg-color-fuchsia": 1,
  49. "wysiwyg-color-gray": 1,
  50. "wysiwyg-color-green": 1,
  51. "wysiwyg-color-lime": 1,
  52. "wysiwyg-color-maroon": 1,
  53. "wysiwyg-color-navy": 1,
  54. "wysiwyg-color-olive": 1,
  55. "wysiwyg-color-purple": 1,
  56. "wysiwyg-color-red": 1,
  57. "wysiwyg-color-silver": 1,
  58. "wysiwyg-color-teal": 1,
  59. "wysiwyg-color-white": 1,
  60. "wysiwyg-color-yellow": 1,
  61. "wysiwyg-float-left": 1,
  62. "wysiwyg-float-right": 1,
  63. "wysiwyg-font-size-large": 1,
  64. "wysiwyg-font-size-larger": 1,
  65. "wysiwyg-font-size-medium": 1,
  66. "wysiwyg-font-size-small": 1,
  67. "wysiwyg-font-size-smaller": 1,
  68. "wysiwyg-font-size-x-large": 1,
  69. "wysiwyg-font-size-x-small": 1,
  70. "wysiwyg-font-size-xx-large": 1,
  71. "wysiwyg-font-size-xx-small": 1,
  72. "wysiwyg-text-align-center": 1,
  73. "wysiwyg-text-align-justify": 1,
  74. "wysiwyg-text-align-left": 1,
  75. "wysiwyg-text-align-right": 1
  76. },
  77. "type_definitions": {
  78. "visible_content_object": {
  79. "methods": {
  80. "has_visible_contet": 1
  81. }
  82. },
  83. "alignment_object": {
  84. "classes": {
  85. "wysiwyg-text-align-center": 1,
  86. "wysiwyg-text-align-justify": 1,
  87. "wysiwyg-text-align-left": 1,
  88. "wysiwyg-text-align-right": 1,
  89. "wysiwyg-float-left": 1,
  90. "wysiwyg-float-right": 1
  91. },
  92. "styles": {
  93. "float": ["left", "right"],
  94. "text-align": ["left", "right", "center", "justify"]
  95. }
  96. },
  97. "valid_image_src": {
  98. "attrs": {
  99. "src": /^[^data\:]/i
  100. }
  101. },
  102. "text_color_object": {
  103. "styles": {
  104. "color": true,
  105. "background-color": true
  106. }
  107. },
  108. "text_fontfamily_object": {
  109. "styles": {
  110. "font-family": true
  111. }
  112. },
  113. "text_fontsize_object": {
  114. "styles": {
  115. "font-size": true
  116. }
  117. },
  118. "text_formatting_object": {
  119. "classes": {
  120. "wysiwyg-color-aqua": 1,
  121. "wysiwyg-color-black": 1,
  122. "wysiwyg-color-blue": 1,
  123. "wysiwyg-color-fuchsia": 1,
  124. "wysiwyg-color-gray": 1,
  125. "wysiwyg-color-green": 1,
  126. "wysiwyg-color-lime": 1,
  127. "wysiwyg-color-maroon": 1,
  128. "wysiwyg-color-navy": 1,
  129. "wysiwyg-color-olive": 1,
  130. "wysiwyg-color-purple": 1,
  131. "wysiwyg-color-red": 1,
  132. "wysiwyg-color-silver": 1,
  133. "wysiwyg-color-teal": 1,
  134. "wysiwyg-color-white": 1,
  135. "wysiwyg-color-yellow": 1,
  136. "wysiwyg-font-size-large": 1,
  137. "wysiwyg-font-size-larger": 1,
  138. "wysiwyg-font-size-medium": 1,
  139. "wysiwyg-font-size-small": 1,
  140. "wysiwyg-font-size-smaller": 1,
  141. "wysiwyg-font-size-x-large": 1,
  142. "wysiwyg-font-size-x-small": 1,
  143. "wysiwyg-font-size-xx-large": 1,
  144. "wysiwyg-font-size-xx-small": 1
  145. }
  146. }
  147. },
  148. "comments": 1, // if set allows comments to pass
  149. /**
  150. * Tag list
  151. *
  152. * The following options are available:
  153. *
  154. * - add_class: converts and deletes the given HTML4 attribute (align, clear, ...) via the given method to a css class
  155. * The following methods are implemented in wysihtml5.dom.parse:
  156. * - align_text: converts align attribute values (right/left/center/justify) to their corresponding css class "wysiwyg-text-align-*")
  157. * <p align="center">foo</p> ... becomes ... <p class="wysiwyg-text-align-center">foo</p>
  158. * - clear_br: converts clear attribute values left/right/all/both to their corresponding css class "wysiwyg-clear-*"
  159. * <br clear="all"> ... becomes ... <br class="wysiwyg-clear-both">
  160. * - align_img: converts align attribute values (right/left) on <img> to their corresponding css class "wysiwyg-float-*"
  161. *
  162. * - add_style: converts and deletes the given HTML4 attribute (align) via the given method to a css style
  163. * The following methods are implemented in wysihtml5.dom.parse:
  164. * - align_text: converts align attribute values (right/left/center) to their corresponding css style)
  165. * <p align="center">foo</p> ... becomes ... <p style="text-align:center">foo</p>
  166. *
  167. * - remove: removes the element and its content
  168. *
  169. * - unwrap removes element but leaves content
  170. *
  171. * - rename_tag: renames the element to the given tag
  172. *
  173. * - set_class: adds the given class to the element (note: make sure that the class is in the "classes" white list above)
  174. *
  175. * - set_attributes: sets/overrides the given attributes
  176. *
  177. * - check_attributes: checks the given HTML attribute via the given method
  178. * - url: allows only valid urls (starting with http:// or https://)
  179. * - src: allows something like "/foobar.jpg", "http://google.com", ...
  180. * - href: allows something like "mailto:bert@foo.com", "http://google.com", "/foobar.jpg"
  181. * - alt: strips unwanted characters. if the attribute is not set, then it gets set (to ensure valid and compatible HTML)
  182. * - numbers: ensures that the attribute only contains numeric characters
  183. * - any: allows anything to pass
  184. */
  185. "tags": {
  186. "tr": {
  187. "add_class": {
  188. "align": "align_text"
  189. }
  190. },
  191. "strike": {
  192. "unwrap": 1
  193. },
  194. "form": {
  195. "unwrap": 1
  196. },
  197. "rt": {
  198. "rename_tag": "span"
  199. },
  200. "code": {},
  201. "acronym": {
  202. "rename_tag": "span"
  203. },
  204. "br": {
  205. "add_class": {
  206. "clear": "clear_br"
  207. }
  208. },
  209. "details": {
  210. "unwrap": 1
  211. },
  212. "h4": {
  213. "add_class": {
  214. "align": "align_text"
  215. }
  216. },
  217. "em": {},
  218. "title": {
  219. "remove": 1
  220. },
  221. "multicol": {
  222. "unwrap": 1
  223. },
  224. "figure": {
  225. "unwrap": 1
  226. },
  227. "xmp": {
  228. "unwrap": 1
  229. },
  230. "small": {
  231. "rename_tag": "span",
  232. "set_class": "wysiwyg-font-size-smaller"
  233. },
  234. "area": {
  235. "remove": 1
  236. },
  237. "time": {
  238. "unwrap": 1
  239. },
  240. "dir": {
  241. "rename_tag": "ul"
  242. },
  243. "bdi": {
  244. "unwrap": 1
  245. },
  246. "command": {
  247. "unwrap": 1
  248. },
  249. "ul": {
  250. "keep_styles": {
  251. "textAlign": 1,
  252. "float": 1,
  253. "font-family":1,
  254. "font-size":1,
  255. "color":1
  256. }
  257. },
  258. "progress": {
  259. "rename_tag": "span"
  260. },
  261. "dfn": {
  262. "unwrap": 1
  263. },
  264. "iframe": {
  265. "remove": 1
  266. },
  267. "figcaption": {
  268. "unwrap": 1
  269. },
  270. "a": {
  271. "check_attributes": {
  272. "href": "any", // if you compiled master manually then change this from 'url' to 'href'
  273. "target": "any"
  274. },
  275. /*
  276. "set_attributes": {
  277. "rel": "nofollow"
  278. },
  279. */
  280. "keep_styles": {
  281. "textAlign": 1,
  282. "float": 1,
  283. "font-family":1,
  284. "font-size":1,
  285. "color":1
  286. }
  287. },
  288. "img": {
  289. "one_of_type": {
  290. "valid_image_src": 1
  291. },
  292. "check_attributes": {
  293. "width": "numbers",
  294. "alt": "alt",
  295. "src": "src", // if you compiled master manually then change this from 'url' to 'src'
  296. "height": "numbers"
  297. },
  298. "add_class": {
  299. "align": "align_img"
  300. }
  301. },
  302. "rb": {
  303. "unwrap": 1
  304. },
  305. "footer": {
  306. "rename_tag": "div"
  307. },
  308. "noframes": {
  309. "remove": 1
  310. },
  311. "abbr": {
  312. "unwrap": 1
  313. },
  314. "u": {
  315. "keep_styles": {
  316. "textAlign": 1,
  317. "float": 1,
  318. "font-family":1,
  319. "font-size":1,
  320. "color":1
  321. }
  322. },
  323. "bgsound": {
  324. "remove": 1
  325. },
  326. "sup": {
  327. "unwrap": 1
  328. },
  329. "address": {
  330. "unwrap": 1
  331. },
  332. "basefont": {
  333. "remove": 1
  334. },
  335. "nav": {
  336. "unwrap": 1
  337. },
  338. "h1": {
  339. "add_class": {
  340. "align": "align_text"
  341. }
  342. },
  343. "head": {
  344. "unwrap": 1
  345. },
  346. "tbody": {
  347. "add_class": {
  348. "align": "align_text"
  349. }
  350. },
  351. "dd": {
  352. "unwrap": 1
  353. },
  354. "s": {
  355. "unwrap": 1
  356. },
  357. "li": {
  358. "keep_styles": {
  359. "textAlign": 1,
  360. "float": 1,
  361. "font-family":1,
  362. "font-size":1,
  363. "color":1,
  364. "fontFamily": 1
  365. }
  366. },
  367. "td": {
  368. "check_attributes": {
  369. "rowspan": "numbers",
  370. "colspan": "numbers",
  371. "valign": "any",
  372. "align": "any"
  373. },
  374. "add_class": {
  375. "align": "align_text"
  376. }
  377. },
  378. "object": {
  379. "remove": 1
  380. },
  381. "div": {
  382. "one_of_type": {
  383. "visible_content_object": 1
  384. },
  385. "remove_action": "unwrap",
  386. "keep_styles": {
  387. "textAlign": 1,
  388. "float": 1,
  389. "font-family":1
  390. },
  391. "add_class": {
  392. "align": "align_text"
  393. }
  394. },
  395. "option": {
  396. "remove":1
  397. },
  398. "select": {
  399. "remove":1
  400. },
  401. "i": {},
  402. "track": {
  403. "remove": 1
  404. },
  405. "wbr": {
  406. "remove": 1
  407. },
  408. "fieldset": {
  409. "unwrap": 1
  410. },
  411. "big": {
  412. "rename_tag": "span",
  413. "set_class": "wysiwyg-font-size-larger"
  414. },
  415. "button": {
  416. "unwrap": 1
  417. },
  418. "noscript": {
  419. "remove": 1
  420. },
  421. "svg": {
  422. "remove": 1
  423. },
  424. "input": {
  425. "remove": 1
  426. },
  427. "table": {},
  428. "keygen": {
  429. "remove": 1
  430. },
  431. "h5": {
  432. "add_class": {
  433. "align": "align_text"
  434. }
  435. },
  436. "meta": {
  437. "remove": 1
  438. },
  439. "map": {
  440. "remove": 1
  441. },
  442. "isindex": {
  443. "remove": 1
  444. },
  445. "mark": {
  446. "unwrap": 1
  447. },
  448. "caption": {
  449. "add_class": {
  450. "align": "align_text"
  451. }
  452. },
  453. "tfoot": {
  454. "add_class": {
  455. "align": "align_text"
  456. }
  457. },
  458. "base": {
  459. "remove": 1
  460. },
  461. "video": {
  462. "remove": 1
  463. },
  464. "strong": {},
  465. "canvas": {
  466. "remove": 1
  467. },
  468. "output": {
  469. "unwrap": 1
  470. },
  471. "marquee": {
  472. "unwrap": 1
  473. },
  474. "b": {},
  475. "q": {
  476. "check_attributes": {
  477. "cite": "url"
  478. }
  479. },
  480. "applet": {
  481. "remove": 1
  482. },
  483. "span": {
  484. /*"one_of_type": {
  485. "text_formatting_object": 1,
  486. "text_color_object": 1,
  487. "text_fontsize_object": 1
  488. },*/
  489. "keep_styles": {
  490. "color": 1,
  491. "backgroundColor": 1,
  492. "fontSize": 1,
  493. "fontFamily": 1,
  494. "font-family": 1
  495. },
  496. "remove_action": "unwrap"
  497. },
  498. "rp": {
  499. "unwrap": 1
  500. },
  501. "spacer": {
  502. "remove": 1
  503. },
  504. "source": {
  505. "remove": 1
  506. },
  507. "aside": {
  508. "rename_tag": "div"
  509. },
  510. "frame": {
  511. "remove": 1
  512. },
  513. "section": {
  514. "rename_tag": "div"
  515. },
  516. "body": {
  517. "unwrap": 1
  518. },
  519. "ol": {
  520. "keep_styles": {
  521. "textAlign": 1,
  522. "float": 1,
  523. "font-family":1,
  524. "font-size":1,
  525. "color":1
  526. }
  527. },
  528. "nobr": {
  529. "unwrap": 1
  530. },
  531. "html": {
  532. "unwrap": 1
  533. },
  534. "summary": {
  535. "unwrap": 1
  536. },
  537. "var": {
  538. "unwrap": 1
  539. },
  540. "del": {
  541. "unwrap": 1
  542. },
  543. "blockquote": {
  544. "check_attributes": {
  545. "cite": "url"
  546. }
  547. },
  548. "style": {
  549. "remove": 1
  550. },
  551. "device": {
  552. "remove": 1
  553. },
  554. "meter": {
  555. "unwrap": 1
  556. },
  557. "h3": {
  558. "add_class": {
  559. "align": "align_text"
  560. }
  561. },
  562. "textarea": {
  563. "unwrap": 1
  564. },
  565. "embed": {
  566. "remove": 1
  567. },
  568. "hgroup": {
  569. "unwrap": 1
  570. },
  571. "font": {
  572. "rename_tag": "span",
  573. "add_class": {
  574. "size": "size_font"
  575. }
  576. },
  577. "tt": {
  578. "unwrap": 1
  579. },
  580. "noembed": {
  581. "remove": 1
  582. },
  583. "thead": {
  584. "add_class": {
  585. "align": "align_text"
  586. }
  587. },
  588. "blink": {
  589. "unwrap": 1
  590. },
  591. "plaintext": {
  592. "unwrap": 1
  593. },
  594. "xml": {
  595. "remove": 1
  596. },
  597. "h6": {
  598. "add_class": {
  599. "align": "align_text"
  600. }
  601. },
  602. "param": {
  603. "remove": 1
  604. },
  605. "th": {
  606. "check_attributes": {
  607. "rowspan": "numbers",
  608. "colspan": "numbers"
  609. },
  610. "add_class": {
  611. "align": "align_text"
  612. }
  613. },
  614. "legend": {
  615. "unwrap": 1
  616. },
  617. "hr": {},
  618. "label": {
  619. "unwrap": 1
  620. },
  621. "dl": {
  622. "unwrap": 1
  623. },
  624. "kbd": {
  625. "unwrap": 1
  626. },
  627. "listing": {
  628. "unwrap": 1
  629. },
  630. "dt": {
  631. "unwrap": 1
  632. },
  633. "nextid": {
  634. "remove": 1
  635. },
  636. "pre": {},
  637. "center": {
  638. "rename_tag": "div",
  639. "set_class": "wysiwyg-text-align-center"
  640. },
  641. "audio": {
  642. "remove": 1
  643. },
  644. "datalist": {
  645. "unwrap": 1
  646. },
  647. "samp": {
  648. "unwrap": 1
  649. },
  650. "col": {
  651. "remove": 1
  652. },
  653. "article": {
  654. "rename_tag": "div"
  655. },
  656. "cite": {},
  657. "link": {
  658. "remove": 1
  659. },
  660. "script": {
  661. "remove": 1
  662. },
  663. "bdo": {
  664. "unwrap": 1
  665. },
  666. "menu": {
  667. "rename_tag": "ul"
  668. },
  669. "colgroup": {
  670. "remove": 1
  671. },
  672. "ruby": {
  673. "unwrap": 1
  674. },
  675. "h2": {
  676. "add_class": {
  677. "align": "align_text"
  678. }
  679. },
  680. "ins": {
  681. "unwrap": 1
  682. },
  683. "p": {
  684. "add_class": {
  685. "align": "align_text"
  686. },
  687. "keep_styles": {
  688. "textAlign": 1,
  689. "float": 1,
  690. "fontSize": 1,
  691. "fontFamily": 1,
  692. "color": 1
  693. }
  694. },
  695. "sub": {
  696. "unwrap": 1
  697. },
  698. "comment": {
  699. "remove": 1
  700. },
  701. "frameset": {
  702. "remove": 1
  703. },
  704. "optgroup": {
  705. "unwrap": 1
  706. },
  707. "header": {
  708. "rename_tag": "div"
  709. }
  710. }
  711. };