From dea6f279958956168ed15b7fc5adac76431c29eb Mon Sep 17 00:00:00 2001 From: Ludwig Schubert Date: Thu, 10 Aug 2017 13:00:41 -0700 Subject: [PATCH] Wrap pure text nodes and warn about them --- src/components/d-article.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/d-article.js b/src/components/d-article.js index accd247..b612cbf 100644 --- a/src/components/d-article.js +++ b/src/components/d-article.js @@ -13,6 +13,8 @@ const T = Template('d-article', ` // } // } +const isOnlyWhitespace = /^\s*$/; + export class Article extends T(HTMLElement) { constructor() { @@ -21,8 +23,20 @@ export class Article extends T(HTMLElement) { new MutationObserver( (mutations) => { for (const mutation of mutations) { for (const addedNode of mutation.addedNodes) { - if (addedNode.nodeName === 'HR') { + switch (addedNode.nodeName) { + case 'HR': console.warn('Use of
tags in distill articles is discouraged!'); + break; + case '#text': { // usually text nodes are only linebreaks. + const text = addedNode.nodeValue; + if (!isOnlyWhitespace.test(text)) { + console.warn('Use of unwrapped text in distill articles is discouraged as it breaks layout! Please wrap any text in a or

tag. We found the following text: ' + text); + const wrapper = document.createElement('span'); + wrapper.innerHTML = addedNode.nodeValue; + addedNode.parentNode.insertBefore(wrapper, addedNode); + addedNode.parentNode.removeChild(addedNode); + } + } break; } } }