Skip to content
This repository was archived by the owner on Jun 15, 2021. It is now read-only.

Commit 7dfe13f

Browse files
committed
Merge pull request #476 from guardian/rr-node-content-check
Node module: add hasContent check
2 parents 5270781 + 04b55e3 commit 7dfe13f

File tree

4 files changed

+143
-3
lines changed

4 files changed

+143
-3
lines changed

examples/amd-inline.html

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<!--
2+
This example demonstrates how to consume the Scribe
3+
editor using RequireJS and the AMD module syntax.
4+
5+
Note that you'll need to install scribe's dependencies through
6+
`bower install`. See http://bower.io/ if you are unfamiliar.
7+
-->
8+
<style>
9+
button {
10+
height: 3em;
11+
}
12+
13+
.active {
14+
border-style: inset;
15+
}
16+
17+
.rte, .rte-toolbar {
18+
display: block;
19+
}
20+
21+
p {
22+
margin-top: 0;
23+
}
24+
25+
.rte {
26+
border: 1px solid gray;
27+
height: 300px;
28+
overflow: auto;
29+
}
30+
.rte-output {
31+
width: 100%;
32+
height: 10em;
33+
}
34+
</style>
35+
<script src="../bower_components/requirejs/require.js"></script>
36+
<script>
37+
require({
38+
paths: {
39+
'scribe-common': '../bower_components/scribe-common',
40+
'lodash-amd': '../bower_components/lodash-amd',
41+
'html-janitor': '../bower_components/html-janitor/html-janitor',
42+
'immutable': '../bower_components/immutable/dist/immutable'
43+
}
44+
}, [
45+
'../src/scribe',
46+
'../bower_components/scribe-plugin-toolbar/src/scribe-plugin-toolbar',
47+
'../bower_components/scribe-plugin-formatter-plain-text-convert-new-lines-to-html/src/scribe-plugin-formatter-plain-text-convert-new-lines-to-html',
48+
'../bower_components/scribe-plugin-sanitizer/src/scribe-plugin-sanitizer',
49+
'../bower_components/scribe-plugin-inline-styles-to-elements/src/scribe-plugin-inline-styles-to-elements',
50+
'../bower_components/scribe-plugin-heading-command/src/scribe-plugin-heading-command',
51+
'../bower_components/scribe-plugin-link-prompt-command/src/scribe-plugin-link-prompt-command',
52+
'../bower_components/scribe-plugin-blockquote-command/src/scribe-plugin-blockquote-command'
53+
54+
], function (
55+
Scribe,
56+
scribePluginToolbar,
57+
scribePluginFormatterPlainTextConvertNewLinesToHtml,
58+
scribePluginSanitizer,
59+
scribePluginInlineStyles,
60+
scribePluginHeadingCommand,
61+
scribePluginLinkPromptCommand,
62+
scribePluginBlockquoteCommand
63+
) {
64+
var scribe = new Scribe(document.querySelector('.rte'),
65+
{allowBlockElements: false});
66+
window.scribe = scribe;
67+
68+
scribe.setContent('<p>Hello, World!<\/p>');
69+
70+
scribe.use(scribePluginToolbar(document.querySelector('.rte-toolbar')));
71+
scribe.use(scribePluginFormatterPlainTextConvertNewLinesToHtml());
72+
scribe.use(scribePluginInlineStyles());
73+
scribe.use(scribePluginHeadingCommand(2));
74+
scribe.use(scribePluginBlockquoteCommand());
75+
scribe.use(scribePluginSanitizer({ tags: {
76+
p: {},
77+
b: {},
78+
i: {},
79+
br: {},
80+
h2: {},
81+
a: {},
82+
blockquote: {}
83+
}}));
84+
scribe.use(scribePluginLinkPromptCommand());
85+
86+
scribe.on('content-changed', updateHtml);
87+
88+
function updateHtml() {
89+
document.querySelector('.rte-output').value = scribe.getHTML();
90+
}
91+
92+
updateHtml();
93+
});
94+
</script>
95+
<div class="rte-toolbar">
96+
<button data-command-name="bold">Bold</button>
97+
<button data-command-name="italic">Italic</button>
98+
<button data-command-name="linkPrompt">Link</button>
99+
<button data-command-name="h2">H2</button>
100+
<button data-command-name="blockquote">Blockquote</button>
101+
</div>
102+
<div class="rte"></div>
103+
<section>
104+
<h1>Output</h1>
105+
<textarea class="rte-output" readonly></textarea>
106+
</section>

src/node.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ define([
1414
return inlineElementNames.includes(node.nodeName);
1515
}
1616

17+
function hasContent(node) {
18+
if(node && node.children && node.children.length > 0) {
19+
return true;
20+
}
21+
22+
if(node && node.nodeName === 'BR') {
23+
return true;
24+
}
25+
26+
return false;
27+
}
28+
1729
// return true if nested inline tags ultimately just contain <br> or ""
1830
function isEmptyInlineElement(node) {
1931
if( node.children.length > 1 ) return false;
@@ -177,7 +189,8 @@ define([
177189
wrap: wrap,
178190
unwrap: unwrap,
179191
removeChromeArtifacts: removeChromeArtifacts,
180-
elementHasClass: elementHasClass
192+
elementHasClass: elementHasClass,
193+
hasContent: hasContent
181194
};
182195

183196
});

src/plugins/core/inline-elements-mode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
define(function () {
1+
define(['../../node'], function (nodeHelpers) {
22

33
'use strict';
44

@@ -9,7 +9,7 @@ define(function () {
99
while (treeWalker.nextNode()) {
1010
if (treeWalker.currentNode) {
1111
// If the node is a non-empty element or has content
12-
if (~['br'].indexOf(treeWalker.currentNode.nodeName.toLowerCase()) || treeWalker.currentNode.length > 0) {
12+
if(nodeHelpers.hasContent(treeWalker.currentNode)) {
1313
return true;
1414
}
1515
}

test/node.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ describe('Node type checking', function() {
4848
});
4949
});
5050

51+
describe('for whether a node has content', function() {
52+
it('should detect a BR element has no content', function() {
53+
var fakeNode = {nodeName: "BR"};
54+
55+
assert.isTrue(nodeHelpers.hasContent(fakeNode));
56+
});
57+
58+
it('should detect a node has children', function() {
59+
var fakeNode = {nodeName: "DIV", children: [{}]};
60+
61+
assert.isTrue(nodeHelpers.hasContent(fakeNode));
62+
63+
});
64+
65+
it('should detect a non-BR node', function() {
66+
var fakeNode = {nodeName: "P"};
67+
68+
assert.isFalse(nodeHelpers.hasContent(fakeNode));
69+
});
70+
});
71+
5172
describe('text nodes', function() {
5273
describe('that are whitespace-only', function() {
5374
it('are detected', function() {

0 commit comments

Comments
 (0)