Skip to content

Commit 3709efa

Browse files
committed
introduce resetAttributes
1 parent 9cc1a26 commit 3709efa

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/ElementPortal.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ const toNodesArray = (x) => (
1717
: null
1818
);
1919

20+
const removeNodeAttributes = (node, attrs) => {
21+
if (attrs === true) {
22+
while (node.attributes.length > 0) {
23+
node.removeAttribute(node.attributes[0].name);
24+
}
25+
}
26+
else {
27+
attrs.forEach(attr => {
28+
node.removeAttribute(attr);
29+
});
30+
}
31+
};
32+
2033
const ElementPortal = createReactClass({
2134
propTypes: {
2235
selector: PropTypes.string,
@@ -27,7 +40,10 @@ const ElementPortal = createReactClass({
2740
]),
2841
component: PropTypes.func,
2942
mapNodeToProps: PropTypes.func,
30-
resetStyle: PropTypes.bool
43+
resetAttributes: PropTypes.oneOfType([
44+
PropTypes.bool,
45+
PropTypes.arrayOf(PropTypes.string)
46+
])
3147
},
3248

3349
componentDidMount() {
@@ -53,9 +69,8 @@ const ElementPortal = createReactClass({
5369
renderNode(node) {
5470
const mapNodeToProps = this.props.mapNodeToProps || noop;
5571

56-
if (this.props.resetStyle) {
57-
node.removeAttribute('class');
58-
node.removeAttribute('style');
72+
if (this.props.resetAttributes) {
73+
removeNodeAttributes(node, this.props.resetAttributes);
5974
}
6075

6176
const children = this.props.component

test/ElementPortal.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ test('erases classes and styles', t => {
228228
const Greeting = () => (<div>Hello</div>);
229229
render(
230230
<div>
231-
<ElementPortal selector={`#${headerId}`} resetStyle>
231+
<ElementPortal selector={`#${headerId}`} resetAttributes={['class', 'style']}>
232232
<Greeting/>
233233
</ElementPortal>
234234
</div>,
@@ -239,6 +239,36 @@ test('erases classes and styles', t => {
239239
t.is(document.getElementById(headerId).getAttribute('style'), null);
240240
});
241241

242+
test('erases all attributes', t => {
243+
const node = document.createElement('div');
244+
document.body.appendChild(node);
245+
const headerId = uniqueId();
246+
const appId = uniqueId();
247+
node.innerHTML = `
248+
<div id="${headerId}" class="foo" data-bar="baz" qux>
249+
</div>
250+
<div id="${appId}">
251+
</div>
252+
`;
253+
const headerElement = document.getElementById(headerId);
254+
t.is(headerElement.classList.length, 1);
255+
t.is(headerElement.getAttribute('data-bar'), 'baz');
256+
t.is(headerElement.getAttribute('quz'), null);
257+
const Greeting = () => (<div>Hello</div>);
258+
render(
259+
<div>
260+
<ElementPortal selector={`#${headerId}`} resetAttributes={true}>
261+
<Greeting/>
262+
</ElementPortal>
263+
</div>,
264+
document.getElementById(appId)
265+
);
266+
t.is(headerElement.textContent, 'Hello');
267+
t.is(headerElement.classList.length, 0);
268+
t.is(headerElement.getAttribute('data-bar'), null);
269+
t.is(headerElement.getAttribute('qux'), null);
270+
});
271+
242272
test('transfers context to the portal', t => {
243273
const store = createStore((state = 0, action) => {
244274
if (action && action.type === 'INC') {

0 commit comments

Comments
 (0)