browserfeature.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2005 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Trogedit constants for browser features and quirks that should
  16. * be used by the rich text editor.
  17. */
  18. goog.provide('goog.editor.BrowserFeature');
  19. goog.require('goog.editor.defines');
  20. goog.require('goog.labs.userAgent.browser');
  21. goog.require('goog.userAgent');
  22. goog.require('goog.userAgent.product');
  23. goog.require('goog.userAgent.product.isVersion');
  24. /**
  25. * Maps browser quirks to boolean values, detailing what the current
  26. * browser supports.
  27. * @const
  28. */
  29. goog.editor.BrowserFeature = {
  30. // Whether this browser uses the IE TextRange object.
  31. HAS_IE_RANGES: goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9),
  32. // Whether this browser uses the W3C standard Range object.
  33. // Assumes IE higher versions will be compliance with W3C standard.
  34. HAS_W3C_RANGES: goog.userAgent.GECKO || goog.userAgent.WEBKIT ||
  35. goog.userAgent.OPERA || goog.userAgent.EDGE ||
  36. (goog.userAgent.IE && goog.userAgent.isDocumentModeOrHigher(9)),
  37. // Has the contentEditable attribute, which makes nodes editable.
  38. //
  39. // NOTE(nicksantos): FF3 has contentEditable, but there are 3 major reasons
  40. // why we don't use it:
  41. // 1) In FF3, we listen for key events on the document, and we'd have to
  42. // filter them properly. See TR_Browser.USE_DOCUMENT_FOR_KEY_EVENTS.
  43. // 2) In FF3, we listen for focus/blur events on the document, which
  44. // simply doesn't make sense in contentEditable. focus/blur
  45. // on contentEditable elements still has some quirks, which we're
  46. // talking to Firefox-team about.
  47. // 3) We currently use Mutation events in FF3 to detect changes,
  48. // and these are dispatched on the document only.
  49. // If we ever hope to support FF3/contentEditable, all 3 of these issues
  50. // will need answers. Most just involve refactoring at our end.
  51. HAS_CONTENT_EDITABLE: goog.userAgent.IE || goog.userAgent.WEBKIT ||
  52. goog.userAgent.OPERA || goog.userAgent.EDGE ||
  53. (goog.editor.defines.USE_CONTENTEDITABLE_IN_FIREFOX_3 &&
  54. goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9')),
  55. // Whether to use mutation event types to detect changes
  56. // in the field contents.
  57. USE_MUTATION_EVENTS: goog.userAgent.GECKO,
  58. // Whether the browser has a functional DOMSubtreeModified event.
  59. // TODO(user): Enable for all FF3 once we're confident this event fires
  60. // reliably. Currently it's only enabled if using contentEditable in FF as
  61. // we have no other choice in that case but to use this event.
  62. HAS_DOM_SUBTREE_MODIFIED_EVENT: goog.userAgent.WEBKIT ||
  63. (goog.editor.defines.USE_CONTENTEDITABLE_IN_FIREFOX_3 &&
  64. goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9')),
  65. // Whether nodes can be copied from one document to another
  66. HAS_DOCUMENT_INDEPENDENT_NODES: goog.userAgent.GECKO,
  67. // Whether the cursor goes before or inside the first block element on
  68. // focus, e.g., <body><p>foo</p></body>. FF will put the cursor before the
  69. // paragraph on focus, which is wrong.
  70. PUTS_CURSOR_BEFORE_FIRST_BLOCK_ELEMENT_ON_FOCUS: goog.userAgent.GECKO,
  71. // Whether the selection of one frame is cleared when another frame
  72. // is focused.
  73. CLEARS_SELECTION_WHEN_FOCUS_LEAVES:
  74. goog.userAgent.IE || goog.userAgent.WEBKIT || goog.userAgent.OPERA,
  75. // Whether "unselectable" is supported as an element style.
  76. HAS_UNSELECTABLE_STYLE: goog.userAgent.GECKO || goog.userAgent.WEBKIT,
  77. // Whether this browser's "FormatBlock" command does not suck.
  78. FORMAT_BLOCK_WORKS_FOR_BLOCKQUOTES:
  79. goog.userAgent.GECKO || goog.userAgent.WEBKIT || goog.userAgent.OPERA,
  80. // Whether this browser's "FormatBlock" command may create multiple
  81. // blockquotes.
  82. CREATES_MULTIPLE_BLOCKQUOTES:
  83. (goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('534.16')) ||
  84. goog.userAgent.OPERA,
  85. // Whether this browser's "FormatBlock" command will wrap blockquotes
  86. // inside of divs, instead of replacing divs with blockquotes.
  87. WRAPS_BLOCKQUOTE_IN_DIVS: goog.userAgent.OPERA,
  88. // Whether the readystatechange event is more reliable than load.
  89. PREFERS_READY_STATE_CHANGE_EVENT: goog.userAgent.IE,
  90. // Whether hitting the tab key will fire a keypress event.
  91. // see http://www.quirksmode.org/js/keys.html
  92. // TODO(user): This is fixed in IE8 and higher.
  93. TAB_FIRES_KEYPRESS: !goog.userAgent.IE,
  94. // Has a standards mode quirk where width=100% doesn't do the right thing,
  95. // but width=99% does.
  96. // TODO(user|user): This should be fixable by less hacky means
  97. NEEDS_99_WIDTH_IN_STANDARDS_MODE: goog.userAgent.IE,
  98. // Whether keyboard events only reliably fire on the document.
  99. // On Gecko without contentEditable, keyboard events only fire reliably on the
  100. // document element. With contentEditable, the field itself is focusable,
  101. // which means that it will fire key events. This does not apply if
  102. // application is using ContentEditableField or otherwise overriding Field
  103. // not to use an iframe.
  104. USE_DOCUMENT_FOR_KEY_EVENTS: goog.userAgent.GECKO &&
  105. !goog.editor.defines.USE_CONTENTEDITABLE_IN_FIREFOX_3,
  106. // Whether this browser shows non-standard attributes in innerHTML.
  107. SHOWS_CUSTOM_ATTRS_IN_INNER_HTML: goog.userAgent.IE,
  108. // Whether this browser shrinks empty nodes away to nothing.
  109. // (If so, we need to insert some space characters into nodes that
  110. // shouldn't be collapsed)
  111. COLLAPSES_EMPTY_NODES:
  112. goog.userAgent.GECKO || goog.userAgent.WEBKIT || goog.userAgent.OPERA,
  113. // Whether we must convert <strong> and <em> tags to <b>, <i>.
  114. CONVERT_TO_B_AND_I_TAGS: goog.userAgent.GECKO || goog.userAgent.OPERA,
  115. // Whether this browser likes to tab through images in contentEditable mode,
  116. // and we like to disable this feature.
  117. TABS_THROUGH_IMAGES: goog.userAgent.IE,
  118. // Whether this browser unescapes urls when you extract it from the href tag.
  119. UNESCAPES_URLS_WITHOUT_ASKING:
  120. goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7.0'),
  121. // Whether this browser supports execCommand("styleWithCSS") to toggle between
  122. // inserting html tags or inline styling for things like bold, italic, etc.
  123. HAS_STYLE_WITH_CSS:
  124. goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.8') ||
  125. goog.userAgent.WEBKIT || goog.userAgent.OPERA,
  126. // Whether clicking on an editable link will take you to that site.
  127. FOLLOWS_EDITABLE_LINKS: goog.userAgent.WEBKIT ||
  128. goog.userAgent.IE && goog.userAgent.isVersionOrHigher('9'),
  129. // Whether this browser has document.activeElement available.
  130. HAS_ACTIVE_ELEMENT: goog.userAgent.IE || goog.userAgent.EDGE ||
  131. goog.userAgent.OPERA ||
  132. goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9'),
  133. // Whether this browser supports the setCapture method on DOM elements.
  134. HAS_SET_CAPTURE: goog.userAgent.IE,
  135. // Whether this browser can't set background color when the selection
  136. // is collapsed.
  137. EATS_EMPTY_BACKGROUND_COLOR: goog.userAgent.GECKO ||
  138. goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('527'),
  139. // Whether this browser supports the "focusin" or "DOMFocusIn" event
  140. // consistently.
  141. // NOTE(nicksantos): FF supports DOMFocusIn, but doesn't seem to do so
  142. // consistently.
  143. SUPPORTS_FOCUSIN: goog.userAgent.IE || goog.userAgent.OPERA,
  144. // Whether clicking on an image will cause the selection to move to the image.
  145. // Note: Gecko moves the selection, but it won't always go to the image.
  146. // For example, if the image is wrapped in a div, and you click on the img,
  147. // anchorNode = focusNode = div, anchorOffset = 0, focusOffset = 1, so this
  148. // is another way of "selecting" the image, but there are too many special
  149. // cases like this so we will do the work manually.
  150. SELECTS_IMAGES_ON_CLICK: goog.userAgent.IE || goog.userAgent.OPERA,
  151. // Whether this browser moves <style> tags into new <head> elements.
  152. MOVES_STYLE_TO_HEAD: goog.userAgent.WEBKIT,
  153. // Whether this browser collapses the selection in a contenteditable when the
  154. // mouse is pressed in a non-editable portion of the same frame, even if
  155. // Event.preventDefault is called. This field is deprecated and unused -- only
  156. // old versions of Opera have this bug.
  157. COLLAPSES_SELECTION_ONMOUSEDOWN: false,
  158. // Whether the user can actually create a selection in this browser with the
  159. // caret in the MIDDLE of the selection by double-clicking.
  160. CARET_INSIDE_SELECTION: goog.userAgent.OPERA,
  161. // Whether the browser focuses <body contenteditable> automatically when
  162. // the user clicks on <html>. This field is deprecated and unused -- only old
  163. // versions of Opera don't have this behavior.
  164. FOCUSES_EDITABLE_BODY_ON_HTML_CLICK: true,
  165. // Whether to use keydown for key listening (uses keypress otherwise). Taken
  166. // from goog.events.KeyHandler.
  167. USES_KEYDOWN: goog.userAgent.IE || goog.userAgent.EDGE ||
  168. goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('525'),
  169. // Whether this browser converts spaces to non-breaking spaces when calling
  170. // execCommand's RemoveFormat.
  171. // See: https://bugs.webkit.org/show_bug.cgi?id=14062
  172. ADDS_NBSPS_IN_REMOVE_FORMAT:
  173. goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('531'),
  174. // Whether the browser will get stuck inside a link. That is, if your cursor
  175. // is after a link and you type, does your text go inside the link tag.
  176. // Bug: http://bugs.webkit.org/show_bug.cgi?id=17697
  177. GETS_STUCK_IN_LINKS:
  178. goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('528'),
  179. // Whether the browser corrupts empty text nodes in Node#normalize,
  180. // removing them from the Document instead of merging them.
  181. NORMALIZE_CORRUPTS_EMPTY_TEXT_NODES:
  182. goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9') ||
  183. goog.userAgent.IE || goog.userAgent.EDGE || goog.userAgent.OPERA ||
  184. goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('531'),
  185. // Whether the browser corrupts all text nodes in Node#normalize,
  186. // removing them from the Document instead of merging them.
  187. NORMALIZE_CORRUPTS_ALL_TEXT_NODES: goog.userAgent.IE,
  188. // Browsers where executing subscript then superscript (or vv) will cause both
  189. // to be applied in a nested fashion instead of the first being overwritten by
  190. // the second.
  191. NESTS_SUBSCRIPT_SUPERSCRIPT: goog.userAgent.IE || goog.userAgent.EDGE ||
  192. goog.userAgent.GECKO || goog.userAgent.OPERA,
  193. // Whether this browser can place a cursor in an empty element natively.
  194. CAN_SELECT_EMPTY_ELEMENT: !goog.userAgent.IE && !goog.userAgent.WEBKIT,
  195. FORGETS_FORMATTING_WHEN_LISTIFYING: goog.userAgent.GECKO ||
  196. goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher('526'),
  197. LEAVES_P_WHEN_REMOVING_LISTS: goog.userAgent.IE || goog.userAgent.OPERA,
  198. CAN_LISTIFY_BR: !goog.userAgent.IE && !goog.userAgent.OPERA,
  199. // See bug 1286408. When somewhere inside your selection there is an element
  200. // with a style attribute that sets the font size, if you change the font
  201. // size, the browser creates a font tag, but the font size in the style attr
  202. // overrides the font tag. Only webkit removes that font size from the style
  203. // attr.
  204. DOESNT_OVERRIDE_FONT_SIZE_IN_STYLE_ATTR:
  205. !goog.userAgent.WEBKIT && !goog.userAgent.EDGE,
  206. // Implements this spec about dragging files from the filesystem to the
  207. // browser: http://www.whatwg/org/specs/web-apps/current-work/#dnd
  208. SUPPORTS_HTML5_FILE_DRAGGING: (goog.userAgent.product.CHROME &&
  209. goog.userAgent.product.isVersion('4')) ||
  210. (goog.userAgent.product.SAFARI &&
  211. goog.userAgent.isVersionOrHigher('533')) ||
  212. (goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('2.0')) ||
  213. (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('10')) ||
  214. // TODO(user): Remove when b/27923889 is fixed.
  215. (goog.userAgent.OPERA &&
  216. goog.labs.userAgent.browser.isVersionOrHigher('15')) ||
  217. goog.userAgent.EDGE,
  218. // Version of Opera that supports the opera-defaultBlock execCommand to change
  219. // the default block inserted when [return] is pressed. Note that this only is
  220. // used if the caret is not already in a block that can be repeated.
  221. // TODO(user): Link to public documentation of this feature if Opera puts
  222. // something up about it.
  223. SUPPORTS_OPERA_DEFAULTBLOCK_COMMAND:
  224. goog.userAgent.OPERA && goog.userAgent.isVersionOrHigher('11.10'),
  225. SUPPORTS_FILE_PASTING:
  226. goog.userAgent.product.CHROME && goog.userAgent.product.isVersion('12')
  227. };