Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Use CSS selector query-based instead of Regular Expressions #43

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

guesant
Copy link
Contributor

@guesant guesant commented Apr 28, 2020

Query and change HTML tags with CSS Selectors using the modules CSSselect, htmlparser2 and domutils. The sergey's source code uses a lot of while loops and Regular Expressions. This proposal can handle html tags by querying with CSSselect and making a DOM-based changes with domutils.

Before was:

while ((m = patterns.templates.exec(content)) !== null) {
  if (m.index === patterns.templates.lastIndex) {
    patterns.templates.lastIndex++;
  }
  const [find, name, data] = m;
  if (name !== "default") {
    slots.default = slots.default.replace(find, "");
  }
  slots[name] = formatContent(data);
}

Now can be:

const { nodes } = queryNodesByHTML({
  html: content,
  selector: patterns.template,
});
nodes.forEach((node) => {
  const find = domutils.getOuterHTML(node);
  const name = domutils.getAttributeValue(node, "name");
  const data = domutils.getInnerHTML(node);

  if (name !== "default") {
    slots.default = slots.default.replace(find, "");
  }
  slots[name] = formatContent(data);
});

Some implemented functions:

  • html/prepare-html

    Prepare and format HTML code to be used later.

    For example, parse <sergey-slot /> to <sergey-slot ...args></sergey-slot>

  • html/change-tag

    • changeItemsByHTML

      changeItemsByHTML({
        html: "<p></p>",
        selector: "p",
        changeItem: (node: Node) => {
          node.atttribs.class = "foo";
          return domutils.getOuterHTML(node);
        }
      })

      Output: <p class="foo"></p>

    • changeItemsByHTMLFallback

      The same implementation of changeItemsByHTML but works with tags inside tags (e.g. <a title="<some-getter />">text</a>)

    • main

      Applies changeItemsByHTML and changeItemsByHTMLFallback (runs once changeItem for the selected nodes).


Maybe related with #36 #24

guesant and others added 10 commits April 27, 2020 23:17
`<sergey-slot ...args />` -> `<sergey-slot ...args></sergey-slot>`

the function ignores void elements like `<img />`
```js
changeItemsByHTML({
  html: '<p><!-- your html code --></p>',
  selector: 'p',
  changeItem: (node: Node) => {
    node.atttribs.class += "foo";
    return (domutils).getOuterHTML(node);
  }
})
```
Extends `changeItemsByHTML`.

Can match tags inside tags
(e.g. `<meta foo="<sergey-slot></sergey-slot>">`)

```js
changeItemsByHTMLFallback({
  html: '<p><!-- your html code --></p>',
  selector: 'p',
  changeItem: (node: Node) => {
    node.atttribs.class += "foo";
    return (domutils).getOuterHTML(node);
  }
})
```
Exports `main`, `changeItemsByHTML` and `changeItemsByHTMLFallback`

The `main` function recives `options` used in `changeItemsByHTML+` and a
callback function to change the selected node.

Returns string
@vercel
Copy link

vercel bot commented Apr 28, 2020

Deployment has failed due to an internal error. (code: jDTv0ZV)

Contact our support with [email protected] for more information.

@guesant guesant changed the title Smart changetag [WIP] Smart changetag Apr 29, 2020
@guesant guesant changed the title [WIP] Smart changetag Change nodes with css-selector based query May 29, 2020
@guesant guesant changed the title Change nodes with css-selector based query Use CSS selector query-based instead of Regular Expressions Jun 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant