Permalink
Browse files

Now encoding special characters in attributes with a dedicated functi…

…on; fixing some bugs.
  • Loading branch information...
1 parent 79677f6 commit bb183b5073531303f996786c482bc51175e31fb0 @michelf committed Mar 12, 2008
Showing with 21 additions and 11 deletions.
  1. +21 −11 markdown.php
View
@@ -325,9 +325,9 @@ function stripLinkDefinitions($text) {
}
function _stripLinkDefinitions_callback($matches) {
$link_id = strtolower($matches[1]);
- $this->urls[$link_id] = $this->encodeAmpsAndAngles($matches[2]);
+ $this->urls[$link_id] = $this->encodeAttribute($matches[2]);
if (isset($matches[3]))
- $this->titles[$link_id] = str_replace('"', '"', $matches[3]);
+ $this->titles[$link_id] = $this->encodeAttribute($matches[3]);
return ''; # String that will replace the block
}
@@ -701,12 +701,12 @@ function _doAnchors_reference_callback($matches) {
if (isset($this->urls[$link_id])) {
$url = $this->urls[$link_id];
- $url = $this->encodeAmpsAndAngles($url);
+ $url = $this->encodeAttribute($url);
$result = "<a href=\"$url\"";
if ( isset( $this->titles[$link_id] ) ) {
$title = $this->titles[$link_id];
- $title = $this->encodeAmpsAndAngles($title);
+ $title = $this->encodeAttribute($title);
$result .= " title=\"$title\"";
}
@@ -725,12 +725,11 @@ function _doAnchors_inline_callback($matches) {
$url = $matches[3] == '' ? $matches[4] : $matches[3];
$title =& $matches[7];
- $url = $this->encodeAmpsAndAngles($url);
+ $url = $this->encodeAttribute($url);
$result = "<a href=\"$url\"";
if (isset($title)) {
- $title = str_replace('"', '&quot;', $title);
- $title = $this->encodeAmpsAndAngles($title);
+ $title = $this->encodeAttribute($title);
$result .= " title=\"$title\"";
}
@@ -805,7 +804,7 @@ function _doImages_reference_callback($matches) {
$link_id = strtolower($alt_text); # for shortcut links like ![this][].
}
- $alt_text = str_replace('"', '&quot;', $alt_text);
+ $alt_text = $this->encodeAttribute($alt_text);
if (isset($this->urls[$link_id])) {
$url = $this->urls[$link_id];
$result = "<img src=\"$url\" alt=\"$alt_text\"";
@@ -829,10 +828,11 @@ function _doImages_inline_callback($matches) {
$url = $matches[3] == '' ? $matches[4] : $matches[3];
$title =& $matches[7];
- $alt_text = str_replace('"', '&quot;', $alt_text);
+ $alt_text = $this->encodeAttribute($alt_text);
+ $url = $this->encodeAttribute($url);
$result = "<img src=\"$url\" alt=\"$alt_text\"";
if (isset($title)) {
- $title = str_replace('"', '&quot;', $title);
+ $title = $this->encodeAttribute($title);
$result .= " title=\"$title\""; # $title already quoted
}
$result .= $this->empty_element_suffix;
@@ -1218,6 +1218,16 @@ function formParagraphs($text) {
}
+ function encodeAttribute($text) {
+ #
+ # Encode text for a double-quoted HTML attribute.
+ #
+ $text = $this->encodeAmpsAndAngles($text);
+ $text = str_replace('"', '&quot;', $text);
+ return $text;
+ }
+
+
function encodeAmpsAndAngles($text) {
# Smart processing for ampersands and angle brackets that need to be encoded.
if ($this->no_entities) {
@@ -1258,7 +1268,7 @@ function doAutoLinks($text) {
return $text;
}
function _doAutoLinks_url_callback($matches) {
- $url = $this->encodeAmpsAndAngles($matches[1]);
+ $url = $this->encodeAttribute($matches[1]);
$link = "<a href=\"$url\">$url</a>";
return $this->hashPart($link);
}

0 comments on commit bb183b5

Please sign in to comment.