mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 22:45:18 +02:00
Reckon with inconsistencies between parse5 and native DOMParser (#952)
This commit is contained in:
committed by
Ian Storm Taylor
parent
1e3cdafb59
commit
f8b103d75e
@@ -80,7 +80,7 @@ const MARK_TAGS = {
|
||||
const RULES = [
|
||||
{
|
||||
deserialize(el, next) {
|
||||
const block = BLOCK_TAGS[el.tagName]
|
||||
const block = BLOCK_TAGS[el.tagName.toLowerCase()]
|
||||
if (!block) return
|
||||
return {
|
||||
kind: 'block',
|
||||
@@ -91,7 +91,7 @@ const RULES = [
|
||||
},
|
||||
{
|
||||
deserialize(el, next) {
|
||||
const mark = MARK_TAGS[el.tagName]
|
||||
const mark = MARK_TAGS[el.tagName.toLowerCase()]
|
||||
if (!mark) return
|
||||
return {
|
||||
kind: 'mark',
|
||||
@@ -103,9 +103,9 @@ const RULES = [
|
||||
{
|
||||
// Special case for code blocks, which need to grab the nested childNodes.
|
||||
deserialize(el, next) {
|
||||
if (el.tagName != 'pre') return
|
||||
if (el.tagName.toLowerCase() != 'pre') return
|
||||
const code = el.childNodes[0]
|
||||
const childNodes = code && code.tagName == 'code'
|
||||
const childNodes = code && code.tagName.toLowerCase() == 'code'
|
||||
? code.childNodes
|
||||
: el.childNodes
|
||||
|
||||
@@ -119,7 +119,7 @@ const RULES = [
|
||||
{
|
||||
// Special case for links, to grab their href.
|
||||
deserialize(el, next) {
|
||||
if (el.tagName != 'a') return
|
||||
if (el.tagName.toLowerCase() != 'a') return
|
||||
return {
|
||||
kind: 'inline',
|
||||
type: 'link',
|
||||
|
@@ -33,12 +33,12 @@ const TEXT_RULE = {
|
||||
}
|
||||
}
|
||||
|
||||
if (el.nodeName == '#text') {
|
||||
if (el.tagName == 'span' || el.nodeName == '#text') {
|
||||
if (el.value && el.value.match(/<!--.*?-->/)) return
|
||||
|
||||
return {
|
||||
kind: 'text',
|
||||
text: el.value
|
||||
text: el.value || el.nodeValue
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -83,11 +83,13 @@ class Html {
|
||||
this.defaultBlockType = options.defaultBlockType || 'paragraph'
|
||||
|
||||
// Set DOM parser function or fallback to native DOMParser if present.
|
||||
if (options.parseHtml !== null) {
|
||||
if (typeof options.parseHtml === 'function') {
|
||||
this.parseHtml = options.parseHtml
|
||||
} else if (typeof DOMParser !== 'undefined') {
|
||||
this.parseHtml = (html) => {
|
||||
return new DOMParser().parseFromString(html, 'application/xml')
|
||||
const parsed = new DOMParser().parseFromString(html, 'text/html')
|
||||
// Unwrap from <html> and <body>
|
||||
return parsed.childNodes[0].childNodes[1]
|
||||
}
|
||||
} else {
|
||||
throw new Error(
|
||||
@@ -106,7 +108,7 @@ class Html {
|
||||
*/
|
||||
|
||||
deserialize = (html, options = {}) => {
|
||||
const children = this.parseHtml(html).childNodes
|
||||
const children = Array.from(this.parseHtml(html).childNodes)
|
||||
let nodes = this.deserializeElements(children)
|
||||
|
||||
const { defaultBlockType } = this
|
||||
@@ -196,7 +198,14 @@ class Html {
|
||||
deserializeElement = (element) => {
|
||||
let node
|
||||
|
||||
if (!element.tagName) {
|
||||
element.tagName = ''
|
||||
}
|
||||
|
||||
const next = (elements) => {
|
||||
if (typeof NodeList !== 'undefined' && elements instanceof NodeList) {
|
||||
elements = Array.from(elements)
|
||||
}
|
||||
switch (typeOf(elements)) {
|
||||
case 'array':
|
||||
return this.deserializeElements(elements)
|
||||
|
Reference in New Issue
Block a user