forked from xwiki/xwiki-platform
/
ToggleInlineStyleExecutable.java
258 lines (233 loc) · 9.19 KB
/
ToggleInlineStyleExecutable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.gwt.user.client.ui.rta.cmd.internal;
import java.util.List;
import org.xwiki.gwt.dom.client.Element;
import org.xwiki.gwt.dom.client.Property;
import org.xwiki.gwt.dom.client.Range;
import org.xwiki.gwt.dom.client.Selection;
import org.xwiki.gwt.dom.client.Text;
import org.xwiki.gwt.dom.client.TextFragment;
import org.xwiki.gwt.user.client.ui.rta.RichTextArea;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.dom.client.Node;
/**
* Toggles a style property on the current selection.
*
* @version $Id$
*/
public class ToggleInlineStyleExecutable extends InlineStyleExecutable
{
/**
* The value of the style property when the style is applied.
*/
private final String value;
/**
* The tag used to toggle the style. It is a formatting tag that can be used as a shorthand in place of the style
* property.
*/
private final String tagName;
/**
* Flag indicating if this executable has been executed on the current selection. This flag determines if the style
* is toggled on or off.
*/
private boolean executed;
/**
* Creates a new executable that toggles the given style property on the current selection using the specified tag
* name.
*
* @param rta the execution target
* @param property the style property used when detecting style
* @param value the value of the style property when the style is applied
* @param tagName the tag used to toggle the style
*/
public ToggleInlineStyleExecutable(RichTextArea rta, Property property, String value, String tagName)
{
super(rta, property);
this.value = value;
this.tagName = tagName;
}
@Override
public boolean execute(String parameter)
{
executed = isExecuted();
return super.execute(parameter);
}
/**
* @return the value of the style property
*/
public String getValue()
{
return this.value;
}
@Override
protected TextFragment execute(Text text, int startIndex, int endIndex, String parameter)
{
return isExecuted() ? removeStyle(text, startIndex, endIndex) : addStyle(text, startIndex, endIndex);
}
/**
* Removes the underlying style from the given text node.
*
* @param text the target text node
* @param firstCharIndex the first character on which we remove the style
* @param lastCharIndex the last character on which we remove the style
* @return a text fragment indicating what has been unformatted
*/
protected TextFragment removeStyle(Text text, int firstCharIndex, int lastCharIndex)
{
// Make sure we remove the style only from the selected text.
text.crop(firstCharIndex, lastCharIndex);
// Look for the element ancestor that has the underlying style.
Node child = text;
Node parent = child.getParentNode();
while (parent != null && matchesStyle(parent) && domUtils.isInline(parent)) {
domUtils.isolate(child);
child = child.getParentNode();
parent = child.getParentNode();
}
if (tagName.equalsIgnoreCase(child.getNodeName())) {
// The style is enforced by a formatting element. We have to remove or rename it.
Element element = (Element) child;
if (element.hasAttributes()) {
// We must keep the attributes. Let's rename the element.
Element replacement = element.getOwnerDocument().createSpanElement().cast();
JsArrayString attributes = element.getAttributeNames();
for (int i = 0; i < attributes.length(); i++) {
replacement.setAttribute(attributes.get(i), element.getAttribute(attributes.get(i)));
}
replacement.appendChild(element.extractContents());
element.getParentNode().replaceChild(replacement, element);
} else {
// We remove the element but keep its child nodes.
element.unwrap();
}
} else {
if (child.getNodeType() != Node.ELEMENT_NODE) {
// Wrap the child with a span element.
Node wrapper = child.getOwnerDocument().createSpanElement();
child.getParentNode().replaceChild(wrapper, child);
wrapper.appendChild(child);
child = wrapper;
}
// The style is enforced using CSS. Let's reset the style property to its default value.
((Element) child).getStyle().setProperty(getProperty().getJSName(), getProperty().getDefaultValue());
}
return new TextFragment(text, 0, text.getLength());
}
/**
* Adds the underlying style to the given text node.
*
* @param text the target text node
* @param firstCharIndex the first character on which we apply the style
* @param lastCharIndex the last character on which we apply the style
* @return a text fragment indicating what has been formatted
*/
protected TextFragment addStyle(Text text, int firstCharIndex, int lastCharIndex)
{
if (matchesStyle(text)) {
// Already styled. Skip.
return new TextFragment(text, firstCharIndex, lastCharIndex);
}
// Make sure we apply the style only to the selected text.
text.crop(firstCharIndex, lastCharIndex);
Element element = (Element) text.getOwnerDocument().createElement(tagName);
text.getParentNode().replaceChild(element, text);
element.appendChild(text);
return new TextFragment(text, 0, text.getLength());
}
@Override
public boolean isExecuted()
{
Selection selection = rta.getDocument().getSelection();
for (int i = 0; i < selection.getRangeCount(); i++) {
if (!isExecuted(selection.getRangeAt(i))) {
return false;
}
}
return selection.getRangeCount() > 0;
}
/**
* @param range the range to be inspected
* @return {@code true} if this executable was executed on the given range
*/
protected boolean isExecuted(Range range)
{
if (range.isCollapsed()) {
return matchesStyle(range.getStartContainer());
} else {
List<Text> textNodes = getNonEmptyTextNodes(range);
for (int i = 0; i < textNodes.size(); i++) {
if (!matchesStyle(textNodes.get(i))) {
return false;
}
}
return textNodes.size() > 0;
}
}
/**
* @param inputNode a DOM node
* @return {@code true} if the given node matches the style associated with this executable, {@code false} otherwise
*/
protected boolean matchesStyle(Node inputNode)
{
Node node = inputNode;
if (node.getNodeType() != Node.ELEMENT_NODE) {
node = node.getParentNode();
}
if (node == null || node.getNodeType() != Node.ELEMENT_NODE) {
return false;
}
return matchesStyle(Element.as(node));
}
/**
* @param inputElement a DOM element
* @return {@code true} if the given element matches the style associated with this executable, {@code false}
* otherwise
*/
protected boolean matchesStyle(Element inputElement)
{
if (getProperty().isInheritable()) {
return matchesInheritedStyle(inputElement);
} else {
Node node = inputElement;
while (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
if (matchesInheritedStyle((Element) node)) {
return true;
}
node = node.getParentNode();
}
return false;
}
}
/**
* @param element a DOM element
* @return {@code true} if the given element matches the style associated with this executable, without testing the
* ancestors of the element
*/
protected boolean matchesInheritedStyle(Element element)
{
String computedValue = element.getComputedStyleProperty(getProperty().getJSName());
if (getProperty().isMultipleValue()) {
return computedValue != null && computedValue.toLowerCase().contains(value);
} else {
return value.equalsIgnoreCase(computedValue);
}
}
}