@@ -43,10 +43,12 @@ const SANITIZE = (string) =>
4343 ADD_TAGS : [
4444 "#comment" , // comments are vital for configuring revealjs
4545 "foreignObject" , // unfortunately some mermaid diagrams use it, despite being a potential security risk: https://github.com/cure53/DOMPurify/issues/469
46- "iframe" , // allow iframes
46+ "iframe" , // allow iframes to support youtube videos
4747 ] ,
4848 ADD_ATTR : [
4949 "target" ,
50+ "allow" , // required for youtube videos
51+ "allowfullscreen" , // required for youtube videos
5052 ] ,
5153 } ,
5254 ) ;
@@ -61,25 +63,23 @@ const SANITIZE = (string) =>
6163 * http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development/7000899#answer-11786277
6264 */
6365function addAttributeInElement ( node , elementTarget , separator ) {
64- const markdownClassesInElementsRegex = new RegExp ( separator , "mg" ) ;
65- const markdownClassRegex = new RegExp (
66- '([^"= ]+?)="([^"]+?)"|(data-[^"= ]+?)(?=[" ])' ,
67- "mg" ,
66+ const attrsInNode = new RegExp ( separator , "mg" ) ;
67+ const attrsRegex = new RegExp (
68+ // attributes are limited to prevent code injection
69+ "(?:^|\s)(?<attr>class|style|data-[a-z-]+)=(?:\"(?<dval>[^\"]+?)\"|'(?<sval>[^']+?)')" ,
70+ "gm" ,
6871 ) ;
69- let nodeValue = node . nodeValue ;
7072 let matches ,
71- matchesClass ;
72- if ( ( matches = markdownClassesInElementsRegex . exec ( nodeValue ) ) !== null ) {
73+ matchesAttrs ;
74+ if ( ( matches = attrsInNode . exec ( node . nodeValue ) ) !== null ) {
7375 const classes = matches [ 1 ] ;
74- nodeValue = nodeValue . substring ( 0 , matches . index ) +
75- nodeValue . substring ( markdownClassesInElementsRegex . lastIndex ) ;
76- node . nodeValue = nodeValue ;
77- while ( ( matchesClass = markdownClassRegex . exec ( classes ) ) !== null ) {
78- if ( matchesClass [ 2 ] ) {
79- elementTarget . setAttribute ( matchesClass [ 1 ] , matchesClass [ 2 ] ) ;
80- } else {
81- elementTarget . setAttribute ( matchesClass [ 3 ] , "" ) ;
82- }
76+ node . nodeValue = node . nodeValue . substring ( 0 , matches . index ) +
77+ node . nodeValue . substring ( attrsInNode . lastIndex ) ;
78+ while ( ( matchesAttrs = attrsRegex . exec ( classes ) ) !== null ) {
79+ elementTarget . setAttribute (
80+ matchesAttrs . groups . attr ,
81+ matchesAttrs . groups . dval || matchesAttrs . groups . sval || "" ,
82+ ) ;
8383 }
8484 return true ;
8585 }
@@ -97,10 +97,7 @@ function addAttributes(
9797 separatorElementAttributes ,
9898 separatorSectionAttributes ,
9999) {
100- if (
101- element != null && element . childNodes != undefined &&
102- element . childNodes . length > 0
103- ) {
100+ if ( element ?. childNodes && element . childNodes . length > 0 ) {
104101 let previousParentElement = element ;
105102 for ( let i = 0 ; i < element . childNodes . length ; i ++ ) {
106103 const childElement = element . childNodes [ i ] ;
0 commit comments