ruby.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*!
  2. {
  3. "name": "ruby, rp, rt Elements",
  4. "caniuse": "ruby",
  5. "property": "ruby",
  6. "tags": ["elem"],
  7. "builderAliases": ["elem_ruby"],
  8. "authors": ["Cătălin Mariș"],
  9. "notes": [{
  10. "name": "WHATWG Spec",
  11. "href": "https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-ruby-element"
  12. }]
  13. }
  14. !*/
  15. define(['Modernizr', 'createElement', 'docElement'], function(Modernizr, createElement, docElement) {
  16. Modernizr.addTest('ruby', function() {
  17. var ruby = createElement('ruby');
  18. var rt = createElement('rt');
  19. var rp = createElement('rp');
  20. var displayStyleProperty = 'display';
  21. // 'fontSize' - because it`s only used for IE6 and IE7
  22. var fontSizeStyleProperty = 'fontSize';
  23. ruby.appendChild(rp);
  24. ruby.appendChild(rt);
  25. docElement.appendChild(ruby);
  26. // browsers that support <ruby> hide the <rp> via "display:none"
  27. if (getStyle(rp, displayStyleProperty) === 'none' || // for non-IE browsers
  28. // but in IE browsers <rp> has "display:inline" so, the test needs other conditions:
  29. getStyle(ruby, displayStyleProperty) === 'ruby' && getStyle(rt, displayStyleProperty) === 'ruby-text' || // for IE8+
  30. getStyle(rp, fontSizeStyleProperty) === '6pt' && getStyle(rt, fontSizeStyleProperty) === '6pt') { // for IE6 & IE7
  31. cleanUp();
  32. return true;
  33. } else {
  34. cleanUp();
  35. return false;
  36. }
  37. function getStyle(element, styleProperty) {
  38. var result;
  39. if (window.getComputedStyle) { // for non-IE browsers
  40. result = document.defaultView.getComputedStyle(element, null).getPropertyValue(styleProperty);
  41. } else if (element.currentStyle) { // for IE
  42. result = element.currentStyle[styleProperty];
  43. }
  44. return result;
  45. }
  46. function cleanUp() {
  47. docElement.removeChild(ruby);
  48. // the removed child node still exists in memory, so ...
  49. ruby = null;
  50. rt = null;
  51. rp = null;
  52. }
  53. });
  54. });