Skip to content

Commit

Permalink
Changed auto-typography mode to preserve entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
gWestenberger committed Sep 26, 2024
1 parent fbab7fd commit c040c7e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void onValueChange(ValueChangeEvent<String> event) {
// we set the text area content to the new value, which causes a new change event. We prevent an infinite recursion
// using the m_rewriting member.
if ((m_typograf != null) && !m_rewriting) {
String newContent = m_typograf.execute(event.getValue());
String newContent = CmsTypografUtil.transform(m_typograf, event.getValue());
if (!newContent.equals(event.getValue())) {
m_rewriting = true;
int savedPosition = m_textarea.getPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> hand
if (m_typograf != null) {
try {
String oldValue = inputElem.value;
String newValue = m_typograf.execute(oldValue);
String newValue = CmsTypografUtil.transform(m_typograf, oldValue);
if (!Objects.equals(oldValue, newValue)) {
int pos = inputElem.selectionStart;
inputElem.value = newValue;
Expand Down
56 changes: 56 additions & 0 deletions src-gwt/org/opencms/acacia/client/widgets/CmsTypografUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import org.opencms.gwt.shared.CmsGwtLog;

import com.google.gwt.regexp.shared.RegExp;

import elemental2.core.JsArray;
import elemental2.core.JsObject;
import jsinterop.annotations.JsConstructor;
Expand All @@ -53,17 +55,32 @@ public Typograf(JsPropertyMap<Object> options) { /* must be empty */ }

public static native boolean hasLocale(String locale);

public native void addSafeTag(String beginRegex, String endRegex);

public native void disableRule(String rule);

public native void enableRule(String rule);

public native String execute(String input);
}

/** Pattern to match entities. */
public static final RegExp ENTITY_PATTERN = RegExp.compile("(&[#a-z0-9]+;)", "gi");

/** Pattern to match safe tags. */
public static final RegExp SAFE_TAG_PATTERN = RegExp.compile("%OC(BEGIN|END)ENTITY%", "g");

/** Tag to insert before an entity. */
private static final String OC_BEGIN_ENTITY = "%OCBEGINENTITY%";

/** Tag to insert after an entity. */
private static final String OC_END_ENTITY = "%OCENDENTITY%";

/**
* Hidden default constructor.
*/
private CmsTypografUtil() {

// empty
}

Expand All @@ -84,11 +101,50 @@ public static Typograf createLiveInstance(String typografLocale) {
typograf = new Typograf(options);
typograf.disableRule("*");
typograf.enableRule("common/punctuation/quote");
typograf.addSafeTag(OC_BEGIN_ENTITY, OC_END_ENTITY);
} catch (Exception e) {
CmsGwtLog.log(e.getLocalizedMessage());
}
}
return typograf;
}

/**
* Wraps entities in safe tags.
*
* @param input the input string
* @return the string with wrapped entities
*/
public static String protectEntities(String input) {

return ENTITY_PATTERN.replace(input, OC_BEGIN_ENTITY + "$1" + OC_END_ENTITY);
}

/**
* Removes the safe tags around entities.
*
* @param input the input string
* @return the transformed string
*/
public static String restoreEntities(String input) {

return SAFE_TAG_PATTERN.replace(input, "");

}

/**
* Transforms things with typograf, preserving all entities.
*
* @param typograf the typograf instance
* @param value the value to transform
* @return the transformed value
*/
public static String transform(Typograf typograf, String value) {

value = protectEntities(value);
value = typograf.execute(value);
value = restoreEntities(value);
return value;
}

}

0 comments on commit c040c7e

Please sign in to comment.