Skip to content

Commit

Permalink
Replace tab characters with emspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyBushy committed Aug 11, 2022
1 parent 08315f4 commit fe6e051
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/utils/dom.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function addHTMLSpaces(text) {
let nbsp = '\u00A0';
return text.replace(/ /g, ' ' + nbsp);
const NBSP = '\u00A0';
const EMSP = '\u2003';

function prepareText(text) {
return text.replace(/ /g, ' ' + NBSP).replace(/\t/g, EMSP);
}

export function createTextNode(dom, text) {
return dom.createTextNode(addHTMLSpaces(text));
return dom.createTextNode(prepareText(text));
}

export function normalizeTagName(tagName) {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/renderers/0-2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,24 @@ test('multiple spaces should preserve whitespace with nbsps', (assert) => {
assert.equal(textNode.nodeValue, expectedText, 'renders the text');
});

test('replaces tab characters with EM SPACE', (assert) => {
let mobiledoc = {
version: MOBILEDOC_VERSION,
sections: [
[], // markers
[ // sections
[MARKUP_SECTION_TYPE, 'P', [
[[], 0, "\tHello world"]]
]
]
]
};

let { result: rendered } = renderer.render(mobiledoc);
let elementNode = rendered.firstChild.firstChild;
assert.equal(elementNode.nodeValue, '\u2003Hello\u2003world', 'replaces tabs with  ');
});

test('throws when given unexpected mobiledoc version', (assert) => {
let mobiledoc = {
version: '0.1.0',
Expand Down

0 comments on commit fe6e051

Please sign in to comment.