diff --git a/application/utils.inc.php b/application/utils.inc.php index 21d5eb1e82..951c8c3b87 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -3094,7 +3094,6 @@ public static function ToAcronym(string $sInput): string * Note: Only works for backoffice URLs for now * * @param string $sText Text containing the mentioned objects to be found - * @param string $sFormat {@uses static::ENUM_TEXT_FORMAT_HTML, ...} * * @return array Array of object classes / IDs for the ones found in $sText * @@ -3109,21 +3108,24 @@ public static function ToAcronym(string $sInput): string public static function GetMentionedObjectsFromText(string $sText): array { $aMentionedObjects = []; - $aMentionMatches = []; - $sText = html_entity_decode($sText); + $oDom = new \DOMDocument(); + libxml_use_internal_errors(true); // to keep processing even in case of "invalid" HTML, cf. testGetMentionedObjectsFromText + $oDom->loadHTML($sText); - preg_match_all('/]*)data-object-class="([^"]*)"\s.*data-object-key="([^"]*)"/Ui', $sText, $aMentionMatches); - foreach ($aMentionMatches[0] as $iMatchIdx => $sCompleteMatch) { - $sMatchedClass = $aMentionMatches[2][$iMatchIdx]; - $sMatchedId = $aMentionMatches[3][$iMatchIdx]; + $oXpath = new \DOMXPath($oDom); + $oNodes = $oXpath->query('//a[@data-object-class and @data-object-key]'); + + foreach ($oNodes as $oObjNode) { + $sObjClass = $oObjNode->getAttribute('data-object-class'); + $sObjId = $oObjNode->getAttribute('data-object-key'); // Prepare array for matched class if not already present - if (!array_key_exists($sMatchedClass, $aMentionedObjects)) { - $aMentionedObjects[$sMatchedClass] = array(); + if (!array_key_exists($sObjClass, $aMentionedObjects)) { + $aMentionedObjects[$sObjClass] = []; } // Add matched ID if not already there - if (!in_array($sMatchedId, $aMentionedObjects[$sMatchedClass])) { - $aMentionedObjects[$sMatchedClass][] = $sMatchedId; + if (!in_array($sObjId, $aMentionedObjects[$sObjClass])) { + $aMentionedObjects[$sObjClass][] = $sObjId; } }