Giter VIP home page Giter VIP logo

xmlwash's Introduction

XML Wash

XML Wash is an operator allows to clean in a clever way an xhtml field. Its main goal is twofold:

  • allows to use the shorten operator and get a valid result
  • allows to use the literal class=html and be safe from XSS attacks

Shorten Problem

Let's take the line tpl of article, it displays the title+ introduction. Unfortunately, there isn't a simple way to limit the length of that introduction, and some authors have a rather extensive interpretation of it (bless them, some put the complete article into the introduction). You can go the hard way and try to make them understand that introduction means, err, an introduction, or do it the easy way and truncate the introduction to a length you find normal.

Unfortunately, the shorten operator doesn't work with xml content, as it might cut the text between an opening tag (eg or

) and its closing ( and
) or in the middle of a tag (eg '<a hre'). If you use xhtml+css positionning for your layout (you should), you are going to have some really funcky results. No good.

For instance, if your output text is "Xavier has really simple examples."

{$node.data_map.intro.content.output.output_text|shorten(22)} <a href="...>Read more</a>

Will have "Read more" in bold.

(rem: to make it funnier, you have to add 4 to the length because it starts with

+newline )

It can also stop in the middle of a tag "Xavier has <b" and so on.

Moreover, you might want to "clean" the intro, for instance you don't want to display titles (hn) or embeded files in the line view. In other words you need to limit what tags can be in the intro (eg only keep and )

Security problem

As soon as you need to have minimaly complex layout in an article or want to be able to paste html code from elsewhere, you need to allow the html class in the literal tag, otherwise, any editor can inject any XSS code (javascript attacks...). Ez solution to this security risk has been to disable it by default (settings/content.ini)

[literal]
AvailableClasses[]
# The class 'html' is disabled by default because it gives editors the
# possibility to insert html and javascript code in XML blocks.
# Don't enable the 'html' class unless you really trust all users who has
# privileges to edit objects containing XML blocks.
#AvailableClasses[]=html

This is unfortunately a rather expensive option as you end up overriding templates to allow the html code you want in specific pages instead of just do that from an xml block.

Solution

The Safe HTML library is very good at cleaning the input and get rid of all these security problems.

http://pixel-apes.com/safehtml/

As a positive side effect, it also clean the generated xhtml (for instance missing closing tags), this make it possible to shorten without having problems.

How to use ?

The extension override the default template used by {attribute_view_gui} for the xml fields. You can now safely allows the html class for literals

You have a new maxlength parameter:

{attribute_view attribute=$node.object.data_map.intro maxlength=42}

what it does is to add a xmlwash() operator, you can also use it directly like that:

{$node.data_map.intro.content.output.output_tex|shorten(42)|xmlwash()}

(obvioulsy xmlwash has to be the last one called)

This extension also offers a strip_tags operator (same syntax than the php version)

If you want to keep only the paragraphs, italic and links:

 {$node.data_map.intro.content.output.output_text|strip_tags(array('<p>','<i>','<a>'))|shorten(42)|xmlwash()}

Known bugs and limitations

None, but feel free to send me a mail (ez AT sydesy DOT com) if you find one.

As for the limitations, they are mine and I didn't succeed using the svn here, so I used pubsvn ;(

Screenshot

Your mother was right: you need to wash your xml before showing it

xmlwash's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.