Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
format_text.inc
1 <?php
18 global $ROOT_PATH;
19 include_once($ROOT_PATH.'wysiwyg_plugin.inc');
20 
33 class Format_Text extends WYSIWYG_Plugin
34 {
35 
36 
44  function format_text()
45  {
46  $this->_add_button('bold','Bold','Bold','false','14');
47  $this->_add_button('italic','Italic','Italic','false','15');
48  $this->_add_button('underline','Underline','Underline','false','16');
49  $this->_add_button('strikethrough','StrikeThrough','StrikeThrough','false','17');
50  }
51 
52 
59  function paint_generic()
60  {
61  ?>
62  <script type="text/javascript" language="Javascript">
63 
64  // Replaces the 'fromTag' tag to 'toTag' in the given selection
65  HTMLArea.prototype._convertTags = function(fromTag, toTag) {
66 
67  if (HTMLArea.is_ie) {
68  // We dont need to convert tags for IE, as IE will have the required tags already
69  return;
70  }
71 
72  if (fromTag == null || toTag == null) {
73  return;
74  }
75 
76  fromTag = fromTag.toLowerCase();
77  toTag = toTag.toLowerCase();
78 
79  // Get the required tags within the selection
80  var sel = this._getSelection();
81  var parent = this.parentBlock(this.getParentElement());
82  var elements = parent.getElementsByTagName(fromTag);
83 
84  // Restrict the element list to within the selection
85  var elementsArray = [];
86  for(var i = 0; i<elements.length; i++) {
87  if (sel.containsNode(elements[i], true)) {
88  elementsArray.push(elements[i]);
89  }
90  }
91 
92  // Get the selection anchor and focus nodes
93  var anchorNode = sel.anchorNode;
94  var anchorOffset = sel.anchorOffset;
95  var focusNode = sel.focusNode;
96  var focusOffset = sel.focusOffset;
97 
98  // Carry out the replacement
99  for(var j = 0; j< elementsArray.length; j++) {
100  var replacementEl = document.createElement(toTag);
101  replacementEl.innerHTML = elementsArray[j].innerHTML;
102  elementsArray[j].parentNode.replaceChild(replacementEl, elementsArray[j]);
103 
104  // Update the anchor and focus nodes
105  var oldAnchorTextNode = elementsArray[j].firstChild;
106  var newAnchorTextNode = replacementEl.firstChild;
107  while(oldAnchorTextNode.nodeName != '#text' && oldAnchorTextNode.hasChildNodes()) {
108  newAnchorTextNode = newAnchorTextNode.firstChild;
109  oldAnchorTextNode = oldAnchorTextNode.firstChild;
110  }
111  var oldFocusTextNode = elementsArray[j].lastChild;
112  var newFocusTextNode = replacementEl.lastChild;
113  while(oldFocusTextNode.nodeName != '#text' && oldFocusTextNode.hasChildNodes()) {
114  newFocusTextNode = newFocusTextNode.lastChild;
115  oldFocusTextNode = oldFocusTextNode.lastChild;
116  }
117  if (oldAnchorTextNode === anchorNode) {
118  anchorNode = newAnchorTextNode;
119  }
120  if (oldFocusTextNode === focusNode) {
121  focusNode = newFocusTextNode;
122  }
123  }
124 
125  // Update the selection range
126  if (anchorNode != null && focusNode != null) {
127  var range = this._createRange();
128  range.setStart(anchorNode, anchorOffset);
129  range.setEnd(focusNode, focusOffset);
130  sel.removeAllRanges();
131  sel.addRange(range);
132  }
133  }
134  </script>
135 
136  <?php
137 
138  }//end paint_generic()
139 
140 
150  {
151  ?>
152  case 'b': // KEY bold
153  (!HTMLArea.is_ie) && (cmd = "bold");
154  break;
155  case 'i': // KEY italic
156  (!HTMLArea.is_ie) && (cmd = "italic");
157  break;
158  case 'u': // KEY underline
159  (!HTMLArea.is_ie) && (cmd = "underline");
160  break;
161  case 's': // KEY strikethrough
162  cmd = "strikethrough";
163  break;
164  <?php
165 
166  }//end print_plugin_shortcuts()
167 
168 
176  {
177  ?>
178  case "bold":
179  this._doc.execCommand(cmdID, UI, param);
180  // Convert the added 'b' tags to 'strong'
181  if (this._doc.queryCommandState("bold")) {
182  this._convertTags('b', 'strong');
183  }
184  break;
185  case "italic":
186  this._doc.execCommand(cmdID, UI, param);
187  // Convert the added 'i' tags to 'em'
188  if (this._doc.queryCommandState("italic")) {
189  this._convertTags('i', 'em');
190  }
191  break;
192  case "underline":
193  this._doc.execCommand(cmdID, UI, param);
194  break;
195  case "strikethrough":
196  this._doc.execCommand(cmdID, UI, param);
197  break;
198  <?php
199 
200  }//end print_plugin_exec_command()
201 
202 
203 }//end class
204 
205 ?>