text-node.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. // Make this available to both AMD and CJS environments
  7. define(function (require, exports, module) {
  8. // ReactJS
  9. const React = require("devtools/client/shared/vendor/react");
  10. // Reps
  11. const { isGrip, cropString } = require("./rep-utils");
  12. // Shortcuts
  13. const DOM = React.DOM;
  14. /**
  15. * Renders DOM #text node.
  16. */
  17. let TextNode = React.createClass({
  18. displayName: "TextNode",
  19. propTypes: {
  20. object: React.PropTypes.object.isRequired,
  21. mode: React.PropTypes.string,
  22. },
  23. getTextContent: function (grip) {
  24. return cropString(grip.preview.textContent);
  25. },
  26. getTitle: function (grip) {
  27. if (this.props.objectLink) {
  28. return this.props.objectLink({
  29. object: grip
  30. }, "#text ");
  31. }
  32. return "";
  33. },
  34. render: function () {
  35. let grip = this.props.object;
  36. let mode = this.props.mode || "short";
  37. if (mode == "short" || mode == "tiny") {
  38. return (
  39. DOM.span({className: "objectBox objectBox-textNode"},
  40. this.getTitle(grip),
  41. DOM.span({className: "nodeValue"},
  42. "\"" + this.getTextContent(grip) + "\""
  43. )
  44. )
  45. );
  46. }
  47. let objectLink = this.props.objectLink || DOM.span;
  48. return (
  49. DOM.span({className: "objectBox objectBox-textNode"},
  50. this.getTitle(grip),
  51. objectLink({
  52. object: grip
  53. }, "<"),
  54. DOM.span({className: "nodeTag"}, "TextNode"),
  55. " textContent=\"",
  56. DOM.span({className: "nodeValue"},
  57. this.getTextContent(grip)
  58. ),
  59. "\"",
  60. objectLink({
  61. object: grip
  62. }, ">;")
  63. )
  64. );
  65. },
  66. });
  67. // Registration
  68. function supportsObject(grip, type) {
  69. if (!isGrip(grip)) {
  70. return false;
  71. }
  72. return (grip.preview && grip.class == "Text");
  73. }
  74. // Exports from this module
  75. exports.TextNode = {
  76. rep: TextNode,
  77. supportsObject: supportsObject
  78. };
  79. });