Skip to content

Commit 1c78ad9

Browse files
committed
auto merge of #20990 : estsauver/rust/playpen_20732, r=alexcrichton
In #20732, that all links in some modules point to the same code examples was reported. The ID's generated for documents in librustdoc are not all unique, which means the code rendered as text is not being properly selected. This change makes the link to the code section that is next to the current link.
2 parents 0c96037 + 2a320f2 commit 1c78ad9

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

src/librustdoc/html/markdown.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use libc;
3131
use std::ascii::AsciiExt;
3232
use std::ffi::CString;
33-
use std::cell::{RefCell, Cell};
33+
use std::cell::RefCell;
3434
use std::collections::HashMap;
3535
use std::fmt;
3636
use std::slice;
@@ -155,7 +155,6 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
155155
thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, uint>> = {
156156
RefCell::new(HashMap::new())
157157
});
158-
thread_local!(static TEST_IDX: Cell<uint> = Cell::new(0));
159158

160159
thread_local!(pub static PLAYGROUND_KRATE: RefCell<Option<Option<String>>> = {
161160
RefCell::new(None)
@@ -195,26 +194,19 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
195194
if rendered { return }
196195
PLAYGROUND_KRATE.with(|krate| {
197196
let mut s = String::new();
198-
let id = krate.borrow().as_ref().map(|krate| {
199-
let idx = TEST_IDX.with(|slot| {
200-
let i = slot.get();
201-
slot.set(i + 1);
202-
i
203-
});
204-
197+
krate.borrow().as_ref().map(|krate| {
205198
let test = origtext.lines().map(|l| {
206199
stripped_filtered_line(l).unwrap_or(l)
207200
}).collect::<Vec<&str>>().connect("\n");
208201
let krate = krate.as_ref().map(|s| s.as_slice());
209202
let test = test::maketest(test.as_slice(), krate, false, false);
210-
s.push_str(format!("<span id='rust-example-raw-{}' \
211-
class='rusttest'>{}</span>",
212-
idx, Escape(test.as_slice())).as_slice());
213-
format!("rust-example-rendered-{}", idx)
203+
s.push_str(format!("<span class='rusttest'>{}</span>",
204+
Escape(test.as_slice())).as_slice());
214205
});
215-
let id = id.as_ref().map(|a| a.as_slice());
216-
s.push_str(highlight::highlight(text.as_slice(), None, id)
217-
.as_slice());
206+
s.push_str(highlight::highlight(text.as_slice(),
207+
None,
208+
Some("rust-example-rendered"))
209+
.as_slice());
218210
let output = CString::from_vec(s.into_bytes());
219211
hoedown_buffer_puts(ob, output.as_ptr());
220212
})
@@ -432,7 +424,6 @@ impl LangString {
432424
/// previous state (if any).
433425
pub fn reset_headers() {
434426
USED_HEADER_MAP.with(|s| s.borrow_mut().clear());
435-
TEST_IDX.with(|s| s.set(0));
436427
}
437428

438429
impl<'a> fmt::String for Markdown<'a> {

src/librustdoc/html/static/playpen.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
(function() {
1515
if (window.playgroundUrl) {
1616
$('pre.rust').hover(function() {
17-
if (!$(this).attr('id')) { return; }
18-
var id = '#' + $(this).attr('id').replace('rendered', 'raw');
1917
var a = $('<a>').text('⇱').attr('class', 'test-arrow');
20-
var code = $(id).text();
18+
var code = $(this).siblings(".rusttest").text();
2119
a.attr('href', window.playgroundUrl + '?code=' +
2220
encodeURIComponent(code));
2321
a.attr('target', '_blank');

0 commit comments

Comments
 (0)