prism-wpd.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. (function(){
  2. if (
  3. typeof self !== 'undefined' && !self.Prism ||
  4. typeof global !== 'undefined' && !global.Prism
  5. ) {
  6. return;
  7. }
  8. if (Prism.languages.css) {
  9. // check whether the selector is an advanced pattern before extending it
  10. if (Prism.languages.css.selector.pattern)
  11. {
  12. Prism.languages.css.selector.inside['pseudo-class'] = /:[\w-]+/;
  13. Prism.languages.css.selector.inside['pseudo-element'] = /::[\w-]+/;
  14. }
  15. else
  16. {
  17. Prism.languages.css.selector = {
  18. pattern: Prism.languages.css.selector,
  19. inside: {
  20. 'pseudo-class': /:[\w-]+/,
  21. 'pseudo-element': /::[\w-]+/
  22. }
  23. };
  24. }
  25. }
  26. if (Prism.languages.markup) {
  27. Prism.languages.markup.tag.inside.tag.inside['tag-id'] = /[\w-]+/;
  28. var Tags = {
  29. HTML: {
  30. 'a': 1, 'abbr': 1, 'acronym': 1, 'b': 1, 'basefont': 1, 'bdo': 1, 'big': 1, 'blink': 1, 'cite': 1, 'code': 1, 'dfn': 1, 'em': 1, 'kbd': 1, 'i': 1,
  31. 'rp': 1, 'rt': 1, 'ruby': 1, 's': 1, 'samp': 1, 'small': 1, 'spacer': 1, 'strike': 1, 'strong': 1, 'sub': 1, 'sup': 1, 'time': 1, 'tt': 1, 'u': 1,
  32. 'var': 1, 'wbr': 1, 'noframes': 1, 'summary': 1, 'command': 1, 'dt': 1, 'dd': 1, 'figure': 1, 'figcaption': 1, 'center': 1, 'section': 1, 'nav': 1,
  33. 'article': 1, 'aside': 1, 'hgroup': 1, 'header': 1, 'footer': 1, 'address': 1, 'noscript': 1, 'isIndex': 1, 'main': 1, 'mark': 1, 'marquee': 1,
  34. 'meter': 1, 'menu': 1
  35. },
  36. SVG: {
  37. 'animateColor': 1, 'animateMotion': 1, 'animateTransform': 1, 'glyph': 1, 'feBlend': 1, 'feColorMatrix': 1, 'feComponentTransfer': 1,
  38. 'feFuncR': 1, 'feFuncG': 1, 'feFuncB': 1, 'feFuncA': 1, 'feComposite': 1, 'feConvolveMatrix': 1, 'feDiffuseLighting': 1, 'feDisplacementMap': 1,
  39. 'feFlood': 1, 'feGaussianBlur': 1, 'feImage': 1, 'feMerge': 1, 'feMergeNode': 1, 'feMorphology': 1, 'feOffset': 1, 'feSpecularLighting': 1,
  40. 'feTile': 1, 'feTurbulence': 1, 'feDistantLight': 1, 'fePointLight': 1, 'feSpotLight': 1, 'linearGradient': 1, 'radialGradient': 1, 'altGlyph': 1,
  41. 'textPath': 1, 'tref': 1, 'altglyph': 1, 'textpath': 1, 'altglyphdef': 1, 'altglyphitem': 1, 'clipPath': 1, 'color-profile': 1, 'cursor': 1,
  42. 'font-face': 1, 'font-face-format': 1, 'font-face-name': 1, 'font-face-src': 1, 'font-face-uri': 1, 'foreignObject': 1, 'glyphRef': 1,
  43. 'hkern': 1, 'vkern': 1
  44. },
  45. MathML: {}
  46. }
  47. }
  48. var language;
  49. Prism.hooks.add('wrap', function(env) {
  50. if ((env.type == 'tag-id'
  51. || (env.type == 'property' && env.content.indexOf('-') != 0)
  52. || (env.type == 'rule'&& env.content.indexOf('@-') != 0)
  53. || (env.type == 'pseudo-class'&& env.content.indexOf(':-') != 0)
  54. || (env.type == 'pseudo-element'&& env.content.indexOf('::-') != 0)
  55. || (env.type == 'attr-name' && env.content.indexOf('data-') != 0)
  56. ) && env.content.indexOf('<') === -1
  57. ) {
  58. if (env.language == 'css'
  59. || env.language == 'scss'
  60. || env.language == 'markup'
  61. ) {
  62. var href = 'https://webplatform.github.io/docs/';
  63. var content = env.content;
  64. if (env.language == 'css' || env.language == 'scss') {
  65. href += 'css/';
  66. if (env.type == 'property') {
  67. href += 'properties/';
  68. }
  69. else if (env.type == 'rule') {
  70. href += 'atrules/';
  71. content = content.substring(1);
  72. }
  73. else if (env.type == 'pseudo-class') {
  74. href += 'selectors/pseudo-classes/';
  75. content = content.substring(1);
  76. }
  77. else if (env.type == 'pseudo-element') {
  78. href += 'selectors/pseudo-elements/';
  79. content = content.substring(2);
  80. }
  81. }
  82. else if (env.language == 'markup') {
  83. if (env.type == 'tag-id') {
  84. // Check language
  85. language = getLanguage(env.content) || language;
  86. if (language) {
  87. href += language + '/elements/';
  88. }
  89. else {
  90. return; // Abort
  91. }
  92. }
  93. else if (env.type == 'attr-name') {
  94. if (language) {
  95. href += language + '/attributes/';
  96. }
  97. else {
  98. return; // Abort
  99. }
  100. }
  101. }
  102. href += content;
  103. env.tag = 'a';
  104. env.attributes.href = href;
  105. env.attributes.target = '_blank';
  106. }
  107. }
  108. });
  109. function getLanguage(tag) {
  110. var tagL = tag.toLowerCase();
  111. if (Tags.HTML[tagL]) {
  112. return 'html';
  113. }
  114. else if (Tags.SVG[tag]) {
  115. return 'svg';
  116. }
  117. else if (Tags.MathML[tag]) {
  118. return 'mathml';
  119. }
  120. // Not in dictionary, perform check
  121. if (Tags.HTML[tagL] !== 0 && typeof document !== 'undefined') {
  122. var htmlInterface = (document.createElement(tag).toString().match(/\[object HTML(.+)Element\]/) || [])[1];
  123. if (htmlInterface && htmlInterface != 'Unknown') {
  124. Tags.HTML[tagL] = 1;
  125. return 'html';
  126. }
  127. }
  128. Tags.HTML[tagL] = 0;
  129. if (Tags.SVG[tag] !== 0 && typeof document !== 'undefined') {
  130. var svgInterface = (document.createElementNS('http://www.w3.org/2000/svg', tag).toString().match(/\[object SVG(.+)Element\]/) || [])[1];
  131. if (svgInterface && svgInterface != 'Unknown') {
  132. Tags.SVG[tag] = 1;
  133. return 'svg';
  134. }
  135. }
  136. Tags.SVG[tag] = 0;
  137. // Lame way to detect MathML, but browsers don’t expose interface names there :(
  138. if (Tags.MathML[tag] !== 0) {
  139. if (tag.indexOf('m') === 0) {
  140. Tags.MathML[tag] = 1;
  141. return 'mathml';
  142. }
  143. }
  144. Tags.MathML[tag] = 0;
  145. return null;
  146. }
  147. })();