Index: wordpress/wp-includes/formatting.php
===================================================================
--- wordpress/wp-includes/formatting.php	(revision 16544)
+++ wordpress/wp-includes/formatting.php	(working copy)
@@ -31,7 +31,7 @@
 	static $static_setup = false, $opening_quote, $closing_quote, $default_no_texturize_tags, $default_no_texturize_shortcodes, $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements;
 	$output = '';
 	$curl = '';
-	$textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
+	$textarr = preg_split('/(<!--.*-->|<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
 	$stop = count($textarr);
 
 	// No need to set up these variables more than once
@@ -56,9 +56,23 @@
 		$static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn&#8211;', '...', '``', '\'\'', ' (tm)'), $cockney);
 		$static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', ' &#8211; ', 'xn--', '&#8230;', $opening_quote, $closing_quote, ' &#8482;'), $cockneyreplace);
 
-		$dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/\'(\d)/', '/(\s|\A|[([{<]|")\'/', '/(\d)"/', '/(\d)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A|[([{<])"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/\b(\d+)x(\d+)\b/');
-		$dynamic_replacements = array('&#8217;$1','&#8217;$1', '$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '&#8217;$1', '$1&#215;$2');
+		$dynamic_map = array(
+			'/([\A\s])\'(\d)/' => '$1&#8217;$2', // '99
 
+			'/([\w\]})])\'([\w])/' => '$1&#8217;$2', // test's
+
+			'/\'([^\']*)\'/' => '&#8216;$1&#8217;', // 'asd'
+			'/"([^"]*)"/' => $opening_quote . '$1' . $closing_quote, // "qwe"
+
+			'/(\d)\'/' => '$1&#8242;', // 9'
+			'/(\d)"/' => '$1&#8243;', // 9"
+
+			'/\b(\d+)x(\d+)\b/' => '$1&#215;$2' // 97x34
+		);
+
+		$dynamic_characters = array_keys( $dynamic_map );
+		$dynamic_replacements = array_values( $dynamic_map );
+
 		$static_setup = true;
 	}
 
@@ -70,6 +84,12 @@
 	$no_texturize_tags_stack = array();
 	$no_texturize_shortcodes_stack = array();
 
+	$single_quote_state = '&#8216;';
+	$double_quote_state = $opening_quote;
+
+	$text_node_count = 0;
+	$whitespace_before_last_tag = false;
+
 	for ( $i = 0; $i < $stop; $i++ ) {
 		$curl = $textarr[$i];
 
@@ -78,8 +98,28 @@
 			// This is not a tag, nor is the texturization disabled
 			// static strings
 			$curl = str_replace($static_characters, $static_replacements, $curl);
+			// quotes after tags, e.g. <b>somebody</b>'s
+			if ( ( $text_node_count > 0 ) && ( ! $whitespace_before_last_tag ) ) {
+				$curl = preg_replace( '/^(\')/', '&#8217;', $curl );
+			}
+			
 			// regular expressions
 			$curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
+
+			// Quotes that span across multiple tags & shortcodes
+			// Dynamic regexps above ensure that there is at most 1 quote left in the string.
+			// Otherwise, the less efficient preg_replace() within a while loop would be required.
+			if ( strpos( $curl, '\'' ) !== false ) {
+				$curl = str_replace( '\'', $single_quote_state, $curl);
+				$single_quote_state = ( ( '&#8216;' == $single_quote_state ) ? '&#8217;' : '&#8216;' );
+			}
+			if ( strpos($curl, '"' ) !== false ) {
+				$curl = str_replace( '"', $double_quote_state, $curl);
+				$double_quote_state = ( $opening_quote == $double_quote_state ) ? $closing_quote : $opening_quote;
+			}
+			// stats for quotes after tags above
+			$text_node_count++;
+			$whitespace_before_last_tag = ( preg_match('/\s$/', $curl) > 0 );
 		} elseif (!empty($curl)) {
 			/*
 			 * Only call _wptexturize_pushpop_element if first char is correct
Index: wp-testcase/test_includes_formatting.php
===================================================================
--- wp-testcase/test_includes_formatting.php	(revision 320)
+++ wp-testcase/test_includes_formatting.php	(working copy)
@@ -371,9 +371,23 @@
 		
 		$invalid_nest = '<pre></code>"baba"</pre>';
 		$this->assertEquals($invalid_nest, wptexturize($invalid_nest));
+	}
 
+	//WP Ticket #8912, #10033
+	function test_skip_html_comments() {
+		$this->assertEquals('<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>', wptexturize('<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>'));
+
+		$html = '<!--[if !IE]>-->' . "\n";
+		$html .= '<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/_nkZ3eHeXlc" width="320" height="260">' . "\n";
+		$html .= '<!--<![endif]-->' . "\n";
+		$this->assertEquals($html, wptexturize($html));
+
+		$html = '<!--[if !IE]>-->' . "\n";
+		$html .= '</object>' . "\n";
+		$html .= '<!--<![endif]-->' . "\n";
+		$this->assertEquals($html, wptexturize($html));
 	}
-	
+
 	//WP Ticket #1418
 	function test_bracketed_quotes_1418() {
 		$this->assertEquals('(&#8220;test&#8221;)', wptexturize('("test")'));
@@ -385,38 +399,92 @@
 	function test_bracketed_quotes_3810() {
 		$this->assertEquals('A dog (&#8220;Hubertus&#8221;) was sent out.', wptexturize('A dog ("Hubertus") was sent out.'));
 	}
-	
+
 	//WP Ticket #4539
 	function test_basic_quotes() {
+		$this->assertNotEquals('&#8216;&#8217;', wptexturize("''")); // this does not work as expected due to a static replacement
+		$this->assertEquals('&#8220;&#8221;', wptexturize('""'));
+		$this->assertEquals('&#8220;&#8221;', wptexturize("``''")); // this is what causes '' to fail
+		$this->assertEquals('&#8216; &#8217;', wptexturize("' '"));
+		$this->assertEquals('&#8220; &#8221;', wptexturize('" "'));
+		$this->assertEquals('&#8216;<img src="">&#8217;', wptexturize('\'<img src="">\''));
+		$this->assertEquals('&#8220;<img src="">&#8221;', wptexturize('"<img src="">"'));
+		$this->assertEquals('Peter&#8217;s photo: &#8216;<img src="">&#8217;', wptexturize('Peter\'s photo: \'<img src="">\''));
+		$this->assertEquals('Peter&#8217;s photo: &#8220;<img src="">&#8221;', wptexturize('Peter\'s photo: "<img src="">"'));
+
 		$this->assertEquals('test&#8217;s', wptexturize('test\'s'));
 		$this->assertEquals('test&#8217;s', wptexturize('test\'s'));
 
 		$this->assertEquals('&#8216;quoted&#8217;', wptexturize('\'quoted\''));
 		$this->assertEquals('&#8220;quoted&#8221;', wptexturize('"quoted"'));
+		$this->assertEquals('&#8216;quoted&#8217;s&#8217;', wptexturize('\'quoted\'s\''));
+		$this->assertEquals('&#8220;quoted&#8217;s&#8221;', wptexturize('"quoted\'s"'));
 
 		$this->assertEquals('(&#8216;quoted&#8217;)', wptexturize('(\'quoted\')'));
 		$this->assertEquals('{&#8220;quoted&#8221;}', wptexturize('{"quoted"}'));
+		$this->assertEquals('["quoted"]', wptexturize('["quoted"]')); // shortcode
+		$this->assertEquals('<"quoted">', wptexturize('<"quoted">')); // tag
+		$this->assertEquals('(&#8216;quoted&#8217;s&#8217;)', wptexturize('(\'quoted\'s\')'));
+		$this->assertEquals('{&#8220;quoted&#8217;s&#8221;}', wptexturize('{"quoted\'s"}'));
+		$this->assertEquals('["quoted\'s"]', wptexturize('["quoted\'s"]')); // shortcode
+		$this->assertEquals('<"quoted\'s">', wptexturize('<"quoted\'s">')); // tag
 
 		$this->assertEquals('&#8216;qu(ot)ed&#8217;', wptexturize('\'qu(ot)ed\''));
 		$this->assertEquals('&#8220;qu{ot}ed&#8221;', wptexturize('"qu{ot}ed"'));
+		$this->assertEquals('&#8220;qu[ot]ed&#8221;', wptexturize('"qu[ot]ed"'));
+		$this->assertEquals('&#8220;qu<ot>ed&#8221;', wptexturize('"qu<ot>ed"'));
+		$this->assertEquals('&#8216;qu(ot)ed&#8217;s&#8217;', wptexturize('\'qu(ot)ed\'s\''));
+		$this->assertEquals('&#8220;qu{ot}ed&#8217;s&#8221;', wptexturize('"qu{ot}ed\'s"'));
+		$this->assertEquals('&#8220;qu[ot]ed&#8217;s&#8221;', wptexturize('"qu[ot]ed\'s"'));
+		$this->assertEquals('&#8220;qu<ot>ed&#8217;s&#8221;', wptexturize('"qu<ot>ed\'s"'));
+		$this->assertEquals('&#8216;qu(ot)&#8217;s&#8217;', wptexturize('\'qu(ot)\'s\''));
+		$this->assertEquals('&#8220;qu{ot}&#8217;s&#8221;', wptexturize('"qu{ot}\'s"'));
+		$this->assertEquals('&#8220;qu[ot]&#8217;s&#8221;', wptexturize('"qu[ot]\'s"'));
+		$this->assertEquals('&#8220;qu<ot>&#8217;s&#8221;', wptexturize('"qu<ot>\'s"'));
 
 		$this->assertEquals('&#8216;test&#8217;s quoted&#8217;', wptexturize('\'test\'s quoted\''));
 		$this->assertEquals('&#8220;test&#8217;s quoted&#8221;', wptexturize('"test\'s quoted"'));
 	}
 
+	//WP Ticket #4539
+	function test_nested_quotes() {
+		$this->assertEquals('&#8220;This is a &#8216;nested quote&#8217;.&#8221;', wptexturize('"This is a \'nested quote\'."'));
+		$this->assertEquals('&#8216;This is a &#8220;nested quote&#8221;.&#8217;', wptexturize('\'This is a "nested quote".\''));
+		$this->assertEquals('&#8220;These are some &#8216;nested&#8217; &#8216;quotes&#8217;.&#8221;', wptexturize('"These are some \'nested\' \'quotes\'."'));
+		$this->assertEquals('&#8216;These are some &#8220;nested&#8221; &#8220;quotes&#8221;.&#8217;', wptexturize('\'These are some "nested" "quotes".\''));
+	}
+
 	//WP Tickets #4539, #15241
 	function test_full_sentences_with_unmatched_single_quotes() {
 		$this->assertEquals(
 			'That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.',
 			wptexturize("That means every moment you're working on something without it being in the public it's actually dying.")
 		);
+		$this->assertEquals(
+			'&#8216;That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.&#8217;',
+			wptexturize("'That means every moment you're working on something without it being in the public it's actually dying.'")
+		);
+		$this->assertEquals(
+			'&#8220;That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.&#8221;',
+			wptexturize("\"That means every moment you're working on something without it being in the public it's actually dying.\"")
+		);
+		$this->assertEquals(
+			'That means every moment you&#8217;re working on &#8216;something&#8217; without it being in the public it&#8217;s actually dying.',
+			wptexturize("That means every moment you're working on 'something' without it being in the public it's actually dying.")
+		);
+		$this->assertEquals(
+			'That means every moment you&#8217;re working on &#8220;something&#8221; without it being in the public it&#8217;s actually dying.',
+			wptexturize("That means every moment you're working on \"something\" without it being in the public it's actually dying.")
+		);
 	}
 
-	//WP Ticket #4539
+	//WP Tickets #4539, #10606
 	function test_quotes() {
 		$this->knownWPBug(4539);
 		$this->assertEquals('&#8220;Quoted String&#8221;', wptexturize('"Quoted String"'));
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"'));
+		$this->assertEquals('Here is &#8220; <a href="http://example.com">a test with a link</a> &#8221;', wptexturize('Here is " <a href="http://example.com">a test with a link</a> "'));
+		$this->assertEquals('Here is &#8220;<a href="http://example.com"> a test with a link </a>&#8221;', wptexturize('Here is "<a href="http://example.com"> a test with a link </a>"'));
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link and a period</a>&#8221;.', wptexturize('Here is "<a href="http://example.com">a test with a link and a period</a>".'));
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221; and a space.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>" and a space.'));
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a> and some text quoted&#8221;', wptexturize('Here is "<a href="http://example.com">a test with a link</a> and some text quoted"'));
@@ -424,8 +492,10 @@
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;; and a semi-colon.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"; and a semi-colon.'));
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;- and a dash.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"- and a dash.'));
 		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;&#8230; and ellipses.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"... and ellipses.'));
+		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test</a> with a link&#8221;.', wptexturize('Here is "<a href="http://example.com">a test</a> with a link".'));
 		$this->assertEquals('Here is &#8220;a test <a href="http://example.com">with a link</a>&#8221;.', wptexturize('Here is "a test <a href="http://example.com">with a link</a>".'));
-		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;and a work stuck to the end.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"and a work stuck to the end.'));
+		$this->assertEquals('Here is &#8220;a test <a href="http://example.com">with a link</a>&#8221; .', wptexturize('Here is "a test <a href="http://example.com">with a link</a>" .'));
+		$this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;and a word stuck to the end.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"and a word stuck to the end.'));
 		$this->assertEquals('A test with a finishing number, &#8220;like 23&#8221;.', wptexturize('A test with a finishing number, "like 23".'));
 		$this->assertEquals('A test with a number, &#8220;like 62&#8221;, is nice to have.', wptexturize('A test with a number, "like 62", is nice to have.'));
 	}
@@ -451,23 +521,66 @@
 		$this->assertEquals('&#8220;Class of 99&#8221;', wptexturize("\"Class of 99\""));
 		$this->assertEquals('&#8220;Class of &#8217;99&#8221;', wptexturize("\"Class of '99\""));
 	}
-	
-	function test_quotes_after_numbers() {
-		$this->assertEquals('Class of &#8217;99', wptexturize("Class of '99"));
-	}
-	
+
 	//WP Ticket #15241
 	function test_other_html() {
 		$this->knownWPBug(15241);
 		$this->assertEquals('&#8216;<strong>', wptexturize("'<strong>"));
+		$this->assertEquals('&#8220;<strong>', wptexturize('"<strong>'));
+		$this->assertEquals('&#8216;<strong></strong>&#8217;', wptexturize("'<strong></strong>'"));
+		$this->assertEquals('&#8220;<strong></strong>&#8221;', wptexturize('"<strong></strong>"'));
 		$this->assertEquals('&#8216;<strong>Quoted Text</strong>&#8217;,', wptexturize("'<strong>Quoted Text</strong>',"));
 		$this->assertEquals('&#8220;<strong>Quoted Text</strong>&#8221;,', wptexturize('"<strong>Quoted Text</strong>",'));
 	}
-	
+
+	//WP Ticket #15241
+	function test_many_single_quotes() {
+		$this->assertEquals('This isn&#8217;t inherently bad, but I don&#8217;t think it&#8217;s normal.', wptexturize("This isn't inherently bad, but I don't think it's normal."));
+	}
+
+	//WP Ticket #1258
+	function test_enumeration() {
+		$this->assertEquals("&#8216;products&#8217;, &#8216;services&#8217;", wptexturize("'products', 'services'"));
+		$this->assertEquals("&#8216;hello&#8217;, &#8216;world&#8217;, &#8217;tis", wptexturize("'hello', 'world', 'tis"));
+		$this->assertEquals("&#8216;hello&#8217;, &#8216;world&#8217;, &#8217;tis ", wptexturize("'hello', 'world', 'tis "));
+	}
+
+	//WP Ticket #11275
+	function test_quoting() {
+		$this->assertEquals('She said—&#8220;No!&#8221;', wptexturize('She said—"No!"'));
+		$this->assertEquals('She said — &#8220;No!&#8221;', wptexturize('She said — "No!"'));
+		$this->assertEquals('She said—&#8220;<a href="#">No!</a>&#8221;', wptexturize('She said—"<a href="#">No!</a>"'));
+		$this->assertEquals('She said—&#8216;<a href="#">It&#8217;s Peter&#8217;s!</a>&#8217;', wptexturize('She said—\'<a href="#">It\'s Peter\'s!</a>\''));
+		$this->assertEquals('She said—&#8220;<a href="#">It&#8217;s Peter&#8217;s!</a>&#8221;', wptexturize('She said—"<a href="#">It\'s Peter\'s!</a>"'));
+	}
+
+	//WP Ticket #15444
+	function test_tag_followed_by_quote() {
+		$this->knownWPBug(15444);
+		$this->assertEquals('<a href="#">Jim</a>&#8217;s red bike.', wptexturize('<a href="#">Jim</a>\'s red bike.'));
+		$this->assertEquals('&#8216;<a href="#">Jim</a>&#8217;s red bike.&#8217;', wptexturize('\'<a href="#">Jim</a>\'s red bike.\''));
+		$this->assertEquals('&#8220;<a href="#">Jim</a>&#8217;s red bike.&#8221;', wptexturize('"<a href="#">Jim</a>\'s red bike."'));
+		$this->assertEquals('<a href="#">Jim</a>&#8217;s &#8216;red bike.&#8217;', wptexturize('<a href="#">Jim</a>\'s \'red bike.\''));
+		$this->assertEquals('<a href="#">Jim</a>&#8217;s &#8220;red bike.&#8221;', wptexturize('<a href="#">Jim</a>\'s "red bike."'));
+		$this->assertEquals('<a href="#">Jim</a>&#8217;s &#8216;<a href="#">red bike</a>.&#8217;', wptexturize('<a href="#">Jim</a>\'s \'<a href="#">red bike</a>.\''));
+		$this->assertEquals('<a href="#">Jim</a>&#8217;s &#8220;<a href="#">red bike</a>.&#8221;', wptexturize('<a href="#">Jim</a>\'s "<a href="#">red bike</a>."'));
+	}
+
 	function test_x() {
 		$this->assertEquals('14&#215;24', wptexturize("14x24"));
+		$this->assertEquals('&#8216;14&#215;24&#8217;', wptexturize("'14x24'"));
+		$this->assertEquals('&#8220;14&#215;24&#8221;', wptexturize('"14x24"'));
+		$this->assertEquals('&#8216;<a href="#">14&#215;24</a>&#8217;', wptexturize('\'<a href="#">14x24</a>\''));
+		$this->assertEquals('&#8220;<a href="#">14&#215;24</a>&#8221;', wptexturize('"<a href="#">14x24</a>"'));
 	}
-	
+
+	//WP Ticket #4116
+	function test_x_4116() {
+		$this->knownWPBug(4116);
+		$this->assertEquals('www.a4x3b.com', wptexturize("www.a4x3b.com"));
+		$this->assertEquals('http://www.youtube.com/watch?v=irWR7F0x2uU', wptexturize("http://www.youtube.com/watch?v=irWR7F0x2uU"));
+	}
+
 	function test_minutes_seconds() {
 		$this->assertEquals('9&#8242;', wptexturize('9\''));
 		$this->assertEquals('9&#8243;', wptexturize("9\""));
@@ -484,6 +597,11 @@
 		$this->assertEquals('&nbsp;&#8220;Testing&#8221;', wptexturize('&nbsp;"Testing"'));
 		$this->assertEquals('&#38;&#8220;Testing&#8221;', wptexturize('&#38;"Testing"'));
 	}
+
+	//WP Ticket #6969
+	function test_shortcode_skip() {
+		$this->assertEquals('[code lang="php"]$foo = \'bar\';[/code]', wptexturize('[code lang="php"]$foo = \'bar\';[/code]'));
+	}
 }
 
 class TestCleanUrl extends WPTestCase {
