mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-20 22:16:52 +00:00
feat: $tw.utils.serializeAttribute
This commit is contained in:
parent
383e1b68b5
commit
63613ceec0
@ -385,4 +385,35 @@ exports.parseAttribute = function(source,pos) {
|
||||
return node;
|
||||
};
|
||||
|
||||
/*
|
||||
Given a parsed attribute node, serialize it back into its source string representation.
|
||||
*/
|
||||
exports.serializeAttribute = function(node) {
|
||||
if(!node || typeof node !== 'object' || !node.name || !node.type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var attributeString = node.name;
|
||||
|
||||
if(node.type === 'string') {
|
||||
if(node.value === 'true') {
|
||||
return attributeString;
|
||||
}
|
||||
attributeString += '="' + node.value + '"';
|
||||
} else if(node.type === 'filtered') {
|
||||
attributeString += '={{{' + node.filter + '}}}';
|
||||
} else if(node.type === 'indirect') {
|
||||
attributeString += '={{' + node.textReference + '}}';
|
||||
} else if(node.type === 'substituted') {
|
||||
attributeString += '=`' + node.rawValue + '`';
|
||||
} else if(node.type === 'macro') {
|
||||
// Assuming macro serialization is complex and handled elsewhere
|
||||
attributeString += '=' + node.value.serialize();
|
||||
} else {
|
||||
return null; // Unsupported type
|
||||
}
|
||||
|
||||
return attributeString;
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -173,6 +173,37 @@ describe("HTML tag new parser tests", function() {
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
describe('serializeAttribute', function () {
|
||||
it('should serialize string attributes', function () {
|
||||
expect($tw.utils.serializeAttribute({ type: 'string', name: 'p', value: 'blah' })).toBe('p="blah"');
|
||||
expect($tw.utils.serializeAttribute({ type: 'string', name: 'p', value: 'true' })).toBe('p');
|
||||
});
|
||||
|
||||
it('should serialize filtered attributes', function () {
|
||||
expect($tw.utils.serializeAttribute({ type: 'filtered', name: 'p', filter: 'blah' })).toBe('p={{{blah}}}');
|
||||
});
|
||||
|
||||
it('should serialize indirect attributes', function () {
|
||||
expect($tw.utils.serializeAttribute({ type: 'indirect', name: 'p', textReference: 'blah' })).toBe('p={{blah}}');
|
||||
});
|
||||
|
||||
it('should serialize substituted attributes', function () {
|
||||
expect($tw.utils.serializeAttribute({ type: 'substituted', name: 'p', rawValue: 'blah' })).toBe('p=`blah`');
|
||||
});
|
||||
|
||||
it('should return null for unsupported types', function () {
|
||||
expect($tw.utils.serializeAttribute({ type: 'unknown', name: 'p', value: 'blah' })).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for invalid input', function () {
|
||||
expect($tw.utils.serializeAttribute(null)).toBeNull();
|
||||
expect($tw.utils.serializeAttribute({})).toBeNull();
|
||||
expect($tw.utils.serializeAttribute({ type: 'string' })).toBeNull();
|
||||
expect($tw.utils.serializeAttribute({ name: 'p' })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse HTML tags", function() {
|
||||
expect(parser.parseTag("<mytag>",1)).toEqual(
|
||||
null
|
||||
|
@ -13,7 +13,7 @@ The main module of the Jasmine test plugin for TiddlyWiki5
|
||||
"use strict";
|
||||
|
||||
// DEBUG: only run my tests for development, remove before PR merge
|
||||
var TEST_TIDDLER_FILTER = "[[test-wikitext-serialize.js]]";
|
||||
var TEST_TIDDLER_FILTER = "[[test-wikitext-serialize.js]] [[test-html-parser.js]]";
|
||||
var TESTS_DONE = false;
|
||||
|
||||
exports.testsWereRun = function() {
|
||||
|
Loading…
Reference in New Issue
Block a user