Liberated UnorderedListFormatter
These have been refacored from the original: rather than abusing what is now the PatternFormatter, it is now its own class. * src/validate/formatter/UnorderedListFormatter.js: Added. * test/validate/formatter/UnorderedListFormatterTest.js: Added.master
parent
ab6be11bb6
commit
391a819536
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
* Formatter for unordered lists
|
||||
*
|
||||
* Copyright (C) 2016 LoVullo Associates, Inc.
|
||||
*
|
||||
* This file is part of liza.
|
||||
*
|
||||
* liza is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
var Class = require( 'easejs' ).Class,
|
||||
ValidatorFormatter = require( '../ValidatorFormatter' );
|
||||
|
||||
|
||||
/**
|
||||
* Formats delimited items as an HTML unordered list, storing as a
|
||||
* delimited string.
|
||||
*/
|
||||
module.exports = Class( 'UnorderedListFormatter' )
|
||||
.implement( ValidatorFormatter )
|
||||
.extend(
|
||||
{
|
||||
/**
|
||||
* Format the given data as a delimited string
|
||||
*
|
||||
* HTML list elements will be stripped and items normalized
|
||||
* (whitespace and empty items removed).
|
||||
*
|
||||
* @param {string} data data to parse
|
||||
*
|
||||
* @return {string} formatted string
|
||||
*/
|
||||
'public parse': function( data )
|
||||
{
|
||||
// strip HTMl elements before processing (closing li tag
|
||||
// is translated into a semicolon)
|
||||
return this.getParts(
|
||||
data
|
||||
.replace( /<\/li>/g, ';' )
|
||||
.replace( /\s*<.*?>\s*/g, '' )
|
||||
).join( '; ' );
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve data that may require formatting for display
|
||||
*
|
||||
* Return formatting is optional. No formatting will be done if no pattern
|
||||
* was given when the instance was constructed.
|
||||
*
|
||||
* To ensure consistency and correctness, *any data returned by this method
|
||||
* must be reversible* --- that is, parse( retrieve( data ) ) should not
|
||||
* throw an exception.
|
||||
*
|
||||
* @param {string} data data to format for display
|
||||
*
|
||||
* @return {string} data formatted for display
|
||||
*/
|
||||
'public retrieve': function( data )
|
||||
{
|
||||
var parts = this.getParts( data ),
|
||||
items = '';
|
||||
|
||||
for ( var i = 0; i < parts.length; i++ )
|
||||
{
|
||||
var part = parts[ i ];
|
||||
|
||||
items += ( part === '' )
|
||||
? ''
|
||||
: '<li>' + part + '</li>';
|
||||
}
|
||||
|
||||
return ( items === '' )
|
||||
? ''
|
||||
: "<ul>" + items + "</ul>";
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Get trimmed, non-empty parts of semicolon-delimited string
|
||||
*
|
||||
* @param {string} input semicolon-delimited string
|
||||
*
|
||||
* @return {Array} non-empty trimmed parts
|
||||
*/
|
||||
'virtual protected getParts': function( data )
|
||||
{
|
||||
var parts = data.split( /(?:\s*;+\s*)+/ ),
|
||||
ret = [];
|
||||
|
||||
for ( var i = 0; i < parts.length; i++ )
|
||||
{
|
||||
if ( parts[ i ] !== '' )
|
||||
{
|
||||
ret.push( parts[ i ] );
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
} );
|
|
@ -0,0 +1,141 @@
|
|||
/**
|
||||
* Tests unordered list formatter
|
||||
*
|
||||
* Copyright (C) 2016 LoVullo Associates, Inc.
|
||||
*
|
||||
* This file is part of liza.
|
||||
*
|
||||
* liza is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
var liza = require( '../../../' ),
|
||||
Sut = liza.validate.formatter.UnorderedListFormatter,
|
||||
testValidate = require( './common' ).testValidate;
|
||||
|
||||
|
||||
describe( 'UnorderedListFormatter', function()
|
||||
{
|
||||
testValidate( Sut(), {
|
||||
"": [ "", "" ],
|
||||
"no semi": [ "no semi", "<ul><li>no semi</li></ul>" ],
|
||||
|
||||
"semi; colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
"semi;colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
"semi; colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
"semi ; colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
"semi ;colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
"semi;;;colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
"semi ; ;; colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
";semi;colon": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
";semi": [
|
||||
"semi",
|
||||
"<ul><li>semi</li></ul>"
|
||||
],
|
||||
" ; semi": [
|
||||
"semi",
|
||||
"<ul><li>semi</li></ul>"
|
||||
],
|
||||
"semi;colon;": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
";semi;": [
|
||||
"semi",
|
||||
"<ul><li>semi</li></ul>"
|
||||
],
|
||||
"semi;": [
|
||||
"semi",
|
||||
"<ul><li>semi</li></ul>"
|
||||
],
|
||||
"semi ; ": [
|
||||
"semi",
|
||||
"<ul><li>semi</li></ul>"
|
||||
],
|
||||
";": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
" ; ": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
|
||||
// single
|
||||
"<ul><li>no semi</li></ul>": [
|
||||
"no semi",
|
||||
"<ul><li>no semi</li></ul>"
|
||||
],
|
||||
// multi
|
||||
"<ul><li>semi</li><li>colon</li></ul>": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
// ensure that all li elements are replaced globally
|
||||
"<ul><li>foo</li><li>bar</li><li>baz</li></ul>": [
|
||||
"foo; bar; baz",
|
||||
"<ul><li>foo</li><li>bar</li><li>baz</li></ul>"
|
||||
],
|
||||
// extra whitespace
|
||||
" <ul><li>semi </li> <li>colon </li></ul> ": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
// malformed
|
||||
" <li>semi </li> <li>colon </li></ul> ": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
// malformed
|
||||
" <li>no semi </li> <li>": [
|
||||
"no semi",
|
||||
"<ul><li>no semi</li></ul>"
|
||||
],
|
||||
// empty node
|
||||
"<ul><li>no semi</li><li></li></ul>": [
|
||||
"no semi",
|
||||
"<ul><li>no semi</li></ul>"
|
||||
],
|
||||
|
||||
// implementation consequence; no way to escape a semicolon
|
||||
"<ul><li>semi;colon</li></ul>": [
|
||||
"semi; colon",
|
||||
"<ul><li>semi</li><li>colon</li></ul>"
|
||||
],
|
||||
} );
|
||||
} );
|
Loading…
Reference in New Issue