jquery.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //= require views/pages/base
  2. app.views.JqueryPage = class JqueryPage extends app.views.BasePage {
  3. static demoClassName = "_jquery-demo";
  4. afterRender() {
  5. // Prevent jQuery Mobile's demo iframes from scrolling the page
  6. for (var iframe of this.findAllByTag("iframe")) {
  7. iframe.style.display = "none";
  8. this.onIframeLoaded = this.onIframeLoaded.bind(this);
  9. $.on(iframe, "load", this.onIframeLoaded);
  10. }
  11. return this.runExamples();
  12. }
  13. onIframeLoaded(event) {
  14. event.target.style.display = "";
  15. $.off(event.target, "load", this.onIframeLoaded);
  16. }
  17. runExamples() {
  18. for (var el of this.findAllByClass("entry-example")) {
  19. try {
  20. this.runExample(el);
  21. } catch (error) {}
  22. }
  23. }
  24. runExample(el) {
  25. let iframe;
  26. const source = el.getElementsByClassName("syntaxhighlighter")[0];
  27. if (!source || source.innerHTML.indexOf("!doctype") === -1) {
  28. return;
  29. }
  30. if (!(iframe = el.getElementsByClassName(JqueryPage.demoClassName)[0])) {
  31. iframe = document.createElement("iframe");
  32. iframe.className = JqueryPage.demoClassName;
  33. iframe.width = "100%";
  34. iframe.height = 200;
  35. el.appendChild(iframe);
  36. }
  37. const doc = iframe.contentDocument;
  38. doc.write(this.fixIframeSource(source.textContent));
  39. doc.close();
  40. }
  41. fixIframeSource(source) {
  42. source = source.replace(
  43. '"/resources/',
  44. '"https://api.jquery.com/resources/',
  45. ); // attr(), keydown()
  46. source = source.replace(
  47. "</head>",
  48. `\
  49. <style>
  50. html, body { border: 0; margin: 0; padding: 0; }
  51. body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
  52. </style>
  53. <script>
  54. $.ajaxPrefilter(function(opt, opt2, xhr) {
  55. if (opt.url.indexOf('http') !== 0) {
  56. xhr.abort();
  57. document.body.innerHTML = "<p><strong>This demo cannot run inside DevDocs.</strong></p>";
  58. }
  59. });
  60. </script>
  61. </head>\
  62. `,
  63. );
  64. return source.replace(/<script>/gi, '<script nonce="devdocs">');
  65. }
  66. };