Giter Club home page Giter Club logo

Comments (4)

wooorm avatar wooorm commented on July 24, 2024

Hi Rose! Sounds about right! Are you able and interested to tackle this and work on a PR?

from hast-util-to-parse5.

inklesspen avatar inklesspen commented on July 24, 2024

Unfortunately I'm not 100% sure how to fix it.

from hast-util-to-parse5.

wooorm avatar wooorm commented on July 24, 2024

Hmm, I can guide you, or take over if you want but I don’t have a lot of time so that could delay it a bit.

If I can guide you: could you provide me with more information on how you noticed this problem? What’s the background and why did you open an issue?

from hast-util-to-parse5.

inklesspen avatar inklesspen commented on July 24, 2024

because I was using parse5 to parse html into HAST, I wanted to use parse5 to serialize as well, reasoning that parse5 would not produce html which it could not then parse.

I implemented a rehype serializer like this:

import { serialize as parse5Serialize } from 'parse5';
import hastUtilToParse5 from 'hast-util-to-parse5';

export default function stringify(options) {
  // eslint-disable-next-line no-unused-vars
  const settings = { ...options, ...this.data('settings') };

  function compiler(tree) {
    const ast = hastUtilToParse5(tree);
    return parse5Serialize(ast);
  }

  this.Compiler = compiler;
}

but it ran into a problem round-tripping a <font size="4"> tag. (Yes, I know nobody should actually use <font> anymore, but I needed to interoperate with older software that needs font sizes to be specified this way.) parse5 could parse it just fine into a value of "4", but hast-util-from-parse5 was converting that into 4 and hast-util-to-parse5 did not convert it back.

I have worked around the issue with the following hack:

import { serialize as parse5Serialize } from 'parse5';
import hastUtilToParse5 from 'hast-util-to-parse5';
import utilVisit from 'unist-util-visit';
import isElement from 'hast-util-is-element';
import reduce from 'immer';

function makeAttrValuesStrings(hast) {
  // due to https://github.com/syntax-tree/hast-util-to-parse5/issues/5
  // we must walk the tree and convert every attribute value to a string
  return reduce(hast, (draftHast) => {
    utilVisit(draftHast, node => isElement(node), (node) => {
      if (node.properties) {
        Object.keys(node.properties).forEach((propName) => {
          const value = node.properties[propName];
          if (value !== null && value !== undefined &&
            value !== false && !(value instanceof String)) {
            // eslint-disable-next-line no-param-reassign
            node.properties[propName] = value.toString();
          }
        });
      }
    });
  });
}

export default function stringify(options) {
  // eslint-disable-next-line no-unused-vars
  const settings = { ...options, ...this.data('settings') };

  function compiler(tree) {
    const hast = makeAttrValuesStrings(tree);
    const ast = hastUtilToParse5(hast);
    return parse5Serialize(ast);
  }

  this.Compiler = compiler;
}

I am worried my workaround will fail to properly handle some edge cases. If you can provide a better way of determining which attribute values to make strings, I can probably give you a PR.

from hast-util-to-parse5.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.