Skip to content

Commit

Permalink
clean validation text
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPilot committed Aug 20, 2015
1 parent 037064c commit b12a3d4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
49 changes: 36 additions & 13 deletions publishing-rules/ban-tk.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
var label, description,
tkText = 'TK',
searchTexts = ['TK', 'tktk'],
articleFields = ['primaryHeadline', 'teaser'],
paragraphField = 'text',
_ = require('lodash'),
dom = require('../services/dom'),
references = require('../services/references'),
refProp = references.referenceProperty,
cutStart = 20,
cutEnd = 20;

label = 'Ban TKs';
description = 'TKs are not allowed';
description = 'Any TK or tktk in the article\'s primary headline, teaser or in any paragraph cannot be published.';

/**
* Return first value of list to be found in str
* @param {string} str
* @param {[string]} list
* @returns {string|null}
*/
function findFirstText(str, list) {
var i;

for (i = 0; i < list.length; i++) {
if (str.indexOf(list[i]) > -1) {
return list[i];
}
}
return null;
}

/**
* @param {string} componentName
Expand All @@ -29,20 +47,22 @@ function getLabel(componentName, label, ref) {
/**
* @param {string} value
* @param {object} data
* @param {string} tkText
* @returns {string}
*/
function getPreview(value, data) {
function getPreview(value, data, tkText) {
var index;

if (_.isString(value)) {
index = value.indexOf(tkText);

if (index > cutStart) {
value = '...' + value.substr(index - cutStart);
index = index - (index - cutStart) + 3;
}

if (value.length > index + cutEnd) {
value = value.substr(0, index + cutEnd) + '...';
value = value.substr(0, index + cutEnd + tkText.length) + '...';
}

return value;
Expand All @@ -56,19 +76,20 @@ function getPreview(value, data) {
* @param {object} component
* @param {string} fieldName
* @param {[object]} errors
* @param {string} tkText
*/
function addError(component, fieldName, errors) {
function addError(component, fieldName, errors, tkText) {
var ref = component[refProp],
componentName = ref && references.getComponentNameFromReference(ref),
data = component && component[fieldName],
schema = data._schema,
label = schema && schema._label,
value = data && data.value,
value = data && data.value && dom.removeTags(data.value),
error = {
ref: ref,
fieldName: fieldName,
label: getLabel(componentName, label, ref),
preview: getPreview(value, data)
preview: getPreview(value, data, tkText)
};

errors.push(error);
Expand All @@ -86,23 +107,25 @@ function validate(state) {

_.each(groups.paragraph, function (component) {
var field = component[paragraphField],
value = field && field.value;
value = field && field.value && dom.removeTags(field.value),
tkText = value && findFirstText(value, searchTexts);

if (_.isString(value) && value.indexOf(tkText) > -1) {
if (tkText) {

addError(component, paragraphField, errors);
addError(component, paragraphField, errors, tkText);

}
});

_.each(groups.article, function (component) {
_.each(articleFields, function (fieldName) {
var field = component[fieldName],
value = field && field.value;
value = field && field.value && dom.removeTags(field.value),
tkText = value && findFirstText(value, searchTexts);

if (_.isString(value) && value.indexOf(tkText) > -1) {
if (tkText) {

addError(component, fieldName, errors);
addError(component, fieldName, errors, tkText);

}
});
Expand Down
6 changes: 3 additions & 3 deletions publishing-rules/ban-tk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ describe(dirname, function () {
ref: 'k/components/paragraph/instances/l',
fieldName: 'text',
label: 'Paragraph',
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfgh...'
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfghjkl;...'
}]);
});

it('returns errors for articles', function () {
var justText = 'TK',
shortText = 'abcTKdef',
longText = 'qwertyuiopasdfghjkl;zTKqwertyuiopasdfghjkl;z',
longText = 'qwer<em>tyuiopasdfghjkl;zTKqwertyuiop</em>asdfghjkl;z',
state = {
refs: {
'f/components/article': {primaryHeadline: {value: shortText}},
Expand Down Expand Up @@ -127,7 +127,7 @@ describe(dirname, function () {
ref: 'm/components/article/instances/n',
fieldName: 'primaryHeadline',
label: 'Article',
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfgh...'
preview: '...wertyuiopasdfghjkl;zTKqwertyuiopasdfghjkl;...'
}, {
ref: 'o/components/article/instances/p',
fieldName: 'primaryHeadline',
Expand Down
18 changes: 18 additions & 0 deletions services/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,23 @@ module.exports = {
observer.observe(el.parentNode, {childList: true});
},

/**
* Remove HTML tags from string
* @param {string} str
* @returns {string}
*/
removeTags: function (str) {
var start = str.indexOf('<'),
end = start > -1 && str.indexOf('>', start) || start;

while (start > -1 && end > -1) {
str = str.substring(0, start) + str.substring(end + 1);
start = str.indexOf('<');
end = start > -1 && str.indexOf('>') || start;
}

return str;
},

create: domify // create elements from strings!
};
8 changes: 8 additions & 0 deletions services/dom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,12 @@ describe('dom service', function () {
expect(mockMutationObserver.disconnect.callCount).to.equal(1);
});
});

describe('removeTags', function () {
var fn = dom[this.title];

it('removes tags', function () {
expect(fn('a<>c<d>e<f>g</h>')).to.equal('aceg');
});
});
});

0 comments on commit b12a3d4

Please sign in to comment.