@@ -536,7 +536,33 @@ function inject_markup( $pre_start_tag, $code_start_tag, $attributes, $content )
536536 }
537537 $ end_tags .= '</pre> ' ;
538538
539- return $ pre_start_tag . get_styles ( $ attributes ) . '<span> ' . $ code_start_tag . $ content . $ end_tags ;
539+ return $ pre_start_tag . get_styles ( $ attributes ) . '<span> ' . $ code_start_tag . escape ( $ content ) . $ end_tags ;
540+ }
541+
542+ /**
543+ * Escape content.
544+ *
545+ * In order to prevent WordPress the_content filters from rendering embeds/shortcodes, it's important
546+ * to re-escape the content in the same way as the editor is doing with the Code block's save function.
547+ * Note this does not need to escape ampersands because they will already be escaped by highlight.php.
548+ * Also, escaping of ampersands was removed in <https://github.com/WordPress/gutenberg/commit/f5c32f8>
549+ * once HTML editing of Code blocks was implemented.
550+ *
551+ * @link <https://github.com/westonruter/syntax-highlighting-code-block/issues/668>
552+ * @link <https://github.com/WordPress/gutenberg/blob/32b4481/packages/block-library/src/code/utils.js>
553+ * @link <https://github.com/WordPress/gutenberg/pull/13996>
554+ *
555+ * @param string $content Highlighted content.
556+ * @return string Escaped content.
557+ */
558+ function escape ( $ content ) {
559+ // See escapeOpeningSquareBrackets: <https://github.com/WordPress/gutenberg/blob/32b4481/packages/block-library/src/code/utils.js#L19-L34>.
560+ $ content = str_replace ( '[ ' , '[ ' , $ content );
561+
562+ // See escapeProtocolInIsolatedUrls: <https://github.com/WordPress/gutenberg/blob/32b4481/packages/block-library/src/code/utils.js#L36-L55>.
563+ $ content = preg_replace ( '/^(\s*https?:)\/\/([^\s<>"]+\s*)$/m ' , '$1//$2 ' , $ content );
564+
565+ return $ content ;
540566}
541567
542568/**
@@ -595,7 +621,10 @@ function render_block( $attributes, $content ) {
595621 }
596622
597623 $ language = $ attributes ['language ' ];
598- $ content = html_entity_decode ( $ matches ['content ' ], ENT_QUOTES );
624+
625+ // Note that the decoding here is reversed later in the escape() function.
626+ // @todo Now that Code blocks may have markup (e.g. bolding, italics, and hyperlinks), these need to be removed and then restored after highlighting is completed.
627+ $ content = html_entity_decode ( $ matches ['content ' ], ENT_QUOTES );
599628
600629 // Convert from Prism.js languages names.
601630 if ( 'clike ' === $ language ) {
0 commit comments