diff --git a/test.html b/test.html index dbe992f2e9..063a8ea172 100644 --- a/test.html +++ b/test.html @@ -138,6 +138,28 @@

Test drive

languages = components.languages, highlightCode = function() { Prism.highlightElement(code); }; + +function updateHashLanguage(lang) { + location.hash = lang ? 'language=' + lang : ''; +} +function getHashLanguage() { + var match = /[#&]language=([^&]+)/.exec(location.hash); + return match ? match[1] : null; +} +function getRadio(lang) { + return $('input[name=language][value="' + lang + '"]'); +} + +window.onhashchange = function () { + var input = getRadio(getHashLanguage()); + + if (input && !input.checked) { + input.checked = true; + input.onclick(); + } +} + + for (var id in languages) { if (id == 'meta') { continue; @@ -160,6 +182,7 @@

Test drive

var lang = this.value; code.className = 'language-' + lang; code.textContent = code.textContent; + updateHashLanguage(lang); // loadLanguage() returns a promise, so we use highlightCode() // as resolve callback. The promise will be immediately @@ -228,15 +251,39 @@

Test drive

var radios = $$('input[name=language]'); -radios[0].checked = true; -radios[0].onclick(); +var selectedRadio = radios[0]; + +var lastLanguageRadio = getRadio(getHashLanguage()); +if (lastLanguageRadio) { + selectedRadio = lastLanguageRadio; +} + +selectedRadio.checked = true; +selectedRadio.onclick(); var textarea = $('textarea', form); -(textarea.oninput = function() { - code.textContent = this.value || ''; +try { + var lastCode = sessionStorage.getItem('test-code'); + if (lastCode) { + textarea.value = lastCode; + } +} catch (e) { + // ignore sessionStorage errors +} + +textarea.oninput = function() { + var codeText = this.value || ''; + code.textContent = codeText; highlightCode(); -}).call(textarea); + + try { + sessionStorage.setItem('test-code', codeText); + } catch (error) { + // ignore sessionStorage errors + } +} +textarea.oninput(); $('#option-show-tokens').onchange = function () { var cls = 'show-tokens';