瀏覽代碼

- Fixed Trac #429: web browser can crash when a text field contains several times the same URL !!!

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1336 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 14 年之前
父節點
當前提交
3b012360be
共有 1 個文件被更改,包括 16 次插入6 次删除
  1. 16 6
      core/attributedef.class.inc.php

+ 16 - 6
core/attributedef.class.inc.php

@@ -1349,13 +1349,23 @@ class AttributeText extends AttributeString
 
 	static public function RenderWikiHtml($sText)
 	{
-		if (preg_match_all(WIKI_URL, $sText, $aAllMatches, PREG_SET_ORDER))
-		{
-			foreach($aAllMatches as $iPos => $aMatches)
+		if (preg_match_all(WIKI_URL, $sText, $aAllMatches, PREG_SET_ORDER /* important !*/ |PREG_OFFSET_CAPTURE /* important ! */))
+		{
+			$aUrls = array();
+			echo "<pre>".print_r($aAllMatches,true)."</pre><br/>\n";
+			$i = count($aAllMatches);
+			// Replace the URLs by an actual hyperlink <a href="...">...</a>
+			// Let's do it backwards so that the initial positions are not modified by the replacement
+			// This works if the matches are captured: in the order they occur in the string  AND
+			// with their offset (i.e. position) inside the string
+			while($i > 0)
 			{
-				$sUrl = $aMatches[0];
-				$sHLink = "<a href=\"$sUrl\">$sUrl</a>"; 
-				$sText = str_replace($sUrl, $sHLink, $sText);
+				$i--;
+				$sUrl = $aAllMatches[$i][0][0]; // String corresponding to the main pattern
+				$iPos = $aAllMatches[$i][0][1]; // Position of the main pattern
+				echo "<p>Replacing: '$sUrl', located at: $iPos, length: ".strlen($sUrl)."</p>\n";
+				$sText = substr_replace($sText, "<a href=\"$sUrl\">$sUrl</a>", $iPos, strlen($sUrl));
+				
 			}
 		}
 		if (preg_match_all(WIKI_OBJECT_REGEXP, $sText, $aAllMatches, PREG_SET_ORDER))