Coder Social home page Coder Social logo

Comments (7)

brandonscholet avatar brandonscholet commented on July 18, 2024

Also, I just wanted to say thank you for what you do.

from soupsieve.

facelessuser avatar facelessuser commented on July 18, 2024

I don't have just a simple regex to tell if an entire complex selector is good. In general, I'm basically tokenizing the selectors.

To better explain, I start at the beginning and I look for allowed things identifiers (tags), classes (identifiers preceded by .), pseudo-class names (identifiers preceded by :). If a pseudo class is followed by (, and it is recognized as a pseudo class that takes functional options, then we switch context and parse according to the rules of that pseudo-class. If we see a [, we switch context to handle an attribute.

I'm mainly giving a general overview, the exact details may be a little different, but you get the idea. There is no simple magic regex that can tell if the entire selector is valid, we just take it apart, piece by piece, making sure each piece makes sense in context of what came before, and if it doesn't, we halt by throwing an error.

from soupsieve.

brandonscholet avatar brandonscholet commented on July 18, 2024

That's fair. Thank you for your time!

You should check out my tool wappybird. It's built on wapppalyzer. I use it to scan very large ranges for penetration tests to search for technologies.

from soupsieve.

facelessuser avatar facelessuser commented on July 18, 2024

@brandonscholet One thing you could do is compile the selector to test it in your own validation function.

>>> import soupsieve as sv
>>> def is_valid_selector(sel):
...     try:
...         sv.compile(sel)
...     except (sv.SelectorSyntaxError, NotImplementedError):
...         return False
...     return True
...
>>> is_valid_selector('> this')
False
>>> is_valid_selector('tag.class[attr=value]')
True

from soupsieve.

brandonscholet avatar brandonscholet commented on July 18, 2024

image

You are a gentlemen and a scholar. It's quick and easy to add to the guy's script. and adds no time to the larger amount of things to parse through without adding to execution time.

from soupsieve.

facelessuser avatar facelessuser commented on July 18, 2024

Yeah, we cache recently compiled patterns, so subsequent compiles should take much less time.

from soupsieve.

facelessuser avatar facelessuser commented on July 18, 2024

Just a note for anyone else checking this issue out. Soupsieve also offers its own API. It was designed this way as we were not sure when we originally implemented this if BeautifulSoup would accept us as the official CSS selector implementation.

We can build a query before using it, and use the query directly by passing in the BeautifulSoup tag.

>>> from bs4 import BeautifulSoup
>>> import soupsieve as sv
>>> soup = BeautifulSoup('<div><span>text</span></div>', 'html.parser')
>>> query = sv.compile('div > span')
>>> query.select(soup)
[<span>text</span>]

This allows you control of the selecting process. You can catch bad selectors and then choose to continue or not continue with the query without having to issue a recompile. The API also allows performing matches with match and finding the closest parent with closest, etc.

One of these days, I may see if BeautifulSop can add closest and match into their API as well, but they are always usable through our API.

from soupsieve.

Related Issues (20)

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.