How Can I Replace Text Characters in RTC via Attribute Customization Javascript?
I am writing an attribute customization script in RTC to evaluate the contents of a Work Item's description field. I want to be able to do a compare on the text and disregard any HTML formatting that may be present. To do this, I am trying to perform a javascript replace function on the text string that I am evaluating. However, the replace function isn't working. It isn't causing an error, but it's not having any effect either. Example: Input:text = "<b>Solutions Considered:</b><br/>"; Replace command: text = text.replace(/<b>|<\/b>|<i>|<\/i>|<br\/>/, ""); Expected result: text = "Solutions Considered:" Actual result: text = "<b>Solutions Considered:</b><br/>"; I've tested this in a JavaScript editor and it produces the expected results there. Does RTC not support the JavaScript replace function? Do I need to specify a dojo.require command in order to have the replace work as expected? P.S. Jazz.net, you guys really need to figure out how to allow me to display HTML tags without interpreting them as HTML. |
3 answers
did you try wrapping the 1st string in quotes?
also, do you know that the string has the html in it at the time you are doing the replace?
Comments
Nate Decker
commented Apr 08 '15, 7:53 a.m.
Do you mean the replace command? The first string in my post already is wrapped in quotes. Although technically, I'm not defining that string in my actual code. It's just what I'm getting from the description attribute of the work item. I could use quotes on the replace command, but then it wouldn't be a regex literal. I may have tried that previously, but I could look at it again. I have a console.log statement which shows the content to have HTML tags in it. Although another answer below suggests that maybe the HTML tags are being displayed as HTML but are really represented as encoded HTML (e.g., < ; b & gt;). I am dubious that the console.log command would render that kind of text as brackets though. I would think console.log would just handle stuff as strings. The console log dumps strings. I referred to the Java API (and the JavaScript API).
Nate Decker
commented Aug 11 '16, 3:00 p.m.
Ralph, I found this old post I had submitted while searching for a new (but related) question. The new question is actually relevant to what you seem to be referencing in this comment above. I want to manipulate an HTML attribute and preserve existing HTML tags. Can you point me to the other thread in this forum where that discussion was had?
Ralph Schoon
commented Aug 12 '16, 2:17 a.m.
| edited Aug 12 '16, 2:18 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
Nate, I don't know where that was, however I know how the Java API does it and it uses a special method to set an XML string which keeps the content as it is and another to set it from plain text in which case the string content is escaped. So the HTML show up as text and don't work. that seems to be the only method I am aware of that the attribute customization API supports.
|
Like Sam said, you'd better do some debugging to make sure the string being processed is what you believe to be. For example, the " < b > " may already be "escaped" to " & l t ; b & g t ; " when your script "sees" the string. Note the script may be evaluated on both the client and server side, and you have to debug it in both cases. Function console.log() should be a good starting point.
Comments
Nate Decker
commented Apr 08 '15, 7:49 a.m.
I did indeed have a console.log statement in there and the text had several <br/> instances in it. There was also at least one instance of <b>. I did a console.log output of the string before and after and there was no change. In my JavaScript test environment though, the replace command worked as expected. I suppose I could try doing a replace on & l t ; I hadn't tried that yet. I'm not terribly optimistic at this point though. If the data in the text shows up as html tag, then it is escaped. If it is a real link, or the tags show e.g. a newline, the text is not escaped.
sam detweiler
commented Apr 08 '15, 8:36 a.m.
Also, I would try some plain text replaces to verify that the javascript function works correctly in the extension
replace 'aa' with 'bb' in some text fields.
|
RTC v 6.0.2
I also encountered the same issue. I was auto-populating Description field based on value of an enumeration and was getting HTML value instead of text in Description field.
I made some modifications in the script which resolved it.
This answer is in continuation of my previous question.
Pasting the working script below.
dojo.provide("com.example.DescriptionValue");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
dojo.require("dojox.html.entities");
(function() {
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("com.example.DescriptionValue", null, {
getValue: function(attributeId, workItem, configuration) {
var attr = "com.ibm.team.workitem.attribute.business"; // ID of attribute Business. (Business is an enumeration)
var val_bus = workItem.getValue(attr);
var descrip = workItem.getValue(WorkItemAttributes.DESCRIPTION);
if(descrip === ""){
if (val_bus === "com.ibm.team.workitem.enumeration.business.literal.l4"){
descrip = "Description: \nWhere and when found: \nHow found: ";
return descrip;
}
else if (val_bus === "com.ibm.team.workitem.enumeration.business.literal.l6"){
descrip = "Description:\nObservation:\nSafety Impact:";
return descrip;
}
}
descrip = dojox.html.entities.decode(descrip); // This is removing all characters except <br/>
var br_def = "<br/>"; // Below lines are to check if multiple <br/> are present and remove all.
while(descrip.includes(br_def)){
var i = 0;
descrip = descrip.replace('<br/>','\n');
}
return descrip;
}
});
})();
|
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.
Comments
Please file an enhancement request, if you want this to happen.