Removing <html> tags from content in the YUI Editor

After integrating the YUI rich text editor, I am looking for a way to automatically remove the <html>, <body>, and DOCTYPE tags from the content when it is saved. Although I can achieve this by parsing the HTML code afterward, I am seeking a more efficient solution.

Currently, the edited text in the YUI editor is saved as:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
    <body>
        <p>foo</p>
    </body>
</html>

...but my goal is to save only the following:

<p>foo</p>

Any suggestions on how to achieve this?

P.S.: My implementation of the YUI editor involves using the yui_editor plugin for ruby on rails. However, I would appreciate a general answer applicable to any YUI editor as well!

Answer №1

I managed to find a solution to the issue on my own by parsing the HTML upon submission. Initially, I wasn't considering this approach, but ultimately realized it was the simplest way to resolve it. Leveraging the Nokogiri RubyGem for Rails, I performed the parsing:

value = Nokogiri::HTML(yui_content).css('body').to_html 
value.gsub!(/<body>/,'') 
value.gsub!(/<\/body>/,'')

Answer №2

A clever approach involves utilizing regular expressions to capture the text between <body> and </body>. For instance, consider this variant based on the YUI editor page:

var myEditor = new YAHOO.widget.Editor('content');
myEditor.render();

YAHOO.util.Event.on('clickbutton', 'click', function() {
    myEditor.saveHTML();

    //The variable html now stores the textarea content
    var html = myEditor.get('element').value, match;

    match = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
    html = match ? match[1] : html;
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transform the result string into a JavaScript array

I am currently exploring the use of the jqplot plugin to generate a pie chart. However, I am facing difficulties in converting the results obtained from my $get function into a format that can be utilized by the jqplot plugin for data visualization. The s ...

Tips for simulating focus on a Material-ui TextField with a button press

My web application features a unique method for indirectly filling in text fields. Since there are multiple clicks involved (specifically in a calendar context) and numerous fields to fill, I want to provide users with a visual indication of which field th ...

Unfortunately, the isoWeek function in moment.js is not functioning correctly on our Heroku instance

When my app relies on moment().startOf('isoWeek') to find the current start of the week, it functions perfectly on my local machine, showing Mon Oct 31 2016 00:00:00 GMT-0400 (EDT) as expected. However, on my Heroku server, this code fails and re ...

Solutions for resolving the issue of not being able to load images when using merged-images in JavaScript

I have a base64 image here and it has already been converted. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUTEhQWFhUXGBoaGBgYGBcXGhgXGBgYGhgbHhoZHiggGholHhgYITEhJSkrLi4uHR8zODMtNygtLisBCgoKDg0OGhAQGi0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0 ...

Utilizing JSON to display multiple markers on a Google Map

How can I display multiple markers on a Google Map using JSON data? I have successfully added a single marker, but I'm facing issues with adding multiple markers. Below is the code for a single marker (which is working): var lat=position.coords.lati ...

Guide: Highlighting a selected link in Navigation without redirecting the page can be achieved in AngularJS by utilizing

I have a list of links that I want to be able to highlight when clicked without redirecting to the page. Below is the code snippet: HTML <ul class="nav nav-list"> <li ng-repeat="navLink in navLinks" ng-class="navClass('{{navLink.Title ...

Express.js refuses to serve static assets

I've been struggling to set up my express server to serve static files, but no matter what I try, I can't seem to get it working. I've attempted various solutions without success. Here is my folder structure: App - Web -- public --- images ...

"Encountering a problem with historical dates in Fullcalendar

One challenge I am facing is that my events are accepted even when dropped into the past. This issue has arisen while using version 2.4. I attempted to resolve this by utilizing eventConstraint, but unfortunately, it proved ineffective. Could you provide ...

What is the best way to parse a JSON file in Angular?

Can you please explain how to read a JSON file? I have been able to successfully read a JSON file using a controller, but when I try to read it from a factory, the content is null. Why is this happening? http://plnkr.co/edit/THdlp00GuSk1NS6rqe5k?p=preview ...

issue encountered when trying to deploy a React application on GitHub

I successfully created a todolist on localhost. Now, I am facing issues while deploying it on GitHub. When I run '$ npm run deploy' in the command prompt, I encounter errors. To troubleshoot, I have added 'homepage', 'predeploy&ap ...

How to control Formik inputs from an external source

I'm currently developing an application with speech-to-text commands functionality. I have created a form, but I am looking to manipulate the input elements from outside the form framework. <form id="myform"> <input type="tex ...

Ways to serve HTML without relying on code generated by JQuery

I am currently utilizing the html() function in JQuery to extract the HTML content of a specific div on my web page. Nevertheless, the output includes all the code generated by JQuery as well: <ul><li id="fact_1" class="jstree-open"><ins cl ...

Guide on crafting a DELETE method in Express

Currently, I am in the process of developing a basic Wiki using Express and Node.js. Up to this point, my application includes an index displaying all wiki pages, views for adding a new page, and viewing a single page. My next objective is to implement fun ...

Bootstrap's square-shaped columns

I would like to implement a grid of squares for navigation purposes. By squares, I mean that the colored areas should have equal width and height. Currently, I have achieved this using JavaScript, but I am interested in a CSS-only solution. My project is ...

Importing specific modules in ES6 when defining an asynchronous component

I am looking to asynchronously load certain elements of my web app: Here is the original import code: import {Popover, PopoverButton, PopoverPanel} from '@headlessui/vue' export default { components: { Popover, PopoverButton ...

Select a variable by referencing a specific div ID

As I work with jQuery, I am currently focusing on obtaining the ID of an element when it is clicked on the page. My goal is to use this ID to determine which variable should be utilized in my JavaScript code. For instance: When I click on the element wit ...

When the user clicks on the login text field or password field, any existing text will

Currently, I am working on the login section of my website and I would like to implement a similar effect to Twitter's login form, where the Username and Password values disappear when the Textfield and Password field are in focus. I have attempted to ...

The behavior of AJAX Search varies between the development and production environments

I recently integrated an instant search feature into my application. During testing on the local server, the functionality met my expectations: It filters the list as I type It is not case-sensitive It allows for resetting the search if I delete the inp ...

The component in React does not refresh after updating the state

I have a React page where I am displaying a list of quizzes fetched from an external API. There's also a "New Quiz" button that opens a dialog with a form for users to create a new quiz. My issue is with making the table re-render once the POST reque ...

Can you choose the stylesheet.cssRule[] based on a class name or ID?

Currently, I am able to modify the font size by accessing the first style sheet using the following code: document.styleSheets[0].cssRules[0].style.fontSize = "16"; Here is the CSS snippet: h1 {font-size: 12} .someClass {} As the CSS file ...