Skip to content

Commit

Permalink
inline tags need get space control with siblings
Browse files Browse the repository at this point in the history
resolved #84
  • Loading branch information
shiren authored and seonim-ryu committed Feb 5, 2020
1 parent 5d0868f commit 85f874e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
8 changes: 3 additions & 5 deletions libs/to-mark/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ Renderer.prototype.getSpaceControlled = function(content, node) {
trail = '',
text;

if (node.previousSibling && isInlineNode(node.previousSibling)) {
if (node.previousSibling && (node.previousSibling.nodeType === Node.TEXT_NODE || isInlineNode(node.previousSibling))) {
text = node.previousSibling.innerHTML || node.previousSibling.nodeValue;

if (FIND_TRAIL_SPACE_RX.test(text) || FIND_LEAD_SPACE_RX.test(node.innerHTML || node.nodeValue)) {
lead = ' ';
}
}

if (node.nextSibling && isInlineNode(node.nextSibling)) {
if (node.nextSibling && (node.nextSibling.nodeType === Node.TEXT_NODE || isInlineNode(node.nextSibling))) {
text = node.nextSibling.innerHTML || node.nextSibling.nodeValue;
if (FIND_LEAD_SPACE_RX.test(text) || FIND_TRAIL_SPACE_RX.test(node.innerHTML || node.nodeValue)) {
trail = ' ';
Expand All @@ -130,11 +130,9 @@ Renderer.prototype.convert = function(node, subContent) {
if (converter) {
result = converter.call(this, node, subContent);
} else if (node) {
result = this._getInlineHtml(node, subContent);
result = this.getSpaceControlled(this._getInlineHtml(node, subContent), node);
}

//console.log(JSON.stringify(result), converter.fname);

return result || '';
};

Expand Down
27 changes: 7 additions & 20 deletions libs/to-mark/test/renderer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,24 @@ describe('renderer', function() {
expect(renderer.convert({tagName: 'H1'})).toEqual('');
});

it('_getInlineHtml() make inline html with subcontent', function() {
it('should make inline html with subcontent', function() {
var renderer = Renderer.factory();
runner = new DomRunner(toDom('<span class="myClass" id="myId" style="color:#ff0"><em>test</em></span>'));
runner.next();
expect(renderer._getInlineHtml(runner.getNode(), '**test**')).toEqual('<span class="myClass" id="myId" style="color:#ff0">**test**</span>');
expect(renderer.convert(runner.getNode(), '**test**')).toEqual('<span class="myClass" id="myId" style="color:#ff0">**test**</span>');

runner = new DomRunner(toDom('<span class="myClass" id="myId" style="color:#ff0"><span>test</span></span>'));
runner.next();
expect(renderer._getInlineHtml(runner.getNode(), '**test**')).toEqual('<span class="myClass" id="myId" style="color:#ff0">**test**</span>');
expect(renderer.convert(runner.getNode(), '**test**')).toEqual('<span class="myClass" id="myId" style="color:#ff0">**test**</span>');
});

xit('if rule\'s converter returns falsy, conveter returns subContent', function() {
var convertedText,
renderer = Renderer.factory({
'H1, H2, H3, H4, H5, H6': function() {
return false;
},
'EM': function() {}
});

runner = new DomRunner(toDom('<h1>test</h1>'));
it('inline tag with getSpaceControlled', function() {
var renderer = Renderer.factory();
runner = new DomRunner(toDom('pre <span class="myClass" id="myId" style="color:#ff0"><em>test</em></span> post'));
runner.next();
convertedText = renderer.convert(runner.getNode(), 'subContents');

expect(convertedText).toEqual('subContents');

runner = new DomRunner(toDom('<em>test</em>'));
runner.next();
convertedText = renderer.convert(runner.getNode(), 'subContents');

expect(convertedText).toEqual('subContents');
expect(renderer.convert(runner.getNode(), '**test**')).toEqual(' <span class="myClass" id="myId" style="color:#ff0">**test**</span> ');
});

it('rules can be assigned separately with comma', function() {
Expand Down

0 comments on commit 85f874e

Please sign in to comment.