Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/plugins/page-icons/page-icons.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import './store'
import { useSelect } from '@wordpress/data'
import { safeHTML } from '@wordpress/dom'

/**
* Parse SVG string to extract attributes and innerHTML without DOM manipulation
Expand Down Expand Up @@ -43,6 +42,11 @@ const parseSVGString = svgString => {
rawInnerSVG = rawInnerSVG.replace( /\s[\w-]+:[\w-]+='[^']*'/g, '' )
rawInnerSVG = rawInnerSVG.replace( /\s[\w-]+:[\w-]+=[^\s"'=<>`]+/g, '' )

// Mimic safeHTML's event handler cleanup without parsing the SVG as HTML.
rawInnerSVG = rawInnerSVG.replace( /\son[\w-]+\s*=\s*"[^"]*"/gi, '' )
rawInnerSVG = rawInnerSVG.replace( /\son[\w-]+\s*=\s*'[^']*'/gi, '' )
rawInnerSVG = rawInnerSVG.replace( /\son[\w-]+\s*=\s*[^\s"'=<>`]+/gi, '' )

// Remove href/data-href/src attributes containing data: uris
rawInnerSVG = rawInnerSVG.replace(
/\s(?:href|data-href|src)\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi,
Expand All @@ -58,8 +62,6 @@ const parseSVGString = svgString => {
}
)

const innerHTML = safeHTML( rawInnerSVG )

// Extract attributes from the SVG tag
const svgAttributes = {}
const attributesPart = svgTag.replace( /^<svg\s*/i, '' ).replace( />$/, '' )
Expand All @@ -71,15 +73,15 @@ const parseSVGString = svgString => {
const key = attrMatch[ 1 ]
const attrNameLower = key.toLowerCase()
// Skip width and height as symbols don't need them
if ( attrNameLower !== 'width' && attrNameLower !== 'height' ) {
if ( attrNameLower !== 'width' && attrNameLower !== 'height' && ! attrNameLower.startsWith( 'on' ) ) {
// Value can be in double quotes, single quotes, or unquoted
const value = attrMatch[ 2 ] || attrMatch[ 3 ] || attrMatch[ 4 ] || ''
svgAttributes[ key ] = value
}
}
}

return { attributes: svgAttributes, innerHTML }
return { attributes: svgAttributes, innerHTML: rawInnerSVG }
}

export const PageIcons = () => {
Expand Down
Loading