prism-ie8.js 946 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function(){
  2. if (!window.Prism) {
  3. return;
  4. }
  5. var dummy = document.createElement('header');
  6. if (!String.prototype.trim) {
  7. String.prototype.trim = function () {
  8. return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
  9. };
  10. }
  11. // textContent polyfill
  12. if (!('textContent' in dummy) && ('innerText' in dummy) && Object.defineProperty) {
  13. Object.defineProperty(Element.prototype, 'textContent', {
  14. get: function() {
  15. return this.innerText;
  16. },
  17. set: function(text) {
  18. this.innerText = text;
  19. }
  20. });
  21. }
  22. // IE8 doesn't have DOMContentLoaded
  23. if (!document.addEventListener && 'textContent' in dummy) {
  24. setTimeout(Prism.highlightAll, 10);
  25. }
  26. // Test if innerHTML line break bug is present
  27. dummy.innerHTML = '\r\n';
  28. if (dummy.textContent.indexOf('\n') === -1) {
  29. // IE8 innerHTML bug: Discards line breaks
  30. Prism.hooks.add('after-highlight', function(env) {
  31. env.element.innerHTML = env.highlightedCode.replace(/\r?\n/g, '<br>');
  32. });
  33. }
  34. })();