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

Consistently use toLowerCase in tag name comparisons. (#1001)

* Consistently use toLowerCase in tag name comparisons.

* Add quick note about tagName casing.
This commit is contained in:
Zach Schneider
2017-08-17 13:23:38 -04:00
committed by Ian Storm Taylor
parent 8469c64d9e
commit e6a4009d75
19 changed files with 35 additions and 33 deletions

View File

@@ -54,7 +54,7 @@ const rules = [
// Add our first rule with a deserializing function. // Add our first rule with a deserializing function.
{ {
deserialize(el, next) { deserialize(el, next) {
if (el.tagName == 'P') { if (el.tagName.toLowerCase() == 'p') {
return { return {
kind: 'block', kind: 'block',
type: 'paragraph', type: 'paragraph',
@@ -70,13 +70,15 @@ If you've worked with the [`Raw`](../reference/serializers/raw.md) serializer be
The `el` argument that the `deserialize` function receives is just a DOM element. And the `next` argument is a function that will deserialize any element(s) we pass it, which is how you recurse through each node's children. The `el` argument that the `deserialize` function receives is just a DOM element. And the `next` argument is a function that will deserialize any element(s) we pass it, which is how you recurse through each node's children.
A quick note on `el.tagName` -- in browser environments, Slate uses the native `DOMParser` to parse HTML, which returns uppercase tag names. In server-side or node environments, we recommend [providing parse5](https://docs.slatejs.org/reference/serializers/html.html#parsehtml) to parse HTML; however, parse5 returns lowercase tag names due to some subtle complexities in specifications. Consequentially, we recommend using case-insensitive tag comparisons, so your code just works everywhere without having to worry about the parser implementation.
Okay, that's `deserialize`, now let's define the `serialize` property of the paragraph rule as well: Okay, that's `deserialize`, now let's define the `serialize` property of the paragraph rule as well:
```js ```js
const rules = [ const rules = [
{ {
deserialize(el, next) { deserialize(el, next) {
if (el.tagName == 'P') { if (el.tagName.toLowerCase() == 'p') {
return { return {
kind: 'block', kind: 'block',
type: 'paragraph', type: 'paragraph',
@@ -105,16 +107,16 @@ Let's add the other types of blocks we want:
```js ```js
// Refactor block tags into a dictionary for cleanliness. // Refactor block tags into a dictionary for cleanliness.
const BLOCK_TAGS = { const BLOCK_TAGS = {
P: 'paragraph', p: 'paragraph',
BLOCKQUOTE: 'quote', blockquote: 'quote',
PRE: 'code' pre: 'code'
} }
const rules = [ const rules = [
{ {
// Switch deserialize to handle more blocks... // Switch deserialize to handle more blocks...
deserialize(el, next) { deserialize(el, next) {
const type = BLOCK_TAGS[el.tagName] const type = BLOCK_TAGS[el.tagName.toLowerCase()]
if (!type) return if (!type) return
return { return {
kind: 'block', kind: 'block',
@@ -144,22 +146,22 @@ Okay. So now our serializer can handle blocks, but we need to add our marks to i
```js ```js
const BLOCK_TAGS = { const BLOCK_TAGS = {
BLOCKQUOTE: 'quote', blockquote: 'quote',
P: 'paragraph', p: 'paragraph',
PRE: 'code' pre: 'code'
} }
// Add a dictionary of mark tags. // Add a dictionary of mark tags.
const MARK_TAGS = { const MARK_TAGS = {
EM: 'italic', em: 'italic',
STRONG: 'bold', strong: 'bold',
U: 'underline', u: 'underline',
} }
const rules = [ const rules = [
{ {
deserialize(el, next) { deserialize(el, next) {
const type = BLOCK_TAGS[el.tagName] const type = BLOCK_TAGS[el.tagName.toLowerCase()]
if (!type) return if (!type) return
return { return {
kind: 'block', kind: 'block',
@@ -179,7 +181,7 @@ const rules = [
// Add a new rule that handles marks... // Add a new rule that handles marks...
{ {
deserialize(el, next) { deserialize(el, next) {
const type = MARK_TAGS[el.tagName] const type = MARK_TAGS[el.tagName.toLowerCase()]
if (!type) return if (!type) return
return { return {
kind: 'mark', kind: 'mark',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,7 +3,7 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
switch (el.tagName) { switch (el.tagName.toLowerCase()) {
case 'p': { case 'p': {
return { return {
kind: 'block', kind: 'block',

View File

@@ -3,14 +3,14 @@ export default {
rules: [ rules: [
{ {
deserialize(el, next) { deserialize(el, next) {
if (el.tagName == 'div') { if (el.tagName.toLowerCase() == 'div') {
return null return null
} }
} }
}, },
{ {
deserialize(el, next) { deserialize(el, next) {
if (el.tagName == 'hr') { if (el.tagName.toLowerCase() == 'hr') {
return { return {
kind: 'block', kind: 'block',
type: 'divider', type: 'divider',