1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-16 20:24:01 +02:00

Remove parse5 (#1531)

Fix stripUnwantedAttrs

Remove .only

Remove parse5 from deps

Better imports

Cleanup

More succint removal of attributes
This commit is contained in:
Conor Cussell
2018-01-14 00:41:48 +01:00
committed by Ian Storm Taylor
parent 48f198b210
commit 62ffb4681b
7 changed files with 16 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
import parse5 from 'parse5' // eslint-disable-line import/no-extraneous-dependencies
import { JSDOM } from 'jsdom' // eslint-disable-line import/no-extraneous-dependencies
const UNWANTED_ATTRS = [
'data-key',
@@ -21,20 +21,20 @@ const UNWANTED_TOP_LEVEL_ATTRS = [
*/
function stripUnwantedAttrs(element) {
if (Array.isArray(element.attrs)) {
element.attrs = element.attrs.filter(({ name }) => { return !UNWANTED_ATTRS.includes(name) })
if (typeof element.removeAttribute === 'function') {
UNWANTED_ATTRS.forEach(attr => element.removeAttribute(attr))
if (element.parentNode.nodeName === '#document-fragment') {
element.attrs = element.attrs.filter(({ name }) => { return !UNWANTED_TOP_LEVEL_ATTRS.includes(name) })
UNWANTED_TOP_LEVEL_ATTRS.forEach(attr => element.removeAttribute(attr))
}
}
if (Array.isArray(element.childNodes)) {
if (element.childNodes.length) {
element.childNodes.forEach(stripUnwantedAttrs)
}
if (element.nodeName === '#text') {
element.value = element.value.trim()
element.textContent = element.textContent.trim()
}
return element
@@ -48,7 +48,7 @@ function stripUnwantedAttrs(element) {
*/
export default function clean(html) {
const $ = parse5.parseFragment(html)
const $ = JSDOM.fragment(html)
$.childNodes.forEach(stripUnwantedAttrs)
return parse5.serialize($)
return $.firstChild.outerHTML
}

View File

@@ -4,7 +4,7 @@ import ReactDOM from 'react-dom/server'
import assert from 'assert'
import clean from '../helpers/clean'
import fs from 'fs-promise' // eslint-disable-line import/no-extraneous-dependencies
import parse5 from 'parse5' // eslint-disable-line import/no-extraneous-dependencies
import { JSDOM } from 'jsdom' // eslint-disable-line import/no-extraneous-dependencies
import { Editor } from '../..'
import { basename, extname, resolve } from 'path'
@@ -27,7 +27,8 @@ describe('rendering', () => {
}
const string = ReactDOM.renderToStaticMarkup(<Editor {...p} />)
const expected = parse5.serialize(parse5.parseFragment(output))
const dom = JSDOM.fragment(output)
const expected = dom.firstChild.outerHTML
.trim()
.replace(/\n/gm, '')
.replace(/>\s*</g, '><')