Skip to content

Commit

Permalink
Merge pull request #137 from ineedsubstance/bug/530
Browse files Browse the repository at this point in the history
Required to save label bug
  • Loading branch information
scottnath authored Nov 15, 2016
2 parents 3818c9c + 8dc77ea commit d01302d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
24 changes: 3 additions & 21 deletions lib/form/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ const addRequired = (html, input, index) => {
const level = `required--${required}`;

// pre-regex strings
const stringClass = 'class=["\'][\\w\\W\\s]+?["\']';
const stringFor = `for=["']\\s*${inp.id}\\s*["']`;

// regex to get the label for THIS input
Expand All @@ -107,35 +106,18 @@ const addRequired = (html, input, index) => {
return;
}

// regex to get first class attributes from the captured label
const regexClass = new RegExp(`.*(${stringClass})`);

// regex to get classes out of `class`
const regexClasses = new RegExp('class=["\']([\\w\\s\\W]+?)["\']');

// regex to search for `for`
const reFor = new RegExp(`(${stringFor})`);

// first, check if `class` exists in label
if (Array.isArray(label[1].match(regexClass))) {
const cls = label[1].match(regexClass);
const classAttr = cls[1].match(regexClasses);
const classes = classAttr[1].split(/\s+/).concat([level]);
const newClass = `class="${classes.join(' ')}"`;
render = render.replace(label[0], label[0].replace(classAttr[0], newClass));
}
else {
// else, we add class after the `for` attribute
render = render.replace(reFor, `for="${inp.id}" class="${level}"`);
}
render = render.replace(reFor, `for="${inp.id}"`);

// add required to input
const reName = new RegExp(`name=['"]\\s*${inp.name}\\s*['"]`);
render = render.replace(reName, `name="${inp.name}" aria-required="true" required`);

// Add required text to label
const reLabel = new RegExp('</label>');
render = render.replace(reLabel, `<mark class="${level}">required to ${required}</mark></label>`);
render = render.replace(reLabel, `\u00a0<mark class="${level}">required to ${required}</mark>`);
render += '</label>';
}
});

Expand Down
4 changes: 2 additions & 2 deletions tests/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ test('Add Required and Repeatable - Pass', t => {

test('Add Object Required - Pass', t => {
const param = {
html: '"<fieldset id="community-links" class="form--fieldset required--publish"><legend class="form--legend">Object</legend><p class="form--description">I am the Foo Object text description</p><div class="form--field"><label for="91f79620-ba21-4a4a-a4c7-02f456129b0f--1" class="required--publish">Add Your Text <mark class="required--publish">required to publish</mark></label><input type="text" id="91f79620-ba21-4a4a-a4c7-02f456129b0f--1" name="name--text" placeholder="Text goes here"></div><div class="form--field"><label for="91f79620-ba21-4a4a-a4c7-02f456129b0f--2">Add Your URL <mark class="required--publish">required to publish</mark></label><input type="url" id="91f79620-ba21-4a4a-a4c7-02f456129b0f--2" name="name--url" placeholder="http://"></div></fieldset>"',
html: '"<fieldset id="community-links" class="form--fieldset"><legend class="form--legend">Object</legend><p class="form--description">I am the Foo Object text description</p><div class="form--field"><label for="91f79620-ba21-4a4a-a4c7-02f456129b0f--1">Add Your Text <mark class="required--publish">required to publish</mark></label><input type="text" id="91f79620-ba21-4a4a-a4c7-02f456129b0f--1" name="name--text" placeholder="Text goes here"></div><div class="form--field"><label for="91f79620-ba21-4a4a-a4c7-02f456129b0f--2">Add Your URL <mark class="required--publish">required to publish</mark></label><input type="url" id="91f79620-ba21-4a4a-a4c7-02f456129b0f--2" name="name--url" placeholder="http://"></div></fieldset>"',
input: {
name: 'Object',
description: 'I am the Foo Object text description',
html: '<fieldset id=\'community-links\' class=\'form--fieldset required--{{text.required}}\'><legend class=\'form--legend\'>{{input.name}}</legend><p class=\'form--description\'>{{input.description}}</p><div class=\'form--field\'><label for=\'{{text.id}}\' class=\'required--{{text.required}}\'>{{text.label}} <mark class=\'required--{{text.required}}\'>required to {{text.required}}</mark></label><input type=\'{{text.type}}\' id=\'{{text.id}}\' name=\'{{text.name}}\' placeholder=\'{{text.placeholder1}}\'></div><div class=\'form--field\'><label for=\'{{url.id}}\'>{{url.label}} <mark class=\'required--{{url.required}}\'>required to {{url.required}}</mark></label><input type=\'{{url.type}}\' id=\'{{url.id}}\' name=\'{{url.name}}\' placeholder=\'{{url.placeholder}}\'></div></fieldset>',
html: '<fieldset id=\'community-links\' class=\'form--fieldset\'><legend class=\'form--legend\'>{{input.name}}</legend><p class=\'form--description\'>{{input.description}}</p><div class=\'form--field\'><label for=\'{{text.id}}\'>{{text.label}} <mark class=\'required--{{text.required}}\'>required to {{text.required}}</mark></label><input type=\'{{text.type}}\' id=\'{{text.id}}\' name=\'{{text.name}}\' placeholder=\'{{text.placeholder1}}\'></div><div class=\'form--field\'><label for=\'{{url.id}}\'>{{url.label}} <mark class=\'required--{{url.required}}\'>required to {{url.required}}</mark></label><input type=\'{{url.type}}\' id=\'{{url.id}}\' name=\'{{url.name}}\' placeholder=\'{{url.placeholder}}\'></div></fieldset>',
id: 'object-required',
inputs: {
text: {
Expand Down

0 comments on commit d01302d

Please sign in to comment.